changeset 158:e8bb107a74e1

all preferences are stored in a single JSON document in the couchdb backend. PreferencesDialog converts QString to a proper Python datatybe before sending it to the backend.
author dirk
date Mon, 29 Aug 2011 02:33:51 +0200
parents 7dd9ae9556fd
children 7d724cf2dcf7
files PreferencesDialog.py backend/couchdb/CouchDbBackend.py backend/couchdb/Preferences.py
diffstat 3 files changed, 77 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/PreferencesDialog.py	Sat Aug 27 10:17:46 2011 +0200
+++ b/PreferencesDialog.py	Mon Aug 29 02:33:51 2011 +0200
@@ -33,7 +33,9 @@
     def fillProxySettings(self):
         if self.preferences.isProxyConfigured():
             self.ui.proxyHost.setText(self.preferences.proxyHost())
-            self.ui.proxyPort.setText(str(self.preferences.proxyPort()))
+            port = self.preferences.proxyPort()
+            if port is not None:
+                self.ui.proxyPort.setText(str(port))
 
     def fillDaysToKeepFeedEntries(self):
         days = self.preferences.daysToKeepFeedEntries()
@@ -60,13 +62,15 @@
     def storeProxySettings(self):
         proxyHost = self.ui.proxyHost.text()
         if proxyHost.isEmpty():
-            proxyHost = None
-        self.preferences.setProxyHost(proxyHost)
+            self.preferences.setProxyHost(None)
+        else:
+            self.preferences.setProxyHost(str(proxyHost))
 
         proxyPort = self.ui.proxyPort.text()
         if proxyPort.isEmpty():
-            proxyPort = None
-        self.preferences.setProxyPort(proxyPort)
+            self.preferences.setProxyPort(None)
+        else:
+            self.preferences.setProxyPort(int(proxyPort))
 
     def daysToKeepFeedEntriesFinishedEditing(self):
         text = self.ui.daysToKeepFeedEntries.text()
--- a/backend/couchdb/CouchDbBackend.py	Sat Aug 27 10:17:46 2011 +0200
+++ b/backend/couchdb/CouchDbBackend.py	Mon Aug 29 02:33:51 2011 +0200
@@ -16,9 +16,12 @@
     def __init__(self):
         server = couchdb.Server()
         self.database = server[DATABASE]
+        self.prefs = None
 
     def preferences(self):
-        return Preferences(self.database)
+        if self.prefs is None:
+            self.prefs = Preferences(self.database)
+        return self.prefs
 
     #
     # handling of feeds
--- a/backend/couchdb/Preferences.py	Sat Aug 27 10:17:46 2011 +0200
+++ b/backend/couchdb/Preferences.py	Mon Aug 29 02:33:51 2011 +0200
@@ -1,49 +1,90 @@
+
+DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries"
+HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries"
+PROXY_HOST = "proxyHost"
+PROXY_PORT = "proxyPort"
+SHOW_ONLY_UNREAD_FEEDS = "showOnlyUnreadFeeds"
+START_MAXIMIZED = "startMaximized"
 
 class Preferences(object):
     def __init__(self, database):
         self.database = database
-        self.viewResults = self.database.view("feedtest/preference_by_key")
+        self._initDocument()
+        self.documentIsDirty = False
 
-    def _valueForKey(self, key):
+    def _initDocument(self):
+        viewResults = self.database.view("feedtest/preference")
         try:
-            resultsForKey = self.viewResults[key]
-            row = iter(resultsForKey).next()
-            return row.value["value"]
+            row = iter(viewResults).next()
+            self.document = self.database[row.id]
         except StopIteration:
-            return None
+            empty = { "doctype" : "preferences" }
+            doc_id, doc_rev = self.database.save(empty)                 #@UnusedVariable
+            self.document = self.database[doc_id]
 
-    def _booleanValueForKey(self, key):
-        value = self._valueForKey(key)
-        if value is None:
-            return False
+    def _documentValue(self, key, defaultValue=None):
+        if key in self.document.keys():
+            return self.document[key]
         else:
-            return value
+            return defaultValue
+
+    def _setDocumentValue(self, key, value):
+        self.document[key] = value
+        self.documentIsDirty = True
 
     def isProxyConfigured(self):
         return self.proxyHost() is not None
 
     def proxyHost(self):
-        return self._valueForKey("proxyHost")
+        return self._documentValue(PROXY_HOST)
+
+    def setProxyHost(self, hostname):
+        if hostname is None:
+            if PROXY_HOST in self.document.keys():
+                del self.document[PROXY_HOST]
+                self.documentIsDirty = True
+        else:
+            self._setDocumentValue(PROXY_HOST, hostname)
 
     def proxyPort(self):
-        return self._valueForKey("proxyPort")
+        return self._documentValue(PROXY_PORT)
+
+    def setProxyPort(self, port):
+        if port is None:
+            if PROXY_PORT in self.document.keys():
+                del self.document[PROXY_PORT]
+                self.documenIsDirty = True
+        else:
+            self._setDocumentValue(PROXY_PORT, port)
 
     def showOnlyUnreadFeeds(self):
-        return self._booleanValueForKey("showOnlyUnreadFeeds")
+        return self._documentValue(SHOW_ONLY_UNREAD_FEEDS, False)
+
+    def setShowOnlyUnreadFeeds(self, flag):
+        self._setDocumentValue(SHOW_ONLY_UNREAD_FEEDS, flag)
 
     def startMaximized(self):
-        return self._booleanValueForKey("startMaximized")
+        return self._documentValue(START_MAXIMIZED, False)
+
+    def setStartMaximized(self, flag):
+        self._setDocumentValue(START_MAXIMIZED, flag)
 
     def hideReadFeedEntries(self):
-        return self._booleanValueForKey("hideReadFeedEntries")
+        return self._documentValue(HIDE_READ_FEED_ENTRIES, False)
+
+    def setHideReadFeedEntries(self, flag):
+        self._setDocumentValue(HIDE_READ_FEED_ENTRIES, flag)
 
     def daysToKeepFeedEntries(self):
-        days = self._valueForKey("daysToKeepFeedEntries")
-        if days is not None:
-            return days
-        else:
-            return 60
+        return self._documentValue(DAYS_TO_KEEP_FEED_ENTRIES, 90)
+
+    def setDaysToKeepFeedEntries(self, days):
+        self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days)
+
+    def commit(self):
+        if self.documentIsDirty:
+            self.database.save(self.document)
 
     def rollback(self):
-        # this method does nothing
-        pass
+        if self.documentIsDirty:
+            self._initDocument()