File: dlgSelectConnection.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (382 lines) | stat: -rw-r--r-- 10,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgSelectConnection.cpp - Connect to a database
//
//////////////////////////////////////////////////////////////////////////

// App headers
#include "pgAdmin3.h"

// wxWindows headers
#include <wx/wx.h>
#include <wx/bmpcbox.h>

// App headers
#include "frm/frmMain.h"
#include "dlg/dlgSelectConnection.h"
#include "dlg/dlgConnect.h"
#include "schema/pgServer.h"
#include "images/connect.pngc"

#define CTRLID_CBSERVER 4242
#define CTRLID_CBDATABASE 4243
#define CTRLID_CBUSERNAME 4244
#define CTRLID_CBROLENAME 4245

BEGIN_EVENT_TABLE(dlgSelectConnection, DialogWithHelp)
	EVT_COMBOBOX(CTRLID_CBSERVER,      dlgSelectConnection::OnChangeServer)
	EVT_COMBOBOX(CTRLID_CBDATABASE,    dlgSelectConnection::OnChangeDatabase)
	EVT_TEXT(CTRLID_CBSERVER,          dlgSelectConnection::OnTextChange)
	EVT_TEXT(CTRLID_CBDATABASE,        dlgSelectConnection::OnTextChange)
	EVT_TEXT(CTRLID_CBUSERNAME,        dlgSelectConnection::OnTextChange)
	EVT_BUTTON (wxID_OK,               dlgSelectConnection::OnOK)
	EVT_BUTTON (wxID_CANCEL,           dlgSelectConnection::OnCancel)
END_EVENT_TABLE()

#define stUsername		CTRL_STATIC("stUsername")


dlgSelectConnection::dlgSelectConnection(wxWindow *parent, frmMain *form) :
	DialogWithHelp(form)
{
	long style = wxCB_DROPDOWN;
	remoteServer = NULL;

	SetFont(settings->GetSystemFont());
	LoadResource(parent, wxT("dlgSelectConnection"));

	SetIcon(*connect_png_ico);
	RestorePosition();

	if (form != NULL)
		style |= wxCB_READONLY;

	cbServer = new ctlComboBoxFix(this, CTRLID_CBSERVER, ConvertDialogToPixels(wxPoint(65, 5)), ConvertDialogToPixels(wxSize(135, 12)), style);
	cbDatabase = new wxComboBox(this, CTRLID_CBDATABASE, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 20)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
	cbUsername = new wxComboBox(this, CTRLID_CBUSERNAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 35)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);
	cbRolename = new wxComboBox(this, CTRLID_CBROLENAME, wxEmptyString, ConvertDialogToPixels(wxPoint(65, 50)), ConvertDialogToPixels(wxSize(135, 12)), wxArrayString(), style);

	if (form == NULL)
	{
		cbServer->SetValue(settings->Read(wxT("QuickConnect/server"), wxEmptyString));
		cbDatabase->SetValue(settings->Read(wxT("QuickConnect/database"), wxEmptyString));
		cbUsername->SetValue(settings->Read(wxT("QuickConnect/username"), wxEmptyString));
		cbRolename->SetValue(settings->Read(wxT("QuickConnect/rolename"), wxEmptyString));
	}

	btnOK->Enable(cbServer->GetValue().Length() > 0 && cbDatabase->GetValue().Length() > 0 && cbUsername->GetValue().Length() > 0);
}


dlgSelectConnection::~dlgSelectConnection()
{
	SavePosition();
}


wxString dlgSelectConnection::GetHelpPage() const
{
	return wxT("connect");
}


