Add lxproxy from Leszek Lesner
[lxde/lxadmin.git] / src / lxproxy / PreferencesLxproxyDialog.py
1 # -*- coding: utf-8 -*-
2 ### BEGIN LICENSE
3 # Copyright (C) YYYY Leszek Lesner leszek@zevenos.com
4 #This program is free software: you can redistribute it and/or modify it
5 #under the terms of the GNU General Public License version 3, as published
6 #by the Free Software Foundation.
7 #
8 #This program is distributed in the hope that it will be useful, but
9 #WITHOUT ANY WARRANTY; without even the implied warranties of
10 #MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 #PURPOSE. See the GNU General Public License for more details.
12 #
13 #You should have received a copy of the GNU General Public License along
14 #with this program. If not, see <http://www.gnu.org/licenses/>.
15 ### END LICENSE
16
17 import sys
18 import os
19 import gtk
20 from desktopcouch.records.server import CouchDatabase
21 from desktopcouch.records.record import Record
22
23 from lxproxy.lxproxyconfig import getdatapath
24
25 class PreferencesLxproxyDialog(gtk.Dialog):
26 __gtype_name__ = "PreferencesLxproxyDialog"
27 prefernces = {}
28
29 def __init__(self):
30 """__init__ - This function is typically not called directly.
31 Creation of a PreferencesLxproxyDialog requires redeading the associated ui
32 file and parsing the ui definition extrenally,
33 and then calling PreferencesLxproxyDialog.finish_initializing().
34
35 Use the convenience function NewPreferencesLxproxyDialog to create
36 NewAboutLxproxyDialog objects.
37 """
38
39 pass
40
41 def finish_initializing(self, builder):
42 """finish_initalizing should be called after parsing the ui definition
43 and creating a AboutLxproxyDialog object with it in order to finish
44 initializing the start of the new AboutLxproxyDialog instance.
45 """
46
47 #get a reference to the builder and set up the signals
48 self.builder = builder
49 self.builder.connect_signals(self)
50
51 #set up couchdb and the preference info
52 self.__db_name = "lxproxy"
53 self.__database = CouchDatabase(self.__db_name, create=True)
54 self.__preferences = None
55 self.__key = None
56
57 #set the record type and then initalize the preferences
58 self.__record_type = "http://wiki.ubuntu.com/Quickly/RecordTypes/Lxproxy/Preferences"
59 self.__preferences = self.get_preferences()
60 #TODO:code for other initialization actions should be added here
61
62 def get_preferences(self):
63 """get_preferences -returns a dictionary object that contain
64 preferences for lxproxy. Creates a couchdb record if
65 necessary.
66 """
67
68 if self.__preferences == None: #the dialog is initializing
69 self.__load_preferences()
70
71 #if there were no saved preference, this
72 return self.__preferences
73
74 def __load_preferences(self):
75 #TODO: add prefernces to the self.__preferences dict
76 #default preferences that will be overwritten if some are saved
77 self.__preferences = {"record_type":self.__record_type}
78
79 results = self.__database.get_records(record_type=self.__record_type, create_view=True)
80
81 if len(results.rows) == 0:
82 #no preferences have ever been saved
83 #save them before returning
84 self.__key = self.__database.put_record(Record(self.__preferences))
85 else:
86 self.__preferences = results.rows[0].value
87 self.__key = results.rows[0].value["_id"]
88
89 def __save_preferences(self):
90 self.__database.update_fields(self.__key, self.__preferences)
91
92 def ok(self, widget, data=None):
93 """ok - The user has elected to save the changes.
94 Called before the dialog returns gtk.RESONSE_OK from run().
95 """
96
97 #make any updates to self.__preferences here
98 #self.__preferences["preference1"] = "value2"
99 self.__save_preferences()
100
101 def cancel(self, widget, data=None):
102 """cancel - The user has elected cancel changes.
103 Called before the dialog returns gtk.RESPONSE_CANCEL for run()
104 """
105
106 #restore any changes to self.__preferences here
107 pass
108
109 def NewPreferencesLxproxyDialog():
110 """NewPreferencesLxproxyDialog - returns a fully instantiated
111 PreferencesLxproxyDialog object. Use this function rather than
112 creating a PreferencesLxproxyDialog instance directly.
113 """
114
115 #look for the ui file that describes the ui
116 ui_filename = os.path.join(getdatapath(), 'ui', 'PreferencesLxproxyDialog.ui')
117 if not os.path.exists(ui_filename):
118 ui_filename = None
119
120 builder = gtk.Builder()
121 builder.add_from_file(ui_filename)
122 dialog = builder.get_object("preferences_lxproxy_dialog")
123 dialog.finish_initializing(builder)
124 return dialog
125
126 if __name__ == "__main__":
127 dialog = NewPreferencesLxproxyDialog()
128 dialog.show()
129 gtk.main()
130