|
1 import gtk |
|
2 |
|
3 from config import cfg |
|
4 |
|
5 |
|
6 class Settings(gtk.Window): |
|
7 def __init__(self, parent): |
|
8 super(Settings, self).__init__(gtk.WINDOW_TOPLEVEL) |
|
9 |
|
10 self._parent = parent |
|
11 |
|
12 self.set_title('Settings') |
|
13 self.set_modal(True) |
|
14 self.set_transient_for(parent.win) |
|
15 self.set_position(gtk.WIN_POS_CENTER) |
|
16 #self.set_border_width(10) |
|
17 self.connect("key_press_event", self.on_keypress) |
|
18 self.connect("destroy", self.on_destroy) |
|
19 |
|
20 self.tabs = gtk.Notebook() |
|
21 self.add(self.tabs) |
|
22 |
|
23 ### Servers |
|
24 vbox = gtk.VBox(spacing=10) |
|
25 vbox.set_border_width(10) |
|
26 self.tabs.append_page(vbox, gtk.Label('Servers')) |
|
27 |
|
28 hbox = gtk.HBox(spacing=10) |
|
29 vbox.pack_start(hbox) |
|
30 sw = gtk.ScrolledWindow() |
|
31 sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) |
|
32 sw.set_shadow_type(gtk.SHADOW_IN) |
|
33 hbox.pack_start(sw) |
|
34 tree = gtk.TreeView() |
|
35 tree.set_headers_visible(False) |
|
36 sw.add(tree) |
|
37 tree.get_selection().set_mode(gtk.SELECTION_BROWSE) |
|
38 |
|
39 model = gtk.ListStore(str, object) |
|
40 tree.set_model(model) |
|
41 column = gtk.TreeViewColumn("Server", gtk.CellRendererText(), text=0) |
|
42 tree.append_column(column) |
|
43 tree.get_selection().connect('changed', self.item_change) |
|
44 tree.set_size_request(100, -1) |
|
45 |
|
46 try: |
|
47 for server in cfg.servers.server: |
|
48 if str(server.name): |
|
49 title = '%s (%s)' % (str(server.name), str(server.host)) |
|
50 else: |
|
51 title = str(server.host) |
|
52 model.append([title, server]) |
|
53 except AttributeError: |
|
54 pass |
|
55 |
|
56 self.itemtree = tree |
|
57 |
|
58 vbox = gtk.VBox(spacing=10) |
|
59 hbox.pack_start(vbox) |
|
60 |
|
61 table = gtk.Table(5, 2) |
|
62 table.set_row_spacings(4) |
|
63 table.set_col_spacings(10) |
|
64 vbox.pack_start(table) |
|
65 |
|
66 label = gtk.Label("Name:") |
|
67 label.set_alignment(0.0, 0.5) |
|
68 table.attach(label, 0, 1, 0, 1) |
|
69 entry = gtk.Entry() |
|
70 table.attach(entry, 1, 2, 0, 1) |
|
71 self.entry_name = entry |
|
72 |
|
73 label = gtk.Label("Host:") |
|
74 label.set_alignment(0.0, 0.5) |
|
75 table.attach(label, 0, 1, 1, 2) |
|
76 entry = gtk.Entry() |
|
77 entry.set_text('127.0.0.1') |
|
78 table.attach(entry, 1, 2, 1, 2) |
|
79 self.entry_host = entry |
|
80 |
|
81 label = gtk.Label("Port:") |
|
82 label.set_alignment(0.0, 0.5) |
|
83 table.attach(label, 0, 1, 2, 3) |
|
84 entry = gtk.Entry() |
|
85 entry.set_text('5432') |
|
86 table.attach(entry, 1, 2, 2, 3) |
|
87 self.entry_port = entry |
|
88 |
|
89 label = gtk.Label("User:") |
|
90 label.set_alignment(0.0, 0.5) |
|
91 table.attach(label, 0, 1, 3, 4) |
|
92 entry = gtk.Entry() |
|
93 entry.set_text('postgres') |
|
94 table.attach(entry, 1, 2, 3, 4) |
|
95 self.entry_user = entry |
|
96 |
|
97 label = gtk.Label("Password:") |
|
98 label.set_alignment(0.0, 0.5) |
|
99 table.attach(label, 0, 1, 4, 5) |
|
100 entry = gtk.Entry() |
|
101 table.attach(entry, 1, 2, 4, 5) |
|
102 self.entry_password = entry |
|
103 |
|
104 hbox = gtk.HBox() |
|
105 vbox.pack_start(hbox) |
|
106 |
|
107 btn = gtk.Button('Add') |
|
108 btn.connect('clicked', self.item_add) |
|
109 hbox.pack_start(btn) |
|
110 btn = gtk.Button('Save') |
|
111 btn.connect('clicked', self.item_save) |
|
112 hbox.pack_start(btn) |
|
113 btn = gtk.Button('Remove') |
|
114 btn.connect('clicked', self.item_remove) |
|
115 hbox.pack_start(btn) |
|
116 |
|
117 ### Editor |
|
118 vbox = gtk.VBox(spacing=10) |
|
119 vbox.set_border_width(10) |
|
120 self.tabs.append_page(vbox, gtk.Label('Editor')) |
|
121 |
|
122 self.show_all() |
|
123 self.itemtree.grab_focus() |
|
124 |
|
125 |
|
126 |
|
127 def item_change(self, w): |
|
128 model, sel = self.itemtree.get_selection().get_selected_rows() |
|
129 if not sel: |
|
130 return |
|
131 el, = model.get(model.get_iter(sel[0]), 1) |
|
132 |
|
133 self.entry_name.set_text(str(el.name)) |
|
134 self.entry_host.set_text(str(el.host)) |
|
135 self.entry_port.set_text(str(el.port)) |
|
136 self.entry_user.set_text(str(el.user)) |
|
137 self.entry_password.set_text(str(el.password)) |
|
138 |
|
139 |
|
140 def item_add(self, w): |
|
141 el = cfg.add_server( |
|
142 self.entry_name.get_text(), |
|
143 self.entry_host.get_text(), |
|
144 self.entry_port.get_text(), |
|
145 self.entry_user.get_text(), |
|
146 self.entry_password.get_text()) |
|
147 if str(el.name): |
|
148 title = '%s (%s)' % (str(el.name), str(el.host)) |
|
149 else: |
|
150 title = str(el.host) |
|
151 iter = self.itemtree.get_model().append([title, el]) |
|
152 self.itemtree.get_selection().select_iter(iter) |
|
153 cfg.save() |
|
154 |
|
155 |
|
156 def item_save(self, w): |
|
157 model, sel = self.itemtree.get_selection().get_selected_rows() |
|
158 el = cfg.servers.server[sel[0][0]] |
|
159 el.name = self.entry_name.get_text() |
|
160 el.host = self.entry_host.get_text() |
|
161 el.port = self.entry_port.get_text() |
|
162 el.user = self.entry_user.get_text() |
|
163 el.password = self.entry_password.get_text() |
|
164 cfg.save() |
|
165 |
|
166 if str(el.name): |
|
167 title = '%s (%s)' % (str(el.name), str(el.host)) |
|
168 else: |
|
169 title = str(el.host) |
|
170 model.set(model.get_iter(sel[0]), 0, title) |
|
171 |
|
172 |
|
173 def item_remove(self, w): |
|
174 model, sel = self.itemtree.get_selection().get_selected_rows() |
|
175 model.remove(model.get_iter(sel[0])) |
|
176 if model.get_iter_first(): |
|
177 self.itemtree.get_selection().select_iter(model.get_iter_first()) |
|
178 del cfg.servers.server[sel[0][0]] |
|
179 cfg.save() |
|
180 |
|
181 |
|
182 def on_keypress(self, w, event): |
|
183 keyname = gtk.gdk.keyval_name(event.keyval) |
|
184 |
|
185 if keyname == 'Escape': |
|
186 self.destroy() |
|
187 return True |
|
188 return False |
|
189 |
|
190 |
|
191 def on_destroy(self, w): |
|
192 model, sel = self.itemtree.get_selection().get_selected_rows() |
|
193 if sel: |
|
194 sel = sel[0][0] |
|
195 else: |
|
196 sel = None |
|
197 self._parent.reload_server_list(sel) |
|
198 |