~paulliu/friends/add-twitter-contacts

« back to all changes in this revision

Viewing changes to friends/utils/menus.py

  • Committer: Ken VanDine
  • Date: 2012-11-08 16:10:59 UTC
  • mfrom: (68.1.2 messaging_menu)
  • Revision ID: ken.vandine@canonical.com-20121108161059-noqi9c3f35b1vn9i
Merged messaging menu refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import subprocess
20
20
 
21
21
from gettext import gettext as _
22
 
 
23
 
Unity = None
24
 
Dbusmenu = None
25
 
try:
26
 
    from gi.repository import Unity, Dbusmenu
27
 
except ImportError:
28
 
    pass
 
22
from gi.repository import Gio
29
23
 
30
24
 
31
25
MessagingMenu = None
35
29
    pass
36
30
 
37
31
 
38
 
DESKTOP_ID = 'friends.desktop'
39
32
log = logging.getLogger(__name__)
40
33
 
41
34
 
63
56
 
64
57
 
65
58
class MenuManager:
66
 
    """Manage the Unity and indicator menus over dbus."""
 
59
    """Manage the indicator menus over dbus."""
67
60
    messaging = None
68
 
    launcher = None
69
61
 
70
62
    def __init__(self, refresh_callback, shutdown_callback):
71
63
        self._refresh = refresh_callback
73
65
        # Only do the menu initializations if they are available.
74
66
        if MessagingMenu:
75
67
            self.init_messaging_menu()
76
 
        if Unity and Dbusmenu:
77
 
            self.init_dbus_menu()
78
68
 
79
69
    def init_messaging_menu(self):
80
 
        self.messaging = MessagingMenu.App(desktop_id=DESKTOP_ID)
 
70
        self.messaging = MessagingMenu.App(desktop_id='gwibber.desktop')
81
71
        self.messaging.register()
82
72
 
83
 
    def init_dbus_menu(self):
84
 
        self.launcher = Unity.LauncherEntry.get_for_desktop_id(DESKTOP_ID)
85
 
        quicklist = Dbusmenu.Menuitem.new()
86
 
        # The update status menu item.
87
 
        post_menu = Dbusmenu.Menuitem.new()
88
 
        post_menu.property_set(Dbusmenu.MENUITEM_PROP_LABEL,
89
 
                               _('Update Status'))
90
 
        post_menu.connect('item-activated', helper('friends-poster'))
91
 
        # The refresh menu item.
92
 
        refresh_menu = Dbusmenu.Menuitem.new()
93
 
        refresh_menu.property_set(Dbusmenu.MENUITEM_PROP_LABEL, _('Refresh'))
94
 
        refresh_menu.connect('item-activated', self._refresh)
95
 
        # The preferences menu item.
96
 
        preferences_menu = Dbusmenu.Menuitem.new()
97
 
        preferences_menu.property_set(Dbusmenu.MENUITEM_PROP_LABEL,
98
 
                                      _('Preferences'))
99
 
        preferences_menu.connect('item-activated',
100
 
                                 helper('friends-preferences'))
101
 
        # The quit menu item.
102
 
        quit_menu = Dbusmenu.Menuitem.new()
103
 
        quit_menu.property_set(Dbusmenu.MENUITEM_PROP_LABEL, _('Quit'))
104
 
        quit_menu.connect('item-activated', self._shutdown)
105
 
        # Make all the menu items visible.
106
 
        for menu in (post_menu, refresh_menu, preferences_menu, quit_menu):
107
 
            menu.property_set_bool(Dbusmenu.MENUITEM_PROP_VISIBLE, True)
108
 
            quicklist.child_append(menu)
109
 
        # Initialize the unread count to zero.
110
 
        self.launcher.set_property('quicklist', quicklist)
111
 
        self.update_unread_count(0)
112
 
 
113
73
    def update_unread_count(self, count):
114
 
        """Update the unread count.  If zoer, make it invisible."""
115
 
        if self.launcher:
116
 
            self.launcher.set_property('count', count)
117
 
            self.launcher.set_property('count_visible', bool(count))
118
 
 
119
 
 
120
 
# XXX This bit allows you to test this file by running it.  This doesn't fit
121
 
# very well into the larger testsuite architecture so we could probably
122
 
# improve this somehow, but I'm not sure how.
123
 
#
124
 
# In the meantime, run this like so (from the directory containing the
125
 
# setup.py file):
126
 
#
127
 
#   $ python3 -m friends.utils.menus
128
 
#
129
 
# You should see the Friends icon in the launcher and switcher get the value
130
 
# '20'.  If Friends is not running, pass in a second positional argument which
131
 
# is the name of a desktop file for an application that is running, e.g.:
132
 
#
133
 
#   $ python3 -m friends.utils.menus gnome-terminal.desktop
134
 
#
135
 
# Pass in another argument to set some number other than 20.  If you pass in
136
 
# 0, the count will not be visible.
137
 
#
138
 
# Hit C-c to quit.
139
 
 
140
 
if __name__ == '__main__':
141
 
    import sys
142
 
    from gi.repository import GObject
143
 
 
144
 
    if len(sys.argv) > 1:
145
 
        DESKTOP_ID = sys.argv[1]
146
 
    if len(sys.argv) > 2:
147
 
        count = int(sys.argv[2])
148
 
    else:
149
 
        count = 20
150
 
 
151
 
    def stub(*ignore):
152
 
        pass
153
 
 
154
 
    menu = MenuManager(stub, stub)
155
 
    menu.update_unread_count(count)
156
 
 
157
 
    try:
158
 
        GObject.MainLoop().run()
159
 
    except KeyboardInterrupt:
160
 
        pass
 
74
        """Update the unread count. If zero, make it invisible."""
 
75
        if not self.messaging:
 
76
            return
 
77
 
 
78
        if self.messaging.has_source('unread') and count > 0:
 
79
            self.messaging.set_source_count('unread', count)
 
80
        elif count > 0:
 
81
            self.messaging.append_source_with_count(
 
82
                'unread',
 
83
                None,
 
84
                'Unread',
 
85
                count)
 
86
        elif self.messaging.has_source('unread') and count < 1:
 
87
            self.messaging.remove_source('unread')