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