~jconti/recent-notifications/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
"""
recent-notifications
by Jason Conti
February 16, 2010

The main applet code.
"""

import logging
import os.path
import sys

from optparse import OptionParser

# Setup the application logging to file and stdout for errors
def setup_logging():
  log_format = "%(asctime)s %(name)s %(levelname)s: %(message)s"

  logging.basicConfig(level=logging.DEBUG, 
      filename=os.path.expanduser("~/.cache/recent-notifications.log"),
      format=log_format,
      filemode="w")

  console = logging.StreamHandler()
  console.setLevel(logging.ERROR)
  console.setFormatter(logging.Formatter(log_format))

  logging.getLogger('').addHandler(console)

setup_logging()
logger = logging.getLogger("recent-notifications")

# Ensure we have the proper modules installed
try:
  import pygtk
  pygtk.require('2.0')
  import gtk
  import gobject
except ImportError:
  logger.error("Failed to import the python gtk modules.")
  logger.error("Please install python-gtk2 and python-gobject.")
  sys.exit(1)

try:
  import gnomeapplet
except ImportError:
  logger.error("Failed to import the python gnomeapplet module.")
  logger.error("Please install python-gnomeapplet.")
  sys.exit(1)

try:
  import dbus
except ImportError:
  logger.error("Failed to import the python dbus module.")
  logger.error("Please install python-dbus.")
  sys.exit(1)

from recent_notifications.Globals import VERSION
from recent_notifications.Main import Main

def applet_factory(applet, iid):
  Main(applet, iid)
  return True

def run_in_window():
  # Running in a window assumes we are running from source
  # so this appends the icon directory to the icon theme
  # search path
  app_path = os.path.abspath(os.path.dirname(__file__))
  icon_path = os.path.join(app_path, "icons")
  theme = gtk.icon_theme_get_default()
  theme.append_search_path(icon_path)

  main_window = gtk.Window()
  main_window.set_title("Applet")
  main_window.connect("destroy", gtk.main_quit)
  app = gnomeapplet.Applet()
  applet_factory(app, None)
  app.reparent(main_window)
  main_window.show_all()
  gtk.main()

def initialize_applet():
  gnomeapplet.bonobo_factory("OAFIID:RecentNotifications_Factory", 
      gnomeapplet.Applet.__gtype__, 
      "recent-notifications", VERSION, applet_factory)

# Get command line options
def parse_options(args):
  usage = "Usage: %prog [options]"
  parser = OptionParser(usage=usage)

  parser.add_option("-w", "--windowed", action="store_true", dest="windowed", default=False, metavar=" ",
      help="run the applet in a window for testing purposes [default: %default]")
  # Need to specify these options, because they are provided when adding the applet to a panel
  # and OptionParser raises an exception for invalid args
  parser.add_option("--oaf-activate-iid")
  parser.add_option("--oaf-ior-fd")

  options, extra = parser.parse_args(args)

  return options

def main():
  options = parse_options(sys.argv)

  if options.windowed:
    run_in_window()
  else:
    initialize_applet()

if __name__ == '__main__':
  main()