1#
2# Demo RSS client using tracker as backend
3# Copyright (C) 2009 Nokia <ivan.frade@nokia.com>
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18# 02110-1301, USA.
19#
20
21import dbus
22import os
23
24TRACKER = 'org.freedesktop.Tracker3'
25TRACKER_OBJ = '/org/freedesktop/Tracker3/Resources'
26
27FALSE = "false"
28TRUE = "true"
29
30QUERY_FIRST_ENTRIES = """
31    SELECT ?entry ?title ?date ?isRead WHERE {
32      ?entry a mfo:FeedMessage ;
33         nie:title ?title ;
34         nie:contentLastModified ?date .
35    OPTIONAL {
36       ?entry nmo:isRead ?isRead.
37    }
38    } ORDER BY DESC(?date) LIMIT %s
39    """
40
41SET_URI_AS_READED = """
42    DELETE {<%s> nmo:isRead "%s".}
43    INSERT {<%s> nmo:isRead "%s".}
44    """
45
46QUERY_ALL_SUBSCRIBED_FEEDS ="""
47    SELECT ?feeduri ?title COUNT (?entries) AS e WHERE {
48       ?feeduri a mfo:FeedChannel ;
49                nie:title ?title.
50       ?entries a mfo:FeedMessage ;
51                nmo:communicationChannel ?feeduri.
52    } GROUP BY ?feeduri
53"""
54
55QUERY_FOR_URI = """
56    SELECT ?title ?date ?isRead ?channel WHERE {
57      <%s> a mfo:FeedMessage ;
58             nie:title ?title ;
59             nie:contentLastModified ?date ;
60             nmo:communicationChannel ?channel .
61      OPTIONAL {
62      <%s> nmo:isRead ?isRead.
63      }
64    }
65"""
66
67QUERY_FOR_TEXT = """
68    SELECT ?text WHERE {
69    <%s> nie:plainTextContent ?text .
70    }
71"""
72
73CONF_FILE = os.path.expanduser ("~/.config/rss_tracker/rss.conf")
74
75class TrackerRSS:
76
77    def __init__ (self):
78        bus = dbus.SessionBus ()
79        self.tracker = bus.get_object (TRACKER, TRACKER_OBJ)
80        self.iface = dbus.Interface (self.tracker,
81                                     "org.freedesktop.Tracker3.Resources")
82        self.invisible_feeds = []
83        self.load_config ()
84
85
86    def load_config (self):
87        if (os.path.exists (CONF_FILE)):
88            print "Loading %s" % (CONF_FILE)
89            for line in open (CONF_FILE):
90                line = line.replace ('\n','')
91                if (len (line) > 0):
92                    self.invisible_feeds.append (line)
93            print "Hiding feeds from:", self.invisible_feeds
94        else:
95            if (not os.path.exists (os.path.dirname (CONF_FILE))):
96                os.makedirs (os.path.dirname (CONF_FILE))
97            f = open (CONF_FILE, 'w')
98            f.close ()
99
100    def get_post_sorted_by_date (self, amount):
101        results = self.iface.SparqlQuery (QUERY_FIRST_ENTRIES % (amount))
102        return results
103
104    def set_is_read (self, uri, value):
105        if (value):
106            dbus_value = TRUE
107            anti_value = FALSE
108        else:
109            dbus_value = FALSE
110            anti_value = TRUE
111
112        print "Sending ", SET_URI_AS_READED % (uri, anti_value, uri, dbus_value)
113        self.iface.SparqlUpdate (SET_URI_AS_READED % (uri, anti_value, uri, dbus_value))
114
115    def get_all_subscribed_feeds (self):
116        """ Returns [(uri, feed channel name, entries, visible)]
117        """
118        componed = []
119        results = self.iface.SparqlQuery (QUERY_ALL_SUBSCRIBED_FEEDS)
120        for result in results:
121            print "Looking for", result[0]
122            if (result[0] in self.invisible_feeds):
123                visible = False
124            else:
125                visible = True
126            componed.insert (0, result + [visible])
127
128        componed.reverse ()
129        return componed
130
131    def get_info_for_entry (self, uri):
132        """  Returns (?title ?date ?isRead)
133        """
134        details = self.iface.SparqlQuery (QUERY_FOR_URI % (uri, uri))
135        if (len (details) < 1):
136            print "No details !??!!"
137            return None
138        if (len (details) > 1):
139            print "OMG what are you asking for?!?!?!"
140            return None
141
142        info = details [0]
143        if (info[3] in self.invisible_feeds):
144            print "That feed is not visible"
145            return None
146        else:
147            if (info[2] == TRUE):
148                return (info[0], info[1], True)
149            else:
150                return (info[0], info[1], False)
151
152    def get_text_for_uri (self, uri):
153        text = self.iface.SparqlQuery (QUERY_FOR_TEXT % (uri))
154        if (text[0]):
155            text = text[0][0].replace ("\\n", "\n")
156        else:
157            text = ""
158        return text
159
160    def mark_as_invisible (self, uri):
161        self.invisible_feeds.append (uri)
162
163    def mark_as_visible (self, uri):
164        self.invisible_feeds.remove (uri)
165
166    def flush_to_file (self):
167        f = open (CONF_FILE, 'w')
168        for line in self.invisible_feeds:
169            f.write (line + "\n")
170        f.close ()
171