~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to recent_notifications/Main.py

  • Committer: Jason Conti
  • Date: 2011-04-24 20:48:00 UTC
  • Revision ID: jason.conti@gmail.com-20110424204800-znrodggqqte9nvxb
Ported Config.py to the unity app, promoting it to a proper GObject in the process, so value changes are handled by signals instead of calling functions. Adding a basic set of tests for the new Config class. Adding __init__.py to unity to make it a proper module for testing (will need renaming later). Changed the edge radius to 5px for the message icon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
import gnomeapplet
10
10
import gtk
 
11
import logging
11
12
import os.path
12
13
 
13
14
from string import Template
14
15
 
15
16
from About import show_about
 
17
from Config import Config
16
18
from Icon import clear_icon_cache, load_icon
 
19
from ImageToggleButton import ImageToggleButton
17
20
from MessageWindow import MessageWindow
18
21
from Notification import Notification
 
22
from Preferences import show_preferences
19
23
from Translations import _, ngettext
20
24
 
 
25
logger = logging.getLogger("Main")
 
26
 
21
27
class Main(object):
22
28
  """The main applet wrapper."""
23
29
  def __init__(self, applet, iid):
24
30
    self._applet = applet
25
 
    self._applet.connect("size-allocate", self.on_allocate)
 
31
    self._applet.set_background_widget(applet)
 
32
    self._applet.connect("change-size", self.on_change_size)
 
33
    self._applet.connect("change-orient", self.on_change_orient)
26
34
 
27
35
    self._unread_messages = 0
28
 
 
29
 
    size = self.get_icon_size(self.get_applet_size())
30
 
    self.load_icons(size)
31
 
 
32
 
    self._image = gtk.Image()
33
 
    self._image.set_from_pixbuf(self._read_icon)
34
 
 
35
 
    self._button = gtk.ToggleButton()
36
 
    self._button.set_relief(gtk.RELIEF_NONE)
37
 
    self._button.set_image(self._image)
38
 
    self._button.connect("button_press_event", self.do_not_eat_button_press)
 
36
    self._show_count = False
 
37
 
 
38
    self._applet_size = self.get_applet_size()
 
39
 
 
40
    icon_size = self.get_icon_size(self._applet_size)
 
41
    self.load_icons(icon_size)
 
42
 
 
43
    self._button = ImageToggleButton()
 
44
    self._button.set_from_pixbuf(self._read_icon)
 
45
    self._button.set_orient(self.get_orient())
39
46
    self._button.connect("toggled", self.on_button_toggled)
 
47
    self._button.show()
40
48
    self._applet.add(self._button)
41
49
 
42
50
    self._applet.setup_menu(Template("""
44
52
        <menuitem name="Clear" verb="Clear" _label="$clear_label" pixtype="stock" pixname="gtk-clear"/>
45
53
        <separator/>
46
54
        <menuitem name="About" verb="About" _label="$about_label" pixtype="stock" pixname="gtk-about"/>
 
55
        <menuitem name="Preferences" verb="Preferences" _label="$preferences_label" pixtype="stock" pixname="gtk-preferences"/>
47
56
      </popup>
48
57
      """).substitute(
49
58
          about_label=_("_About"), 
50
 
          clear_label=_("_Clear Messages")), [
 
59
          clear_label=_("_Clear Messages"),
 
60
          preferences_label=_("_Preferences")), [
51
61
      ("Clear", lambda a, b: self.on_clear_messages(a)),
52
 
      ("About", lambda a, b: self.on_show_about(a))
 
62
      ("About", lambda a, b: self.on_show_about(a)),
 
63
      ("Preferences", lambda a, b: self.on_show_preferences(a))
53
64
      ])
54
65
 
55
66
    self._window = MessageWindow(self)
59
70
 
60
71
    self.update()
61
72
 
 
73
    self._blacklist = Config("~/.config/recent-notifications/blacklist")
 
74
 
 
75
    self._preferences = Config("~/.config/recent-notifications/preferences")
 
76
    self._preferences.set_if_unset("debug", False)
 
77
    self._preferences.set_if_unset("message_limit", 0)
 
78
    self._preferences.set_if_unset("show_count", False)
 
79
    self._preferences.add_listener("debug", self.on_preference_change)
 
80
    self._preferences.add_listener("message_limit", self.on_preference_change)
 
81
    self._preferences.add_listener("show_count", self.on_preference_change)
 
82
    self._preferences.notify_all()
 
83
 
62
84
    self._notify = Notification()
 
85
    self._notify.set_blacklist(self._blacklist)
63
86
    self._notify.connect("message-received", self.on_message_received)
64
87
 
 
88
    self._theme = gtk.icon_theme_get_default()
 
89
    self._theme.connect("changed", self.on_theme_changed)
 
90
 
 
91
  def add_to_blacklist(self, app_name):
 
92
    """Adds the given app_name to the blacklist."""
 
93
    self._blacklist.set(app_name, True)
 
94
 
 
95
  def get_blacklist_items(self):
 
96
    """Returns a list of (app_name, boolean) tuples for the blacklist items."""
 
97
    result = []
 
98
    for key in self._blacklist:
 
99
      result.append((key, self._blacklist.get_bool(key)))
 
100
    return result
 
101
 
 
102
  def set_debug(self, debug):
 
103
    """Enables/disables debug messages."""
 
104
    global_logger = logging.getLogger("")
 
