Added proxy support
[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.4.3
23 # Description : Simple RSS Reader
24 # ============================================================================
25
26 import gtk
27 import hildon
28 import ConfigParser
29 import gobject
30 import gconf
31 import urllib2
32
33 section = "FeedingIt"
34 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]}
35 titles = {"updateInterval":"Auto-update Interval", "expiry":"Expiry For Articles", "fontSize":"Font Size For Article Listing", "orientation":"Display Orientation", "artFontSize":"Font Size For Articles"}
36 subtitles = {"updateInterval":"Update every %s hours", "expiry":"Delete articles after %s hours", "fontSize":"%s pixels", "orientation":"%s", "artFontSize":"%s pixels"}
37
38 class Config():
39     def __init__(self, parent, configFilename, has_webkit):
40         self.configFilename = configFilename
41         self.parent = parent
42         self.has_webkit = has_webkit
43         # Load config
44         self.loadConfig()
45         
46     def createDialog(self):
47         
48         self.window = gtk.Dialog("Preferences", self.parent)
49         self.window.set_default_size(-1, 600)
50         panArea = hildon.PannableArea()
51         
52         vbox = gtk.VBox(False, 10)
53         self.buttons = {}
54         if self.has_webkit:
55             settings = ["fontSize", "artFontSize", "expiry", "orientation", "updateInterval",]
56         else:
57             settings = ["fontSize", "expiry", "orientation", "updateInterval",]
58         for setting in settings:
59             picker = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
60             selector = self.create_selector(ranges[setting], setting)
61             picker.set_selector(selector)
62             picker.set_title(titles[setting])
63             picker.set_text(titles[setting], subtitles[setting] % self.config[setting])
64             picker.set_name('HildonButton-finger')
65             picker.set_alignment(0,0,1,1)
66             self.buttons[setting] = picker
67             vbox.pack_start(picker, expand=False)
68         
69         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
70         button.set_label("Auto-update Enabled")
71         button.set_active(self.config["autoupdate"])
72         button.connect("toggled", self.button_toggled, "autoupdate")
73         vbox.pack_start(button, expand=False)
74
75         if self.has_webkit:
76             button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
77             button.set_label("Webkit Articles Enabled")
78             button.set_active(self.config["webkit"])
79             button.connect("toggled", self.button_toggled, "webkit")
80             vbox.pack_start(button, expand=False)
81         
82         panArea.add_with_viewport(vbox)
83         
84         self.window.vbox.add(panArea)        
85         self.window.connect("destroy", self.onExit)
86         #self.window.add(self.vbox)
87         self.window.show_all()
88         return self.window
89
90     def onExit(self, *widget):
91         self.saveConfig()
92         self.window.destroy()
93
94     def button_toggled(self, widget, configName):
95         #print "widget", widget.get_active()
96         if (widget.get_active()):
97             self.config[configName] = True
98         else:
99             self.config[configName] = False
100         #print "autoup",  self.autoupdate
101         self.saveConfig()
102         
103     def selection_changed(self, selector, button, setting):
104         current_selection = selector.get_current_text()
105         if current_selection:
106             self.config[setting] = current_selection
107         gobject.idle_add(self.updateButton, setting)
108         self.saveConfig()
109         
110     def updateButton(self, setting):
111         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
112         
113     def loadConfig(self):
114         self.config = {}
115         try:
116             configParser = ConfigParser.RawConfigParser()
117             configParser.read(self.configFilename)
118             self.config["fontSize"] = configParser.getint(section, "fontSize")
119             self.config["artFontSize"] = configParser.getint(section, "artFontSize")
120             self.config["expiry"] = configParser.getint(section, "expiry")
121             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
122             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
123             self.config["orientation"] = configParser.get(section, "orientation")
124             self.config["webkit"] = configParser.getboolean(section, "webkit")
125         except:
126             self.config["fontSize"] = 17
127             self.config["artFontSize"] = 14
128             self.config["expiry"] = 24
129             self.config["autoupdate"] = False
130             self.config["updateInterval"] = 4
131             self.config["orientation"] = "Automatic"
132             self.config["webkit"] = self.has_webkit
133         
134     def saveConfig(self):
135         configParser = ConfigParser.RawConfigParser()
136         configParser.add_section(section)
137         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
138         configParser.set(section, 'artFontSize', str(self.config["artFontSize"]))
139         configParser.set(section, 'expiry', str(self.config["expiry"]))
140         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
141         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
142         configParser.set(section, 'orientation', str(self.config["orientation"]))
143         configParser.set(section, 'webkit', str(self.config["webkit"]))
144
145         # Writing our configuration file
146         file = open(self.configFilename, 'wb')
147         configParser.write(file)
148         file.close()
149
150     def create_selector(self, choices, setting):
151         #self.pickerDialog = hildon.PickerDialog(self.parent)
152         selector = hildon.TouchSelector(text=True)
153         index = 0
154         for item in choices:
155             iter = selector.append_text(str(item))
156             if str(self.config[setting]) == str(item): 
157                 selector.set_active(0, index)
158             index += 1
159         selector.connect("changed", self.selection_changed, setting)
160         #self.pickerDialog.set_selector(selector)
161         return selector
162         #self.pickerDialog.show_all()
163
164     def getFontSize(self):
165         return self.config["fontSize"]
166     def getArtFontSize(self):
167         return self.config["artFontSize"]
168     def getExpiry(self):
169         return self.config["expiry"]
170     def isAutoUpdateEnabled(self):
171         return self.config["autoupdate"]
172     def getUpdateInterval(self):
173         return float(self.config["updateInterval"])
174     def getReadFont(self):
175         return "sans %s" % self.config["fontSize"]
176     def getUnreadFont(self):
177         return "sans bold %s" % self.config["fontSize"]
178     def getOrientation(self):
179         return ranges["orientation"].index(self.config["orientation"])
180     def getWebkitSupport(self):
181         if self.has_webkit:
182             return self.config["webkit"]
183         else:
184             return False
185     def getProxy(self):
186         if gconf.client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
187             port = gconf.client_get_default().get_int('/system/http_proxy/port')
188             http = gconf.client_get_default().get_string('/system/http_proxy/host')
189             proxy = proxy = urllib2.ProxyHandler( {"http":"http://%s:%s/"% (http,port)} )
190             return (True, proxy)
191         return (False, None)