20 |
|
|
21 |
from Thuban import _ |
from Thuban import _ |
22 |
from Thuban.UI.dialogs import NonModalDialog |
from Thuban.UI.dialogs import NonModalDialog |
23 |
from Thuban.Model.postgisdb import PostGISConnection |
from Thuban.Model.postgisdb import ConnectionError, PostGISConnection |
24 |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
25 |
from messages import SESSION_REPLACED |
from messages import SESSION_REPLACED |
26 |
|
|
|
ID_DBADD_OK = 9001 |
|
|
ID_DBADD_CANCEL = 9002 |
|
27 |
|
|
28 |
ID_DB_ADD = 9101 |
ID_DB_ADD = 9101 |
29 |
ID_DB_REMOVE = 9102 |
ID_DB_REMOVE = 9102 |
123 |
|
|
124 |
"""Dialog for the parameters of a database connection""" |
"""Dialog for the parameters of a database connection""" |
125 |
|
|
126 |
def __init__(self, selection=None, *args, **kwds): |
def __init__(self, parent, title, parameters, message = ""): |
127 |
kwds["style"] = wxDEFAULT_DIALOG_STYLE |
"""Initialize the dialog box. |
|
wxDialog.__init__(self, *args, **kwds) |
|
|
self.DBAdd_Label_Host = wxStaticText(self, -1, _("Hostname:")) |
|
|
self.DBAdd_Host = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Port = wxStaticText(self, -1, _("Port:")) |
|
|
self.DBAdd_Port = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Database = wxStaticText(self, -1, _("Database:")) |
|
|
self.DBAdd_Database = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_User = wxStaticText(self, -1, _("User:")) |
|
|
self.DBAdd_User = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Pwd = wxStaticText(self, -1, _("Password:")) |
|
|
self.DBAdd_Pwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD) |
|
|
self.DBAdd_OK = wxButton(self, ID_DBADD_OK, _("OK")) |
|
|
self.DBAdd_Cancel = wxButton(self, ID_DBADD_CANCEL, _("Cancel")) |
|
|
if selection != None: |
|
|
self.DBAdd_Host.SetEditable(false) |
|
|
self.DBAdd_Host.SetValue(selection.host) |
|
|
self.DBAdd_Port.SetValue(selection.port) |
|
|
self.DBAdd_Database.SetEditable(false) |
|
|
self.DBAdd_Database.SetValue(selection.dbname()) |
|
|
self.DBAdd_User.SetValue(selection.user) |
|
|
self.__set_properties() |
|
|
self.__do_layout() |
|
|
EVT_BUTTON(self, ID_DBADD_OK, self.OnOK) |
|
|
EVT_BUTTON(self, ID_DBADD_CANCEL, self.OnCancel) |
|
128 |
|
|
129 |
def __set_properties(self): |
The parameters argument should be a dictionary containing known |
130 |
self.SetTitle(_("Add database")) |
connection parameters. |
|
self.SetSize((480, 185)) |
|
|
self.DBAdd_Label_Host.SetSize((60, 16)) |
|
|
self.DBAdd_Host.SetSize((200, 22)) |
|
|
self.DBAdd_Label_Database.SetSize((60, 16)) |
|
|
self.DBAdd_Label_User.SetSize((60, 16)) |
|
131 |
|
|
132 |
def __do_layout(self): |
The optional message parameter will be displayed at the top of |
133 |
grid_sizer_1 = wxFlexGridSizer(4, 1, 0, 0) |
the dialog box and can be used to display error messages when |
134 |
grid_sizer_2 = wxGridSizer(1, 2, 0, 0) |
using the dialog to ask for correct parameters when the |
135 |
grid_sizer_5 = wxFlexGridSizer(1, 5, 0, 0) |
connection can't be established. |
136 |
grid_sizer_6 = wxFlexGridSizer(1, 5, 0, 0) |
""" |
137 |
grid_sizer_3 = wxFlexGridSizer(1, 5, 0, 0) |
wxDialog.__init__(self, parent, -1, title) |
138 |
grid_sizer_3.Add(self.DBAdd_Label_Host, 0, |
|
139 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
top = wxBoxSizer(wxVERTICAL) |
140 |
grid_sizer_3.Add(self.DBAdd_Host, 0, wxALL, 4) |
|
141 |
grid_sizer_3.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
if message: |
142 |
grid_sizer_3.Add(self.DBAdd_Label_Port, 0, |
top.Add(wxStaticText(self, -1, message), 0, |
143 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
144 |
grid_sizer_3.Add(self.DBAdd_Port, 0, wxALL, 4) |
|
145 |
grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxEXPAND, 4) |
box = wxBoxSizer(wxHORIZONTAL) |
146 |
grid_sizer_6.Add(self.DBAdd_Label_Database, 0, |
box.Add(wxStaticText(self, -1, _("Hostname:")), 0, |
147 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
148 |
grid_sizer_6.Add(self.DBAdd_Database, 0, wxALL, 4) |
self.text_host = wxTextCtrl(self, -1, parameters.get("host", "")) |
149 |
grid_sizer_6.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
box.Add(self.text_host, 2, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
150 |
grid_sizer_1.Add(grid_sizer_6, 1, wxALL|wxEXPAND, 4) |
box.Add(wxStaticText(self, -1, _("Port:")), 0, |
151 |
grid_sizer_5.Add(self.DBAdd_Label_User, 0, |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
152 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
self.text_port = wxTextCtrl(self, -1, parameters.get("port", "")) |
153 |
grid_sizer_5.Add(self.DBAdd_User, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
box.Add(self.text_port, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
154 |
grid_sizer_5.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
top.Add(box, 0, wxEXPAND) |
|
grid_sizer_5.Add(self.DBAdd_Label_Pwd, 0, |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_5.Add(self.DBAdd_Pwd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_1.Add(grid_sizer_5, 1, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_2.Add(self.DBAdd_OK, 0, |
|
|
wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_2.Add(self.DBAdd_Cancel, 0, wxALL|wxALIGN_CENTER_VERTICAL, |
|
|
4) |
|
|
grid_sizer_1.Add(grid_sizer_2, 1, wxALL|wxEXPAND, 4) |
|
|
self.SetAutoLayout(1) |
|
|
self.SetSizer(grid_sizer_1) |
|
|
self.Layout() |
|
155 |
|
|
156 |
|
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 |
|
|
164 |
def GetHostname(self): |
box = wxBoxSizer(wxHORIZONTAL) |
165 |
return self.DBAdd_Host.GetValue() |
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 |
|
|
179 |
def GetPort(self): |
buttons = wxBoxSizer(wxHORIZONTAL) |
180 |
return self.DBAdd_Port.GetValue() |
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 |
|
|
186 |
def GetDatabase(self): |
self.SetAutoLayout(1) |
187 |
return self.DBAdd_Database.GetValue() |
self.SetSizer(top) |
188 |
|
top.Fit(self) |
189 |
|
top.SetSizeHints(self) |
190 |
|
|
191 |
def GetUser(self): |
EVT_BUTTON(self, wxID_OK, self.OnOK) |
192 |
return self.DBAdd_User.GetValue() |
EVT_BUTTON(self, wxID_CANCEL, self.OnCancel) |
193 |
|
|
194 |
def GetPassword(self): |
def RunDialog(self): |
195 |
return self.DBAdd_Pwd.GetValue() |
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 |
|
self.Show(false) |
206 |
|
|
207 |
def OnOK(self, event): |
def OnOK(self, event): |
208 |
self.EndModal(wxID_OK) |
result = {} |
209 |
self.Show(false) |
for name in ("host", "port", "dbname", "user", "password"): |
210 |
|
result[name] = getattr(self, "text_" + name).GetValue() |
211 |
|
self.end_dialog(result) |
212 |
|
|
213 |
def OnCancel(self, event): |
def OnCancel(self, event): |
214 |
self.EndModal(wxID_CANCEL) |
self.end_dialog(None) |
215 |
self.Show(false) |
|
216 |
|
|
217 |
|
|
218 |
class DBFrame(NonModalDialog): |
class DBFrame(NonModalDialog): |
315 |
return result |
return result |
316 |
|
|
317 |
def OnAdd(self, event): |
def OnAdd(self, event): |
318 |
adddialog = DBDialog(None, self, -1, "") |
message = "" |
319 |
|
parameters = {} |
320 |
while 1: |
while 1: |
321 |
if adddialog.ShowModal() == wxID_OK: |
dialog = DBDialog(self, _("Add Database"), parameters, message) |
322 |
host = adddialog.GetHostname() |
parameters = dialog.RunDialog() |
323 |
port = adddialog.GetPort() |
if parameters is not None: |
324 |
database = adddialog.GetDatabase() |
host = parameters["host"] |
325 |
user = adddialog.GetUser() |
database = parameters["dbname"] |
|
password = adddialog.GetPassword() |
|
326 |
for conn in self.session.DBConnections(): |
for conn in self.session.DBConnections(): |
327 |
if (host == conn.host and |
if (host == conn.host and |
328 |
database == conn.dbname): |
database == conn.dbname): |
329 |
self.RunMessageBox(_("Add Database"), |
self.RunMessageBox(_("Add Database"), |
330 |
_("Connection to '%s' already exists") |
_("Connection to '%s' already exists") |
331 |
% database) |
% database) |
332 |
break |
break |
333 |
try: |
try: |
334 |
conn = PostGISConnection(database, host = host, port=port, |
conn = PostGISConnection(**parameters) |
335 |
user = user, password = password) |
except ConnectionError, val: |
336 |
|
message = str(val) |
337 |
|
else: |
338 |
self.session.AddDBConnection(conn) |
self.session.AddDBConnection(conn) |
339 |
break |
break |
|
except psycopg.OperationalError, strerror: |
|
|
self.RunMessageBox(_("Error"), str(strerror)) |
|
|
except: |
|
|
self.RunMessageBox(_("Add Database"), |
|
|
_("Can't establish connection to '%s'." |
|
|
% database)) |
|
340 |
else: |
else: |
341 |
break |
break |
|
adddialog.Destroy() |
|
342 |
|
|
343 |
def OnRemove(self, event): |
def OnRemove(self, event): |
344 |
i = self.DB_ListBox.GetSelection() |
i = self.DB_ListBox.GetSelection() |