Added config dialogs + auto-update
[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.2.2
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.02, 0.5, 1, 2, 4, 12, 24], "expiry":[24, 48, 72], "fontSize":range(12,24) }
33 titles = {"updateInterval":"Auto-update Interval", "expiry":"Expiry For Articles", "fontSize":"Font Size For Article Listing"}
34 subtitles = {"updateInterval":"Update every %s hours", "expiry":"Delete articles after %s hours", "fontSize":"%s pixels"}
35
36 class Config():
37     def __init__(self, parent, configFilename):
38         self.configFilename = configFilename
39         self.parent = parent
40         # Load config
41         self.loadConfig()
42         
43     def createDialog(self):
44         
45         self.window = gtk.Dialog("Preferences", self.parent)
46         #self.vbox = gtk.VBox(False, 10)
47         self.buttons = {}
48         for setting in ["fontSize", "expiry", "updateInterval"]:
49             picker = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
50             selector = self.create_selector(ranges[setting], setting)
51             picker.set_selector(selector)
52             picker.set_title(titles[setting])
53             picker.set_text(titles[setting], subtitles[setting] % self.config[setting])
54             picker.set_name('HildonButton-finger')
55             picker.set_alignment(0,0,1,1)
56             self.buttons[setting] = picker
57             self.window.vbox.pack_start(picker)
58         
59         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
60         button.set_label("Auto-update Enabled")
61         button.set_active(self.config["autoupdate"])
62         button.connect("toggled", self.button_toggled)
63         
64         self.window.vbox.pack_start(button)
65         
66         self.window.connect("destroy", self.onExit)
67         #self.window.add(self.vbox)
68         self.window.show_all()
69         return self.window
70
71     def onExit(self, *widget):
72         self.saveConfig()
73         self.window.destroy()
74
75     def button_toggled(self, widget):
76         #print "widget", widget.get_active()
77         if (widget.get_active()):
78             self.config["autoupdate"] = True
79         else:
80             self.config["autoupdate"] = False
81         #print "autoup",  self.autoupdate
82         self.saveConfig()
83         
84     def selection_changed(self, selector, button, setting):
85         current_selection = selector.get_current_text()
86         if current_selection:
87             self.config[setting] = current_selection
88         gobject.idle_add(self.updateButton, setting)
89         self.saveConfig()
90         
91     def updateButton(self, setting):
92         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
93         
94     def loadConfig(self):
95         self.config = {}
96         try:
97             configParser = ConfigParser.RawConfigParser()
98             configParser.read(self.configFilename)
99             self.config["fontSize"] = configParser.getint(section, "fontSize")
100             self.config["expiry"] = configParser.getint(section, "expiry")
101             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
102             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
103         except:
104             self.config["fontSize"] = 16
105             self.config["expiry"] = 24
106             self.config["autoupdate"] = False
107             self.config["updateInterval"] = 4
108         
109     def saveConfig(self):
110         configParser = ConfigParser.RawConfigParser()
111         configParser.add_section(section)
112         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
113         configParser.set(section, 'expiry', str(self.config["expiry"]))
114         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
115         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
116
117         # Writing our configuration file
118         file = open(self.configFilename, 'wb')
119         configParser.write(file)
120         file.close()
121
122     def create_selector(self, choices, setting):
123         #self.pickerDialog = hildon.PickerDialog(self.parent)
124         selector = hildon.TouchSelector(text=True)
125         index = 0
126         for item in choices:
127             iter = selector.append_text(str(item))
128             if str(self.config[setting]) == str(item): 
129                 selector.set_active(0, index)
130             index += 1
131         selector.connect("changed", self.selection_changed, setting)
132         #self.pickerDialog.set_selector(selector)
133         return selector
134         #self.pickerDialog.show_all()
135
136     def getFontSize(self):
137         return self.config["fontSize"]
138     def getExpiry(self):
139         return self.config["expiry"]
140     def isAutoUpdateEnabled(self):
141         return self.config["autoupdate"]
142     def getUpdateInterval(self):
143         return float(self.config["updateInterval"])
144     def getReadFont(self):
145         return "sans %s" % self.config["fontSize"]
146     def getUnreadFont(self):
147         return "sans bold %s" % self.config["fontSize"]