3d37d37d16f4bfe3bbaac12666c68ca97a1cb3a9
[feedingit] / psa_harmattan / feedingit / pysrc / 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 import logging
37 logger = logging.getLogger(__name__)
38
39 VERSION = "52"
40
41 section = "FeedingIt"
42 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"] }
43 titles = {"updateInterval":"Auto-update interval", "expiry":"Delete articles", "fontSize":"List font size", "orientation":"Display orientation", "artFontSize":"Article font size","feedsort":"Feed sort order"}
44 subtitles = {"updateInterval":"Every %s hours", "expiry":"After %s hours", "fontSize":"%s pixels", "orientation":"%s", "artFontSize":"%s pixels", "feedsort":"%s"}
45
46 class Config():
47     def __init__(self, parent, configFilename):
48         self.configFilename = configFilename
49         self.parent = parent
50         # Load config
51         self.loadConfig()
52
53         # Backup current settings for later restore
54         self.config_backup = dict(self.config)
55         self.do_restore_backup = True
56
57     def on_save_button_clicked(self, button):
58         self.do_restore_backup = False
59         self.window.destroy()
60
61     def createDialog(self):
62         import gtk
63         import hildon
64         from gobject import idle_add
65         self.window = gtk.Dialog("Settings", self.parent)
66         self.window.set_geometry_hints(min_height=600)
67
68         save_button = self.window.add_button(gtk.STOCK_SAVE, gtk.RESPONSE_OK)
69         save_button.connect('clicked', self.on_save_button_clicked)
70         #self.window.set_default_size(-1, 600)
71         panArea = hildon.PannableArea()
72         
73         vbox = gtk.VBox(False, 2)
74         self.buttons = {}
75
76         def heading(text):
77             l = gtk.Label()
78             l.set_size_request(-1, 6)
79             vbox.pack_start(l, expand=False)
80             vbox.pack_start(gtk.Frame(text), expand=False)
81
82         def add_setting(setting):
83             picker = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
84             selector = self.create_selector(ranges[setting], setting)
85             picker.set_selector(selector)
86             picker.set_title(titles[setting])
87             picker.set_text(titles[setting], subtitles[setting] % self.config[setting])
88             picker.set_name('HildonButton-finger')
89             picker.set_alignment(0,0,1,1)
90             self.buttons[setting] = picker
91             vbox.pack_start(picker, expand=False)
92
93         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
94         button.set_label("View Known Issues and Tips")
95         button.connect("clicked", self.button_tips_clicked)
96         button.set_alignment(0,0,1,1)
97         vbox.pack_start(button, expand=False)  
98
99         heading('Display')
100         add_setting('fontSize')
101         add_setting('artFontSize')
102         add_setting('orientation')
103         add_setting('feedsort')
104         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
105         button.set_label("Hide read feeds")
106         button.set_active(self.config["hidereadfeeds"])
107         button.connect("toggled", self.button_toggled, "hidereadfeeds")
108         vbox.pack_start(button, expand=False)
109
110         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
111         button.set_label("Hide read articles")
112         button.set_active(self.config["hidereadarticles"])
113         button.connect("toggled", self.button_toggled, "hidereadarticles")
114         vbox.pack_start(button, expand=False)
115
116
117         heading('Updating')
118         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
119         button.set_label("Automatically update feeds")
120         button.set_active(self.config["autoupdate"])
121         button.connect("toggled", self.button_toggled, "autoupdate")
122         vbox.pack_start(button, expand=False)
123         add_setting('updateInterval')
124         add_setting('expiry')
125
126         heading('Network')
127         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
128         button.set_label('Cache images')
129         button.set_active(self.config["imageCache"])
130         button.connect("toggled", self.button_toggled, "imageCache")
131         vbox.pack_start(button, expand=False)
132
133         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
134         button.set_label("Use HTTP proxy")
135         button.set_active(self.config["proxy"])
136         button.connect("toggled", self.button_toggled, "proxy")
137         vbox.pack_start(button, expand=False)
138         
139         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
140         button.set_label('Open links in external browser')
141         button.set_active(self.config["extBrowser"])
142         button.connect("toggled", self.button_toggled, "extBrowser")
143         vbox.pack_start(button, expand=False)
144         
145         panArea.add_with_viewport(vbox)
146         
147         self.window.vbox.add(panArea)
148         self.window.connect("destroy", self.onExit)
149         #self.window.add(self.vbox)
150         self.window.set_default_size(-1, 600)
151         self.window.show_all()
152         return self.window
153
154     def button_tips_clicked(self, *widget):
155         import dbus
156         bus = dbus.SessionBus()
157         proxy = bus.get_object("com.nokia.osso_browser", "/com/nokia/osso_browser/request")
158         iface = dbus.Interface(proxy, 'com.nokia.osso_browser')
159         iface.open_new_window("http://feedingit.marcoz.org/news/?page_id=%s" % VERSION)
160
161     def onExit(self, *widget):
162         # When the dialog is closed without hitting
163         # the "Save" button, restore the configuration
164         if self.do_restore_backup:
165             logger.debug('Restoring configuration')
166             self.config = self.config_backup
167
168         self.saveConfig()
169         self.window.destroy()
170
171     def button_toggled(self, widget, configName):
172         #print "widget", widget.get_active()
173         if (widget.get_active()):
174             self.config[configName] = True
175         else:
176             self.config[configName] = False
177         #print "autoup",  self.autoupdate
178         self.saveConfig()
179         
180     def selection_changed(self, selector, button, setting):
181         from gobject import idle_add
182         current_selection = selector.get_current_text()
183         if current_selection:
184             self.config[setting] = current_selection
185         idle_add(self.updateButton, setting)
186         self.saveConfig()
187         
188     def updateButton(self, setting):
189         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
190         
191     def loadConfig(self):
192         self.config = {}
193         try:
194             configParser = RawConfigParser()
195             configParser.read(self.configFilename)
196             self.config["fontSize"] = configParser.getint(section, "fontSize")
197             self.config["artFontSize"] = configParser.getint(section, "artFontSize")
198             self.config["expiry"] = configParser.getint(section, "expiry")
199             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
200             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
201             self.config["orientation"] = configParser.get(section, "orientation")
202             self.config["imageCache"] = configParser.getboolean(section, "imageCache")
203         except:
204             self.config["fontSize"] = 17
205             self.config["artFontSize"] = 14
206             self.config["expiry"] = 24
207             self.config["autoupdate"] = False
208             self.config["updateInterval"] = 4
209             self.config["orientation"] = "Automatic"
210             self.config["imageCache"] = False
211         try:
212             self.config["proxy"] = configParser.getboolean(section, "proxy")
213         except:
214             self.config["proxy"] = True
215         try:
216             self.config["hidereadfeeds"] = configParser.getboolean(section, "hidereadfeeds")
217             self.config["hidereadarticles"] = configParser.getboolean(section, "hidereadarticles")
218         except:
219             self.config["hidereadfeeds"] = False
220             self.config["hidereadarticles"] = False
221         try:
222             self.config["extBrowser"] = configParser.getboolean(section, "extBrowser")
223         except:
224             self.config["extBrowser"] = False
225         try:
226             self.config["feedsort"] = configParser.get(section, "feedsort")
227         except:
228             self.config["feedsort"] = "Manual"
229         try:
230             self.config["theme"] = configParser.get(section, "theme")
231         except:
232             self.config["theme"] = True
233         
234     def saveConfig(self):
235         configParser = RawConfigParser()
236         configParser.add_section(section)
237         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
238         configParser.set(section, 'artFontSize', str(self.config["artFontSize"]))
239         configParser.set(section, 'expiry', str(self.config["expiry"]))
240         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
241         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
242         configParser.set(section, 'orientation', str(self.config["orientation"]))
243         configParser.set(section, 'imageCache', str(self.config["imageCache"]))
244         configParser.set(section, 'proxy', str(self.config["proxy"]))
245         configParser.set(section, 'hidereadfeeds', str(self.config["hidereadfeeds"]))
246         configParser.set(section, 'hidereadarticles', str(self.config["hidereadarticles"]))
247         configParser.set(section, 'extBrowser', str(self.config["extBrowser"]))
248         configParser.set(section, 'feedsort', str(self.config["feedsort"]))
249         configParser.set(section, 'theme', str(self.config["theme"]))
250
251         # Writing our configuration file
252         file = open(self.configFilename, 'wb')
253         configParser.write(file)
254         file.close()
255
256     def create_selector(self, choices, setting):
257         import gtk
258         import hildon
259         from gobject import idle_add
260         #self.pickerDialog = hildon.PickerDialog(self.parent)
261         selector = hildon.TouchSelector(text=True)
262         index = 0
263         for item in choices:
264             iter = selector.append_text(str(item))
265             if str(self.config[setting]) == str(item): 
266                 selector.set_active(0, index)
267             index += 1
268         selector.connect("changed", self.selection_changed, setting)
269         #self.pickerDialog.set_selector(selector)
270         return selector
271         #self.pickerDialog.show_all()
272
273     def getFontSize(self):
274         return self.config["fontSize"]
275     def getArtFontSize(self):
276         return self.config["artFontSize"]
277     def getExpiry(self):
278         return self.config["expiry"]
279     def setExpiry(self, expiry):
280         self.config["expiry"] = expiry
281     def isAutoUpdateEnabled(self):
282         return self.config["autoupdate"]
283     def setAutoUpdateEnabled(self, value):
284         self.config["autoupdate"] = value
285     def getUpdateInterval(self):
286         return float(self.config["updateInterval"])
287     def getReadFont(self):
288         return "sans italic %s" % self.config["fontSize"]
289     def getUnreadFont(self):
290         return "sans %s" % self.config["fontSize"]
291     def getOrientation(self, index):
292         return ranges["orientation"].index(self.config["orientation"])
293     def getOrientationChoices(self):
294         return ranges["orientation"]
295     def setOrientation(self, choice):
296         self.config["orientation"] = index 
297     def getImageCache(self):
298         return self.config["imageCache"]
299     def setImageCache(self, cache):
300         self.config["imageCache"] = bool(cache) 
301     @mainthread
302     def getProxy(self):
303         if self.config["proxy"] == False:
304             return (False, None)
305         if client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
306             port = client_get_default().get_int('/system/http_proxy/port')
307             http = client_get_default().get_string('/system/http_proxy/host')
308             proxy = ProxyHandler( {"http":"http://%s:%s/"% (http,port)} )
309             return (True, proxy)
310         return (False, None)
311     def getHideReadFeeds(self):
312         return self.config["hidereadfeeds"]
313     def setHideReadFeeds(self, setting):
314         self.config["hidereadfeeds"] = bool(setting)
315     def getHideReadArticles(self):
316         return self.config["hidereadarticles"]
317     def setHideReadArticles(self, setting):
318         self.config["hidereadarticles"] = bool(setting)
319     def getOpenInExternalBrowser(self):
320         return self.config["extBrowser"]
321     def getFeedSortOrder(self):
322         return self.config["feedsort"]
323     def getFeedSortOrderChoices(self):
324         return ranges["feedsort"]
325     def setFeedSortOrder(self, setting):
326         self.config["feedsort"] = setting
327     def getTheme(self):
328         return self.config["theme"]
329     def setTheme(self, theme):
330         self.config["theme"] = bool(theme)