annotate backend/couchdb/Preferences.py @ 236:c5250fcc3881

remove a workaround along with its wrong explanation
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 26 Jan 2015 13:43:38 +0100
parents e34c53a3e407
children 7c719c4f5655
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
217
bb3c851b18b1 add source file endcoding header
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 194
diff changeset
1 # -*- coding: utf-8 -*-
169
91a24f499318 introdue an abstraction for the name of the database so it can be changed via commandline parameter
dirk
parents: 158
diff changeset
2 import CouchDb
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.
dirk
parents: 156
diff changeset
3
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.
dirk
parents: 156
diff changeset
4 DAYS_TO_KEEP_FEED_ENTRIES = "daysToKeepFeedEntries"
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.
dirk
parents: 156
diff changeset
5 HIDE_READ_FEED_ENTRIES = "hideReadFeedEntries"
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.
dirk
parents: 156
diff changeset
6 PROXY_HOST = "proxyHost"
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.
dirk
parents: 156
diff changeset
7 PROXY_PORT = "proxyPort"
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.
dirk
parents: 156
diff changeset
8 SHOW_ONLY_UNREAD_FEEDS = "showOnlyUnreadFeeds"
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.
dirk
parents: 156
diff changeset
9 START_MAXIMIZED = "startMaximized"
194
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
10 USE_PROXY = "useProxy"
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
11
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
12 class Preferences(object):
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
13 def __init__(self, database):
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
14 self.database = database
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.
dirk
parents: 156
diff changeset
15 self._initDocument()
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.
dirk
parents: 156
diff changeset
16 self.documentIsDirty = False
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
17
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.
dirk
parents: 156
diff changeset
18 def _initDocument(self):
169
91a24f499318 introdue an abstraction for the name of the database so it can be changed via commandline parameter
dirk
parents: 158
diff changeset
19 viewResults = self.database.view(CouchDb.preference())
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
20 try:
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.
dirk
parents: 156
diff changeset
21 row = iter(viewResults).next()
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.
dirk
parents: 156
diff changeset
22 self.document = self.database[row.id]
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
23 except StopIteration:
233
e34c53a3e407 fixes from eric's style check
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 217
diff changeset
24 empty = {"doctype": "preferences"}
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.
dirk
parents: 156
diff changeset
25 doc_id, doc_rev = self.database.save(empty) #@UnusedVariable
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.
dirk
parents: 156
diff changeset
26 self.document = self.database[doc_id]
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
27
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.
dirk
parents: 156
diff changeset
28 def _documentValue(self, key, defaultValue=None):
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.
dirk
parents: 156
diff changeset
29 if key in self.document.keys():
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.
dirk
parents: 156
diff changeset
30 return self.document[key]
156
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
31 else:
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.
dirk
parents: 156
diff changeset
32 return defaultValue
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.
dirk
parents: 156
diff changeset
33
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.
dirk
parents: 156
diff changeset
34 def _setDocumentValue(self, key, value):
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.
dirk
parents: 156
diff changeset
35 self.document[key] = value
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.
dirk
parents: 156
diff changeset
36 self.documentIsDirty = True
156
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
37
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
38 def isProxyConfigured(self):
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
39 return self.proxyHost() is not None
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
40
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
41 def proxyHost(self):
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.
dirk
parents: 156
diff changeset
42 return self._documentValue(PROXY_HOST)
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.
dirk
parents: 156
diff changeset
43
194
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
44 def useProxy(self):
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
45 return self._documentValue(USE_PROXY, True)
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
46
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
47 def setUseProxy(self, value):
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
48 self._setDocumentValue(USE_PROXY, value)
2c91b5653878 enable/disable using the proxy via a preference setting
dirk
parents: 172
diff changeset
49
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.
dirk
parents: 156
diff changeset
50 def setProxyHost(self, hostname):
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.
dirk
parents: 156
diff changeset
51 if hostname is None:
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.
dirk
parents: 156
diff changeset
52 if PROXY_HOST in self.document.keys():
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.
dirk
parents: 156
diff changeset
53 del self.document[PROXY_HOST]
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.
dirk
parents: 156
diff changeset
54 self.documentIsDirty = True
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.
dirk
parents: 156
diff changeset
55 else:
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.
dirk
parents: 156
diff changeset
56 self._setDocumentValue(PROXY_HOST, hostname)
133
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
57
9e1e6b96d8b0 implement proxyHost/proxyPort in Preferences
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff changeset
58 def proxyPort(self):
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.
dirk
parents: 156
diff changeset
59 return self._documentValue(PROXY_PORT)
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.
dirk
parents: 156
diff changeset
60
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.
dirk
parents: 156
diff changeset
61 def setProxyPort(self, port):
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.
dirk
parents: 156
diff changeset
62 if port is None:
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.
dirk
parents: 156
diff changeset
63 if PROXY_PORT in self.document.keys():
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.
dirk
parents: 156
diff changeset
64 del self.document[PROXY_PORT]
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.
dirk
parents: 156
diff changeset
65 self.documenIsDirty = True
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.
dirk
parents: 156
diff changeset
66 else:
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.
dirk
parents: 156
diff changeset
67 self._setDocumentValue(PROXY_PORT, port)
146
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
68
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
69 def showOnlyUnreadFeeds(self):
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.
dirk
parents: 156
diff changeset
70 return self._documentValue(SHOW_ONLY_UNREAD_FEEDS, False)
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.
dirk
parents: 156
diff changeset
71
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.
dirk
parents: 156
diff changeset
72 def setShowOnlyUnreadFeeds(self, flag):
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.
dirk
parents: 156
diff changeset
73 self._setDocumentValue(SHOW_ONLY_UNREAD_FEEDS, flag)
146
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
74
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
75 def startMaximized(self):
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.
dirk
parents: 156
diff changeset
76 return self._documentValue(START_MAXIMIZED, False)
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.
dirk
parents: 156
diff changeset
77
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.
dirk
parents: 156
diff changeset
78 def setStartMaximized(self, flag):
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.
dirk
parents: 156
diff changeset
79 self._setDocumentValue(START_MAXIMIZED, flag)
146
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
80
8ec20377bcb0 move getting the entries for a feed to the backend so that the couchdb backend can use a custom view for feed entries
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 133
diff changeset
81 def hideReadFeedEntries(self):
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.
dirk
parents: 156
diff changeset
82 return self._documentValue(HIDE_READ_FEED_ENTRIES, False)
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.
dirk
parents: 156
diff changeset
83
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.
dirk
parents: 156
diff changeset
84 def setHideReadFeedEntries(self, flag):
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.
dirk
parents: 156
diff changeset
85 self._setDocumentValue(HIDE_READ_FEED_ENTRIES, flag)
156
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
86
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
87 def daysToKeepFeedEntries(self):
172
214addba1f93 always return an int value for daysToKeepFeedEntries
dirk
parents: 169
diff changeset
88 value = self._documentValue(DAYS_TO_KEEP_FEED_ENTRIES, 90)
214addba1f93 always return an int value for daysToKeepFeedEntries
dirk
parents: 169
diff changeset
89 return int(value)
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.
dirk
parents: 156
diff changeset
90
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.
dirk
parents: 156
diff changeset
91 def setDaysToKeepFeedEntries(self, days):
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.
dirk
parents: 156
diff changeset
92 self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days)
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.
dirk
parents: 156
diff changeset
93
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.
dirk
parents: 156
diff changeset
94 def commit(self):
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.
dirk
parents: 156
diff changeset
95 if self.documentIsDirty:
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.
dirk
parents: 156
diff changeset
96 self.database.save(self.document)
156
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
97
2d159eb2a91b displaying the preferences dialog works, saving prefs doesn't yet
Dirk Olmes <dirk@xanthippe.ping.de>
parents: 146
diff changeset
98 def rollback(self):
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.
dirk
parents: 156
diff changeset
99 if self.documentIsDirty:
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.
dirk
parents: 156
diff changeset
100 self._initDocument()