~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to unity/Main.py

  • Committer: Jason Conti
  • Date: 2010-02-18 20:17:33 UTC
  • Revision ID: jason.conti@gmail.com-20100218201733-i671lnpp0g78nyco
Adding template for empty icon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""
3
 
Main.py
4
 
March 26, 2011
5
 
 
6
 
The main recent-notifications-unity application.
7
 
"""
8
 
 
9
 
import logging
10
 
import os
11
 
 
12
 
from gi.repository import GObject, Gtk
13
 
 
14
 
try:
15
 
  from gi.repository import Unity
16
 
except:
17
 
  Unity = None
18
 
 
19
 
import Timestamp
20
 
 
21
 
from About import show_about
22
 
from Config import Config
23
 
from MessageList import MessageList
24
 
from Notification import Notification
25
 
from Theme import Theme
26
 
from Translations import _
27
 
 
28
 
# Setup the application logging to file and stdout for errors
29
 
def setup_logging():
30
 
  log_format = "%(asctime)s %(name)s %(levelname)s: %(message)s"
31
 
 
32
 
  logging.basicConfig(level=logging.DEBUG, 
33
 
      filename=os.path.expanduser("~/.cache/recent-notifications.log"),
34
 
      format=log_format,
35
 
      filemode="w")
36
 
 
37
 
  console = logging.StreamHandler()
38
 
  console.setLevel(logging.ERROR)
39
 
  console.setFormatter(logging.Formatter(log_format))
40
 
 
41
 
  logging.getLogger("").addHandler(console)
42
 
 
43
 
setup_logging()
44
 
logger = logging.getLogger("Main")
45
 
 
46
 
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
47
 
 
48
 
PREFERENCES_PATH = "~/.config/recent-notifications/preferences"
49
 
 
50
 
class Column(object):
51
 
  """Maps column names to column numbers."""
52
 
  APP_NAME  = 0
53
 
  SEPARATOR = 1
54
 
  COUNT     = 2
55
 
 
56
 
class MainWindow(Gtk.Window):
57
 
  def __init__(self):
58
 
    GObject.GObject.__init__(self)
59
 
 
60
 
    self._default_size = 400
61
 
 
62
 
    self.set_title(_("Recent Notifications"))
63
 
    self.set_default_size(self._default_size, self._default_size)
64
 
    self.connect("destroy", self.on_destroy)
65
 
    self.connect("focus-in-event", self.on_focus_in)
66
 
    self.connect("focus-out-event", self.on_focus_out)
67
 
    self.connect("size-allocate", self.on_size_allocate)
68
 
 
69
 
    self._message_count = 0
70
 
 
71
 
    self._uimanager = Gtk.UIManager()
72
 
    self.add_accel_group(self._uimanager.get_accel_group())
73
 
 
74
 
    self._actiongroup = Gtk.ActionGroup("MenuBar")
75
 
    self._actiongroup.add_actions([
76
 
      ("Messages", None, _("_Messages"), None, None, None),
77
 
      ("Clear", Gtk.STOCK_CLEAR, None, None, None, self.on_clear),
78
 
      ("Quit", Gtk.STOCK_QUIT, None, None, None, self.on_destroy),
79
 
      ("Help", None, _("_Help"), None, None, None),
80
 
      ("About", Gtk.STOCK_ABOUT, None, None, None, self.on_show_about)
81
 
    ])
82
 
    self._uimanager.insert_action_group(self._actiongroup)
83
 
 
84
 
    self._uimanager.add_ui_from_string("""
85
 
      <ui>
86
 
        <menubar name="MenuBar">
87
 
          <menu action="Messages">
88
 
            <menuitem action="Clear"/>
89
 
            <menuitem action="Quit"/>
90
 
          </menu>
91
 
          <menu action="Help">
92
 
            <menuitem action="About"/>
93
 
          </menu>
94
 
        </menubar>
95
 
      </ui>
