~zeitgeist/zeitgeist-explorer/git-import

« back to all changes in this revision

Viewing changes to zgexplorer/applicationsviewer.py

  • Committer: Manish Sinha
  • Date: 2013-07-12 03:21:40 UTC
  • Revision ID: git-v1:d42716b0c3beb006f26c77b31c5875aec19eb707
Move to zeitgeist introspected bindings. Monitor view still does not work

Signed-off-by: Manish Sinha <manishsinha@ubuntu.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# You should have received a copy of the GNU General Public License
21
21
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 
23
 
from gi.repository import Gtk, Gdk, Pango, Gio, GdkPixbuf, GLib
 
23
from gi.repository import Zeitgeist, Gtk, Gdk, Pango, Gio, GdkPixbuf, GLib
24
24
 
25
25
from datetime import datetime
26
26
 
27
27
from zgexplorer.eventwidgets import EventDetailsViewer, EventsTreeView, EventsViewer
28
28
from zgexplorer.remote import get_zeitgeist
29
29
 
30
 
from zeitgeist.datamodel import Event, Subject, Interpretation, \
31
 
    Manifestation, StorageState, ResultType, TimeRange
32
30
 
33
31
class ApplicationsViewer(Gtk.VBox):
34
32
 
44
42
        self.margin = 12
45
43
                
46
44
        self.actor_name = {}
 
45
        self.actors= []
47
46
 
48
47
        self.desc_entry = Gtk.Label(xalign=0,yalign=0,wrap=True)
49
48
        self.pack_start(self.desc_entry, False, False, 6)
103
102
        self.hbox.pack_start(self.viewer, True, True, 3)
104
103
 
105
104
        # The data for polling the actors
106
 
        self.template = Event.new_for_values(subjects=[Subject()])
 
105
        self.template = Zeitgeist.Event.new()
 
106
        self.template.add_subject (Zeitgeist.Subject())
107
107
 
108
108
        self.populate_actors()
109
109
        self.show_all()
148
148
        self.viewer.clear()
149
149
        self.populate_actors()
150
150
 
151
 
    def populate_actors_callback(self, events):
 
151
    def populate_actors_callback(self, log, result, data):
 
152
        events = log.find_events_finish(result)
152
153
        miscs = []
 
154
        self.actors = []
153
155
        self.apps.clear()
154
156
        self.actor_name.clear()
155
 
        for event in events:
156
 
            actor = event.get_actor()
157
 
            try:
158
 
                app_info = Gio.DesktopAppInfo.new(actor.replace("application://", ""))
159
 
                self.apps.append([actor, 
160
 
                                app_info.get_display_name(),
161
 
                                self._get_pixbuf_from_gio_icon(
162
 
                                    app_info.get_icon())
163
 
                                ])
164
 
                self.actor_name[actor] = app_info.get_display_name()
165
 
            except TypeError as e:
166
 
                if actor.strip() != "":
167
 
                    miscs.append([actor, actor, None])
168
 
        for misc in miscs:
169
 
            self.apps.append(misc)
 
157
        for i in range(events.size()):
 
158
            event = events.next_value()
 
159
            if event:
 
160
                actor = event.get_property("actor")
 
161
                try:
 
162
                    app_info = Gio.DesktopAppInfo.new(actor.replace("application://", ""))
 
163
                    if actor not in self.actors:
 
164
                        self.actors.append(actor)
 
165
                        self.apps.append([actor, 
 
166
                                    app_info.get_display_name(),
 
167
                                    self._get_pixbuf_from_gio_icon(
 
168
                                        app_info.get_icon())
 
169
                                    ])
 
170
                    self.actor_name[actor] = app_info.get_display_name()
 
171
                except TypeError as e:                
 
172
                    if actor.strip() != "":
 
173
                        miscs.append([actor, actor.replace("application://","").replace(".desktop",""), None])
 
174
            for misc in miscs:
 
175
                if misc[0] not in self.actors:
 
176
                    self.actors.append(misc[0])
 
177
                    self.apps.append(misc)
170
178
 
171
179
    def populate_actors(self):
172
 
        self._client.find_events_for_templates(
173
 
                [self.template], 
174
 
                self.populate_actors_callback,
175
 
                timerange=TimeRange.until_now(),
176
 
                result_type = ResultType.MostPopularActor, 
177
 
                num_events = 100)
178
 
 
 
180
        self._client.find_events (Zeitgeist.TimeRange.anytime (), \
 
181
                                [self.template], \
 
182
                                Zeitgeist.StorageState.ANY, \
 
183
                                100, \
 
184
                                Zeitgeist.ResultType.MOST_POPULAR_ACTOR, \
 
185
                                None, \
 
186
                                self.populate_actors_callback, \
 
187
                                None)
 
188
                
179
189
    def on_treeview_selected(self, treeview):