105
    if debug:
 
106
      global_logger.setLevel(logging.DEBUG)
 
107
    else:
 
108
      global_logger.setLevel(logging.ERROR)
 
109
 
65
110
  def get_applet_size(self):
66
111
    """Gets the size of the applet."""
67
112
    return self._applet.get_size()
114
159
    else:
115
160
      icon = self._read_icon
116
161
 
117
 
    if icon != self._image.get_pixbuf():
118
 
      self._image.set_from_pixbuf(icon)
 
162
    if icon != self._button.get_pixbuf():
 
163
      self._button.set_from_pixbuf(icon)
119
164
 
120
165
  def update_unread_messages(self):
121
166
    """Updates the unread message tooltip."""
122
167
    n = self._unread_messages
123
168
    label = ngettext("1 unread message", "{0} unread messages", n).format(n)
124
169
    self._button.set_tooltip_text(label)
 
170
    if self._show_count:
 
171
      self._button.set_label(str(n))
 
172
 
 
173
  def show_window(self):
 
174
    """Shows the applet window"""
 
175
    self._button.set_has_tooltip(False)
 
176
    self._window.show_dropdown()
 
177
 
 
178
  def hide_window(self):
 
179
    """Hides the applet window"""
 
180
    self._button.set_has_tooltip(True)
 
181
    self._window.hide_dropdown()
 
182
    self._unread_messages = 0
 
183
    self.update()
125
184
 
126
185
  # Callbacks
127
186
 
128
 
  def do_not_eat_button_press(self, widget, event):
129
 
    """Adapted from the GNOME clock applet."""
130
 
    if event.button != 1:
131
 
      widget.stop_emission("button_press_event")
132
 
 
133
 
    return False
134
 
 
135
 
  def on_allocate(self, applet, alloc):
 
187
  def on_change_size(self, applet, size):
136
188
    """Update the icon size if the panel size changes."""
137
 
    if self.get_orient() in [gnomeapplet.ORIENT_DOWN, gnomeapplet.ORIENT_UP]:
138
 
      size_alloc = alloc.height
139
 
    else:
140
 
      size_alloc = alloc.width
141
 
 
142
 
    icon_size = self.get_icon_size(size_alloc)
143
 
 
144
 
    if self._read_icon != None and icon_size != self._read_icon.get_width():
145
 
      self.load_icons(size)
146
 
      self.update_applet_image()
147
 
 
148
 
  def on_applet_destroyed(self, *args):
 
189
    if self._applet_size == size:
 
190
      return
 
191
 
 
192
    self._applet_size = size
 
193
    icon_size = self.get_icon_size(size)
 
194
    self.load_icons(icon_size)
 
195
    self.update_applet_image()
 
196
 
 
197
  def on_change_orient(self, applet, orient):
 
198
    """Updates the button orientation when the panel changes orientation"""
 
199
    self._button.set_orient(orient)
 
200
 
 
201
  def on_applet_destroyed(self, applet):
149
202
    """Stop the message notifications before destroying the applet."""
150
203
    self._notify.close()
151
204
 
152
 
  def on_button_toggled(self, button, *args):
 
205
  def on_button_toggled(self, button):
153
206
    """Show/Hide the message window when the button is toggled."""
154
207
    if button.get_active():
155
 
      self._window.show_all()
156
 
      self._window.align_with_parent()
 
208
      self.show_window()
157
209
    else:
158
 
      self._window.hide_all()
159
 
      self._window.clear_selections()
160
 
      self._unread_messages = 0
161
 
      self.update()
 
210
      self.hide_window()
162
211
 
163
 
  def on_clear_messages(self, *args):
 
212
  def on_clear_messages(self, item):
164
213
    """Clear messages from the message window."""
165
214
    self._window.clear_messages()
166
215
    clear_icon_cache()
167
216
 
168
217
  def on_message_received(self, monitor, message):
169
218
    """Received a message from the notifier."""
170
 
    # Discard volume notifications
171
 
    if not message.is_volume_notification():
172
 
      self._window.add_message(message)
173
 
      self._unread_messages += 1
174
 
      self.update()
 
219
    self._window.add_message(message)
 
220
    self._unread_messages += 1
 
221
    self.update()
175
222
 
176
 
  def on_show_about(self, *args):
 
223
  def on_show_about(self, item):
177
224
    """Show the about dialog."""
178
225
    show_about(self)
179
226
 
 
227
  def on_show_preferences(self, item):
 
228
    """Show the preferences dialog."""
 
229
    show_preferences(self)
 
230
 
 
231
  def on_preference_change(self, key, value):
 
232
    """Triggers actions on preference changes."""
 
233
    if key == "debug":
 
234
      self.set_debug(self._preferences.get_bool(key, False))
 
235
    elif key == "message_limit":
 
236
      self._window.set_message_limit(self._preferences.get_int(key, 0))
 
237
    elif key == "show_count":
 
238
      self._show_count = self._preferences.get_bool(key, False)
 
239
      if self._show_count:
 
240
        self.update()
 
241
      else:
 
242
        self._button.set_label("")
 
243
 
 
244
  def on_theme_changed(self, theme):
 
245
    """Updates applet icons when the theme changes"""
 
246
    clear_icon_cache()
 
247
    icon_size = self.get_icon_size(self._applet_size)
 
248
    self.load_icons(icon_size)
 
249
    self.update_applet_image()
 
250