96
 
    """)
97
 
 
98
 
    self._vbox = Gtk.VBox()
99
 
    self.add(self._vbox)
100
 
 
101
 
    self._menubar = self._uimanager.get_widget("/MenuBar")
102
 
    self._vbox.pack_start(self._menubar, False, False, 0)
103
 
 
104
 
    self._message_list = MessageList()
105
 
    self._vbox.pack_start(self._message_list, True, True, 2)
106
 
 
107
 
    # app_name, is_separator, count
108
 
    self._combolist = Gtk.ListStore(str, bool, int)
109
 
    self._combobox = Gtk.ComboBox()
110
 
    self._combobox.set_model(self._combolist)
111
 
    self._combo_app_cell = Gtk.CellRendererText()
112
 
    self._combobox.pack_start(self._combo_app_cell, True)
113
 
    self._combobox.add_attribute(self._combo_app_cell, "text", Column.APP_NAME)
114
 
    self._combo_count_cell = Gtk.CellRendererText()
115
 
    self._combobox.pack_start(self._combo_count_cell, False)
116
 
    self._combobox.add_attribute(self._combo_count_cell, "text", Column.COUNT)
117
 
    self._combobox.set_row_separator_func(lambda m, i, *args: m.get_value(i, Column.SEPARATOR), None)
118
 
    self._combobox.connect("changed", self.on_combobox_changed)
119
 
    self._vbox.pack_start(self._combobox, False, False, 0)
120
 
 
121
 
    self.initialize_combobox()
122
 
 
123
 
    if Unity:
124
 
      self._launcher = Unity.LauncherEntry.get_for_desktop_id("recent-notifications.desktop")
125
 
    else:
126
 
      self._launcher = None
127
 
 
128
 
    self._notify = Notification()
129
 
    self._notify.connect("message-received", self.on_message_received)
130
 
 
131
 
    self._preferences = Config(PREFERENCES_PATH)
132
 
    self._preferences.set_if_unset("theme", "default")
133
 
 
134
 
    # load the theme
135
 
    theme_name = self._preferences.get("theme", "default")
136
 
    theme = Theme(theme_name)
137
 
    theme.apply_gtk_styles()
138
 
 
139
 
  def zero_count(self):
140
 
    """Zero the message count"""
141
 
    self._message_count = 0
142
 
    self.update_count()
143
 
 
144
 
  def increment_count(self):
145
 
    """Increments the message count"""
146
 
    self._message_count += 1
147
 
    self.update_count()
148
 
 
149
 
  def update_count(self):
150
 
    """Updates the message count display"""
151
 
    if self._launcher:
152
 
      self._launcher.set_property("count", self._message_count)
153
 
      if self._message_count > 0:
154
 
        self._launcher.set_property("count_visible", True)
155
 
      else:
156
 
        self._launcher.set_property("count_visible", False)
157
 
 
158
 
  def increment_count_column(self, iter):
159
 
    """Increments the count column of the given iter"""
160
 
    count = self._combolist.get_value(iter, Column.COUNT)
161
 
    self._combolist.set_value(iter, Column.COUNT, count + 1)
162
 
 
163
 
  def increment_combobox(self, app_name):
164
 
    """Updates the combobox app names and message counts."""
165
 
    # Add a separator if there isn't one yet
166
 
    if len(self._combolist) == 1:
167
 
      self._combolist.append(["Separator", True, 0])
168
 
 
169
 
    iter = self._combolist.get_iter_first()
170
 
    self.increment_count_column(iter)
171
 
 
172
 
    updated_count = False
173
 
    iter = self._combolist.iter_next(iter)
174
 
    while iter != None:
175
 
      name = self._combolist.get_value(iter, Column.APP_NAME)
176
 
      if name == app_name:
177
 
        self.increment_count_column(iter)
178
 
        updated_count = True
179
 
        break
180
 
      iter = self._combolist.iter_next(iter)
181
 
    if not updated_count:
182
 
      iter = self._combolist.append([app_name, False, 1])
183
 
 
184
 
  def initialize_combobox(self):
185
 
    """Clear the combobox and set the initial entry."""
186
 
    self._combolist.clear()
187
 
    self._combolist.append([_("All Applications"), False, 0])
188
 
    self._comboapps = {}
189
 
    self._combobox.set_active(0)
190
 
 
191
 
  def on_clear(self, *args):
192
 
    """Clear the messages"""
193
 
    self._message_list.clear_messages()
194
 
 
195
 
  def on_combobox_changed(self, combobox, *args):
196
 
    """Filter based on app_name"""
197
 
    active = self._combobox.get_active()
198
 
    if active == 0:
199
 
      self._message_list.filter_by(None)
200
 
    else:
201
 
      iter = self._combobox.get_active_iter()
202
 
      app_name = self._combolist.get_value(iter, Column.APP_NAME)
203
 
      self._message_list.filter_by(app_name)
204
 
 
205
 
  def on_destroy(self, *args):
206
 
    """Quit the application"""
207
 
    Gtk.main_quit()
208
 
 
209
 
  def on_focus_in(self, widget, event):
210
 
    """Window receives focus"""
211
 
    pass
212
 
 
213
 
  def on_focus_out(self, widget, event):
214
 
    """Window receives focus"""
215
 
    self.zero_count()
216
 
    self._message_list.mark_as_read()
217
 
 
218
 
  def on_message_received(self, monitor, message):
219
 
    """Received a notification"""
220
 
    self.increment_count()
221
 
    self.increment_combobox(message.app_name)
222
 
    self._message_list.add_message(message)
223
 
 
224
 
  def on_size_allocate(self, widget, size):
225
 
    """Resize event"""
226
 
    self._message_list.set_width(size.width)
227
 
 
228
 
  def on_show_about(self, *args):
229
 
    """Show the about dialog"""
230
 
    show_about()
231
 
 
232
 
def main():
233
 
  icon_path = os.path.join(SCRIPT_PATH, "..", "icons")
234
 
  theme = Gtk.IconTheme.get_default()
235
 
  theme.append_search_path(icon_path)
236
 
 
237
 
  window = MainWindow()
238
 
  window.show_all()
239
 
  Gtk.main()
240
 
 
241
 
if __name__ == "__main__":
242
 
  main()