Ubuntu Unity Launcher Integration
[gpodder.git] / src / gpodder / gtkui / ubuntu.py
blob05c1f0ef506f91bc2662fe0c7654ebbf35ca4633
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2012 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # Ubuntu Unity Launcher Integration
21 # Thomas Perl <thp@gpodder.org>; 2012-02-06
23 # FIXME: Due to the fact that we do not yet use the GI-style bindings, we will
24 # have to run this module in its own interpreter and send commands to it using
25 # the subprocess module. Once we use GI-style bindings, we can get rid of all
26 # this and still expose the same "interface' (LauncherEntry and its methods)
27 # to our callers.
29 import subprocess
30 import logging
32 if __name__ != '__main__':
33 logger = logging.getLogger(__name__)
35 class LauncherEntry:
36 FILENAME = 'gpodder.desktop'
38 def __init__(self):
39 self.process = subprocess.Popen(['python', __file__],
40 stdin=subprocess.PIPE)
42 def set_count(self, count):
43 try:
44 self.process.stdin.write('count %d\n' % count)
45 self.process.stdin.flush()
46 except Exception, e:
47 logger.debug('Ubuntu count update failed.', exc_info=True)
49 def set_progress(self, progress):
50 try:
51 self.process.stdin.write('progress %f\n' % progress)
52 self.process.stdin.flush()
53 except Exception, e:
54 logger.debug('Ubuntu progress update failed.', exc_info=True)
56 if __name__ == '__main__':
57 from gi.repository import Unity, GObject
58 import threading
59 import sys
61 class InputReader:
62 def __init__(self, fileobj, launcher):
63 self.fileobj = fileobj
64 self.launcher = launcher
66 def read(self):
67 while True:
68 line = self.fileobj.readline()
69 if not line:
70 break
71 try:
72 command, value = line.strip().split()
73 if command == 'count':
74 GObject.idle_add(launcher_entry.set_count, int(value))
75 elif command == 'progress':
76 GObject.idle_add(launcher_entry.set_progress, float(value))
77 except:
78 pass
80 class LauncherEntry:
81 FILENAME = 'gpodder.desktop'
83 def __init__(self):
84 self.launcher = Unity.LauncherEntry.get_for_desktop_id(
85 self.FILENAME)
87 def set_count(self, count):
88 self.launcher.set_property('count', count)
89 self.launcher.set_property('count_visible', count > 0)
91 def set_progress(self, progress):
92 self.launcher.set_property('progress', progress)
93 self.launcher.set_property('progress_visible', 0. <= progress < 1.)
95 GObject.threads_init()
96 loop = GObject.MainLoop()
97 threading.Thread(target=loop.run).start()
99 launcher_entry = LauncherEntry()
100 reader = InputReader(sys.stdin, launcher_entry)
101 reader.read()
103 loop.quit()