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