void dlgSelectConnection::OnChangeServer(wxCommandEvent &ev)
{
	int item;
	wxString olddatabase, oldusername;

	if (!GetServer())
		return;

	// Keep old value for these comboboxes so that we can restore them if needed
	olddatabase = cbDatabase->GetValue();
	oldusername = cbUsername->GetValue();

	// Clear the comboboxes
	cbDatabase->Clear();
	cbUsername->Clear();
	cbRolename->Clear();

	int sel = cbServer->GetCurrentSelection();
	if (sel >= 0)
	{
		remoteServer = (pgServer *)cbServer->wxItemContainer::GetClientData(sel);

		if (!remoteServer->GetConnected())
		{
			remoteServer->Connect(mainForm, remoteServer->GetStorePwd());
			if (!remoteServer->GetConnected())
			{
				wxLogError(wxT("%s"), remoteServer->GetLastError().c_str());
				return;
			}
		}
		if (remoteServer->GetConnected())
		{
			pgSetIterator set1(remoteServer->GetConnection(),
			                   wxT("SELECT DISTINCT datname\n")
			                   wxT("  FROM pg_database db\n")
			                   wxT(" WHERE datallowconn ORDER BY datname"));

			item = 0;
			while(set1.RowsLeft())
			{
				cbDatabase->Append(set1.GetVal(wxT("datname")));
				if (set1.GetVal(wxT("datname")) == olddatabase)
					item = cbDatabase->GetCount() - 1;
			}

			if (cbDatabase->GetCount())
				cbDatabase->SetSelection(item);

			pgSetIterator set2(remoteServer->GetConnection(),
			                   wxT("SELECT DISTINCT usename\n")
			                   wxT("FROM pg_user db\n")
			                   wxT("ORDER BY usename"));

			item = 0;
			while(set2.RowsLeft())
			{
				cbUsername->Append(set2.GetVal(wxT("usename")));
				if (set2.GetVal(wxT("usename")) == oldusername)
					item = cbDatabase->GetCount() - 1;
			}

			if (cbUsername->GetCount())
				cbUsername->SetSelection(item);

			if (remoteServer->GetConnection()->BackendMinimumVersion(8, 1))
			{
				pgSetIterator set3(remoteServer->GetConnection(),
				                   wxT("SELECT DISTINCT rolname\n")
				                   wxT("FROM pg_roles db\n")
				                   wxT("ORDER BY rolname"));

				cbRolename->Append(wxEmptyString);

				while(set3.RowsLeft())
					cbRolename->Append(set3.GetVal(wxT("rolname")));

				cbRolename->Enable(true);
			}
			else
				cbRolename->Disable();

			cbRolename->SetValue(wxEmptyString);
		}

	}
	OnChangeDatabase(ev);
}


wxString dlgSelectConnection::GetDatabase()
{
	return cbDatabase->GetValue();
}

wxString dlgSelectConnection::GetServerName()
{
	return cbServer->GetValue();
}

void dlgSelectConnection::OnChangeDatabase(wxCommandEvent &ev)
{
	btnOK->Enable(cbServer->GetValue().Length() > 0 && cbDatabase->GetValue().Length() > 0 && cbUsername->GetValue().Length() > 0);
}

void dlgSelectConnection::OnTextChange(wxCommandEvent &ev)
{
	btnOK->Enable(cbServer->GetValue().Length() > 0 && cbDatabase->GetValue().Length() > 0 && cbUsername->GetValue().Length() > 0);
}

void dlgSelectConnection::OnOK(wxCommandEvent &ev)
{
#ifdef __WXGTK__
	if (!btnOK->IsEnabled())
		return;
#endif

	if (cbDatabase->GetCurrentSelection() == wxNOT_FOUND ||
	        cbServer->GetCurrentSelection() == wxNOT_FOUND)
		remoteServer = NULL;

	EndModal(wxID_OK);
}


void dlgSelectConnection::OnCancel(wxCommandEvent &ev)
{
	EndModal(wxID_CANCEL);
}

pgConn *dlgSelectConnection::CreateConn(wxString &applicationname, bool &createdNew)
{
	/* gcc requires that we store this in temporary variables for some reason... */
	wxString serv = cbServer->GetValue();
	wxString db = cbDatabase->GetValue();

	createdNew = true;

	long port = 0;
	if (serv.Find(':') > 0)
	{
		if (!serv.Mid(serv.Find(':') + 1).ToLong(&port))
		{
			wxMessageBox(_("Invalid port number specified."));
			return NULL;
		}
		serv = serv.Mid(0, serv.Find(':'));
	}

	wxString user = cbUsername->GetValue();
	wxString role = cbRolename->GetValue();

	if (cbConnection)
	{
		/* Check if selected combination already exists */
		for (unsigned int index = 0; index < cbConnection->GetCount() - 1; index++)
		{
			pgConn *conn = (pgConn *)cbConnection->GetClientData(index);
			if (conn &&
			        conn->GetHost() == serv &&
			        conn->GetPort() == port &&
			        conn->GetUser() == user &&
			        conn->GetRole() == role &&
			        conn->GetDbname() == db)
			{
				createdNew = false;
				return conn;
			}
		}
	}


	int sslmode = remoteServer ? remoteServer->GetSSL() : 0;

	return CreateConn(serv, db, user, port, role, sslmode, applicationname, true);
}

