Imported Upstream version 0.0.2
[callnotify] / src / usr / lib / hildon-desktop / CallNotify.py
1 import gtk
2 import gobject
3 import hildondesktop
4 import sqlite3
5 import time
6 import dbus
7 import osso
8 from dbus.mainloop.glib import DBusGMainLoop
9
10
11 class CallNotify(hildondesktop.StatusMenuItem):
12     def __init__(self):
13                 hildondesktop.StatusMenuItem.__init__(self)
14                 
15                 self.path = "/home/user/.rtcom-eventlogger/el.db"
16                 
17                 # Prevent multiple timers to refresh the status icon
18                 self.stop = False
19                 
20                 # Load images
21                 self.loadImages()
22                 self.msgType = ""               
23                 self.toShow = True
24                 self.missed = self.getMissedCallsCount(False)
25                 self.missedSMS = self.getMissedCallsCount(True)
26                 self.missedLastCall = self.missed
27                 self.missedLastSMS = self.missedSMS
28                 
29                 # Register to handle screen off/on events
30                 osso_c = osso.Context("osso_test_device_on", "0.0.1", False)
31                 device = osso.DeviceState(osso_c)
32                 device.set_display_event_cb(self.state_cb)
33
34                 self.tmr_main = gobject.timeout_add(5000, self.handleMissedCall) 
35                 
36                 # add d-bus listener for removing notification after viewing missed call
37                 # Doing timeout_add with return False instead of explicitly raising a thread
38                 gobject.timeout_add(500, self.startDbusListeners)
39
40     def loadImages(self):
41                 # Load phone image
42                 #self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size("/home/user/phone.png",18,18)
43                 icon_theme = gtk.icon_theme_get_default()
44                 self.callPicture = icon_theme.load_icon("general_call", 18, gtk.ICON_LOOKUP_NO_SVG)
45                 self.smsPicture = gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/sms.png",18,18)
46                 
47                 # Load 5 numbers and the "+5" 
48                 self.imgList = []
49                 #self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/home/user/1.png",18,18))
50                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/1.png",18,18))
51                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/2.png",18,18))
52                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/3.png",18,18))
53                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/4.png",18,18))
54                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/5.png",18,18))
55                 self.imgList.append(gtk.gdk.pixbuf_new_from_file_at_size("/usr/share/CallNotify/more.png",18,18))
56                 
57         # Screen off event-handler
58     def state_cb(self, state):
59         if state == osso.device_state.OSSO_DISPLAY_OFF:
60                 gobject.source_remove(self.tmr_main)
61                 gobject.source_remove(self.tmr_ptr)
62         elif state == osso.device_state.OSSO_DISPLAY_ON:
63                 self.tmr_main = gobject.timeout_add(5000, self.handleMissedCall)
64                 self.show()
65         return False
66                 
67         # Method to define the way to add dbus signal receiver
68     def startDbusListeners(self):
69                 DBusGMainLoop(set_as_default=True)                             
70                 bus = dbus.SessionBus()                                        
71                 #bus.add_signal_receiver(self.stop_notification, "NotificationClosed", "org.freedesktop.Notifications", "org.freedesktop.Notifications", "/org/freedesktop/Notifications") 
72                 #bus.add_signal_receiver(self.handleMissedCall, "Notify", None, None, None)
73                 #bus.add_signal_receiver(self.handleMissedCall, "MembersChanged", None, None, None)
74                 bus.add_signal_receiver(self.smsReceived, "MessageReceived", None, None, None)
75                 bus.add_signal_receiver(self.smsRead, "NotificationClosed", "org.freedesktop.Notifications", None, "/org/freedesktop/Notifications")
76                 gobject.MainLoop().run()                                       
77                 return False
78     
79     def smsReceived(self, a):
80         if a[0].has_key('message-type'):
81                 if self.missedLastSMS == self.getMissedCallsCount(True):
82                         if self.msgType == "Call":
83                                 self.msgType = "Both"
84                         else:
85                                 self.msgType = "SMS"
86                         self.show()
87                 self.missedLastSMS = self.getMissedCallsCount(True)
88         
89     def smsRead(self, a):
90         self.stop_notification(a)
91         
92     def handleMissedCall(self): 
93                 if self.missedLastCall != self.getMissedCallsCount(False):
94                         if self.msgType == "SMS":
95                                 self.msgType = "Both"
96                         else:
97                                 self.msgType = "Call"
98                         self.show()
99                         self.missedLastCall = self.getMissedCallsCount(False)
100                 return True
101         
102     def stop_notification(self, a):
103                 self.set_status_area_icon(None)
104                 gobject.source_remove(self.tmr_ptr)
105                 self.set_status_area_icon(None)
106                 # Reset the notification (get recent missed call count)
107                 self.missed = self.getMissedCallsCount(False)
108                 self.missedSMS = self.getMissedCallsCount(True)
109                 self.missedLastCall = self.missed
110                 self.missedLastSMS = self.missedSMS
111                 self.stop = False
112                 self.msgType = ""
113
114     def theLoop(self):
115                 missedCalls = self.getMissedCallsCount(False)
116                 if self.missedLastCall != missedCalls:
117                         self.show()
118                         self.missedLastCall  = missedCalls
119                 return True
120
121     def getMissedCallsCount(self, isSms):
122                 eType = 3
123                 if isSms:
124                         eType=7
125                 conn = sqlite3.connect(self.path)
126                 cur = conn.cursor()
127                 cur.execute("select count(id) from Events where event_type_id = " + str(eType))
128                 return cur.fetchone()[0]
129
130     def show(self):
131                 # blink the icon every 1 second
132                 if not(self.stop):
133                         self.tmr_ptr = gobject.timeout_add(1000, self.blinkIcon)
134                         self.stop = True
135                         
136     def blinkIcon(self):
137                 if self.toShow:
138                         self.toShow = False
139                         img = self.callPicture
140                         if self.msgType == "SMS":
141                                 img = self.smsPicture
142                         self.set_status_area_icon(img)
143                         return True
144                 else:
145                         img = self.smsPicture
146                         isSMS = False
147                         counter = self.missed
148                         if self.msgType == "SMS":
149                                 counter = self.missedSMS
150                                 isSMS = True
151                         index = self.getMissedCallsCount(isSMS) - counter - 1
152                         if index >= 5:
153                                 index = 5
154                                 if index < 0:
155                                         index = 0
156                         if self.msgType != "Both":
157                                 img = self.imgList[index]
158                         self.toShow = True
159                         self.set_status_area_icon(img)
160                         return True
161                 
162 hd_plugin_type = CallNotify
163