~kartoch/libnotify-mozilla/simple-support-tbird6

« back to all changes in this revision

Viewing changes to python/indicator.py

  • Committer: Ruben Verweij
  • Date: 2010-06-28 09:16:32 UTC
  • Revision ID: rbnvrw@gmail.com-20100628091632-ov588t17z4yrt4vn
Updated translations, added translations for python file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import gtk, glib, sys, fcntl, os
 
3
import gobject
 
4
import locale
 
5
import gettext
 
6
 
 
7
import indicate
 
8
 
 
9
APP_NAME = "indicator-py"
 
10
locale.setlocale(locale.LC_ALL, 'nl_NL.utf8')
 
11
 
 
12
class MessagingServer:
 
13
  def __init__(self, subtype, desktop):
 
14
    self.indicators = {}
 
15
    self.actions = {}
 
16
    self.user_cb = None
 
17
    self.server_cb = None
 
18
    self.desktop = desktop
 
19
 
 
20
    self.srv = indicate.indicate_server_ref_default()
 
21
    type = "message.%s" % subtype
 
22
    self.srv.set_type(type)
 
23
    self.srv.set_desktop_file(desktop)
 
24
    self.srv.show()
 
25
 
 
26
  def show_indicator(self, name, count, draw_attention=True):
 
27
    # update existing indicator, or create new one
 
28
    try:
 
29
      ind = self.indicators[name]
 
30
    except KeyError:
 
31
      print "NEW"
 
32
      ind = indicate.Indicator()
 
33
      self.indicators[name] = ind
 
34
 
 
35
    ind.set_property('name', name)
 
36
    ind.set_property('count', str(count))
 
37
    ind.set_property('draw-attention', 'true' if draw_attention else 'false')
 
38
    ind.connect('user-display', self.cb_user_display)
 
39
 
 
40
    # hide and reshow actions to keep them at top of list
 
41
    for a in self.actions.values():
 
42
      a.hide()
 
43
    ind.show()
 
44
    for a in self.actions.values():
 
45
      a.show()
 
46
 
 
47
    return ind
 
48
 
 
49
  def hide_indicator(self, name):
 
50
    try:
 
51
      self.indicators[name].hide()
 
52
      del(self.indicators[name])
 
53
    except KeyError:
 
54
      print "ERROR: No indicator named '%s' to hide" % name
 
55
 
 
56
  def add_action(self, name, cb):
 
57
    ind = indicate.Indicator()
 
58
    self.actions[name] = ind
 
59
    ind.set_property('name', name)
 
60
    ind.set_property('subtype', 'menu')
 
61
    ind.connect('user-display', cb)
 
62
    ind.show()
 
63
    return ind
 
64
 
 
65
  def set_user_cb(self, cb):
 
66
    self.user_cb = cb
 
67
 
 
68
  def set_server_cb(self, cb):
 
69
    self.server_cb = cb
 
70
 
 
71
  def cb_server_display(self, srv, id):
 
72
    print "SERVER DISPLAY"
 
73
    if (self.server_cb):
 
74
      self.server_cb(self)
 
75
 
 
76
  def cb_user_display(self, ind, id):
 
77
    print "USER DISPLAY"
 
78
    if (self.user_cb):
 
79
      self.user_cb(ind.get_property('name'), ind.get_property('count'))
 
80
    ind.hide()
 
81
 
 
82
def set_nonblock(fd, nonblock):
 
83
  fl = fcntl.fcntl(fd, fcntl.F_GETFL)
 
84
  if nonblock:
 
85
    fl |= os.O_NONBLOCK
 
86
  else:
 
87
    fl &= ~os.O_NONBLOCK
 
88
  fcntl.fcntl(fd, fcntl.F_SETFL, fl)
 
89
 
 
90
def user_display(name, count):
 
91
  os.system("thunderbird -mail&")
 
92
 
 
93
def server_display(srv):
 
94
  os.system("thunderbird -mail&")
 
95
 
 
96
def io_cb(f, condition, srv):
 
97
  commands = {
 
98
      'show': [srv.show_indicator, 2],
 
99
      'hide': [srv.hide_indicator, 1],
 
100
      'exit': [exit, 0]
 
101
    }
 
102
 
 
103
  if condition == glib.IO_IN:
 
104
    data = f.read().strip()
 
105
    args = data.strip().split("::")
 
106
    cmd = args.pop(0)
 
107
 
 
108
    try:
 
109
      fn, numargs = commands[cmd]
 
110
    except KeyError:
 
111
      print "ERROR: command '%s' not known" % cmd
 
112
      return True
 
113
 
 
114
    if numargs != len(args):
 
115
      print "ERROR: '%s' command takes %d arguments but were %d given" % (cmd,
 
116
          numargs, len(args))
 
117
      return True
 
118
 
 
119
    print "CMD: %s" % cmd
 
120
    if fn:
 
121
      fn(*args)
 
122
 
 
123
  else:
 
124
    print "ERROR: I/O Error"
 
125
    exit()
 
126
  return True
 
127
 
 
128
if __name__ == "__main__":
 
129
    #Translation stuff
 
130
 
 
131
    #Get the local directory since we are not installing anything
 
132
    local_path = os.path.realpath(os.path.dirname(sys.argv[0]))
 
133
    # Init the list of languages to support
 
134
    langs = []
 
135
    #Check the default locale
 
136
    lc, encoding = locale.getdefaultlocale()
 
137
    if (lc):
 
138
        #If we have a default, it's the first in the list
 
139
        langs = [lc]
 
140
    # Now lets get all of the supported languages on the system
 
141
    language = os.environ.get('LANGUAGE', None)
 
142
    if (language):
 
143
        langs += language.split(":")
 
144
    langs += ["en_US", "nl"]
 
145
 
 
146
    gettext.bindtextdomain(APP_NAME, local_path)
 
147
    gettext.textdomain(APP_NAME)
 
148
    # Get the language to use
 
149
    lang = gettext.translation(APP_NAME, local_path, languages=langs, fallback = True)
 
150
    _ = lang.gettext
 
151
 
 
152
    def action_compose(indicator, id):
 
153
        os.system("thunderbird -compose&")
 
154
 
 
155
    def action_addressbook(indicator, id):
 
156
        os.system("thunderbird -addressbook&")
 
157
 
 
158
    def timeout(srv):
 
159
        srv.add_action(_("Contacts"), action_addressbook)
 
160
        srv.add_action(_("Compose email"), action_compose)
 
161
 
 
162
    srv = MessagingServer('email', '/usr/share/applications/thunderbird.desktop')
 
163
    srv.set_user_cb(user_display)
 
164
    srv.set_server_cb(server_display)
 
165
 
 
166
    fifopath = sys.argv[1]
 
167
    if not os.path.exists(fifopath):
 
168
        os.mkfifo(fifopath)
 
169
 
 
170
    if len(sys.argv) > 2 and sys.argv[2] == 'mkfifoonly':
 
171
        exit()
 
172
 
 
173
    fdr = os.open(fifopath, os.O_RDONLY | os.O_NONBLOCK)
 
174
    fdw = os.open(fifopath, os.O_WRONLY | os.O_NONBLOCK)
 
175
    f = os.fdopen(fdr)
 
176
    glib.io_add_watch(f, glib.IO_IN | glib.IO_ERR, io_cb, srv)
 
177
 
 
178
    gobject.timeout_add_seconds(0, timeout, srv)
 
179
    gtk.main()