~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/service/notifier.py

  • Committer: Marco Ferragina
  • Date: 2007-05-06 15:51:12 UTC
  • Revision ID: marco.ferragina@gmail.com-20070506155112-874uk2m8blrknyuf
Restructured package source dir. Now is much more organized. Implemented right click menu on account window treeview

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
try:
 
2
        import dbus
 
3
        from dbus.mainloop.glib import DBusGMainLoop
 
4
        HAVE_DBUS = True
 
5
except ImportError:
 
6
        HAVE_DBUS = False
 
7
 
 
8
import gtk
 
9
import gobject
 
10
import os
 
11
 
 
12
#import webbrowser
 
13
 
 
14
cwd = os.getcwd()
 
15
from lib.common import *
 
16
from lib.gconfhelper import GconfHelper
 
17
from lib import utils
 
18
 
 
19
ICON = os.path.join(cwd, NOTIFY_ICON)
 
20
GLADE_FILE = os.path.join(GLADE_BASE_PATH, "popup_notification_window.glade")
 
21
 
 
22
notification_instances = 0
 
23
class NotificationWindow:
 
24
        def __init__(self, title, text, icon = ICON, msec = 3000):
 
25
                global notification_instances
 
26
                notification_instances += 1
 
27
 
 
28
                widgets = gtk.glade.XML(GLADE_FILE, domain="planimo")
 
29
                self.window = widgets.get_widget('popup_notification_window')
 
30
                close_button = widgets.get_widget('close_button')
 
31
                event_type_label = widgets.get_widget('event_type_label')
 
32
                
 
33
                event_description_label = widgets.get_widget('event_description_label')
 
34
                eventbox = widgets.get_widget('eventbox')
 
35
                image = widgets.get_widget('notification_image')
 
36
                
 
37
 
 
38
                event_type_label.set_markup(
 
39
                        '<span foreground="black" weight="bold">%s</span>' % title)
 
40
 
 
41
                self.window.modify_bg(gtk.STATE_NORMAL, 
 
42
                                        gtk.gdk.color_parse('#dab255'))
 
43
 
 
44
 
 
45
                bg_color = '#ffffbf'
 
46
                popup_bg_color = gtk.gdk.color_parse(bg_color)
 
47
                close_button.modify_bg(gtk.STATE_NORMAL, popup_bg_color)
 
48
                eventbox.modify_bg(gtk.STATE_NORMAL, popup_bg_color)
 
49
                event_description_label.set_markup(
 
50
                        '<span foreground="black">%s</span>' % text)    
 
51
                        
 
52
                # set the image
 
53
                image.set_from_file(icon)
 
54
                
 
55
                # position the window to bottom-right of screen
 
56
                window_width, window_height = self.window.get_size()
 
57
                pos_x = gtk.gdk.screen_width() - window_width - 1
 
58
                pos_y = gtk.gdk.screen_height() - \
 
59
                                notification_instances * (window_height + 2)
 
60
                self.window.move(pos_x, pos_y)
 
61
 
 
62
                widgets.signal_autoconnect(self)
 
63
                self.window.show_all()
 
64
                gobject.timeout_add(msec, self.on_timeout)
 
65
 
 
66
        def on_close_button_clicked(self, widget):
 
67
                global notification_instances
 
68
 
 
69
                self.window.destroy()
 
70
                notification_instances -= 1
 
71
 
 
72
        def on_timeout(self):
 
73
                global notification_instances
 
74
 
 
75
                self.window.destroy()
 
76
                notification_instances -= 1
 
77
 
 
78
 
 
79
class Notifier:
 
80
        def __init__(self):
 
81
                self.gconf_helper = GconfHelper()
 
82
 
 
83
                if HAVE_DBUS:
 
84
                        dbus_loop = DBusGMainLoop()
 
85
                        self.session_bus = dbus.SessionBus(mainloop = dbus_loop)
 
86
 
 
87
                        obj = self.session_bus.get_object("org.freedesktop.Notifications", 
 
88
                                                        "/org/freedesktop/Notifications")
 
89
                        self.notif = dbus.Interface(obj, "org.freedesktop.Notifications")
 
90
                        self.notif.connect_to_signal("ActionInvoked", self.action_cb)
 
91
        
 
92
        def action_cb(self, id, act):
 
93
                if act == "openbrowser":
 
94
                        #try:
 
95
                        #       webbrowser.open_new_tab(GMAIL_URL)
 
96
                        #except:
 
97
                        #       webbrowser.open_new(GMAIL_URL)
 
98
                        utils.open_browser(GMAIL_URL)
 
99
                elif act == "mailreader":
 
100
                        utils.open_mail_reader()
 
101
 
 
102
        def notify(self, title, message, 
 
103
                        icon = ICON, error = False, 
 
104
                        msec = 3000, force = False,
 
105
                        buttons = True):
 
106
 
 
107
                if error:
 
108
                        notify_errors = self.gconf_helper.get_key("notify_errors")
 
109
                        if not notify_errors and not force: return
 
110
                if not error:
 
111
                        mustplaysound = self.gconf_helper.get_key("play_sounds_on_new_mails")
 
112
                        if mustplaysound:
 
113
                                utils.playsnd(SND_NOTIFY)
 
114
 
 
115
                        runcommand = self.gconf_helper.get_key("exec_command")
 
116
                        if runcommand:
 
117
                                cmd = self.gconf_helper.get_key("new_mail_command")
 
118
                                cmdline = cmd.split()
 
119
                                utils.invoke_subprocess(cmdline)
 
120
 
 
121
                mustnotify = self.gconf_helper.get_key("display_notifications")
 
122
                if not mustnotify and not force: return
 
123
 
 
124
                iconfile = "file://" + icon
 
125
                
 
126
                if buttons:
 
127
                        actions = ["openbrowser", _("Go to Gmail Account"),\
 
128
                                        "mailreader", _("Open mail reader")]
 
129
                else:
 
130
                        actions = []
 
131
 
 
132
                if not HAVE_DBUS:
 
133
                        # Revert to classic window
 
134
                        NotificationWindow(title, message, icon, msec)
 
135
                else:
 
136
                        try:
 
137
                                self.notif.Notify(
 
138
                                        "cgmail",
 
139
                                        dbus.UInt32(0), 
 
140
                                        iconfile, 
 
141
                                        title, 
 
142
                                        message,
 
143
                                        actions, 
 
144
                                        [], 
 
145
                                        dbus.Int32(msec), 
 
146
                                        dbus.UInt32(0)
 
147
                                        )
 
148
                        except:
 
149
                                # new api
 
150
                                try:
 
151
                                        self.notif.Notify(
 
152
                                                "cgmail",
 
153
                                                dbus.UInt32(0), 
 
154
                                                iconfile, 
 
155
                                                title, 
 
156
                                                message,
 
157
                                                actions, 
 
158
                                                [], 
 
159
                                                dbus.Int32(msec)
 
160
                                                )
 
161
                                except Exception, detail:
 
162
                                        # Nothing to do with dbus: 
 
163
                                        # Revert to classic window
 
164
                                        print "Warning: Error while trying to use notification-daemon.\n Reverting to classic method.\n Please upgrade your dbus and libnotify version", detail
 
165
                                        NotificationWindow(title, message, icon, msec)
 
166
 
 
167
 
 
168
if __name__ == "__main__":
 
169
        n = Notifier()
 
170
        msg = '<span size="larger">Test</span> ok'
 
171
        n.notify("Title", msg, True, msec=10000)
 
172
 
 
173
        import gobject
 
174
        loop = gobject.MainLoop()
 
175
        loop.run()
 
176