aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik/parrot_zik_tray
blob: 914a37e6187d858c1319c5dadd2e3e7698b8a1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import gtk

from parrot_zik.interface.version1 import ParrotZikVersion1Interface
from parrot_zik.interface.version2 import ParrotZikVersion2Interface
from parrot_zik import resource_manager
from parrot_zik import bluetooth_paired_devices
from parrot_zik.indicator import MenuItem
from parrot_zik.indicator import Menu
from parrot_zik.indicator import SysIndicator
from parrot_zik.utils import repeat

REFRESH_FREQUENCY = 30000
RECONNECT_FREQUENCY = 5000



class ParrotZikIndicator(SysIndicator):
    def __init__(self):
 
        self.menu = Menu()

        self.info_item = MenuItem("Parrot Zik Not connected",
                                  None, sensitive=False)
        self.menu.append(self.info_item)

        self.version_1_interface = ParrotZikVersion1Interface(self)
        self.version_2_interface = ParrotZikVersion2Interface(self)
        self.quit = MenuItem("Quit", gtk.main_quit, checkitem=True)
        self.menu.append(self.quit)

        SysIndicator.__init__(self, icon="zik-audio-headset", menu=self.menu)

        self.active_interface = None

    @repeat
    def reconnect(self):
        if self.active_interface:
            self.reconnect.stop()
        else:
            self.info("Trying to connect")
            try:
                manager = bluetooth_paired_devices.connect()
            except bluetooth_paired_devices.BluetoothIsNotOn:
                self.info("Bluetooth is turned off")
            except bluetooth_paired_devices.DeviceNotConnected:
                self.info("Parrot Zik Not connected")
            except bluetooth_paired_devices.ConnectionFailure:
                self.info("Failed to connect")
            else:
                if manager.api_version.startswith('1'):
                    interface = self.version_1_interface
                else:
                    interface = self.version_2_interface
                try:
                    interface.activate(manager)
                except resource_manager.DeviceDisconnected:
                    interface.deactivate()
                else:
                    self.autorefresh(self)
                    self.autorefresh.start(self, REFRESH_FREQUENCY)
                    self.reconnect.stop()

    def info(self, message):
        self.info_item.set_label(message)
        print(message)

    @repeat
    def autorefresh(self):
        if self.active_interface:
            self.active_interface.refresh()
        else:
            self.reconnect.start(self, RECONNECT_FREQUENCY)
            self.autorefresh.stop()

    def main(self):
        self.reconnect.start(self, RECONNECT_FREQUENCY)
        SysIndicator.main(self)


if __name__ == "__main__":
    try:
        indicator = ParrotZikIndicator()
        indicator.main()
    except KeyboardInterrupt:
        pass