1
# -*- coding: utf-8 -*-
3
# This file is in the public domain
6
from desktopcouch.records.server import CouchDatabase
7
from desktopcouch.records.record import Record
10
from grumble.helpers import get_builder
13
from gettext import gettext as _
14
gettext.textdomain('grumble')
16
class PreferencesGrumbleDialog(gtk.Dialog):
17
__gtype_name__ = "PreferencesGrumbleDialog"
21
"""Special static method that's automatically called by Python when
22
constructing a new instance of this class.
24
Returns a fully instantiated PreferencesGrumbleDialog object.
26
builder = get_builder('PreferencesGrumbleDialog')
27
new_object = builder.get_object("preferences_grumble_dialog")
28
new_object.finish_initializing(builder)
31
def finish_initializing(self, builder):
32
"""Called while initializing this instance in __new__
34
finish_initalizing should be called after parsing the ui definition
35
and creating a PreferencesGrumbleDialog object with it in order to
36
finish initializing the start of the new PerferencesGrumbleDialog
39
Put your initialization code in here and leave __init__ undefined.
42
# Get a reference to the builder and set up the signals.
43
self.builder = builder
44
self.builder.connect_signals(self)
46
# Set up couchdb and the preference info.
47
self._db_name = "grumble"
48
self._database = CouchDatabase(self._db_name, create=True)
49
self._preferences = None
52
# Set the record type and then initalize the preferences.
54
"http://wiki.ubuntu.com/Quickly/RecordTypes/Grumble/"
56
self._preferences = self.get_preferences()
57
# TODO: code for other initialization actions should be added here
59
def get_preferences(self):
60
"""Return a dict of preferences for grumble.
62
Creates a couchdb record if necessary.
64
if self._preferences == None:
65
# The dialog is initializing.
66
self._load_preferences()
68
# If there were no saved preference, this.
69
return self._preferences
71
def _load_preferences(self):
72
# TODO: add preferences to the self._preferences dict default
73
# preferences that will be overwritten if some are saved
74
self._preferences = {"record_type": self._record_type}
76
results = self._database.get_records(
77
record_type=self._record_type, create_view=True)
79
if len(results.rows) == 0:
80
# No preferences have ever been saved, save them before returning.
81
self._key = self._database.put_record(Record(self._preferences))
83
self._preferences = results.rows[0].value
84
del self._preferences['_rev']
85
self._key = results.rows[0].value["_id"]
87
def _save_preferences(self):
88
self._database.update_fields(self._key, self._preferences)
90
def ok(self, widget, data=None):
91
"""The user has elected to save the changes.
93
Called before the dialog returns gtk.RESONSE_OK from run().
96
# Make any updates to self._preferences here. e.g.
97
#self._preferences["preference1"] = "value2"
98
self._save_preferences()
100
def cancel(self, widget, data=None):
101
"""The user has elected cancel changes.
103
Called before the dialog returns gtk.RESPONSE_CANCEL for run()
105
# Restore any changes to self._preferences here.
108
if __name__ == "__main__":
109
dialog = PreferencesGrumbleDialog()