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