1
# -*- coding: utf-8 -*-
3
# This file is in the public domain
9
from desktopcouch.records.server import CouchDatabase
10
from desktopcouch.records.record import Record
12
from project_name.project_nameconfig import getdatapath
14
class Preferencescamel_case_nameDialog(gtk.Dialog):
15
__gtype_name__ = "Preferencescamel_case_nameDialog"
19
"""__init__ - This function is typically not called directly.
20
Creation of a Preferencescamel_case_nameDialog requires redeading the associated ui
21
file and parsing the ui definition extrenally,
22
and then calling Preferencescamel_case_nameDialog.finish_initializing().
24
Use the convenience function NewPreferencescamel_case_nameDialog to create
25
NewAboutcamel_case_nameDialog objects.
30
def finish_initializing(self, builder):
31
"""finish_initalizing should be called after parsing the ui definition
32
and creating a Aboutcamel_case_nameDialog object with it in order to finish
33
initializing the start of the new Aboutcamel_case_nameDialog instance.
36
#get a reference to the builder and set up the signals
37
self.builder = builder
38
self.builder.connect_signals(self)
40
#set up couchdb and the preference info
41
self.__db_name = "project_name"
42
self.__database = CouchDatabase(self.__db_name, create=True)
43
self.__preferences = None
46
#set the record type and then initalize the preferences
47
self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/camel_case_name/Preferences"
48
self.__preferences = self.get_preferences()
49
#TODO:code for other initialization actions should be added here
51
def get_preferences(self):
52
"""get_preferences -returns a dictionary object that contain
53
preferences for project_name. Creates a couchdb record if
57
if self.__preferences == None: #the dialog is initializing
58
self.__load_preferences()
60
#if there were no saved preference, this
61
return self.__preferences
63
def __load_preferences(self):
64
#TODO: add prefernces to the self.__preferences dict
65
#default preferences that will be overwritten if some are saved
66
self.__preferences = {"record_type":self.__record_type}
68
results = self.__database.get_records(record_type=self.__record_type, create_view=True)
70
if len(results.rows) == 0:
71
#no preferences have ever been saved
72
#save them before returning
73
self.__key = self.__database.put_record(Record(self.__preferences))
75
self.__preferences = results.rows[0].value
76
self.__key = results.rows[0].value["_id"]
78
def __save_preferences(self):
79
self.__database.update_fields(self.__key, self.__preferences)
81
def ok(self, widget, data=None):
82
"""ok - The user has elected to save the changes.
83
Called before the dialog returns gtk.RESONSE_OK from run().
86
#make any updates to self.__preferences here
87
#self.__preferences["preference1"] = "value2"
88
self.__save_preferences()
90
def cancel(self, widget, data=None):
91
"""cancel - The user has elected cancel changes.
92
Called before the dialog returns gtk.RESPONSE_CANCEL for run()
95
#restore any changes to self.__preferences here
98
def NewPreferencescamel_case_nameDialog():
99
"""NewPreferencescamel_case_nameDialog - returns a fully instantiated
100
Preferencescamel_case_nameDialog object. Use this function rather than
101
creating a Preferencescamel_case_nameDialog instance directly.
104
#look for the ui file that describes the ui
105
ui_filename = os.path.join(getdatapath(), 'ui', 'Preferencescamel_case_nameDialog.ui')
106
if not os.path.exists(ui_filename):
109
builder = gtk.Builder()
110
builder.add_from_file(ui_filename)
111
dialog = builder.get_object("preferences_project_name_dialog")
112
dialog.finish_initializing(builder)
115
if __name__ == "__main__":
116
dialog = NewPreferencescamel_case_nameDialog()