changeset 87:b8bfd1bd6c55

use a proxy if one is configured. TODO: GUI for configuring the proxy server
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 17 Nov 2010 21:22:31 +0100
parents 94118d504d2b
children 48d1d7bba548
files Preferences.py feedworm-gui.py
diffstat 2 files changed, 46 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Preferences.py	Thu Sep 09 16:47:38 2010 +0200
+++ b/Preferences.py	Wed Nov 17 21:22:31 2010 +0100
@@ -2,26 +2,28 @@
 from Preference import Preference
 import util
 
+HIDE_READ_ENTRIES = "HIDE_READ_FEED_ENTRIES"
+PROXY_HOST = "PROXY_HOST"
+PROXY_PORT = "PROXY_PORT"
+SHOW_ONLY_UNREAD_FEEDS = "SHOW_ONLY_UNREAD_FEEDS"
 START_MAXIMIZED = "START_MAXIMIZED"
-HIDE_READ_ENTRIES = "HIDE_READ_FEED_ENTRIES"
-SHOW_ONLY_UNREAD_FEEDS = "SHOW_ONLY_UNREAD_FEEDS"
 
 class Preferences(object):
     def __init__(self, session):
         self.session = session
         self.cache = {}
-        
-    def _cachedPreference(self, key, defaultValue=None):
+
+    def _cachedPreference(self, key, defaultValue=None, addIfMissing=True):
         if self.cache.has_key(key):
             return self.cache[key]
         else:
             pref = Preference.forKey(key, self.session)
-            if pref is None:
+            if pref is None and addIfMissing:
                 pref = Preference(key, str(defaultValue))
                 self.session.add(pref)
-            self.cache[key] = pref
+                self.cache[key] = pref
             return pref
-        
+
     def startMaximized(self):
         pref = self._cachedPreference(START_MAXIMIZED, False)
         return util.str2bool(pref.value)
@@ -33,15 +35,35 @@
     def hideReadFeedEntries(self):
         pref = self._cachedPreference(HIDE_READ_ENTRIES, False)
         return util.str2bool(pref.value)
-    
+
     def setHideReadFeedEntries(self, flag):
         pref = self._cachedPreference(HIDE_READ_ENTRIES)
         pref.value = util.bool2str(flag)
-        
+
     def showOnlyUnreadFeeds(self):
         pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS, False)
         return util.str2bool(pref.value)
-    
+
     def setShowOnlyUnreadFeeds(self, flag):
         pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS)
         pref.value = util.bool2str(flag)
+
+    def proxyHost(self):
+        pref = self._cachedPreference(PROXY_HOST)
+        return pref.value
+
+    def setProxyHost(self, hostname):
+        pref = self._cachedPreference(PROXY_HOST)
+        pref.value = hostname
+
+    def proxyPort(self):
+        pref = self._cachedPreference(PROXY_PORT, 3128)
+        return int(pref.value)
+
+    def setProxyPort(self, port):
+        pref = self._cachedPreference(PROXY_PORT)
+        pref.value = str(port)
+
+    def isProxyConfigured(self):
+        pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
+        return pref is not None
--- a/feedworm-gui.py	Thu Sep 09 16:47:38 2010 +0200
+++ b/feedworm-gui.py	Wed Nov 17 21:22:31 2010 +0100
@@ -3,17 +3,28 @@
 from MainWindow import MainWindow
 from Preferences import Preferences
 from PyQt4 import QtGui
+from PyQt4.QtNetwork import QNetworkProxy
 import sys
 import util
 
+def setupProxy(preferences):
+    if preferences.isProxyConfigured():
+        proxyHost = preferences.proxyHost()
+        proxyPort = preferences.proxyPort()
+        proxy = QNetworkProxy(QNetworkProxy.HttpProxy, proxyHost, proxyPort)
+        QNetworkProxy.setApplicationProxy(proxy)
+
 if __name__ == '__main__':
     util.configureLogging()
     session = Database.createSession()
-    
+    preferences = Preferences(session)
+
+    setupProxy(preferences)
+
     app = QtGui.QApplication(sys.argv)
     mainWindow = MainWindow(session)
-    
-    maximized = Preferences(session).startMaximized()
+
+    maximized = preferences.startMaximized()
     if maximized:
         mainWindow.showMaximized()
     else: