Merge lp:~seif/dockmanager/enhance-zeitgeist-menus into lp:dockmanager

Proposed by Seif Lotfy
Status: Merged
Merged at revision: 80
Proposed branch: lp:~seif/dockmanager/enhance-zeitgeist-menus
Merge into: lp:dockmanager
Diff against target: 271 lines (+129/-88)
1 file modified
scripts/zeitgeist_menus.py (+129/-88)
To merge this branch: bzr merge lp:~seif/dockmanager/enhance-zeitgeist-menus
Reviewer Review Type Date Requested Status
Rico Tzschichholz Approve
Review via email: mp+43714@code.launchpad.net

Description of the change

I enhanced the menus by allowing them to update themselves on the fly using the zeitgeist monitors as well as use reduce memory consumption by using negations in queries (no need to ask for all events)

To post a comment you must log in.
Revision history for this message
Rico Tzschichholz (ricotz) wrote :

Nice to see you working on it :)

But the version check isn't working the current natty version 0.5.2 doesn't fit the check. Also the added items aren't right. So currently it is quite broken for me :(

review: Needs Fixing
Revision history for this message
Seif Lotfy (seif) wrote :

ah Shit I know why. There is a bug in Zeitgeist that did not get released.
Let me work aroudn that.

On Wed, Dec 15, 2010 at 8:17 AM, Rico Tzschichholz <email address hidden>wrote:

> Review: Needs Fixing
> Nice to see you working on it :)
>
> But the version check isn't working the current natty version 0.5.2 doesn't
> fit the check. Also the added items aren't right. So currently it is quite
> broken for me :(
>
>
> --
>
> https://code.launchpad.net/~seif/dockmanager/enhance-zeitgeist-menus/+merge/43714
> You are the owner of lp:~seif/dockmanager/enhance-zeitgeist-menus.
>

--
This is me doing some advertisement for my blog http://seilo.geekyogre.com

Revision history for this message
Seif Lotfy (seif) wrote :

The remaining issue is from you side guys. I finished my part.

78. By Robert Dyer

fix the add_menu_with_uri calls to properly fall back on add_menu_item

Revision history for this message
Rico Tzschichholz (ricotz) wrote :

The menu_pressed method need some work. Now it will throw a exception in too many cases and if it fails it might not use the right application.
I think it is better to use:

if uri.endswith(".desktop"):
    try:
        appinfo = gio.unix.DesktopAppInfo(uri)
        if appinfo:
            appinfo.launch(self.ids[menu_id]])
    except ...

review: Needs Fixing
79. By Robert Dyer

better code

Revision history for this message
Rico Tzschichholz (ricotz) :
review: Approve
80. By Rico Tzschichholz

update zeitgeist-menu-helper

