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