aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik/parrot_zik_tray
blob: ae57e4dff9ec48b27013d45947677ae0783c8f5a (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
from threading import Lock
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

REFRESH_FREQUENCY = 30000
RECONNECT_FREQUENCY = 5000


class repeat(object):
    def __init__(self, f):
        self.f = f
        self.id = None
        self.lock = Lock()

    def __call__(self, cls):
        self.f(cls)

    def start(self, cls, frequency):
        self.lock.acquire()
        if not self.id:
            def run():
                self.f(cls)
                return True

            self.id = gtk.timeout_add(frequency, run)
        self.lock.release()

    def stop(self):
        self.lock.acquire()
        if self.id:
            gtk.timeout_remove(self.id)
            self.id = None
        self.lock.release()


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