/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
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
24
import pynotify
25
from bzrlib.bzrdir import BzrDir
26
from bzrlib import errors
27
from bzrlib.osutils import format_date
28
from bzrlib.transport import get_transport
29
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
30
	import dbus.glib
31
BROADCAST_INTERFACE = "org.bazaarvcs.plugins.dbus.Broadcast"
32
bus = dbus.SessionBus()
33
614 by Jelmer Vernooij
Hide notify icon when not notifying.
34
def hide_icon():
35
	icon.set_visible(False)
36
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
37
def catch_branch(revision_id, urls):
38
	# TODO: show all the urls, or perhaps choose the 'best'.
39
	url = urls[0]
40
	try:
41
		if isinstance(revision_id, unicode):
42
			revision_id = revision_id.encode('utf8')
43
		transport = get_transport(url)
44
		a_dir = BzrDir.open_from_transport(transport)
45
		branch = a_dir.open_branch()
46
		revno = branch.revision_id_to_revno(revision_id)
47
		revision = branch.repository.get_revision(revision_id)
48
		summary = 'New revision %d in %s' % (revno, url)
49
		body  = 'Committer: %s\n' % revision.committer
50
		body += 'Date: %s\n' % format_date(revision.timestamp,
51
			revision.timezone)
52
		body += '\n'
53
		body += revision.message
54
		body = cgi.escape(body)
55
		nw = pynotify.Notification(summary, body)
56
		def start_viz(notification=None, action=None, data=None):
57
			"""Start the viz program."""
58
			pp = start_viz_window(branch, revision_id)
59
			pp.show()
60
		def start_branch(notification=None, action=None, data=None):
61
			"""Start a Branch dialog"""
62
			from bzrlib.plugins.gtk.branch import BranchDialog
63
			bd = BranchDialog(remote_path=url)
64
			bd.run()
65
		nw.add_action("inspect", "Inspect", start_viz, None)
66
		nw.add_action("branch", "Branch", start_branch, None)
614 by Jelmer Vernooij
Hide notify icon when not notifying.
67
		icon.set_visible(True)
68
		gobject.timeout_add(5000, hide_icon)
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
69
		nw.set_timeout(5000)
70
		nw.show()
71
	except Exception, e:
72
		print e
73
		raise
74
bus.add_signal_receiver(catch_branch,
75
						dbus_interface=BROADCAST_INTERFACE,
76
						signal_name="Revision")
77
pynotify.init("bzr-notify")
78
gtk.main()