aboutsummaryrefslogtreecommitdiff
path: root/ParrotZikTray
blob: 05285ef253d781bd5f0afe2a4965e95232b1ba7b (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python

import sys
import gtk
import re
import os
import ParrotZik
import BluetoothPairedDevices

UPDATE_FREQUENCY = 30 # seconds

class ParrotZikIndicator:
    def __init__(self):

        self.menu_setup()      
                
        if sys.platform=="linux2":
            import appindicator
            self.icon_directory = os.path.sep + 'usr' + os.path.sep+ 'share' + os.path.sep+'icons' + os.path.sep+'zik'+ os.path.sep
            self.statusicon = appindicator.Indicator("new-parrotzik-indicator",
                                           "indicator-messages",
                                           appindicator.CATEGORY_APPLICATION_STATUS)
            self.statusicon.set_status(appindicator.STATUS_ACTIVE)
            self.statusicon.set_icon_theme_path(self.icon_directory)            
            self.statusicon.set_menu(self.menu)
        else:
            print "Win32"
            self.icon_directory = os.path.dirname(os.path.realpath(sys.argv[0])) + os.path.sep+ 'share' + os.path.sep+'icons' + os.path.sep+'zik'+ os.path.sep
            self.statusicon = gtk.StatusIcon()            
            self.statusicon.connect("popup-menu", self.gtk_right_click_event)
            self.statusicon.set_tooltip("Parrot Zik")
            self.menu_shown=False            
            sys.stdout = open("debug.log", "w")
            sys.stderr = open("debug.log", "w")
        
        self.setIcon("audio-headset")
        self.connected=False

        self.p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}')

        return

    def pos(menu, ignore, icon):
        return (Gtk.StatusIcon.position_menu(menu, icon))

    def setIcon(self,   name):
        if sys.platform=="linux2":
            self.statusicon.set_icon(name)
        else:
            self.statusicon.set_from_file(self.icon_directory+name+'.png') 

    def gtk_right_click_event(self, icon, button, time):
        if not self.menu_shown:
            self.menu_shown=True
            self.menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon)
        else:
            self.menu_shown=False
            self.menu.popdown()

    def menu_setup(self):
        self.menu = gtk.Menu()

        self.info_item = gtk.MenuItem("Parrot Zik Not connected..")  
        self.info_item.set_sensitive(False)      
        self.info_item.show()
        self.menu.append(self.info_item)

        self.check = gtk.CheckMenuItem("Noise Cancellation")
        self.check.connect("activate", self.toggleANC)
        self.check.set_sensitive(False)
        self.check.show()
        self.menu.append(self.check)

        self.check2 = gtk.CheckMenuItem("Auto Connection")
        self.check2.connect("activate", self.toggleAuto)
        self.check2.set_sensitive(False)
        self.check2.show()
        self.menu.append(self.check2)

        self.check3 = gtk.CheckMenuItem("Lou Reed Mode")
        self.check3.connect("activate", self.toggleLouReedMode)
        self.check3.set_sensitive(False)
        self.check3.show()
        self.menu.append(self.check3)

        self.check4 = gtk.CheckMenuItem("Concert Hall Mode")
        self.check4.connect("activate", self.toggleLouReedMode)
        self.check4.set_sensitive(False)
        self.check4.show()
        self.menu.append(self.check4)

        self.about = gtk.MenuItem()
        self.about.set_label("About")
        self.about.connect("activate", self.show_about_dialog)
        self.about.show()
        self.menu.append(self.about)

        self.quit_item = gtk.MenuItem("Quit")
        self.quit_item.connect("activate", self.quit)
        self.quit_item.show()
        self.menu.append(self.quit_item)

    def EstablishConnection(self):
        if self.connected:
            if not self.parrot.sock:
                print "Lost connection"
                self.connected = False
            else:
                print "Connection already established"
        else:
            mac=BluetoothPairedDevices.ParrotZikMac()
            if mac:
                self.parrot = ParrotZik.ParrotZik(mac)
                if not self.parrot.sock:
                    print "Failed to connect to Parrot Zik %s" % mac
                    return False

                self.connected = True
                self.name = self.parrot.getFriendlyName()
                self.version = self.parrot.getVersion()

                self.check.set_sensitive(True)
                self.check2.set_sensitive(True)
                self.check3.set_sensitive(True)
                self.check4.set_sensitive(True)

                if self.parrot.getNoiseCancel() == "true":
                    self.check.set_active(True)
                else:
                    self.check.set_active(False)

                if self.parrot.getAutoConnection() == "true":
                    self.check2.set_active(True)
                else:
                    self.check2.set_active(False)

                if self.parrot.getLouReedMode() == "true":
                    self.check3.set_active(True)
                else:
                    self.check3.set_active(False)

                if self.parrot.getParrotConcertHall() == "true":
                    self.check4.set_active(True)
                else:
                    self.check4.set_active(False)

                self.CheckBattery()
        return True

    def toggleANC(self,widget):
        if self.connected:
            if self.check.get_active():
                self.parrot.setNoiseCancel("true")
            else:
                self.parrot.setNoiseCancel("false")

    def toggleAuto(self,widget):
        if self.connected:
            if self.check2.get_active():
                self.parrot.setAutoConnection("true")
            else:
                self.parrot.setAutoConnection("false")

    def toggleLouReedMode(self,widget):
        if self.connected:
            if self.check3.get_active():
                self.parrot.setLouReedMode("true")
            else:
                self.parrot.setLouReedMode("false")            
            if self.parrot.getLouReedMode() == "true":
                self.check3.set_active(True)
            else:
                self.check3.set_active(False)

            if self.parrot.getParrotConcertHall() == "true":
                self.check4.set_active(True)
            else:
                self.check4.set_active(False)

    def toggleParrotConcertHall(self,widget):
        if self.connected:
            if self.check4.get_active():
                self.parrot.setParrotConcertHall("true")
            else:
                self.parrot.setParrotConcertHall("false")
            
            if self.parrot.getLouReedMode() == "true":
                self.check3.set_active(True)
            else:
                self.check3.set_active(False)

            if self.parrot.getParrotConcertHall() == "true":
                self.check4.set_active(True)
            else:
                self.check4.set_active(False)

    def CheckBattery(self):
        if self.connected:
            print "Updating battery"
            self.batteryLevel = int(self.parrot.getBatteryLevel())
            
            if self.parrot.BatteryCharging:
                self.batteryLevel = "Charging"
                self.setIcon("zik-battery-charging")
                self.batteryLevel="Unknown"
                self.batteryState="Charging"
            elif self.batteryLevel>80:
                self.setIcon("zik-battery-100")
                self.batteryState="In Use"
            elif self.batteryLevel>60:
                self.setIcon("zik-battery-080")
                self.batteryState="In Use"
            elif self.batteryLevel>40:                
                self.setIcon("zik-battery-060")
                self.batteryState="In Use"
            elif self.batteryLevel>20:
                self.setIcon("zik-battery-040")
                self.batteryState="In Use"
            else:
                self.setIcon("zik-battery-low")
                self.batteryState="In Use"

            self.info_item.set_label("Connected to: "+self.name+
                                    "\nFirmware version: "+self.version+
                                    "\nState: "+self.batteryState+
                                    "\nBattery Level: "+str(self.batteryLevel))
        else:
            self.setIcon("zik-audio-headset")
            self.info_item.set_label("Parrot Zik Not connected..")
            self.check.set_sensitive(False)
            self.check2.set_sensitive(False)
            self.check3.set_sensitive(False)
            self.check4.set_sensitive(False)
        return True

    def show_about_dialog(self, widget):
        about_dialog = gtk.AboutDialog()

        about_dialog.set_destroy_with_parent(True)
        about_dialog.set_name("Parrot Zik Tray")
        about_dialog.set_version("0.1")
        about_dialog.set_authors(["Dmitry Moiseev m0sia@m0sia.ru"])
        about_dialog.run()
        about_dialog.destroy()

    def main(self):
        self.EstablishConnection()
        gtk.timeout_add(UPDATE_FREQUENCY * 1000, self.EstablishConnection)
        gtk.timeout_add(UPDATE_FREQUENCY * 1000, self.CheckBattery)
        gtk.main()       

    def quit(self, widget):
        sys.exit(0)

if __name__ == "__main__":
    indicator = ParrotZikIndicator()
    indicator.main()