1 |
bh |
1620 |
# Copyright (c) 2001, 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Martin Mueller <[email protected]> |
4 |
|
|
# Bernhard Herzog <[email protected]> |
5 |
|
|
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
"""Dialogs to manage database connections""" |
11 |
|
|
|
12 |
|
|
import sys, traceback |
13 |
|
|
|
14 |
|
|
from wxPython.wx import * |
15 |
|
|
|
16 |
bh |
1625 |
try: |
17 |
|
|
import psycopg |
18 |
|
|
except ImportError: |
19 |
|
|
psycopg = None |
20 |
bh |
1620 |
|
21 |
|
|
from Thuban import _ |
22 |
|
|
from Thuban.UI.dialogs import NonModalDialog |
23 |
bh |
1648 |
from Thuban.Model.postgisdb import ConnectionError, PostGISConnection |
24 |
bh |
1620 |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
25 |
|
|
from messages import SESSION_REPLACED |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
ID_DB_ADD = 9101 |
29 |
|
|
ID_DB_REMOVE = 9102 |
30 |
|
|
|
31 |
|
|
ID_DBCHOOSE_RETRIEVE = 9201 |
32 |
|
|
ID_DBCHOOSE_OK = 9202 |
33 |
|
|
ID_DBCHOOSE_CANCEL = 9203 |
34 |
|
|
ID_LB_DCLICK = 9204 |
35 |
|
|
|
36 |
|
|
|
37 |
|
|
class ChooseDBTableDialog(wxDialog): |
38 |
|
|
|
39 |
|
|
def __init__(self, session, *args, **kwds): |
40 |
|
|
kwds["style"] = wxDIALOG_MODAL|wxCAPTION |
41 |
|
|
wxDialog.__init__(self, *args, **kwds) |
42 |
|
|
self.session = session |
43 |
|
|
self.dbconns = self.session.DBConnections() |
44 |
|
|
self.tables = [] |
45 |
|
|
self.list_box_4 = wxListBox(self, -1) |
46 |
|
|
for i in range(len(self.dbconns)): |
47 |
|
|
self.list_box_4.Append(self.dbconns[i].BriefDescription()) |
48 |
|
|
self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE, |
49 |
|
|
_("Retrieve")) |
50 |
|
|
self.list_box_5 = wxListBox(self, ID_LB_DCLICK) |
51 |
|
|
self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
52 |
|
|
self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
53 |
|
|
self.__set_properties() |
54 |
|
|
self.__do_layout() |
55 |
|
|
|
56 |
|
|
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
57 |
|
|
EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel) |
58 |
|
|
EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve) |
59 |
|
|
EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick) |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
def __set_properties(self): |
63 |
|
|
self.SetTitle(_("Choose layer from database")) |
64 |
|
|
self.list_box_4.SetSelection(0) |
65 |
|
|
self.list_box_5.SetSelection(0) |
66 |
|
|
|
67 |
|
|
def __do_layout(self): |
68 |
|
|
grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0) |
69 |
|
|
grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0) |
70 |
|
|
grid_sizer_2 = wxFlexGridSizer(1, 3, 0, 0) |
71 |
|
|
sizer_4 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")), |
72 |
|
|
wxHORIZONTAL) |
73 |
|
|
grid_sizer_4 = wxFlexGridSizer(3, 1, 0, 0) |
74 |
|
|
sizer_3 = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")), |
75 |
|
|
wxHORIZONTAL) |
76 |
|
|
sizer_3.Add(self.list_box_4, 0, wxEXPAND, 0) |
77 |
|
|
grid_sizer_2.Add(sizer_3, 1, wxEXPAND, 0) |
78 |
|
|
grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0) |
79 |
|
|
grid_sizer_4.Add(self.DB_CHOOSE_RETRIEVE, 0, wxALL |
80 |
|
|
|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4) |
81 |
|
|
grid_sizer_4.Add(20, 80, 0, wxEXPAND, 0) |
82 |
|
|
grid_sizer_2.Add(grid_sizer_4, 1, wxEXPAND, 0) |
83 |
|
|
sizer_4.Add(self.list_box_5, 0, wxEXPAND, 0) |
84 |
|
|
grid_sizer_2.Add(sizer_4, 1, wxEXPAND, 0) |
85 |
|
|
grid_sizer_1.Add(grid_sizer_2, 1, wxEXPAND, 0) |
86 |
|
|
grid_sizer_3.Add(self.DB_CHOOSE_OK, 0, wxALL|wxALIGN_RIGHT, 4) |
87 |
|
|
grid_sizer_3.Add(self.DB_CHOOSE_CANCEL, 0, wxALL, 4) |
88 |
|
|
grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4) |
89 |
|
|
self.SetAutoLayout(1) |
90 |
|
|
self.SetSizer(grid_sizer_1) |
91 |
|
|
grid_sizer_1.Fit(self) |
92 |
|
|
grid_sizer_1.SetSizeHints(self) |
93 |
|
|
self.Layout() |
94 |
|
|
|
95 |
|
|
def GetTable(self): |
96 |
|
|
i = self.list_box_5.GetSelection() |
97 |
|
|
if i >= 0: |
98 |
|
|
return self.selected_conn, self.tables[i] |
99 |
|
|
return None |
100 |
|
|
|
101 |
|
|
def OnRetrieve(self, event): |
102 |
|
|
i = self.list_box_4.GetSelection() |
103 |
|
|
if i >= 0: |
104 |
|
|
self.selected_conn = self.dbconns[i] |
105 |
|
|
self.tables = self.selected_conn.GeometryTables() |
106 |
|
|
self.list_box_5.Set(self.tables) |
107 |
|
|
|
108 |
|
|
def OnLBDClick(self, event): |
109 |
|
|
if self.list_box_5.GetSelection() >= 0: |
110 |
|
|
self.EndModal(wxID_OK) |
111 |
|
|
self.Show(false) |
112 |
|
|
|
113 |
|
|
def OnOK(self, event): |
114 |
|
|
self.EndModal(wxID_OK) |
115 |
|
|
self.Show(false) |
116 |
|
|
|
117 |
|
|
def OnCancel(self, event): |
118 |
|
|
self.EndModal(wxID_CANCEL) |
119 |
|
|
self.Show(false) |
120 |
|
|
|
121 |
|
|
|
122 |
|
|
class DBDialog(wxDialog): |
123 |
|
|
|
124 |
|
|
"""Dialog for the parameters of a database connection""" |
125 |
|
|
|
126 |
bh |
1648 |
def __init__(self, parent, title, parameters, message = ""): |
127 |
|
|
"""Initialize the dialog box. |
128 |
bh |
1620 |
|
129 |
bh |
1648 |
The parameters argument should be a dictionary containing known |
130 |
|
|
connection parameters. |
131 |
bh |
1620 |
|
132 |
bh |
1648 |
The optional message parameter will be displayed at the top of |
133 |
|
|
the dialog box and can be used to display error messages when |
134 |
|
|
using the dialog to ask for correct parameters when the |
135 |
|
|
connection can't be established. |
136 |
|
|
""" |
137 |
|
|
wxDialog.__init__(self, parent, -1, title) |
138 |
bh |
1620 |
|
139 |
bh |
1648 |
top = wxBoxSizer(wxVERTICAL) |
140 |
bh |
1620 |
|
141 |
bh |
1648 |
if message: |
142 |
|
|
top.Add(wxStaticText(self, -1, message), 0, |
143 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
144 |
bh |
1620 |
|
145 |
bh |
1648 |
box = wxBoxSizer(wxHORIZONTAL) |
146 |
|
|
box.Add(wxStaticText(self, -1, _("Hostname:")), 0, |
147 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
148 |
|
|
self.text_host = wxTextCtrl(self, -1, parameters.get("host", "")) |
149 |
|
|
box.Add(self.text_host, 2, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
150 |
|
|
box.Add(wxStaticText(self, -1, _("Port:")), 0, |
151 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
152 |
|
|
self.text_port = wxTextCtrl(self, -1, parameters.get("port", "")) |
153 |
|
|
box.Add(self.text_port, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
154 |
|
|
top.Add(box, 0, wxEXPAND) |
155 |
bh |
1620 |
|
156 |
bh |
1648 |
box = wxBoxSizer(wxHORIZONTAL) |
157 |
|
|
box.Add(wxStaticText(self, -1, _("Database Name:")), 0, |
158 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
159 |
|
|
self.text_dbname = wxTextCtrl(self, -1, |
160 |
|
|
parameters.get("dbname", "")) |
161 |
|
|
box.Add(self.text_dbname, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
162 |
|
|
top.Add(box, 0, wxEXPAND) |
163 |
bh |
1620 |
|
164 |
bh |
1648 |
box = wxBoxSizer(wxHORIZONTAL) |
165 |
|
|
box.Add(wxStaticText(self, -1, _("User:")), 0, |
166 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
167 |
|
|
self.text_user = wxTextCtrl(self, -1, |
168 |
|
|
parameters.get("user", "")) |
169 |
|
|
box.Add(self.text_user, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
170 |
|
|
box.Add(wxStaticText(self, -1, _("Password:")), 0, |
171 |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
172 |
|
|
self.text_password = wxTextCtrl(self, -1, |
173 |
|
|
parameters.get("password", ""), |
174 |
|
|
style = wxTE_PASSWORD) |
175 |
|
|
box.Add(self.text_password, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, |
176 |
|
|
4) |
177 |
|
|
top.Add(box, 0, wxEXPAND) |
178 |
bh |
1620 |
|
179 |
bh |
1648 |
buttons = wxBoxSizer(wxHORIZONTAL) |
180 |
|
|
button = wxButton(self, wxID_OK, _("OK")) |
181 |
|
|
buttons.Add(button, 0, wxALL, 4) |
182 |
|
|
button = wxButton(self, wxID_CANCEL, _("Cancel")) |
183 |
|
|
buttons.Add(button, 0, wxALL, 4) |
184 |
|
|
top.Add(buttons, 0, wxALIGN_RIGHT, 4) |
185 |
bh |
1620 |
|
186 |
bh |
1648 |
self.SetAutoLayout(1) |
187 |
|
|
self.SetSizer(top) |
188 |
|
|
top.Fit(self) |
189 |
|
|
top.SetSizeHints(self) |
190 |
|
|
|
191 |
|
|
EVT_BUTTON(self, wxID_OK, self.OnOK) |
192 |
|
|
EVT_BUTTON(self, wxID_CANCEL, self.OnCancel) |
193 |
|
|
|
194 |
|
|
def RunDialog(self): |
195 |
|
|
self.ShowModal() |
196 |
|
|
self.Destroy() |
197 |
|
|
return self.result |
198 |
|
|
|
199 |
|
|
def end_dialog(self, result): |
200 |
|
|
self.result = result |
201 |
|
|
if result is not None: |
202 |
|
|
self.EndModal(wxID_OK) |
203 |
|
|
else: |
204 |
|
|
self.EndModal(wxID_CANCEL) |
205 |
bh |
1620 |
self.Show(false) |
206 |
|
|
|
207 |
bh |
1648 |
def OnOK(self, event): |
208 |
|
|
result = {} |
209 |
|
|
for name in ("host", "port", "dbname", "user", "password"): |
210 |
|
|
result[name] = getattr(self, "text_" + name).GetValue() |
211 |
|
|
self.end_dialog(result) |
212 |
|
|
|
213 |
bh |
1620 |
def OnCancel(self, event): |
214 |
bh |
1648 |
self.end_dialog(None) |
215 |
bh |
1620 |
|
216 |
|
|
|
217 |
bh |
1648 |
|
218 |
bh |
1620 |
class DBFrame(NonModalDialog): |
219 |
|
|
|
220 |
|
|
"""Databse connection management dialog""" |
221 |
|
|
|
222 |
|
|
def __init__(self, parent, name, session, *args, **kwds): |
223 |
|
|
kwds["style"] = wxICONIZE|wxCAPTION|wxMINIMIZE |
224 |
|
|
NonModalDialog.__init__(self, parent, name, "") |
225 |
|
|
self.session = session |
226 |
|
|
self.app = self.parent.application |
227 |
|
|
|
228 |
|
|
self.app.Subscribe(SESSION_REPLACED, self.session_replaced) |
229 |
|
|
self.subscribe_session() |
230 |
|
|
|
231 |
|
|
self.DB_ListBox = wxListBox(self, -1, |
232 |
|
|
style=wxLB_SINGLE|wxLB_HSCROLL|wxLB_ALWAYS_SB) |
233 |
|
|
self.DB_Add = wxButton(self, ID_DB_ADD, _("Add")) |
234 |
|
|
self.DB_Remove = wxButton(self, ID_DB_REMOVE, _("Remove")) |
235 |
|
|
self.DB_CLOSE = wxButton(self, wxID_CLOSE, _("Close")) |
236 |
|
|
self.__set_properties() |
237 |
|
|
self.__do_layout() |
238 |
|
|
EVT_BUTTON(self, ID_DB_ADD, self.OnAdd) |
239 |
|
|
EVT_BUTTON(self, ID_DB_REMOVE, self.OnRemove) |
240 |
|
|
EVT_BUTTON(self, wxID_CLOSE, self.OnClose) |
241 |
|
|
|
242 |
|
|
self.conns_changed() |
243 |
|
|
|
244 |
|
|
def __set_properties(self): |
245 |
|
|
self.SetTitle(_("Database Management")) |
246 |
|
|
self.DB_ListBox.SetSize((200, 157)) |
247 |
|
|
self.DB_ListBox.SetSelection(0) |
248 |
|
|
|
249 |
|
|
def __do_layout(self): |
250 |
|
|
top = wxBoxSizer(wxVERTICAL) |
251 |
|
|
|
252 |
|
|
box = wxBoxSizer(wxHORIZONTAL) |
253 |
|
|
|
254 |
|
|
box.Add(self.DB_ListBox, 1, wxALL|wxEXPAND |
255 |
|
|
|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4) |
256 |
|
|
|
257 |
|
|
buttons = wxBoxSizer(wxVERTICAL) |
258 |
|
|
buttons.Add(self.DB_Add, 0, wxALL, 4) |
259 |
|
|
buttons.Add(self.DB_Remove, 0, wxALL, 4) |
260 |
|
|
|
261 |
|
|
box.Add(buttons, 0, wxEXPAND) |
262 |
|
|
top.Add(box, 1, wxEXPAND) |
263 |
|
|
|
264 |
|
|
buttons = wxBoxSizer(wxHORIZONTAL) |
265 |
|
|
buttons.Add(self.DB_CLOSE, 1, wxALL|wxALIGN_RIGHT, 4) |
266 |
|
|
top.Add(buttons, 0, wxALIGN_RIGHT) |
267 |
|
|
|
268 |
|
|
self.SetAutoLayout(1) |
269 |
|
|
self.SetSizer(top) |
270 |
|
|
top.Fit(self) |
271 |
|
|
top.SetSizeHints(self) |
272 |
|
|
|
273 |
|
|
def subscribe_session(self): |
274 |
|
|
"""Internal: Subscribe to some session messages""" |
275 |
|
|
self.session.Subscribe(DBCONN_ADDED, self.conns_changed) |
276 |
|
|
self.session.Subscribe(DBCONN_REMOVED, self.conns_changed) |
277 |
|
|
|
278 |
|
|
def unsubscribe_session(self): |
279 |
|
|
"""Internal: Subscribe from messages subscribe in subscribe_session""" |
280 |
|
|
self.session.Subscribe(DBCONN_ADDED, self.conns_changed) |
281 |
|
|
self.session.Subscribe(DBCONN_REMOVED, self.conns_changed) |
282 |
|
|
|
283 |
|
|
def session_replaced(self, *args): |
284 |
|
|
"""Internal: resubscribe the session messages |
285 |
|
|
|
286 |
|
|
This method is a subscriber for the application's |
287 |
|
|
SESSION_REPLACED messages. |
288 |
|
|
""" |
289 |
|
|
self.unsubscribe_session() |
290 |
|
|
self.session = self.app.Session() |
291 |
|
|
self.subscribe_session() |
292 |
|
|
self.conns_changed() |
293 |
|
|
|
294 |
|
|
def conns_changed(self, *args): |
295 |
|
|
"""Internal: update the db connection list box |
296 |
|
|
|
297 |
|
|
Subscribed to the DBCONN_REMOVED and DBCONN_REMOVED. |
298 |
|
|
""" |
299 |
|
|
self.DB_ListBox.Clear() |
300 |
|
|
for conn in self.session.DBConnections(): |
301 |
|
|
self.DB_ListBox.Append(conn.BriefDescription(), conn) |
302 |
|
|
|
303 |
|
|
def OnClose(self, event): |
304 |
|
|
self.unsubscribe_session() |
305 |
|
|
self.app.Unsubscribe(SESSION_REPLACED, self.session_replaced) |
306 |
|
|
NonModalDialog.OnClose(self, event) |
307 |
|
|
|
308 |
|
|
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
309 |
|
|
"""Run a modal message box with the given text, title and flags |
310 |
|
|
and return the result""" |
311 |
|
|
dlg = wxMessageDialog(self, text, title, flags) |
312 |
|
|
dlg.CenterOnParent() |
313 |
|
|
result = dlg.ShowModal() |
314 |
|
|
dlg.Destroy() |
315 |
|
|
return result |
316 |
|
|
|
317 |
|
|
def OnAdd(self, event): |
318 |
bh |
1648 |
message = "" |
319 |
|
|
parameters = {} |
320 |
bh |
1620 |
while 1: |
321 |
bh |
1648 |
dialog = DBDialog(self, _("Add Database"), parameters, message) |
322 |
|
|
parameters = dialog.RunDialog() |
323 |
|
|
if parameters is not None: |
324 |
|
|
host = parameters["host"] |
325 |
|
|
database = parameters["dbname"] |
326 |
bh |
1620 |
for conn in self.session.DBConnections(): |
327 |
|
|
if (host == conn.host and |
328 |
|
|
database == conn.dbname): |
329 |
|
|
self.RunMessageBox(_("Add Database"), |
330 |
bh |
1648 |
_("Connection to '%s' already exists") |
331 |
bh |
1620 |
% database) |
332 |
|
|
break |
333 |
|
|
try: |
334 |
bh |
1648 |
conn = PostGISConnection(**parameters) |
335 |
|
|
except ConnectionError, val: |
336 |
|
|
message = str(val) |
337 |
|
|
else: |
338 |
bh |
1620 |
self.session.AddDBConnection(conn) |
339 |
|
|
break |
340 |
|
|
else: |
341 |
|
|
break |
342 |
|
|
|
343 |
|
|
def OnRemove(self, event): |
344 |
|
|
i = self.DB_ListBox.GetSelection() |
345 |
|
|
if i >= 0: |
346 |
bh |
1630 |
conn = self.DB_ListBox.GetClientData(i) |
347 |
|
|
if self.session.CanRemoveDBConnection(conn): |
348 |
|
|
self.session.RemoveDBConnection(conn) |
349 |
|
|
else: |
350 |
|
|
self.RunMessageBox(_("Remove Database Connection"), |
351 |
|
|
_("The connection %s\nis still in use") |
352 |
|
|
% conn.BriefDescription()) |