GnomeShellGenericMonitor/examples/genericmonitor.py

142 lines
4.6 KiB
Python

#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import json
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, GLib, Gst
class GenericMonitor:
def setupMonitor(self):
self._activated = True
self._encoder = json.JSONEncoder()
self._dbus_interface = 'com.soutade.GenericMonitor'
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self._dbus = dbus.SessionBus()
self.systray_proxy = self._dbus.get_object('org.gnome.Shell', '/com/soutade/GenericMonitor')
self._dbus.add_signal_receiver(self.onClick, 'onClick', self._dbus_interface)
self._dbus.add_signal_receiver(self.onDblClick, 'onDblClick', self._dbus_interface)
self._dbus.add_signal_receiver(self.onRightClick, 'onRightClick', self._dbus_interface)
self._dbus.add_signal_receiver(self.onDblRightClick, 'onDblRightClick', self._dbus_interface)
self._dbus.add_signal_receiver(self.onActivate, 'onActivate', self._dbus_interface)
self._dbus.add_signal_receiver(self.onDeactivate, 'onDeactivate', self._dbus_interface)
def runMainLoop(self):
self._mainLoop = GLib.MainLoop()
self._mainLoop.run()
def stopMainLoop(self):
self._mainLoop.quit()
# Generic Monitor functions
def notify(self, group):
if self._activated:
if type(group) == GenericMonitorGroup:
group = group.getValues()
self.systray_proxy.notify(self._encoder.encode(group), dbus_interface=self._dbus_interface)
def deleteItems(self, items):
if self._activated:
self.systray_proxy.deleteItems(self._encoder.encode(items), dbus_interface=self._dbus_interface)
def deleteGroups(self, groups):
if self._activated:
self.systray_proxy.deleteGroups(self._encoder.encode(groups), dbus_interface=self._dbus_interface)
# Generic Monitor signals
def onClick(self, sender):
pass
def onRightClick(self, sender):
pass
def onDblClick(self, sender):
pass
def onDblRightClick(self, sender):
pass
def onActivate(self):
self._activated = True
def onDeactivate(self):
self._activated = False
# DBUS method
def add_signal_receiver(self, callback, signalName, interface):
self._dbus.add_signal_receiver(callback, signalName, interface)
class GenericMonitorItem:
def __init__(self, name, text='', style='', icon='', iconStyle='', onClick='', box=''):
self.name = name
self.text = text
self.style= style
self.icon = icon
self.iconStyle = iconStyle
self.onClick = onClick
self.box = box
self._checkValues()
def _checkValues(self):
if self.onClick and not self.onClick in ('signal', 'delete'):
raise ValueError('Invalid onClick value')
if self.box and not self.box in ('left', 'center', 'right'):
raise ValueError('Invalid box value')
def toMap(self):
myMap = {"name":self.name}
for p in ('text', 'style', 'icon', 'box'):
if self.__dict__[p]:
myMap[p] = self.__dict__[p]
if self.iconStyle:
myMap['icon-style'] = self.iconStyle
if self.onClick:
myMap['on-click'] = self.onClick
return [myMap]
class GenericMonitorGroup:
def __init__(self, name, items=[]):
self.name = name
if type(items) != list:
self.items = [items]
else:
self.items = items
def addItem(self, item):
self.items.append(item)
def addItems(self, items):
for item in items:
self.addItem(item)
def getValues(self):
res = {'group': self.name, 'items':[]}
for item in self.items:
res['items'] += item.toMap()
return res
def __str__(self):
return str(self.getValues())