pgConn *dlgSelectConnection::CreateConn(wxString &server, wxString &dbname, wxString &username, int port, wxString &rolename, int sslmode, wxString &applicationname, bool writeMRU)
{
	pgConn *newconn;
	newconn = new pgConn(server, wxEmptyString, wxEmptyString, dbname, username, wxT(""), port, rolename, sslmode, 0, applicationname);
	if (newconn->GetStatus() != PGCONN_OK &&
	        newconn->GetLastError().Cmp(wxString(PQnoPasswordSupplied, wxConvUTF8)) == 0)
	{
		/* Needs password */
		delete newconn;
		newconn = NULL;

		wxString txt;
		txt.Printf(_("Please enter password for user %s\non server %s"), username.c_str(), server.c_str());
		dlgConnect dlg(NULL, txt, false);
		if (dlg.Go() != wxID_OK)
			return NULL;

		newconn = new pgConn(server, wxEmptyString, wxEmptyString, dbname, username, dlg.GetPassword(), port, rolename, sslmode, 0, applicationname);
	}

	if (newconn)
	{
		if (newconn->GetStatus() != PGCONN_OK)
		{
			wxMessageBox(wxT("Connection failed: ") + newconn->GetLastError());
			return NULL;
		}

		if (writeMRU)
		{
			settings->Write(wxT("QuickConnect/server"), cbServer->GetValue());
			settings->Write(wxT("QuickConnect/database"), cbDatabase->GetValue());
			settings->Write(wxT("QuickConnect/username"), cbUsername->GetValue());
			settings->Write(wxT("QuickConnect/rolename"), cbRolename->GetValue());
		}
	}
	return newconn;
}

int dlgSelectConnection::Go(pgConn *conn, wxBitmapComboBox *cb)
{
	bool foundServer = false;
	cbConnection = cb;
	if (mainForm != NULL)
	{
		ctlTree *browser = mainForm->GetBrowser();
		wxTreeItemIdValue foldercookie, servercookie;
		wxTreeItemId folderitem, serveritem;
		pgObject *object;
		pgServer *server;

		folderitem = browser->GetFirstChild(browser->GetRootItem(), foldercookie);
		while (folderitem)
		{
			if (browser->ItemHasChildren(folderitem))
			{
				serveritem = browser->GetFirstChild(folderitem, servercookie);
				while (serveritem)
				{
					object = browser->GetObject(serveritem);
					if (object && object->IsCreatedBy(serverFactory))
					{
						server = (pgServer *)object;
						cbServer->Append(server->GetIdentifier(), (void *)server);
						if (server->GetConnected() &&
						        server->GetConnection()->GetHost() == conn->GetHost() &&
						        server->GetConnection()->GetPort() == conn->GetPort())
						{
							cbServer->SetSelection(cbServer->GetCount() - 1);
							remoteServer = server;
							foundServer = true;
						}
					}
					serveritem = browser->GetNextChild(folderitem, servercookie);
				}
			}
			folderitem = browser->GetNextChild(browser->GetRootItem(), foldercookie);
		}
	}

	cbServer->SetFocus();

	wxCommandEvent ev;
	OnChangeServer(ev);

	if (foundServer)
	{

		unsigned int index = 0;
		for (; index < cbDatabase->GetCount(); index++)
		{
			if (cbDatabase->GetString(index) == conn->GetDbname())
			{
				cbDatabase->SetSelection(index);
				break;
			}
		}
		for (index = 0; index < cbUsername->GetCount(); index++)
		{
			if (cbUsername->GetString(index) == conn->GetUser())
			{
				cbUsername->SetSelection(index);
				break;
			}
		}
		for (index = 0; index < cbRolename->GetCount(); index++)
		{
			if (cbRolename->GetString(index) == conn->GetRole())
			{
				cbRolename->SetSelection(index);
				break;
			}
		}
	}

	return ShowModal();
}