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