/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
17
import cgi
18
import dbus
19
import dbus.service
625.5.1 by James Westby
Import gobject in bzr-notify, as it is used there to set a timeout.
20
import gobject
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
21
import pynotify
22
from bzrlib.bzrdir import BzrDir
23
from bzrlib.osutils import format_date
24
from bzrlib.transport import get_transport
674.1.1 by Sense Hofstede
* Use Indicator Application rather than GtkStatusIcon
25
26
menu = NotifyPopupMenu()
682 by Jelmer Vernooij
Merge qense's indicator application work, but don't require appindicator to be installed.
27
try:
28
	import appindicator
29
except ImportError:
30
	icon = gtk.status_icon_new_from_file(icon_path("bzr-icon-64.png"))
31
	icon.connect('popup-menu', menu.display)
32
	icon.set_visible(False)
33
	hide_icon = lambda: icon.set_visible(False)
34
	show_icon = lambda: icon.set_visible(True)
35
else:
36
	indicator = appindicator.Indicator ("bzr-gtk-notify",
37
							  "bzr-icon-64",
38
							  appindicator.CATEGORY_APPLICATION_STATUS)
39
	indicator.set_status (appindicator.STATUS_PASSIVE)
40
	indicator.set_attention_icon("bzr-icon-64")
41
	indicator.set_menu(menu)
42
	hide_icon = lambda: indicator.set_status (appindicator.STATUS_PASSIVE)
43
	show_icon = lambda: indicator.set_status (appindicator.STATUS_ATTENTION)
674.1.1 by Sense Hofstede
* Use Indicator Application rather than GtkStatusIcon
44
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
45
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
46
	import dbus.glib
47
BROADCAST_INTERFACE = "org.bazaarvcs.plugins.dbus.Broadcast"
48
bus = dbus.SessionBus()
49
50
def catch_branch(revision_id, urls):
51
	# TODO: show all the urls, or perhaps choose the 'best'.
52
	url = urls[0]
53
	try:
54
		if isinstance(revision_id, unicode):
55
			revision_id = revision_id.encode('utf8')
56
		transport = get_transport(url)
57
		a_dir = BzrDir.open_from_transport(transport)
58
		branch = a_dir.open_branch()
59
		revno = branch.revision_id_to_revno(revision_id)
60
		revision = branch.repository.get_revision(revision_id)
61
		summary = 'New revision %d in %s' % (revno, url)
682 by Jelmer Vernooij
Merge qense's indicator application work, but don't require appindicator to be installed.
62
		body = 'Committer: %s\n' % revision.committer
505.1.3 by Jelmer Vernooij
Move notify icon to a separate script so it's easier to add to startup scripts.
63
		body += 'Date: %s\n' % format_date(revision.timestamp,
64
			revision.timezone)
65
		body += '\n'
66
		body += revision.message
67
		body = cgi.escape(body)
68
		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.
69
		def start_viz(notification=None, action=None, data=None):
70
			"""Start the viz program."""
682 by Jelmer Vernooij
Merge qense's indicator application work, but don't require appindicator to be installed.
71
			from bzrlib.plugins.gtk.commands import start_viz_window
625.4.1 by James Westby
Don't use actions in the notifications if the server doesn't support them.
72
			pp = start_viz_window(branch, revision_id)
73
			pp.show()
74
		def start_branch(notification=None, action=None, data=None):
75
			"""Start a Branch dialog"""
76
			from bzrlib.plugins.gtk.branch import BranchDialog
77
			bd = BranchDialog(remote_path=url)
78
			bd.run()
79
		if "actions" in pynotify.get_server_caps():
80
			nw.add_action("inspect", "Inspect", start_viz, None)
81
			nw.add_action("branch", "Branch", start_branch, None)
682 by Jelmer Vernooij
Merge qense's indicator application work, but don't require appindicator to be installed.
82
		show_icon()
625.4.1 by James Westby
Don't use actions in the notifications if the server doesn't support them.
83
		gobject.timeout_add(5000, hide_icon)
84
		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.
85
		nw.show()
86
	except Exception, e:
87
		print e
88
		raise
89
bus.add_signal_receiver(catch_branch,
90
						dbus_interface=BROADCAST_INTERFACE,
91
						signal_name="Revision")
92
pynotify.init("bzr-notify")
93
gtk.main()