~olive-team/olive/trunk

505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
1
#!/usr/bin/env python
2
3
"""Run the bzr tray icon.
4
5
This is a background program which will pop up a notification on the users
6
screen when a commit occurs.
7
"""
8
9
from bzrlib.plugin import load_plugins
10
load_plugins()
11
12
from bzrlib.plugins.gtk import open_display, icon_path
13
553.1.1 by Jelmer Vernooij
Fix notify import.
14
from bzrlib.plugins.gtk.notify import NotifyPopupMenu
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
15
gtk = open_display()
16
menu = NotifyPopupMenu()
17
icon = gtk.status_icon_new_from_file(icon_path("bzr-icon-64.png"))
18
icon.connect('popup-menu', menu.display)
614 by Jelmer Vernooij
Hide notify icon when not notifying.
19
icon.set_visible(False)
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
20
21
import cgi
22
import dbus
23
import dbus.service
625.5.1 by James Westby
Import gobject in bzr-notify, as it is used there to set a timeout.
24
import gobject
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
25
import pynotify
26
from bzrlib.bzrdir import BzrDir
27
from bzrlib import errors
28
from bzrlib.osutils import format_date
29
from bzrlib.transport import get_transport
30
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
31
	import dbus.glib
32
BROADCAST_INTERFACE = "org.bazaarvcs.plugins.dbus.Broadcast"
33
bus = dbus.SessionBus()
34
625.4.1 by James Westby
Don't use actions in the notifications if the server doesn't support them.
35
def hide_icon():
36
	icon.set_visible(False)
37
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
38
def catch_branch(revision_id, urls):
39
	# TODO: show all the urls, or perhaps choose the 'best'.
40
	url = urls[0]
41
	try:
42
		if isinstance(revision_id, unicode):
43
			revision_id = revision_id.encode('utf8')
44
		transport = get_transport(url)
45
		a_dir = BzrDir.open_from_transport(transport)
46
		branch = a_dir.open_branch()
47
		revno = branch.revision_id_to_revno(revision_id)
48
		revision = branch.repository.get_revision(revision_id)
49
		summary = 'New revision %d in %s' % (revno, url)
50
		body  = 'Committer: %s\n' % revision.committer
51
		body += 'Date: %s\n' % format_date(revision.timestamp,
52
			revision.timezone)
53
		body += '\n'
54
		body += revision.message
55
		body = cgi.escape(body)
56
		nw = pynotify.Notification(summary, body)
625.4.1 by James Westby
Don't use actions in the notifications if the server doesn't support them.
57
		def start_viz(notification=None, action=None, data=None):
58
			"""Start the viz program."""
59
			pp = start_viz_window(branch, revision_id)
60
			pp.show()
61
		def start_branch(notification=None, action=None, data=None):
62
			"""Start a Branch dialog"""
63
			from bzrlib.plugins.gtk.branch import BranchDialog
64
			bd = BranchDialog(remote_path=url)
65
			bd.run()
66
		if "actions" in pynotify.get_server_caps():
67
			nw.add_action("inspect", "Inspect", start_viz, None)
68
			nw.add_action("branch", "Branch", start_branch, None)
614 by Jelmer Vernooij
Hide notify icon when not notifying.
69
		icon.set_visible(True)
625.4.1 by James Westby
Don't use actions in the notifications if the server doesn't support them.
70
		gobject.timeout_add(5000, hide_icon)
71
		nw.set_timeout(5000)
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
72
		nw.show()
73
	except Exception, e:
74
		print e
75
		raise
76
bus.add_signal_receiver(catch_branch,
77
						dbus_interface=BROADCAST_INTERFACE,
78
						signal_name="Revision")
79
pynotify.init("bzr-notify")
80
gtk.main()