comparison backend/couchdb/Preferences.py @ 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 2d159eb2a91b
children 91a24f499318
comparison
equal deleted inserted replaced
157:7dd9ae9556fd 158:e8bb107a74e1
1
2 DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries"
3 HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries"
4 PROXY_HOST = "proxyHost"
5 PROXY_PORT = "proxyPort"
6 SHOW_ONLY_UNREAD_FEEDS = "showOnlyUnreadFeeds"
7 START_MAXIMIZED = "startMaximized"
1 8
2 class Preferences(object): 9 class Preferences(object):
3 def __init__(self, database): 10 def __init__(self, database):
4 self.database = database 11 self.database = database
5 self.viewResults = self.database.view("feedtest/preference_by_key") 12 self._initDocument()
13 self.documentIsDirty = False
6 14
7 def _valueForKey(self, key): 15 def _initDocument(self):
16 viewResults = self.database.view("feedtest/preference")
8 try: 17 try:
9 resultsForKey = self.viewResults[key] 18 row = iter(viewResults).next()
10 row = iter(resultsForKey).next() 19 self.document = self.database[row.id]
11 return row.value["value"]
12 except StopIteration: 20 except StopIteration:
13 return None 21 empty = { "doctype" : "preferences" }
22 doc_id, doc_rev = self.database.save(empty) #@UnusedVariable
23 self.document = self.database[doc_id]
14 24
15 def _booleanValueForKey(self, key): 25 def _documentValue(self, key, defaultValue=None):
16 value = self._valueForKey(key) 26 if key in self.document.keys():
17 if value is None: 27 return self.document[key]
18 return False
19 else: 28 else:
20 return value 29 return defaultValue
30
31 def _setDocumentValue(self, key, value):
32 self.document[key] = value
33 self.documentIsDirty = True
21 34
22 def isProxyConfigured(self): 35 def isProxyConfigured(self):
23 return self.proxyHost() is not None 36 return self.proxyHost() is not None
24 37
25 def proxyHost(self): 38 def proxyHost(self):
26 return self._valueForKey("proxyHost") 39 return self._documentValue(PROXY_HOST)
40
41 def setProxyHost(self, hostname):
42 if hostname is None:
43 if PROXY_HOST in self.document.keys():
44 del self.document[PROXY_HOST]
45 self.documentIsDirty = True
46 else:
47 self._setDocumentValue(PROXY_HOST, hostname)
27 48
28 def proxyPort(self): 49 def proxyPort(self):
29 return self._valueForKey("proxyPort") 50 return self._documentValue(PROXY_PORT)
51
52 def setProxyPort(self, port):
53 if port is None:
54 if PROXY_PORT in self.document.keys():
55 del self.document[PROXY_PORT]
56 self.documenIsDirty = True
57 else:
58 self._setDocumentValue(PROXY_PORT, port)
30 59
31 def showOnlyUnreadFeeds(self): 60 def showOnlyUnreadFeeds(self):
32 return self._booleanValueForKey("showOnlyUnreadFeeds") 61 return self._documentValue(SHOW_ONLY_UNREAD_FEEDS, False)
62
63 def setShowOnlyUnreadFeeds(self, flag):
64 self._setDocumentValue(SHOW_ONLY_UNREAD_FEEDS, flag)
33 65
34 def startMaximized(self): 66 def startMaximized(self):
35 return self._booleanValueForKey("startMaximized") 67 return self._documentValue(START_MAXIMIZED, False)
68
69 def setStartMaximized(self, flag):
70 self._setDocumentValue(START_MAXIMIZED, flag)
36 71
37 def hideReadFeedEntries(self): 72 def hideReadFeedEntries(self):
38 return self._booleanValueForKey("hideReadFeedEntries") 73 return self._documentValue(HIDE_READ_FEED_ENTRIES, False)
74
75 def setHideReadFeedEntries(self, flag):
76 self._setDocumentValue(HIDE_READ_FEED_ENTRIES, flag)
39 77
40 def daysToKeepFeedEntries(self): 78 def daysToKeepFeedEntries(self):
41 days = self._valueForKey("daysToKeepFeedEntries") 79 return self._documentValue(DAYS_TO_KEEP_FEED_ENTRIES, 90)
42 if days is not None: 80
43 return days 81 def setDaysToKeepFeedEntries(self, days):
44 else: 82 self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days)
45 return 60 83
84 def commit(self):
85 if self.documentIsDirty:
86 self.database.save(self.document)
46 87
47 def rollback(self): 88 def rollback(self):
48 # this method does nothing 89 if self.documentIsDirty:
49 pass 90 self._initDocument()