enhanced the menus by allowing them to update themselves on the fly
using the zeitgeist monitors as well as use reduce memory consumption
by using negations in queries (no need to ask for all events

merge lp:~seif/dockmanager/enhance-zeitgeist-menus

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'scripts/zeitgeist_menus.py'
2--- scripts/zeitgeist_menus.py 2010-08-06 23:14:20 +0000
3+++ scripts/zeitgeist_menus.py 2010-12-20 16:08:12 +0000
4@@ -28,11 +28,14 @@
5 import urllib
6 import os
7 import subprocess
8+import gio
9+
10+home = "file://" + os.path.expanduser("~")
11
12 try:
13 from dockmanager.dockmanager import DockManagerItem, DockManagerSink, DOCKITEM_IFACE
14 from zeitgeist.client import ZeitgeistClient
15- from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation, StorageState
16+ from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation, StorageState, TimeRange
17 from signal import signal, SIGTERM
18 from sys import exit
19 except ImportError, e:
20@@ -41,9 +44,9 @@
21 try:
22 CLIENT = ZeitgeistClient()
23 version = [int(x) for x in CLIENT.get_version()]
24- MIN_VERSION = [0, 3, 2, 0]
25+ MIN_VERSION = [0, 4, 0, 0]
26 if version < MIN_VERSION:
27- print "PLEASE USE ZEITGEIST 0.3.2 or above"
28+ print "PLEASE USE ZEITGEIST 0.4.0 or above"
29 exit()
30
31
32@@ -51,105 +54,146 @@
33 print "Unable to connect to Zeitgeist, won't send events. Reason: '%s'" %e
34 exit()
35
36-class MostUsedProvider():
37- def __init__(self):
38- self._zg = CLIENT
39- self.results = []
40-
41- def get_path_most_used(self, path, handler, is_directoy=True):
42- today = time.time() * 1000
43- delta = (today - 14 * 86400000)
44-
45- def exists(uri):
46- return not uri.startswith("file://") or os.path.exists(urllib.unquote(str(uri[7:])))
47-
48- def _handle_find_events(ids):
49- self._zg.get_events(ids, _handle_get_events)
50-
51- def _handle_get_events(events):
52- uris = []
53- uris_counter = {}
54- titles = {}
55- for event in events:
56- for subject in event.subjects:
57- if exists(subject.uri):
58- if not subject.uri in uris:
59- uris.append(subject.uri)
60- titles[subject.uri] = subject.text
61- uris_counter[subject.uri] = 0
62- uris_counter[subject.uri] += 1
63-
64- counter = []
65- for k, v in uris_counter.iteritems():
66- counter.append((v, k))
67- counter.sort(reverse = True)
68-
69- recent =[]
70- temp = [i[1] for i in counter]
71- for uri in uris:
72- if not uri in temp[0:5]:
73- recent.append(uri)
74- results = []
75- results.append(recent[0:5])
76-
77- results.append(counter[0:5])
78-
79- # add also titles to the result list
80- new_results = []
81- new_results.append(map(lambda x: (x, titles[x]), results[0]))
82- new_results.append(map(lambda x: (x[1], titles[x[1]]), results[1]))
83-
84- handler(new_results)
85-
86- event = Event()
87- if is_directoy:
88- subject = Subject()
89- subject.set_origin(path)
90- event.set_subjects([subject])
91- self._zg.find_events_for_templates([event],_handle_get_events, [delta, today], StorageState.Any, 0, 0)
92- else:
93- path = "application://" + path.split("/")[-1]
94- event.set_actor(path)
95- self._zg.find_events_for_templates([event],_handle_get_events, [delta, today], StorageState.Any, 0, 0)
96-
97-
98 class ZGItem(DockManagerItem):
99 def __init__(self, sink, path):
100 DockManagerItem.__init__(self, sink, path)
101- self.mostusedprovider = MostUsedProvider()
102- self.update_most_used()
103-
104- def update_most_used(self):
105- has_uri = True
106+ self.monitor = None
107+ self.recent = []
108+ self.most = []
109+ self.ids = {}
110+
111+ self.has_uri = True
112 self.uri = self.iface.Get(DOCKITEM_IFACE, "Uri", dbus_interface="org.freedesktop.DBus.Properties")
113-
114 if not self.uri:
115- has_uri = False
116+ self.has_uri = False
117 self.uri = self.iface.Get(DOCKITEM_IFACE, "DesktopFile", dbus_interface="org.freedesktop.DBus.Properties")
118 self.uri = 'application://%s' % self.uri[self.uri.rfind('/')+1:]
119+
120+ self.app_info = None
121+ app_uri = self.iface.Get(DOCKITEM_IFACE, "DesktopFile", dbus_interface="org.freedesktop.DBus.Properties")
122+ apps = gio.app_info_get_all()
123+ app = None
124+ t_app = app_uri.split("/")[-1].replace(".desktop", "")
125+ if t_app:
126+ for app in apps:
127+ if t_app in app.get_executable():
128+ self.app_info = app
129+ break
130+
131+ self.update_entries()
132
133+ def update_entries(self, x=None, y=None, z=None):
134+ self.recent = []
135+ self.most = []
136+
137 if not self.uri:
138 return
139
140- self.mostusedprovider.get_path_most_used (self.uri, self._handle_get_most_used, has_uri)
141-
142- def _handle_get_most_used(self, results):
143- uris = results[0]
144- if len(uris) > 0:
145- for uri, title in uris:
146- self.add_menu_item_uri(uri, "Other Recently Used Items", title)
147- uris = results[1]
148- if len(uris) > 0:
149- for uri, title in uris:
150- self.add_menu_item_uri(uri, "Most Used Items", title)
151-
152+ def reformat_strings():
153+ titles = {}
154+ for r in self.recent:
155+ if not r[1] in titles:
156+ titles[r[1]] = []
157+ titles[r[1]].append(r)
158+ for m in self.most:
159+ if not m[1] in titles:
160+ titles[m[1]] = []
161+ titles[m[1]].append(m)
162+ for t in titles:
163+ if len(titles[t]) > 1:
164+ for l in titles[t]:
165+ for i, r in enumerate(self.recent):
166+ if l == r:
167+ self.recent[i][1] = r[1] + " (" + r[0].replace(home, "~") + ")"
168+ break
169+ for i, r in enumerate(self.most):
170+ if l == r:
171+ self.most[i][1] = r[1] + " (" + r[0].replace(home, "~") + ")"
172+ break
173+
174+ def exists(uri):
175+ return uri.startswith("note://") or os.path.exists(urllib.unquote(str(uri[7:])))
176+
177+ def handle_most(results):
178+ self.most = []
179+ for event in results:
180+ if exists(event.subjects[0].uri):
181+ self.most.append([event.subjects[0].uri, event.subjects[0].text])
182+ if len(self.most) >= 5:
183+ break
184+ reformat_strings()
185+ self.update_menu()
186+
187+ def handle_recent(results):
188+ self.recent = []
189+ for event in results:
190+ if exists(event.subjects[0].uri):
191+ self.recent.append([event.subjects[0].uri, event.subjects[0].text])
192+ subj = Subject()
193+ subj.uri = "!"+event.subjects[0].uri
194+ template.subjects.append(subj)
195+ if len(self.recent) >= 5:
196+ break
197+ CLIENT.find_events_for_templates([template], handle_most, num_events = 1000, result_type = 4)
198+
199+ template = Event()
200+ if self.has_uri:
201+ subj = Subject()
202+ subj.set_uri(self.uri+"/*")
203+ template.set_subjects([subj])
204+ else:
205+ template.set_actor(self.uri)
206+
207+ CLIENT.find_events_for_templates([template], handle_recent, num_events = 1000, result_type = 2)
208+
209+ if not self.monitor:
210+ self.monitor = CLIENT.install_monitor(TimeRange.always(), [template], self.update_entries, self.update_entries)
211+
212+ def update_menu(self):
213+ for id in self.ids:
214+ self.remove_menu_item(id)
215+ self.ids.clear()
216+ uris = self.most
217+ for uri in uris:
218+ uri, title = uri[0], uri[1]
219+ icon = self.get_icon(uri)
220+ self.ids[(self.add_menu_item(title, icon, "Other Popular"))] = uri
221+ uris = self.recent
222+ for uri in uris:
223+ uri, title = uri[0], uri[1]
224+ icon = self.get_icon(uri)
225+ print icon
226+ self.ids[(self.add_menu_item(title, icon, "Latest"))] = uri
227+
228+ def get_icon(self, uri):
229+ if uri.startswith("note://"):
230+ return "tomboy-note"
231+ file_ = gio.File(uri)
232+ info = file_.query_info('standard::icon,thumbnail::path')
233+ thumbnail_path = info.get_attribute_as_string('thumbnail::path')
234+ if thumbnail_path:
235+ return thumbnail_path
236+ else:
237+ icon = info.get_icon()
238+ icon_path = None
239+ if isinstance(icon, gio.ThemedIcon):
240+ icon_path = icon.get_names()[0]
241+ elif isinstance(icon, gio.FileIcon):
242+ icon_path = icon.get_file().get_path()
243+ return icon_path
244+
245+ def menu_pressed(self, menu_id):
246+ if menu_id in self.ids:
247+ if self.app_info:
248+ self.app_info.launch([gio.File(self.ids[menu_id])])
249+ else:
250+ subprocess.Popen(['xdg-open', self.ids[menu_id]])
251
252 class ZGSink(DockManagerSink):
253 def item_path_found(self, pathtoitem, item):
254 if item.Get(DOCKITEM_IFACE, "Uri", dbus_interface="org.freedesktop.DBus.Properties") != "" or item.Get(DOCKITEM_IFACE, "DesktopFile", dbus_interface="org.freedesktop.DBus.Properties") != "":
255 self.items[pathtoitem] = ZGItem(self, pathtoitem)
256
257-
258 zgsink = ZGSink()
259
260 def cleanup ():
261@@ -157,10 +201,7 @@
262
263 if __name__ == "__main__":
264 mainloop = gobject.MainLoop(is_running=True)
265-
266 atexit.register (cleanup)
267-
268 signal(SIGTERM, lambda signum, stack_frame: exit(1))
269-
270 while mainloop.is_running():
271 mainloop.run()

Subscribers

People subscribed via source and target branches

to status/vote changes: