~jblount/+junk/grumble

« back to all changes in this revision

Viewing changes to grumble/PreferencesGrumbleDialog.py

  • Committer: joshua at canonical
  • Date: 2010-04-07 19:35:56 UTC
  • Revision ID: joshua@canonical.com-20100407193556-d5r8kq2k4c2qljd6
Initial project creation with Quickly!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
### BEGIN LICENSE
 
3
# This file is in the public domain
 
4
### END LICENSE
 
5
 
 
6
from desktopcouch.records.server import CouchDatabase
 
7
from desktopcouch.records.record import Record
 
8
import gtk
 
9
 
 
10
from grumble.helpers import get_builder
 
11
 
 
12
import gettext
 
13
from gettext import gettext as _
 
14
gettext.textdomain('grumble')
 
15
 
 
16
class PreferencesGrumbleDialog(gtk.Dialog):
 
17
    __gtype_name__ = "PreferencesGrumbleDialog"
 
18
    preferences = {}
 
19
 
 
20
    def __new__(cls):
 
21
        """Special static method that's automatically called by Python when 
 
22
        constructing a new instance of this class.
 
23
        
 
24
        Returns a fully instantiated PreferencesGrumbleDialog object.
 
25
        """
 
26
        builder = get_builder('PreferencesGrumbleDialog')
 
27
        new_object = builder.get_object("preferences_grumble_dialog")
 
28
        new_object.finish_initializing(builder)
 
29
        return new_object
 
30
 
 
31
    def finish_initializing(self, builder):
 
32
        """Called while initializing this instance in __new__
 
33
 
 
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
 
37
        instance.
 
38
        
 
39
        Put your initialization code in here and leave __init__ undefined.
 
40
        """
 
41
 
 
42
        # Get a reference to the builder and set up the signals.
 
43
        self.builder = builder
 
44
        self.builder.connect_signals(self)
 
45
 
 
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
 
50
        self._key = None
 
51
 
 
52
        # Set the record type and then initalize the preferences.
 
53
        self._record_type = (
 
54
            "http://wiki.ubuntu.com/Quickly/RecordTypes/Grumble/"
 
55
            "Preferences")
 
56
        self._preferences = self.get_preferences()
 
57
        # TODO: code for other initialization actions should be added here
 
58
 
 
59
    def get_preferences(self):
 
60
        """Return a dict of preferences for grumble.
 
61
 
 
62
        Creates a couchdb record if necessary.
 
63
        """
 
64
        if self._preferences == None:
 
65
            # The dialog is initializing.
 
66
            self._load_preferences()
 
67
 
 
68
        # If there were no saved preference, this.
 
69
        return self._preferences
 
70
 
 
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}
 
75
 
 
76
        results = self._database.get_records(
 
77
            record_type=self._record_type, create_view=True)
 
78
 
 
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))
 
82
        else:
 
83
            self._preferences = results.rows[0].value
 
84
            del self._preferences['_rev']
 
85
            self._key = results.rows[0].value["_id"]
 
86
 
 
87
    def _save_preferences(self):
 
88
        self._database.update_fields(self._key, self._preferences)
 
89
 
 
90
    def ok(self, widget, data=None):
 
91
        """The user has elected to save the changes.
 
92
 
 
93
        Called before the dialog returns gtk.RESONSE_OK from run().
 
94
        """
 
95
 
 
96
        # Make any updates to self._preferences here. e.g.
 
97
        #self._preferences["preference1"] = "value2"
 
98
        self._save_preferences()
 
99
 
 
100
    def cancel(self, widget, data=None):
 
101
        """The user has elected cancel changes.
 
102
 
 
103
        Called before the dialog returns gtk.RESPONSE_CANCEL for run()
 
104
        """
 
105
        # Restore any changes to self._preferences here.
 
106
        pass
 
107
 
 
108
if __name__ == "__main__":
 
109
    dialog = PreferencesGrumbleDialog()
 
110
    dialog.show()
 
111
    gtk.main()