194e1d3de0c80a8b5fda6c3cd225a1934f0ca1bf
[feedingit] / psa_harmattan / feedingit / pysrc / feedingit.py
1 #!/usr/bin/python
2
3 import sys
4
5 from PySide import QtGui
6 from PySide import QtDeclarative
7 import os
8 from os import mkdir, remove, stat, environ
9 from os.path import isfile, isdir, exists
10 import codecs
11 from gconf import client_get_default
12
13 # Comment the line below if you don't want to use OpenGL for QML rendering or if it is not supported
14 from PySide import QtOpenGL,  QtCore
15
16 from rss_sqlite import Listing
17 CONFIGDIR = environ.get("HOME", "/home/user") + "/.feedingit"
18 #CONFIGDIR = "/home/user/.feedingit"
19
20 import logging
21 logger = logging.getLogger(__name__)
22
23 import debugging
24 debugging.init(dot_directory=".feedingit", program_name="feedingit-pyside")
25
26 from cgi import escape
27 from re import sub
28
29 class Controller(QtCore.QObject):
30     
31     def __init__(self, listing):
32         QtCore.QObject.__init__(self)
33         from XmlHandler import XmlHandler
34         self._handler = XmlHandler(listing)
35
36     @QtCore.Slot(str,str, result=str)
37     def getArticle(self, key, article):
38        feed = listing.getFeed(key)
39        try:
40           file = codecs.open(feed.getContentLink(article), "r", "utf-8")
41           html = file.read().replace("body", "body bgcolor='#ffffff'", 1)
42           file.close()
43        except:
44           html = u"<html><body>Error retrieving article</body></html>"
45        return html
46     
47     @QtCore.Slot(str, result=str)
48     def getFeedsXml(self, catid):
49         return self._handler.generateFeedsXml(catid)
50     
51     @QtCore.Slot(str,result=str)
52     def getArticlesXml(self, key):
53         #onlyUnread = arguments.get("onlyUnread","False")
54         return self._handler.generateArticlesXml(key, config.getHideReadArticles())
55     
56     @QtCore.Slot(result=str)
57     def getCategoryXml(self):
58         return self._handler.generateCategoryXml()
59     
60     @QtCore.Slot(QtCore.QObject)
61     def feedClicked(self, wrapper):
62         #print 'User clicked on:', wrapper._key
63         #articlesModel.updateModel(wrapper._key)
64         pass
65         
66     @QtCore.Slot(str)
67     def updateFeed(self, key):
68         listing.updateFeed(key)
69         
70     @QtCore.Slot()
71     def updateAll(self):
72         for catid in listing.getListOfCategories():
73             for feed in listing.getSortedListOfKeys("Manual", category=catid):
74                 listing.updateFeed(feed)
75
76     @QtCore.Slot(str)
77     def updateCategory(self, catid):
78         for feed in listing.getSortedListOfKeys("Manual", category=catid):
79             listing.updateFeed(feed)           
80
81     @QtCore.Slot(str,str,str)
82     def addFeed(self, title, url, catid):
83         listing.addFeed(title,url, category=catid)
84         
85     @QtCore.Slot(str)
86     def addCategory(self, name):
87         listing.addCategory(name)
88
89     @QtCore.Slot(str)
90     def removeFeed(self, key):
91         listing.removeFeed(key)
92
93     @QtCore.Slot(str)
94     def removeCategory(self, catid):
95         listing.removeCategory(catid)
96
97     @QtCore.Slot(str)
98     def markAllAsRead(self, key):
99         feed = listing.getFeed(key)
100         feed.markAllAsRead()
101
102     @QtCore.Slot(str, str)
103     def setEntryRead(self, key, articleid):
104         feed = listing.getFeed(key)
105         feed.setEntryRead(articleid)
106         listing.updateUnread(key)
107
108     @QtCore.Slot(str, result=str)
109     def getConfig(self, item):
110         if (item == "hideReadFeed"):
111             return "True"
112         if (item == "hideReadArticles"):
113             return "False"
114         return ""
115     
116     @QtCore.Slot(str, str)
117     def populateFileDialog(self, path, type):
118         import glob
119         import os.path
120         for file in glob.glob(path+type):
121             logger.debug(file)
122             root.addFileNotification(file, os.path.basename(file))
123     
124     @QtCore.Slot(str, result=int)
125     def importOpml(self, filename):
126         from opml_lib import parseOpml
127         file = open(filename, "r")
128         feeds = parseOpml(file.read())
129         file.close()
130         for (title, url) in feeds:
131             listing.addFeed(title, url)
132         return len(feeds)
133     
134     @QtCore.Slot(str, result=str)
135     def exportOpml(self, filename="/home/user/MyDocs/feedingit-export.opml"):
136         logger.debug("ExportOpmlData: %s" % filename)
137         from opml_lib import getOpmlText
138         try:
139             str = getOpmlText(listing)
140             file = open(filename, "w")
141             file.write(str)
142             file.close()
143             return filename
144         except:
145             logger.debug("Error exporting: %s" % filename)
146             return "error"
147         
148     @QtCore.Slot(str, result=bool)
149     def getBooleanSetting(self, setting):
150         if (setting == "theme"):
151             return config.getTheme()
152         elif (setting == "imageCache" ):
153             return config.getImageCache()
154         elif (setting == "hideReadFeeds"):
155             return config.getHideReadFeeds()
156         elif (setting == "hideReadArticles"):
157             return config.getHideReadArticles()
158         elif (setting == "autoupdate"):
159             return config.isAutoUpdateEnabled()
160         else:
161             return 'True'
162         
163     @QtCore.Slot(str, bool)
164     def setBooleanSetting(self, setting, value):
165         if (setting == "theme"):
166             config.setTheme(value)
167         elif (setting == "imageCache" ):
168             config.setImageCache(value)
169         elif (setting == "hideReadFeeds"):
170             config.setHideReadFeeds(value)
171         elif (setting == "hideReadArticles"):
172             config.setHideReadArticles(value)
173         elif (setting == "autoupdate"):
174             config.setAutoUpdateEnabled(value)
175         config.saveConfig()
176
177 def main():
178
179     if not isdir(CONFIGDIR):
180         try:
181             mkdir(CONFIGDIR)
182         except:
183             logger.error("Error: Can't create configuration directory")
184             from sys import exit
185             exit(1)
186             
187     from config import Config
188     global config
189     config = Config(None,CONFIGDIR+"config.ini")
190
191     global listing
192     listing = Listing(config, CONFIGDIR)
193     
194     import mainthread
195     mainthread.init()
196
197     from jobmanager import JobManager
198     JobManager(True)
199
200     app = QtGui.QApplication(sys.argv)
201     view = QtDeclarative.QDeclarativeView()
202
203     controller = Controller(listing)
204  
205     global root
206     rc = view.rootContext()
207  
208     rc.setContextProperty('controller', controller)
209
210     # Comment the two lines below if you don't want to use OpenGL for QML rendering or if it is not supported
211     #glw = QtOpenGL.QGLWidget()
212     #view.setViewport(glw)
213
214     if os.path.exists('/usr/share/feedingit/qml'):
215         glw = QtOpenGL.QGLWidget()
216         view.setViewport(glw)
217         view.setSource('/usr/share/feedingit/qml/main.qml')
218         view.showFullScreen()
219     else:
220         view.setSource(os.path.join('qml','main.qml'))
221         view.show()
222         #view.setSource(os.path.join('qml','FeedingIt.qml'))
223     root = view.rootObject()
224
225     #view.showFullScreen()
226     #view.show()
227     sys.exit(app.exec_())
228
229 if __name__ == "__main__":
230     
231     main()