0f9263c15dd9f07492af9eddecb9e7fd2a5dfdef
[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.1
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), "orientation":["Automatic", "Landscape", "Portrait"]}
33 titles = {"updateInterval":"Auto-update Interval", "expiry":"Expiry For Articles", "fontSize":"Font Size For Article Listing", "orientation":"Display Orientation"}
34 subtitles = {"updateInterval":"Update every %s hours", "expiry":"Delete articles after %s hours", "fontSize":"%s pixels", "orientation":"%s"}
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", "orientation", "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             self.config["orientation"] = configParser.get(section, "orientation")
104         except:
105             self.config["fontSize"] = 16
106             self.config["expiry"] = 24
107             self.config["autoupdate"] = False
108             self.config["updateInterval"] = 4
109             self.config["orientation"] = "Automatic"
110         
111     def saveConfig(self):
112         configParser = ConfigParser.RawConfigParser()
113         configParser.add_section(section)
114         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
115         configParser.set(section, 'expiry', str(self.config["expiry"]))
116         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
117         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
118         configParser.set(section, 'orientation', str(self.config["orientation"]))
119
120         # Writing our configuration file
121         file = open(self.configFilename, 'wb')
122         configParser.write(file)
123         file.close()
124
125     def create_selector(self, choices, setting):
126         #self.pickerDialog = hildon.PickerDialog(self.parent)
127         selector = hildon.TouchSelector(text=True)
128         index = 0
129         for item in choices:
130             iter = selector.append_text(str(item))
131             if str(self.config[setting]) == str(item): 
132                 selector.set_active(0, index)
133             index += 1
134         selector.connect("changed", self.selection_changed, setting)
135         #self.pickerDialog.set_selector(selector)
136         return selector
137         #self.pickerDialog.show_all()
138
139     def getFontSize(self):
140         return self.config["fontSize"]
141     def getExpiry(self):
142         return self.config["expiry"]
143     def isAutoUpdateEnabled(self):
144         return self.config["autoupdate"]
145     def getUpdateInterval(self):
146         return float(self.config["updateInterval"])
147     def getReadFont(self):
148         return "sans %s" % self.config["fontSize"]
149     def getUnreadFont(self):
150         return "sans bold %s" % self.config["fontSize"]
151     def getOrientation(self):
152         return ranges["orientation"].index(self.config["orientation"])