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