180
190
        selection = self.apps_tree.get_selection()
181
191
        if selection is not None:
272
282
        self.toolbar.insert(list_remove, 1)
273
283
 
274
284
        self.actor=""
275
 
        self.template = Event.new_for_values(subjects=[Subject()], actor=self.actor)
 
285
        self.template = Zeitgeist.Event.new()
 
286
        self.template.set_property ("actor", self.actor)
 
287
        self.template.add_subject (Zeitgeist.Subject())
276
288
        
277
289
        self.apps_tree.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
278
290
 
321
333
    def show(self, actor):
322
334
        self.actor = actor
323
335
        self.apps.clear()
324
 
        self.template.set_actor(actor)
325
 
        self._client.find_events_for_templates(
 
336
        self.template.set_property("actor", actor)
 
337
        self._client.find_events(
 
338
                Zeitgeist.TimeRange.to_now() ,
326
339
                [self.template], 
 
340
                Zeitgeist.StorageState.ANY,
 
341
                100, 
 
342
                Zeitgeist.ResultType.MOST_RECENT_EVENTS, 
 
343
                None,
327
344
                self.populate_events_callback,
328
 
                timerange=TimeRange.until_now(),
329
 
                result_type = ResultType.MostRecentEvents, 
330
 
                num_events = 100)
 
345
                None)
331
346
 
332
347
    def get_timestamp_from_event(self, event):
333
 
        ev_time = datetime.fromtimestamp(int(event.get_timestamp())/1000)
 
348
        ev_time = datetime.fromtimestamp(int(event.get_property("timestamp")/1000))
334
349
        return ev_time.strftime("%a, %x %X")
335
350
        
336
 
    def populate_events_callback(self, events):
 
351
    def populate_events_callback(self, log, result, data):
 
352
        events = log.find_events_finish(result)
337
353
        self.events = events
338
 
        for event in events:
339
 
            _iter = self.apps.append(None, 
340
 
                                [self.get_timestamp_from_event(event),
341
 
                                event.get_interpretation().split("#")[-1],
342
 
                                event.get_manifestation().split("#")[-1],
343
 
                                True, event.get_id()])
344
 
            for subject in event.get_subjects():
345
 
                uri = subject.get_uri().strip()
346
 
                try:
347
 
                    file_inst = Gio.file_new_for_uri(uri)
348
 
                    uri_scheme = file_inst.get_uri_scheme()
349
 
                    if uri_scheme == "file":
350
 
                        uri = file_inst.get_path()
351
 
                    elif uri_scheme == "application":
352
 
                        try:
353
 
                            app_info = Gio.DesktopAppInfo.new(uri.replace("application://", ""))
354
 
                            app_name = app_info.get_display_name()
355
 
                            uri = "{0} - {1}".format(app_name, uri) if app_name != "" else uri
356
 
                        except TypeError as e:
357
 
                            pass
358
 
                except GLib.ConvertError:
359
 
                    pass
360
 
                inter = subject.get_interpretation().split("#")[-1]
361
 
                manifes = subject.get_manifestation().split("#")[-1]
362
 
                if uri != "":
363
 
                    self.apps.append(_iter, [uri, inter, manifes, False, -1])
 
354
        for i in range(events.size()):
 
355
            event = events.next_value()
 
356
            if event:
 
357
                _iter = self.apps.append(None, 
 
358
                                    [self.get_timestamp_from_event(event),
 
359
                                    event.get_property("interpretation").split("#")[-1],
 
360
                                    event.get_property("manifestation").split("#")[-1],
 
361
                                    True, event.get_property("id")])
 
362
                for i in range(event.num_subjects()):
 
363
                    subject = event.get_subject(i)
 
364
                    uri = subject.get_property("uri").strip()
 
365
                    try:
 
366
                        file_inst = Gio.file_new_for_uri(uri)
 
367
                        uri_scheme = file_inst.get_uri_scheme()
 
368
                        if uri_scheme == "file":
 
369
                            uri = file_inst.get_path()
 
370
                        elif uri_scheme == "application":
 
371
                            try:
 
372
                                app_info = Gio.DesktopAppInfo.new(uri.replace("application://", ""))
 
373
                                app_name = app_info.get_display_name()
 
374
                                uri = "{0} - {1}".format(app_name, uri) if app_name != "" else uri
 
375
                            except TypeError as e:
 
376
                                pass
 
377
                    except GLib.ConvertError:
 
378
                        pass
 
379
                    inter = subject.get_property("interpretation").split("#")[-1]
 
380
                    manifes = subject.get_property("manifestation").split("#")[-1]
 
381
                    if uri != "":
 
382
                        self.apps.append(_iter, [uri, inter, manifes, False, -1])
364
383
                
365
384
        self.apps_tree.expand_all()