/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to bzr-notify

  • Committer: Vincent Ladeuil
  • Date: 2010-03-01 08:52:45 UTC
  • mfrom: (682.1.2 setup)
  • Revision ID: v.ladeuil+lp@free.fr-20100301085245-506msq0tu9bmji95
Guard setup() call when not loaded as __main__

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
14
from bzrlib.plugins.gtk.notify import NotifyPopupMenu
 
15
gtk = open_display()
 
16
 
 
17
import cgi
 
18
import dbus
 
19
import dbus.service
 
20
import gobject
 
21
import pynotify
 
22
from bzrlib.bzrdir import BzrDir
 
23
from bzrlib.osutils import format_date
 
24
from bzrlib.transport import get_transport
 
25
 
 
26
menu = NotifyPopupMenu()
 
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)
 
44
 
 
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)
 
62
                body = 'Committer: %s\n' % revision.committer
 
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)
 
69
                def start_viz(notification=None, action=None, data=None):
 
70
                        """Start the viz program."""
 
71
                        from bzrlib.plugins.gtk.commands import start_viz_window
 
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)
 
82
                show_icon()
 
83
                gobject.timeout_add(5000, hide_icon)
 
84
                nw.set_timeout(5000)
 
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()