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