Move download management from frontends to rss_sqlite.py.
[feedingit] / src / config.py
1 #!/usr/bin/env python2.5
2
3
4 # Copyright (c) 2007-2008 INdT.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (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 Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # ============================================================================
20 # Name        : FeedingIt.py
21 # Author      : Yves Marcoz
22 # Version     : 0.6.1
23 # Description : Simple RSS Reader
24 # ============================================================================
25 #try:
26 #    import gtk
27 #    import hildon
28 #    from gobject import idle_add
29 #except:
30 #    pass
31
32 from ConfigParser import RawConfigParser
33 from gconf import client_get_default
34 from urllib2 import ProxyHandler
35 from mainthread import mainthread
36
37 VERSION = "52"
38
39 section = "FeedingIt"
40 ranges = { "updateInterval":[0.5, 1, 2, 4, 12, 24], "expiry":[24, 48, 72, 144, 288], "fontSize":range(12,24), "orientation":["Automatic", "Landscape", "Portrait"], "artFontSize":[10, 12, 14, 16, 18, 20], "feedsort":["Manual", "Most unread", "Least unread", "Most recent", "Least recent"] }
41 titles = {"updateInterval":"Auto-update interval", "expiry":"Delete articles", "fontSize":"List font size", "orientation":"Display orientation", "artFontSize":"Article font size","feedsort":"Feed sort order"}
42 subtitles = {"updateInterval":"Every %s hours", "expiry":"After %s hours", "fontSize":"%s pixels", "orientation":"%s", "artFontSize":"%s pixels", "feedsort":"%s"}
43
44 class Config():
45     def __init__(self, parent, configFilename):
46         self.configFilename = configFilename
47         self.parent = parent
48         # Load config
49         self.loadConfig()
50
51         # Backup current settings for later restore
52         self.config_backup = dict(self.config)
53         self.do_restore_backup = True
54
55     def on_save_button_clicked(self, button):
56         self.do_restore_backup = False
57         self.window.destroy()
58
59     def createDialog(self):
60         import gtk
61         import hildon
62         from gobject import idle_add
63         self.window = gtk.Dialog("Settings", self.parent)
64         self.window.set_geometry_hints(min_height=600)
65
66         save_button = self.window.add_button(gtk.STOCK_SAVE, gtk.RESPONSE_OK)
67         save_button.connect('clicked', self.on_save_button_clicked)
68         #self.window.set_default_size(-1, 600)
69         panArea = hildon.PannableArea()
70         
71         vbox = gtk.VBox(False, 2)
72         self.buttons = {}
73
74         def heading(text):
75             l = gtk.Label()
76             l.set_size_request(-1, 6)
77             vbox.pack_start(l, expand=False)
78             vbox.pack_start(gtk.Frame(text), expand=False)
79
80         def add_setting(setting):
81             picker = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
82             selector = self.create_selector(ranges[setting], setting)
83             picker.set_selector(selector)
84             picker.set_title(titles[setting])
85             picker.set_text(titles[setting], subtitles[setting] % self.config[setting])
86             picker.set_name('HildonButton-finger')
87             picker.set_alignment(0,0,1,1)
88             self.buttons[setting] = picker
89             vbox.pack_start(picker, expand=False)
90
91         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
92         button.set_label("View Known Issues and Tips")
93         button.connect("clicked", self.button_tips_clicked)
94         button.set_alignment(0,0,1,1)
95         vbox.pack_start(button, expand=False)  
96
97         heading('Display')
98         add_setting('fontSize')
99         add_setting('artFontSize')
100         add_setting('orientation')
101         add_setting('feedsort')
102         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
103         button.set_label("Hide read feeds")
104         button.set_active(self.config["hidereadfeeds"])
105         button.connect("toggled", self.button_toggled, "hidereadfeeds")
106         vbox.pack_start(button, expand=False)
107
108         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
109         button.set_label("Hide read articles")
110         button.set_active(self.config["hidereadarticles"])
111         button.connect("toggled", self.button_toggled, "hidereadarticles")
112         vbox.pack_start(button, expand=False)
113
114
115         heading('Updating')
116         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
117         button.set_label("Automatically update feeds")
118         button.set_active(self.config["autoupdate"])
119         button.connect("toggled", self.button_toggled, "autoupdate")
120         vbox.pack_start(button, expand=False)
121         add_setting('updateInterval')
122         add_setting('expiry')
123
124         heading('Network')
125         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
126         button.set_label('Cache images')
127         button.set_active(self.config["imageCache"])
128         button.connect("toggled", self.button_toggled, "imageCache")
129         vbox.pack_start(button, expand=False)
130
131         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
132         button.set_label("Use HTTP proxy")
133         button.set_active(self.config["proxy"])
134         button.connect("toggled", self.button_toggled, "proxy")
135         vbox.pack_start(button, expand=False)
136         
137         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
138         button.set_label('Open links in external browser')
139         button.set_active(self.config["extBrowser"])
140         button.connect("toggled", self.button_toggled, "extBrowser")
141         vbox.pack_start(button, expand=False)
142         
143         panArea.add_with_viewport(vbox)
144         
145         self.window.vbox.add(panArea)
146         self.window.connect("destroy", self.onExit)
147         #self.window.add(self.vbox)
148         self.window.set_default_size(-1, 600)
149         self.window.show_all()
150         return self.window
151
152     def button_tips_clicked(self, *widget):
153         import dbus
154         bus = dbus.SessionBus()
155         proxy = bus.get_object("com.nokia.osso_browser", "/com/nokia/osso_browser/request")
156         iface = dbus.Interface(proxy, 'com.nokia.osso_browser')
157         iface.open_new_window("http://feedingit.marcoz.org/news/?page_id=%s" % VERSION)
158
159     def onExit(self, *widget):
160         # When the dialog is closed without hitting
161         # the "Save" button, restore the configuration
162         if self.do_restore_backup:
163             print 'Restoring configuration'
164             self.config = self.config_backup
165
166         self.saveConfig()
167         self.window.destroy()
168
169     def button_toggled(self, widget, configName):
170         #print "widget", widget.get_active()
171         if (widget.get_active()):
172             self.config[configName] = True
173         else:
174             self.config[configName] = False
175         #print "autoup",  self.autoupdate
176         self.saveConfig()
177         
178     def selection_changed(self, selector, button, setting):
179         from gobject import idle_add
180         current_selection = selector.get_current_text()
181         if current_selection:
182             self.config[setting] = current_selection
183         idle_add(self.updateButton, setting)
184         self.saveConfig()
185         
186     def updateButton(self, setting):
187         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
188         
189     def loadConfig(self):
190         self.config = {}
191         try:
192             configParser = RawConfigParser()
193             configParser.read(self.configFilename)
194             self.config["fontSize"] = configParser.getint(section, "fontSize")
195             self.config["artFontSize"] = configParser.getint(section, "artFontSize")
196             self.config["expiry"] = configParser.getint(section, "expiry")
197             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
198             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
199             self.config["orientation"] = configParser.get(section, "orientation")
200             self.config["imageCache"] = configParser.getboolean(section, "imageCache")
201         except:
202             self.config["fontSize"] = 17
203             self.config["artFontSize"] = 14
204             self.config["expiry"] = 24
205             self.config["autoupdate"] = False
206             self.config["updateInterval"] = 4
207             self.config["orientation"] = "Automatic"
208             self.config["imageCache"] = False
209         try:
210             self.config["proxy"] = configParser.getboolean(section, "proxy")
211         except:
212             self.config["proxy"] = True
213         try:
214             self.config["hidereadfeeds"] = configParser.getboolean(section, "hidereadfeeds")
215             self.config["hidereadarticles"] = configParser.getboolean(section, "hidereadarticles")
216         except:
217             self.config["hidereadfeeds"] = False
218             self.config["hidereadarticles"] = False
219         try:
220             self.config["extBrowser"] = configParser.getboolean(section, "extBrowser")
221         except:
222             self.config["extBrowser"] = False
223         try:
224             self.config["feedsort"] = configParser.get(section, "feedsort")
225         except:
226             self.config["feedsort"] = "Manual"
227         
228     def saveConfig(self):
229         configParser = RawConfigParser()
230         configParser.add_section(section)
231         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
232         configParser.set(section, 'artFontSize', str(self.config["artFontSize"]))
233         configParser.set(section, 'expiry', str(self.config["expiry"]))
234         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
235         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
236         configParser.set(section, 'orientation', str(self.config["orientation"]))
237         configParser.set(section, 'imageCache', str(self.config["imageCache"]))
238         configParser.set(section, 'proxy', str(self.config["proxy"]))
239         configParser.set(section, 'hidereadfeeds', str(self.config["hidereadfeeds"]))
240         configParser.set(section, 'hidereadarticles', str(self.config["hidereadarticles"]))
241         configParser.set(section, 'extBrowser', str(self.config["extBrowser"]))
242         configParser.set(section, 'feedsort', str(self.config["feedsort"]))
243
244         # Writing our configuration file
245         file = open(self.configFilename, 'wb')
246         configParser.write(file)
247         file.close()
248
249     def create_selector(self, choices, setting):
250         import gtk
251         import hildon
252         from gobject import idle_add
253         #self.pickerDialog = hildon.PickerDialog(self.parent)
254         selector = hildon.TouchSelector(text=True)
255         index = 0
256         for item in choices:
257             iter = selector.append_text(str(item))
258             if str(self.config[setting]) == str(item): 
259                 selector.set_active(0, index)
260             index += 1
261         selector.connect("changed", self.selection_changed, setting)
262         #self.pickerDialog.set_selector(selector)
263         return selector
264         #self.pickerDialog.show_all()
265
266     def getFontSize(self):
267         return self.config["fontSize"]
268     def getArtFontSize(self):
269         return self.config["artFontSize"]
270     def getExpiry(self):
271         return self.config["expiry"]
272     def isAutoUpdateEnabled(self):
273         return self.config["autoupdate"]
274     def getUpdateInterval(self):
275         return float(self.config["updateInterval"])
276     def getReadFont(self):
277         return "sans italic %s" % self.config["fontSize"]
278     def getUnreadFont(self):
279         return "sans %s" % self.config["fontSize"]
280     def getOrientation(self):
281         return ranges["orientation"].index(self.config["orientation"])
282     def getImageCache(self):
283         return self.config["imageCache"]
284     @mainthread
285     def getProxy(self):
286         if self.config["proxy"] == False:
287             return (False, None)
288         if client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
289             port = client_get_default().get_int('/system/http_proxy/port')
290             http = client_get_default().get_string('/system/http_proxy/host')
291             proxy = ProxyHandler( {"http":"http://%s:%s/"% (http,port)} )
292             return (True, proxy)
293         return (False, None)
294     def getHideReadFeeds(self):
295         return self.config["hidereadfeeds"]
296     def getHideReadArticles(self):
297         return self.config["hidereadarticles"]
298     def getOpenInExternalBrowser(self):
299         return self.config["extBrowser"]
300     def getFeedSortOrder(self):
301         return self.config["feedsort"]