Include manpages.
[laditools.git] / ladi-system-tray
blob59a401df4a79e8838c0d9c4c5c5a4f790b744180
1 #!/usr/bin/python
2 # LADITools - Linux Audio Desktop Integration Tools
3 # ladi-system-tray - System tray integration for LADI
4 # Copyright (C) 2011-2012 Alessio Treglia <quadrispro@ubuntu.com>
5 # Copyright (C) 2007-2010, Marc-Olivier Barre <marco@marcochapeau.org>
6 # Copyright (C) 2007-2009, Nedko Arnaudov <nedko@arnaudov.name>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import os
22 import sys
23 import gettext
24 import argparse
26 from laditools import _gettext_domain
27 gettext.install(_gettext_domain)
29 from laditools import get_version_string
30 from laditools import LadiConfiguration
31 from laditools import LadiManager
32 from laditools import LadiApp
34 from gi.repository import Gtk
35 from gi.repository import Gdk
36 from gi.repository import GObject
38 try:
39 from gi.repository import AppIndicator3
40 except:
41 AppIndicator3 = None
43 from laditools.gtk import LadiMenu
44 from laditools.gtk import find_data_file
46 timeout_add = GObject.timeout_add
48 class LadiStatusIcon (LadiMenu, LadiApp):
50 _appname = 'ladi-system-tray'
51 _appname_long = _("LADI System Tray")
52 _appid = 'org.linuxaudio.ladi.systemtray'
54 # Default configuration
55 _default_config = {
56 'autostart' : False,
59 def on_about(self, *args):
60 LadiMenu.on_about(self, version=get_version_string())
62 def quit(self, *args, **kwargs):
63 # Some default config might need to be injected in the config file,
64 # we handle all that before we quit.
65 self.global_config.set_config_section (self.appname, self.config_dict)
66 self.global_config.save ()
67 Gtk.main_quit()
69 def set_tooltip_text(self, text): pass
71 def __init__ (self, config_filename = None):
72 # Handle the configuration
73 self.icon_state = ""
74 self.last_status_text = ""
75 self.diagnose_text = None
76 self.status_icons = {'started' : 'ladi-started',
77 'stopped' : 'ladi-stopped',
78 'starting' : 'ladi-starting'}
79 self.global_config = LadiConfiguration(self.appname,
80 self._default_config,
81 config_filename)
82 self.config_dict = self.global_config.get_config_section (self.appname)
83 autostart = bool(eval(self.config_dict['autostart']))
84 # Build the UI
85 LadiMenu.__init__(self,
86 autostart,
87 quit = self.quit)
88 LadiApp.__init__(self)
89 self.connect_signals_quit()
91 def menu_activate(self, status_icon, button, activate_time, user_data=None):
92 menu = self.create_menu()
93 menu.popup (parent_menu_shell=None,
94 parent_menu_item=None,
95 func=self.position_menu,
96 data=self,
97 button=button,
98 activate_time=activate_time)
99 menu.reposition ()
101 def set_starting_status (self):
102 self.set_tooltip_safe ("JACK is starting")
103 self.set_icon ("starting")
105 def set_tooltip_safe (self, text):
106 if text != self.last_status_text:
107 self.set_tooltip_text (text)
108 self.last_status_text = text
110 def run(self):
111 Gtk.main ()
113 class LadiStatusTray(Gtk.StatusIcon, LadiStatusIcon):
115 def __init__(self):
116 LadiStatusIcon.__init__(self)
117 GObject.GObject.__init__ (self)
118 self.set_icon ("stopped")
119 # Get the initial status
120 self.update ()
121 # Add the auto update callback
122 self.auto_updater = timeout_add (250, self.update, None)
123 # Make the menu popup when the icon is right clicked
124 self.connect ("popup-menu", self.menu_activate)
125 self.set_title(self.appname_long)
127 def do_button_press_event(self, event):
128 if event.type != Gdk.EventType._2BUTTON_PRESS:
129 return False
130 self.on_menu_launch_handler(None, "gladish")
131 return True
133 def update (self, user_data = None):
134 try:
135 if self.jack_is_started():
136 # Get Realtime status
137 if self.jack_is_realtime():
138 status_text = "RT | "
139 else:
140 status_text = ""
141 # Get DSP Load
142 status_text += str (round (float (self.jack_get_load()),1)) + "% | "
143 # Get Xruns
144 status_text += str (self.jack_get_xruns())
145 # Set a started status
146 self.set_tooltip_safe (status_text)
147 self.set_icon ("started")
148 else:
149 self.set_tooltip_safe ("JACK is stopped")
150 self.set_icon ("stopped")
151 self.clear_diagnose_text()
152 except Exception, e:
153 self.set_tooltip_safe ("JACK is sick")
154 self.set_diagnose_text(repr(e))
155 self.set_icon ("stopped")
156 self.clear_jack_proxies()
157 finally:
158 LadiManager.update(self)
159 return True
161 def set_icon (self, newstate):
162 if self.icon_state == newstate:
163 return
164 self.icon_state = newstate
165 self.set_from_icon_name(self.status_icons[newstate])
167 class LadiStatusIndicator(LadiStatusIcon):
169 def update (self, user_data = None):
170 try:
171 if self.jack_is_started():
172 # Get Realtime status
173 if self.jack_is_realtime():
174 status_text = "RT | "
175 else:
176 status_text = ""
177 # Get DSP Load
178 status_text += str (round (float (self.jack_get_load()),1)) + "% | "
179 # Get Xruns
180 status_text += str (self.jack_get_xruns())
181 # Set a started status
182 self.set_tooltip_safe (status_text)
183 self.set_icon ("started")
184 else:
185 self.set_tooltip_safe ("JACK is stopped")
186 self.set_icon ("stopped")
187 self.clear_diagnose_text()
188 except Exception, e:
189 self.set_tooltip_safe ("JACK is sick")
190 self.set_diagnose_text(repr(e))
191 self.set_icon ("stopped")
192 self.clear_jack_proxies()
193 finally:
194 LadiManager.update(self)
195 return True
197 def set_icon (self, newstate):
198 if self.icon_state == newstate:
199 return
200 self.icon_state = newstate
201 self.indicator.set_icon(self.status_icons[newstate])
203 def create_menu(self):
204 menu_items = []
206 ladish_available = self.ladish_is_available()
208 menu_items.append((Gtk.ImageMenuItem(_("LADI Player")), self.on_menu_launch_handler, "ladi-player"))
209 if ladish_available:
210 menu_items.append((Gtk.ImageMenuItem(_("Session editor")), self.on_menu_launch_handler, "gladish"))
212 menu_items.append((Gtk.SeparatorMenuItem.new(),))
213 menu_items.append((Gtk.ImageMenuItem(_("Settings")), self.on_menu_launch_handler, "ladi-control-center"))
214 menu_items.append((Gtk.ImageMenuItem(_("Log Viewer")), self.on_menu_launch_handler, "ladi-system-log"))
216 menu_items.append((Gtk.SeparatorMenuItem.new(),))
217 if hasattr(self, 'on_about'):
218 menu_items.append((Gtk.ImageMenuItem(_("About")), self.on_about, None))
219 menu_items.append((Gtk.ImageMenuItem(_("Quit")), self.on_menu_command, self.quit))
221 menu = Gtk.Menu()
222 for menu_tuple in menu_items:
223 item = menu_tuple[0]
224 if len(menu_tuple) > 1:
225 callback = menu_tuple[1]
226 exec_path = menu_tuple[2]
227 menu.append(item)
228 if type(item) is not Gtk.SeparatorMenuItem:
229 if callback in (self.studio_list_fill, self.configure_list_fill):
230 item.set_submenu(Gtk.Menu())
231 item.connect("activate", callback, exec_path)
232 menu.show_all()
233 return menu
236 def __init__(self):
237 LadiStatusIcon.__init__(self)
238 self.indicator = indicator = AppIndicator3.Indicator.new('ladi-indicator',
239 find_data_file('scalable/apps/laditools.svg'),
240 AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
241 self.menu = menu = self.create_menu()
243 indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
244 indicator.set_menu(menu)
245 menu.show()
246 # Get the initial status
247 self.update ()
248 # Add the auto update callback
249 self.auto_updater = timeout_add (250, self.update, None)
251 if __name__ == "__main__":
252 parser = argparse.ArgumentParser(description=_('system tray icon that allows users to start, stop and'
253 'monitor JACK, as well as start some JACK related applications'),
254 epilog=_('This program is part of the LADITools suite.'))
255 parser.add_argument('--no-appindicator',
256 action='store_true',
257 help=_('Force fallback to system tray.'))
258 parser.add_argument('--version',
259 action='version',
260 version="%(prog)s " + get_version_string())
261 args = parser.parse_args()
263 Gtk.init(None)
265 if (not args.no_appindicator) and AppIndicator3:
266 LadiStatusIndicator().run()
267 else:
268 LadiStatusTray().run()
270 sys.exit(0)