Skip to content

Commit

Permalink
dbusmenu: implement all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
deltragon committed Jul 8, 2024
1 parent e8ad460 commit 7ebfe7d
Showing 1 changed file with 96 additions and 14 deletions.
110 changes: 96 additions & 14 deletions safeeyes/plugins/trayicon/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,35 @@
<arg type="u" direction="out"/>
<arg type="(ia{sv}av)" direction="out"/>
</method>
<method name="GetGroupProperties">
<arg type="ai" name="ids" direction="in"/>
<arg type="as" name="propertyNames" direction="in" />
<arg type="a(ia{sv})" name="properties" direction="out" />
</method>
<method name="GetProperty">
<arg type="i" name="id" direction="in"/>
<arg type="s" name="name" direction="in"/>
<arg type="v" name="value" direction="out"/>
</method>
<method name="Event">
<arg type="i" direction="in"/>
<arg type="s" direction="in"/>
<arg type="v" direction="in"/>
<arg type="u" direction="in"/>
</method>
<method name="EventGroup">
<arg type="a(isvu)" name="events" direction="in" />
<arg type="ai" name="idErrors" direction="out" />
</method>
<method name="AboutToShow">
<arg type="i" direction="in"/>
<arg type="b" direction="out"/>
</method>
<method name="AboutToShowGroup">
<arg type="ai" name="ids" direction="in" />
<arg type="ai" name="updatesNeeded" direction="out" />
<arg type="ai" name="idErrors" direction="out" />
</method>
<signal name="LayoutUpdated">
<arg type="u"/>
<arg type="i"/>
Expand Down Expand Up @@ -144,7 +163,7 @@ class DBusMenuService(DBusService):
revision = 0

items = []
idToCallback = {}
idToItems = {}

def __init__(self, session_bus, context, items):
super().__init__(
Expand All @@ -158,31 +177,35 @@ def __init__(self, session_bus, context, items):
def set_items(self, items):
self.items = items

self.idToCallback = self.getItemCallbacks(items, {})
self.idToItems = self.getItemsFlat(items, {})

self.revision += 1

self.LayoutUpdated(self.revision, 0)

@staticmethod
def getItemCallbacks(items, idToCallback):
def getItemsFlat(items, idToItems):
for item in items:
if item.get('hidden', False) == True:
continue
if 'callback' in item:
idToCallback[item['id']] = item['callback']
idToItems[item['id']] = item
if 'children' in item:
idToCallback = DBusMenuService.getItemCallbacks(item['children'], idToCallback)
idToItems = DBusMenuService.getItemsFlat(item['children'], idToItems)

return idToCallback
return idToItems

@staticmethod
def itemToDbus(item, recursion_depth):
result = {}

def singleItemToDbus(item):
if item.get('hidden', False) == True:
return None

props = DBusMenuService.itemPropsToDbus(item)

return (item['id'], props)

@staticmethod
def itemPropsToDbus(item):
result = {}

string_props = ['label', 'icon-name', 'type', 'children-display']
for key in string_props:
if key in item:
Expand All @@ -193,13 +216,22 @@ def itemToDbus(item, recursion_depth):
if key in item:
result[key] = GLib.Variant('b', item[key])

return result

@staticmethod
def itemToDbus(item, recursion_depth):
if item.get('hidden', False) == True:
return None

props = DBusMenuService.itemPropsToDbus(item)

children = []
if recursion_depth > 1 or recursion_depth == -1:
if "children" in item:
children = [DBusMenuService.itemToDbus(item, recursion_depth - 1) for item in item['children']]
children = [i for i in children if i is not None]

return GLib.Variant("(ia{sv}av)", (item['id'], result, children))
return GLib.Variant("(ia{sv}av)", (item['id'], props, children))

def findItemsWithParent(self, parent_id, items):
for item in items:
Expand Down Expand Up @@ -238,16 +270,66 @@ def GetLayout(self, parent_id, recursion_depth, property_names):

return ret

def GetGroupProperties(self, ids, property_names):
ret = []

for idx in ids:
if idx in self.idToItems:
props = DBusMenuService.singleItemToDbus(self.idToItems[idx])
if props is not None:
ret.append(props)

return (ret,)

def GetProperty(self, idx, name):
ret = None

if idx in self.idToItems:
props = DBusMenuService.singleItemToDbus(self.idToItems[idx])
if props is not None and name in props:
ret = props[name]

return ret

def Event(self, idx, event_id, data, timestamp):
if event_id != "clicked":
return

if idx in self.idToCallback:
self.idToCallback[idx]()
if idx in self.idToItems:
item = self.idToItems[idx]
if item.get('hidden', False) == False and 'callback' in item:
item['callback']()

def EventGroup(self, events):
not_found = []

for (idx, event_id, data, timestamp) in events:
if idx not in self.idToItems:
not_found.append(idx)
continue

if event_id != "clicked":
continue

item = self.idToItems[idx]
if item.get('hidden', False) == False and 'callback' in item:
item['callback']()

return not_found

def AboutToShow(self, item_id):
return (False,)

def AboutToShowGroup(self, ids):
not_found = []

for idx in ids:
if idx not in self.idToItems:
not_found.append(idx)
continue

return ([], not_found)

def LayoutUpdated(self, revision, parent):
self.emit_signal(
'LayoutUpdated',
Expand Down

0 comments on commit 7ebfe7d

Please sign in to comment.