3
from dbus.mainloop.glib import DBusGMainLoop
15
from lib.common import *
16
from lib.gconfhelper import GconfHelper
19
ICON = os.path.join(cwd, NOTIFY_ICON)
20
GLADE_FILE = os.path.join(GLADE_BASE_PATH, "popup_notification_window.glade")
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
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')
33
event_description_label = widgets.get_widget('event_description_label')
34
eventbox = widgets.get_widget('eventbox')
35
image = widgets.get_widget('notification_image')
38
event_type_label.set_markup(
39
'<span foreground="black" weight="bold">%s</span>' % title)
41
self.window.modify_bg(gtk.STATE_NORMAL,
42
gtk.gdk.color_parse('#dab255'))
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)
53
image.set_from_file(icon)
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)
62
widgets.signal_autoconnect(self)
63
self.window.show_all()
64
gobject.timeout_add(msec, self.on_timeout)
66
def on_close_button_clicked(self, widget):
67
global notification_instances
70
notification_instances -= 1
73
global notification_instances
76
notification_instances -= 1
81
self.gconf_helper = GconfHelper()
84
dbus_loop = DBusGMainLoop()
85
self.session_bus = dbus.SessionBus(mainloop = dbus_loop)
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)
92
def action_cb(self, id, act):
93
if act == "openbrowser":
95
# webbrowser.open_new_tab(GMAIL_URL)
97
# webbrowser.open_new(GMAIL_URL)
98
utils.open_browser(GMAIL_URL)
99
elif act == "mailreader":
100
utils.open_mail_reader()
102
def notify(self, title, message,
103
icon = ICON, error = False,
104
msec = 3000, force = False,
108
notify_errors = self.gconf_helper.get_key("notify_errors")
109
if not notify_errors and not force: return
111
mustplaysound = self.gconf_helper.get_key("play_sounds_on_new_mails")
113
utils.playsnd(SND_NOTIFY)
115
runcommand = self.gconf_helper.get_key("exec_command")
117
cmd = self.gconf_helper.get_key("new_mail_command")
118
cmdline = cmd.split()
119
utils.invoke_subprocess(cmdline)
121
mustnotify = self.gconf_helper.get_key("display_notifications")
122
if not mustnotify and not force: return
124
iconfile = "file://" + icon
127
actions = ["openbrowser", _("Go to Gmail Account"),\
128
"mailreader", _("Open mail reader")]
133
# Revert to classic window
134
NotificationWindow(title, message, icon, msec)
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)
168
if __name__ == "__main__":
170
msg = '<span size="larger">Test</span> ok'
171
n.notify("Title", msg, True, msec=10000)
174
loop = gobject.MainLoop()