[tracker] Add some zeitgeist interaction in the RSS-reader example
- From: Ivan Frade <ifrade src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [tracker] Add some zeitgeist interaction in the RSS-reader example
- Date: Thu, 12 Nov 2009 19:10:07 +0000 (UTC)
commit 12bd295c08f82dfa2b6bd55dcc2d00e5573bd820
Author: Ivan Frade <ivan frade nokia com>
Date: Thu Nov 12 21:07:47 2009 +0200
Add some zeitgeist interaction in the RSS-reader example
python/rss_reader/main.py | 104 +++++++++++++++++---------------
python/rss_reader/tracker_backend.py | 2 +-
python/rss_reader/zeitgeist_backend.py | 92 ++++++++++++++++++++++++++++
3 files changed, 148 insertions(+), 50 deletions(-)
---
diff --git a/python/rss_reader/main.py b/python/rss_reader/main.py
index 81b8bbc..58b551b 100755
--- a/python/rss_reader/main.py
+++ b/python/rss_reader/main.py
@@ -24,16 +24,31 @@ import pango
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from tracker_backend import TrackerRSS
+try:
+ from zeitgeist_backend import ZeitgeistBackend
+ zg = ZeitgeistBackend ()
+except ImportError:
+ class MockZeitgeistBackend:
+ def view_event (self, uri):
+ pass
+ def get_rss_by_usage (self):
+ return []
+ def clean_events (self):
+ pass
+ zg = MockZeitgeistBackend ()
+
# uri, title, date, isread, pango.Weight
+# Be careful, we use this also in the code for the zg_model!
posts_model = gtk.ListStore (str, str, str, bool, int)
# uri, title, entries, enabled
channels_model = gtk.ListStore (str, str, str, bool)
-posts_treeview = None
+ui = gtk.Builder ()
+ui.add_from_file ("./rss_viewer.ui")
+
channels_treeview = None
-text_view = None
sources_dialog = None
# Convenience defines
@@ -44,19 +59,6 @@ BOLD = pango.WEIGHT_BOLD
tracker = TrackerRSS ()
-class SimpleTextView (gtk.TextView):
-
- def __init__ (self, buffer=None):
- gtk.TextView.__init__ (self, buffer)
- self.set_wrap_mode (gtk.WRAP_WORD)
-
- def set_plain_text (self, text):
- self.get_buffer().set_text (text)
-
- def get_plain_text (self):
- ini, end = self.get_buffer().get_bounds ()
- return self.get_buffer ().get_text (ini, end)
-
def populate_initial_posts ():
results = tracker.get_post_sorted_by_date (10)
@@ -69,20 +71,21 @@ def populate_initial_posts ():
posts_model.append (row)
def create_posts_tree_view ():
- treeview = gtk.TreeView ()
- column_title = gtk.TreeViewColumn ("Title",
- gtk.CellRendererText (),
- text=1, weight=4)
- column_date = gtk.TreeViewColumn ("Date",
- gtk.CellRendererText (),
- text=2)
- treeview.append_column (column_title)
- treeview.append_column (column_date)
+ for treeview in [ui.get_object ("posts_treeview"),
+ ui.get_object ("posts_treeview_by_usage")]:
+ column_title = gtk.TreeViewColumn ("Title",
+ gtk.CellRendererText (),
+ text=1, weight=4)
+ column_date = gtk.TreeViewColumn ("Date",
+ gtk.CellRendererText (),
+ text=2)
- treeview.connect ("cursor-changed", cursor_changed_cb)
+ treeview.append_column (column_title)
+ treeview.append_column (column_date)
+
+ treeview.connect ("cursor-changed", cursor_changed_cb)
- return treeview
def create_channels_tree_view ():
treeview = gtk.TreeView ()
@@ -126,7 +129,7 @@ def toggle_row_foreach (treemodel, path, iter):
tracker.set_is_read (uri, True)
def clicked_toggle_cb (widget):
- selected = posts_treeview.get_selection ()
+ selected = ui.get_object ("posts_treeview").get_selection ()
if (selected.count_selected_rows () == 0):
return
@@ -138,7 +141,10 @@ def cursor_changed_cb (tv):
model, it = selection.get_selected ()
uri = model.get_value (it, 0)
text = tracker.get_text_for_uri (uri)
- text_view.set_plain_text (text)
+ tv = ui.get_object ("text_view")
+ tv.get_buffer ().set_text (text)
+ zg.view_event (uri)
+
def clicked_sources_cb (widget, dialog):
# Dont do this all the time!
@@ -187,6 +193,22 @@ def update_uri (model, path, iter, user_data):
3, user_data[3],
4, user_data[4])
return True
+
+def change_tab_cb (notebook, page, page_num):
+ print "Change to tab", page_num
+ if (page_num == 1):
+ uris = zg.get_rss_by_usage ()
+ zg_model = gtk.ListStore (str, str, str, bool, int)
+ for uri in uris:
+ for row in posts_model:
+ print uri, map (str,row)
+ if row[0] == uri :
+ print "Add to the model!"
+ zg_model.append (row)
+
+ ui.get_object ("posts_treeview_by_usage").set_model (zg_model)
+ else:
+ ui.get_object ("posts_treeview").set_model (posts_model)
def notification_update (updated_uris, update_predicates):
@@ -222,18 +244,10 @@ if __name__ == "__main__":
dbus_interface="org.freedesktop.Tracker1.Resources.Class",
path="/org/freedesktop/Tracker1/Resources/Classes/mfo/FeedMessage")
+ window = ui.get_object ("main_window")
gobject.set_application_name ("Rss/tracker")
- window = gtk.Window ()
-
- vbox = gtk.VBox ()
- posts_treeview = create_posts_tree_view ()
- posts_treeview.set_model (posts_model)
- vbox.add (posts_treeview)
-
- frame = gtk.Frame ()
- text_view = SimpleTextView ()
- frame.add (text_view)
- vbox.add (frame)
+ create_posts_tree_view ()
+ ui.get_object("posts_treeview").set_model (posts_model)
channels_treeview = create_channels_tree_view ()
channels_treeview.set_model (channels_model)
@@ -245,17 +259,9 @@ if __name__ == "__main__":
dialog.vbox.add (channels_treeview)
- button_box = gtk.HBox ()
- button_toggle = gtk.Button (label="Read/Unread")
- button_toggle.connect ("clicked", clicked_toggle_cb)
- button_box.add (button_toggle)
- button_sources = gtk.Button (stock=gtk.STOCK_HOME)
- button_sources.connect ("clicked", clicked_sources_cb, dialog)
- button_box.add (button_sources)
+ ui.get_object ("button_toggle").connect ("clicked", clicked_toggle_cb)
+ ui.get_object ("notebook1").connect ("switch-page", change_tab_cb)
- vbox.pack_start (button_box, expand=False)
- window.add (vbox)
-
populate_initial_posts ()
window.show_all ()
diff --git a/python/rss_reader/tracker_backend.py b/python/rss_reader/tracker_backend.py
index 63baa0c..4c6571f 100644
--- a/python/rss_reader/tracker_backend.py
+++ b/python/rss_reader/tracker_backend.py
@@ -99,7 +99,7 @@ class TrackerRSS:
def get_post_sorted_by_date (self, amount):
results = self.iface.SparqlQuery (QUERY_FIRST_ENTRIES % (amount))
return results
-
+
def set_is_read (self, uri, value):
if (value):
dbus_value = TRUE
diff --git a/python/rss_reader/zeitgeist_backend.py b/python/rss_reader/zeitgeist_backend.py
new file mode 100644
index 0000000..b64ad8c
--- /dev/null
+++ b/python/rss_reader/zeitgeist_backend.py
@@ -0,0 +1,92 @@
+#
+# Demo RSS client using tracker as backend
+# Copyright (C) 2009 Nokia <urho konttori nokia com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+# USA
+#
+import dbus
+import os
+import zeitgeist
+import time
+
+import zeitgeist.dbusutils
+from zeitgeist.datamodel import Manifestation, Interpretation, Event, Subject
+
+APP_ID = u"/usr/local/share/tracker/examples/rss-python/rss-pseudo-miner.desktop"
+
+class ZeitgeistBackend:
+
+ def __init__ (self):
+ self.iface = zeitgeist.dbusutils.DBusInterface ()
+
+ def view_event (self, uri):
+ ev = Event.new_for_values(timestamp=int(time.time()),
+ interpretation=Interpretation.VISIT_EVENT.uri,
+ manifestation=Manifestation.USER_ACTIVITY.uri,
+ actor=APP_ID)
+ subj = Subject.new_for_values(uri=uri,
+ interpretation=Interpretation.DOCUMENT.uri,
+ manifestation=Manifestation.FILE.uri)
+ ev.append_subject(subj)
+
+ self.iface.InsertEvents ([ev])
+ print "VISIT event for <%s> " % (uri)
+
+
+ def get_rss_by_usage (self):
+ print "Events: ",
+ event_template = Event.new_for_values(
+ actor=APP_ID,
+ interpretation=Interpretation.VISIT_EVENT.uri)
+
+ results = []
+
+ ids = self.iface.FindEventIds((0,0),
+ [event_template],
+ 0, 0, 5)
+ for event in self.iface.GetEvents (ids):
+ for subject in Event(event).subjects:
+ results.append (str(subject.uri))
+ return results
+
+
+ def clean_events (self):
+ event_template = Event.new_for_values(
+ actor=APP_ID,
+ interpretation=Interpretation.VISIT_EVENT.uri)
+ ids = self.iface.FindEventIds((0,0),
+ [event_template],
+ 0, 10, 0)
+ print "Deleting", map (int, ids)
+ self.iface.DeleteEvents (ids)
+
+
+if __name__ == "__main__":
+ zb = ZeitgeistBackend ()
+ # 1 -> 1 occurrences
+ # 2 -> 2 occurrence
+ # 3 -> 3 occurrences
+ zb.view_event ("rss://feed/3")
+ zb.view_event ("rss://feed/2")
+ time.sleep (1)
+ zb.view_event ("rss://feed/1")
+ zb.view_event ("rss://feed/3")
+ time.sleep (1)
+ zb.view_event ("rss://feed/2")
+ zb.view_event ("rss://feed/3")
+
+ zb.get_rss_by_usage ()
+ zb.clean_events ()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]