a38787f2dc7029dfd9f616ae607f976bfc5f92d6
[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 = "0.6.1"
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]}
37 titles = {"updateInterval":"Auto-update Interval", "expiry":"Expiry For Articles", "fontSize":"Font Size For Article Listing", "orientation":"Display Orientation", "artFontSize":"Font Size For Articles"}
38 subtitles = {"updateInterval":"Update every %s hours", "expiry":"Delete articles after %s hours", "fontSize":"%s pixels", "orientation":"%s", "artFontSize":"%s pixels"}
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     def createDialog(self):
48         
49         self.window = gtk.Dialog("Preferences", self.parent)
50         self.window.set_default_size(-1, 600)
51         panArea = hildon.PannableArea()
52         
53         vbox = gtk.VBox(False, 10)
54         self.buttons = {}
55
56         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
57         button.set_label("View Known Issues and Tips")
58         button.connect("clicked", self.button_tips_clicked)
59         button.set_alignment(0,0,1,1)
60         vbox.pack_start(button, expand=False)  
61
62         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
63         button.set_label("Image Caching Enabled")
64         button.set_active(self.config["imageCache"])
65         button.connect("toggled", self.button_toggled, "imageCache")
66         vbox.pack_start(button, expand=False)      
67
68         settings = ["fontSize", "artFontSize", "expiry", "orientation", "updateInterval",]
69         for setting in settings:
70             picker = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
71             selector = self.create_selector(ranges[setting], setting)
72             picker.set_selector(selector)
73             picker.set_title(titles[setting])
74             picker.set_text(titles[setting], subtitles[setting] % self.config[setting])
75             picker.set_name('HildonButton-finger')
76             picker.set_alignment(0,0,1,1)
77             self.buttons[setting] = picker
78             vbox.pack_start(picker, expand=False)
79         
80         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
81         button.set_label("Auto-update Enabled")
82         button.set_active(self.config["autoupdate"])
83         button.connect("toggled", self.button_toggled, "autoupdate")
84         vbox.pack_start(button, expand=False)
85
86         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
87         button.set_label("Hide read feeds")
88         button.set_active(self.config["hidereadfeeds"])
89         button.connect("toggled", self.button_toggled, "hidereadfeeds")
90         vbox.pack_start(button, expand=False)
91
92         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
93         button.set_label("Hide read articles")
94         button.set_active(self.config["hidereadarticles"])
95         button.connect("toggled", self.button_toggled, "hidereadarticles")
96         vbox.pack_start(button, expand=False)
97         
98         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
99         button.set_label("Proxy Support Enabled")
100         button.set_active(self.config["proxy"])
101         button.connect("toggled", self.button_toggled, "proxy")
102         vbox.pack_start(button, expand=False)
103         
104         panArea.add_with_viewport(vbox)
105         
106         self.window.vbox.add(panArea)        
107         self.window.connect("destroy", self.onExit)
108         #self.window.add(self.vbox)
109         self.window.show_all()
110         return self.window
111
112     def button_tips_clicked(self, *widget):
113         import dbus
114         bus = dbus.SessionBus()
115         proxy = bus.get_object("com.nokia.osso_browser", "/com/nokia/osso_browser/request")
116         iface = dbus.Interface(proxy, 'com.nokia.osso_browser')
117         iface.open_new_window("http://feedingit.marcoz.org/%s.html" % VERSION)
118
119     def onExit(self, *widget):
120         self.saveConfig()
121         self.window.destroy()
122
123     def button_toggled(self, widget, configName):
124         #print "widget", widget.get_active()
125         if (widget.get_active()):
126             self.config[configName] = True
127         else:
128             self.config[configName] = False
129         #print "autoup",  self.autoupdate
130         self.saveConfig()
131         
132     def selection_changed(self, selector, button, setting):
133         current_selection = selector.get_current_text()
134         if current_selection:
135             self.config[setting] = current_selection
136         idle_add(self.updateButton, setting)
137         self.saveConfig()
138         
139     def updateButton(self, setting):
140         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
141         
142     def loadConfig(self):
143         self.config = {}
144         try:
145             configParser = RawConfigParser()
146             configParser.read(self.configFilename)
147             self.config["fontSize"] = configParser.getint(section, "fontSize")
148             self.config["artFontSize"] = configParser.getint(section, "artFontSize")
149             self.config["expiry"] = configParser.getint(section, "expiry")
150             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
151             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
152             self.config["orientation"] = configParser.get(section, "orientation")
153             self.config["imageCache"] = configParser.getboolean(section, "imageCache")
154         except:
155             self.config["fontSize"] = 17
156             self.config["artFontSize"] = 14
157             self.config["expiry"] = 24
158             self.config["autoupdate"] = False
159             self.config["updateInterval"] = 4
160             self.config["orientation"] = "Automatic"
161             self.config["imageCache"] = False
162         try:
163             self.config["proxy"] = configParser.getboolean(section, "proxy")
164         except:
165             self.config["proxy"] = True
166         try:
167             self.config["hidereadfeeds"] = configParser.getboolean(section, "hidereadfeeds")
168             self.config["hidereadarticles"] = configParser.getboolean(section, "hidereadarticles")
169         except:
170             self.config["hidereadfeeds"] = False
171             self.config["hidereadarticles"] = False
172         
173     def saveConfig(self):
174         configParser = RawConfigParser()
175         configParser.add_section(section)
176         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
177         configParser.set(section, 'artFontSize', str(self.config["artFontSize"]))
178         configParser.set(section, 'expiry', str(self.config["expiry"]))
179         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
180         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
181         configParser.set(section, 'orientation', str(self.config["orientation"]))
182         configParser.set(section, 'imageCache', str(self.config["imageCache"]))
183         configParser.set(section, 'proxy', str(self.config["proxy"]))
184         configParser.set(section, 'hidereadfeeds', str(self.config["hidereadfeeds"]))
185         configParser.set(section, 'hidereadarticles', str(self.config["hidereadarticles"]))
186
187         # Writing our configuration file
188         file = open(self.configFilename, 'wb')
189         configParser.write(file)
190         file.close()
191
192     def create_selector(self, choices, setting):
193         #self.pickerDialog = hildon.PickerDialog(self.parent)
194         selector = hildon.TouchSelector(text=True)
195         index = 0
196         for item in choices:
197             iter = selector.append_text(str(item))
198             if str(self.config[setting]) == str(item): 
199                 selector.set_active(0, index)
200             index += 1
201         selector.connect("changed", self.selection_changed, setting)
202         #self.pickerDialog.set_selector(selector)
203         return selector
204         #self.pickerDialog.show_all()
205
206     def getFontSize(self):
207         return self.config["fontSize"]
208     def getArtFontSize(self):
209         return self.config["artFontSize"]
210     def getExpiry(self):
211         return self.config["expiry"]
212     def isAutoUpdateEnabled(self):
213         return self.config["autoupdate"]
214     def getUpdateInterval(self):
215         return float(self.config["updateInterval"])
216     def getReadFont(self):
217         return "sans italic %s" % self.config["fontSize"]
218     def getUnreadFont(self):
219         return "sans %s" % self.config["fontSize"]
220     def getOrientation(self):
221         return ranges["orientation"].index(self.config["orientation"])
222     def getImageCache(self):
223         return self.config["imageCache"]
224     def getProxy(self):
225         if self.config["proxy"] == False:
226             return (False, None)
227         if client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
228             port = client_get_default().get_int('/system/http_proxy/port')
229             http = client_get_default().get_string('/system/http_proxy/host')
230             proxy = ProxyHandler( {"http":"http://%s:%s/"% (http,port)} )
231             return (True, proxy)
232         return (False, None)
233     def getHideReadFeeds(self):
234         return self.config["hidereadfeeds"]
235     def getHideReadArticles(self):
236         return self.config["hidereadarticles"]