0.6.0 added widget
[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.5.4
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 VERSION = "0.5.4"
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         settings = ["fontSize", "artFontSize", "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         button = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
74         button.set_label("Image Caching Enabled")
75         button.set_active(self.config["imageCache"])
76         button.connect("toggled", self.button_toggled, "imageCache")
77         vbox.pack_start(button, expand=False)
78         
79         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL)
80         button.set_label("View Known Issues and Tips")
81         button.connect("clicked", self.button_tips_clicked)
82         button.set_alignment(0,0,1,1)
83         vbox.pack_start(button, expand=False)
84         
85         
86         panArea.add_with_viewport(vbox)
87         
88         self.window.vbox.add(panArea)        
89         self.window.connect("destroy", self.onExit)
90         #self.window.add(self.vbox)
91         self.window.show_all()
92         return self.window
93
94     def button_tips_clicked(self, *widget):
95         import dbus
96         bus = dbus.SessionBus()
97         proxy = bus.get_object("com.nokia.osso_browser", "/com/nokia/osso_browser/request")
98         iface = dbus.Interface(proxy, 'com.nokia.osso_browser')
99         iface.open_new_window("http://feedingit.marcoz.org/%s.html" % VERSION)
100
101     def onExit(self, *widget):
102         self.saveConfig()
103         self.window.destroy()
104
105     def button_toggled(self, widget, configName):
106         #print "widget", widget.get_active()
107         if (widget.get_active()):
108             self.config[configName] = True
109         else:
110             self.config[configName] = False
111         #print "autoup",  self.autoupdate
112         self.saveConfig()
113         
114     def selection_changed(self, selector, button, setting):
115         current_selection = selector.get_current_text()
116         if current_selection:
117             self.config[setting] = current_selection
118         gobject.idle_add(self.updateButton, setting)
119         self.saveConfig()
120         
121     def updateButton(self, setting):
122         self.buttons[setting].set_text(titles[setting], subtitles[setting] % self.config[setting])
123         
124     def loadConfig(self):
125         self.config = {}
126         try:
127             configParser = ConfigParser.RawConfigParser()
128             configParser.read(self.configFilename)
129             self.config["fontSize"] = configParser.getint(section, "fontSize")
130             self.config["artFontSize"] = configParser.getint(section, "artFontSize")
131             self.config["expiry"] = configParser.getint(section, "expiry")
132             self.config["autoupdate"] = configParser.getboolean(section, "autoupdate")
133             self.config["updateInterval"] = configParser.getfloat(section, "updateInterval")
134             self.config["orientation"] = configParser.get(section, "orientation")
135             self.config["imageCache"] = configParser.getboolean(section, "imageCache")
136         except:
137             self.config["fontSize"] = 17
138             self.config["artFontSize"] = 14
139             self.config["expiry"] = 24
140             self.config["autoupdate"] = False
141             self.config["updateInterval"] = 4
142             self.config["orientation"] = "Automatic"
143             self.config["imageCache"] = False
144         
145     def saveConfig(self):
146         configParser = ConfigParser.RawConfigParser()
147         configParser.add_section(section)
148         configParser.set(section, 'fontSize', str(self.config["fontSize"]))
149         configParser.set(section, 'artFontSize', str(self.config["artFontSize"]))
150         configParser.set(section, 'expiry', str(self.config["expiry"]))
151         configParser.set(section, 'autoupdate', str(self.config["autoupdate"]))
152         configParser.set(section, 'updateInterval', str(self.config["updateInterval"]))
153         configParser.set(section, 'orientation', str(self.config["orientation"]))
154         configParser.set(section, 'imageCache', str(self.config["imageCache"]))
155
156         # Writing our configuration file
157         file = open(self.configFilename, 'wb')
158         configParser.write(file)
159         file.close()
160
161     def create_selector(self, choices, setting):
162         #self.pickerDialog = hildon.PickerDialog(self.parent)
163         selector = hildon.TouchSelector(text=True)
164         index = 0
165         for item in choices:
166             iter = selector.append_text(str(item))
167             if str(self.config[setting]) == str(item): 
168                 selector.set_active(0, index)
169             index += 1
170         selector.connect("changed", self.selection_changed, setting)
171         #self.pickerDialog.set_selector(selector)
172         return selector
173         #self.pickerDialog.show_all()
174
175     def getFontSize(self):
176         return self.config["fontSize"]
177     def getArtFontSize(self):
178         return self.config["artFontSize"]
179     def getExpiry(self):
180         return self.config["expiry"]
181     def isAutoUpdateEnabled(self):
182         return self.config["autoupdate"]
183     def getUpdateInterval(self):
184         return float(self.config["updateInterval"])
185     def getReadFont(self):
186         return "sans italic %s" % self.config["fontSize"]
187     def getUnreadFont(self):
188         return "sans %s" % self.config["fontSize"]
189     def getOrientation(self):
190         return ranges["orientation"].index(self.config["orientation"])
191     def getImageCache(self):
192         return self.config["imageCache"]
193     def getProxy(self):
194         if gconf.client_get_default().get_bool('/system/http_proxy/use_http_proxy'):
195             port = gconf.client_get_default().get_int('/system/http_proxy/port')
196             http = gconf.client_get_default().get_string('/system/http_proxy/host')
197             proxy = proxy = urllib2.ProxyHandler( {"http":"http://%s:%s/"% (http,port)} )
198             return (True, proxy)
199         return (False, None)