File: dlgServer.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 (744 lines) | stat: -rw-r--r-- 24,200 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// dlgServer.cpp - PostgreSQL Database Property
//
//////////////////////////////////////////////////////////////////////////

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

// App headers
#include "pgAdmin3.h"

// Must be after pgAdmin3.h or MSVC++ complains
#include <wx/colordlg.h>
#include <wx/clrpicker.h>
#include <wx/filepicker.h>

// Other app headers
#include "utils/misc.h"
#include "frm/frmMain.h"
#include "frm/frmHint.h"
#include "dlg/dlgServer.h"
#include "schema/pgDatabase.h"

// pointer to controls
#define txtHostAddr       CTRL_TEXT("txtHostAddr")
#define txtDescription    CTRL_TEXT("txtDescription")
#define txtService        CTRL_TEXT("txtService")
#define txtServiceID      CTRL_TEXT("txtServiceID")
#define cbDatabase        CTRL_COMBOBOX("cbDatabase")
#define txtPort           CTRL_TEXT("txtPort")
#define cbSSL             CTRL_COMBOBOX("cbSSL")
#define txtUsername       CTRL_TEXT("txtUsername")
#define stTryConnect      CTRL_STATIC("stTryConnect")
#define chkTryConnect     CTRL_CHECKBOX("chkTryConnect")
#define stStorePwd        CTRL_STATIC("stStorePwd")
#define chkStorePwd       CTRL_CHECKBOX("chkStorePwd")
#define txtRolename       CTRL_TEXT("txtRolename")
#define stRestore         CTRL_STATIC("stRestore")
#define chkRestore        CTRL_CHECKBOX("chkRestore")
#define stPassword        CTRL_STATIC("stPassword")
#define txtPassword       CTRL_TEXT("txtPassword")
#define txtDbRestriction  CTRL_TEXT("txtDbRestriction")
#define colourPicker      CTRL_COLOURPICKER("colourPicker")
#define cbGroup           CTRL_COMBOBOX("cbGroup")
#define pickerSSLCert     CTRL_FILEPICKER("pickerSSLCert")
#define pickerSSLKey      CTRL_FILEPICKER("pickerSSLKey")
#define pickerSSLRootCert CTRL_FILEPICKER("pickerSSLRootCert")
#define pickerSSLCrl      CTRL_FILEPICKER("pickerSSLCrl")
#define chkSSLCompression CTRL_CHECKBOX("chkSSLCompression")
#define nbNotebook        CTRL_NOTEBOOK("nbNotebook")

// SSH Tunnel Tab
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
#define chkSSHTunnel		 CTRL_CHECKBOX("chkSSHTunnel")
#define txtTunnelHost		 CTRL_TEXT("txtTunnelHost")
#define txtTunnelUsername	 CTRL_TEXT("txtTunnelUsername")
#define radioBtnPassword	 CTRL_RADIOBUTTON("radioBtnPassword")
#define radioBtnKeyfile      CTRL_RADIOBUTTON("radioBtnKeyfile")
#define txtTunnelPassword	 CTRL_TEXT("txtTunnelPassword")
#define pickerPublicKeyFile	 CTRL_FILEPICKER("pickerPublicKeyFile")
#define pickerIdentityFile	 CTRL_FILEPICKER("pickerIdentityFile")
#define stPublicKeyFile		 CTRL_STATIC("stPublicKeyFile")
#define txtTunnelPort		 CTRL_TEXT("txtTunnelPort")
#endif

BEGIN_EVENT_TABLE(dlgServer, dlgProperty)
	EVT_NOTEBOOK_PAGE_CHANGED(XRCID("nbNotebook"),     dlgServer::OnPageSelect)
	EVT_TEXT(XRCID("txtHostAddr"),                     dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtDescription"),                  dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtService"),                      dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtServiceID"),                    dlgProperty::OnChange)
	EVT_TEXT(XRCID("cbDatabase"),                      dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbDatabase"),                  dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtPort")  ,                       dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtUsername"),                     dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtRolename"),                     dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtDbRestriction"),                dlgServer::OnChangeRestr)
	EVT_COMBOBOX(XRCID("cbSSL"),                       dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkStorePwd"),                 dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkRestore"),                  dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkTryConnect"),               dlgServer::OnChangeTryConnect)
	EVT_COLOURPICKER_CHANGED(XRCID("colourPicker"),    dlgServer::OnChangeColour)
	EVT_FILEPICKER_CHANGED(XRCID("pickerSSLCert"),     dlgServer::OnChangeFile)
	EVT_FILEPICKER_CHANGED(XRCID("pickerSSLKey"),      dlgServer::OnChangeFile)
	EVT_FILEPICKER_CHANGED(XRCID("pickerSSLRootCert"), dlgServer::OnChangeFile)
	EVT_FILEPICKER_CHANGED(XRCID("pickerSSLCrl"),      dlgServer::OnChangeFile)
	EVT_TEXT(XRCID("cbGroup"),                         dlgProperty::OnChange)
	EVT_COMBOBOX(XRCID("cbGroup"),                     dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkSSLCompression"),           dlgProperty::OnChange)
	EVT_BUTTON(wxID_OK,                                dlgServer::OnOK)
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
	EVT_TEXT(XRCID("txtTunnelHost"),                   dlgProperty::OnChange)
	EVT_TEXT(XRCID("txtTunnelUsername"),               dlgProperty::OnChange)
	EVT_CHECKBOX(XRCID("chkSSHTunnel"),                dlgServer::OnCheckSSHTunnel)
	EVT_RADIOBUTTON(XRCID("radioBtnPassword"),         dlgServer::OnChangeAuthOption)
	EVT_RADIOBUTTON(XRCID("radioBtnKeyfile"),          dlgServer::OnChangeAuthOption)
	EVT_TEXT(XRCID("txtTunnelPort"),                   dlgProperty::OnChange)
#endif
END_EVENT_TABLE();


dlgProperty *pgServerFactory::CreateDialog(frmMain *frame, pgObject *node, pgObject *parent)
{
	return new dlgServer(this, frame, (pgServer *)node);
}


dlgServer::dlgServer(pgaFactory *f, frmMain *frame, pgServer *node)
	: dlgProperty(f, frame, wxT("dlgServer"))
{
	server = node;
	dbRestrictionOk = true;

	cbDatabase->Append(wxT("postgres"));
	cbDatabase->Append(wxT("edb"));
	cbDatabase->Append(wxT("template1"));
	wxString lastDB = settings->GetLastDatabase();
	if (lastDB != wxT("postgres") && lastDB != wxT("edb") && lastDB != wxT("template1"))
		cbDatabase->Append(lastDB);
	cbDatabase->SetSelection(0);

	txtPort->SetValue(NumToStr((long)settings->GetLastPort()));
	if (!cbSSL->GetValue().IsEmpty())
		cbSSL->SetSelection(settings->GetLastSSL());
	txtUsername->SetValue(settings->GetLastUsername());

	chkTryConnect->SetValue(true);
	chkStorePwd->SetValue(true);
	chkRestore->SetValue(true);
	if (node)
	{
		chkTryConnect->SetValue(false);
		chkTryConnect->Disable();
	}

	// Fill the group combobox
	cbGroup->Append(_("Servers"));
	ctlTree *browser = frame->GetBrowser();
	wxTreeItemId groupitem;
	wxTreeItemIdValue groupcookie, servercookie;
	pgServer *firstserver;
	if (browser->ItemHasChildren(browser->GetRootItem()))
	{
		groupitem = browser->GetFirstChild(browser->GetRootItem(), groupcookie);
		while (groupitem)
		{
			firstserver = (pgServer *)browser->GetObject(browser->GetFirstChild(groupitem, servercookie));
			if (firstserver && !firstserver->GetGroup().IsEmpty() && firstserver->GetGroup() != _("Servers"))
				cbGroup->Append(firstserver->GetGroup());
			groupitem = browser->GetNextChild(browser->GetRootItem(), groupcookie);
		}
	}

#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
	EnableSSHTunnelControls(false);
	radioBtnPassword->SetValue(true);
	radioBtnKeyfile->SetValue(false);
	txtTunnelPassword->SetMaxLength(SSH_MAX_PASSWORD_LEN);
	txtTunnelPort->SetValue(NumToStr((long)DEFAULT_SSH_PORT));
#ifdef HAVE_OPENSSL_CRYPTO
	stPublicKeyFile->Show(false);
	pickerPublicKeyFile->Show(false);
#endif
#else
	for(size_t i = 0; i < nbNotebook->GetPageCount(); i++)
	{
		if(nbNotebook->GetPageText(i).compare(wxT("SSH Tunnel")) == 0)
		{
			nbNotebook->RemovePage(i);
		}
	}
#endif
}


dlgServer::~dlgServer()
{
	if (!server)
	{
		settings->SetLastDatabase(cbDatabase->GetValue());
		settings->SetLastPort(StrToLong(txtPort->GetValue()));
		settings->SetLastSSL(cbSSL->GetCurrentSelection());
		settings->SetLastUsername(txtUsername->GetValue());
	}
}


pgObject *dlgServer::GetObject()
{
	return server;
}


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

	// Display the 'save password' hint if required
	if(chkStorePwd->GetValue())
	{
		if (frmHint::ShowHint(this, HINT_SAVING_PASSWORDS) == wxID_CANCEL)
			return;
	}

	// notice: changes active after reconnect
	EnableOK(false);

	if (server)
	{
		server->iSetName(GetName());
		server->iSetHostAddr(txtHostAddr->GetValue());
		server->iSetDescription(txtDescription->GetValue());
		server->iSetService(txtService->GetValue());
		if (txtServiceID->GetValue() != server->GetServiceID())
		{
			mainForm->StartMsg(_("Checking server status"));
			server->iSetServiceID(txtServiceID->GetValue());
			mainForm->EndMsg();
		}
		server->iSetPort(StrToLong(txtPort->GetValue()));
		server->iSetSSL(cbSSL->GetCurrentSelection());
		server->iSetDatabase(cbDatabase->GetValue());
		server->iSetUsername(txtUsername->GetValue());
		server->iSetRolename(txtRolename->GetValue());
		server->iSetStorePwd(chkStorePwd->GetValue());
		server->iSetRestore(chkRestore->GetValue());
		server->iSetDbRestriction(txtDbRestriction->GetValue().Trim());
		server->SetSSLCert(pickerSSLCert->GetPath());
		server->SetSSLKey(pickerSSLKey->GetPath());
		server->SetSSLRootCert(pickerSSLRootCert->GetPath());
		server->SetSSLCrl(pickerSSLCrl->GetPath());
		server->iSetSSLCompression(chkSSLCompression->GetValue());
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
		server->iSetSSHTunnel(chkSSHTunnel->GetValue());
		server->iSetTunnelPort(StrToLong(txtTunnelPort->GetValue()));
		server->SetTunnelHost(txtTunnelHost->GetValue());
		server->SetTunnelUserName(txtTunnelUsername->GetValue());
		server->iSetAuthModePwd(radioBtnPassword->GetValue());
		server->SetTunnelPassword(txtTunnelPassword->GetValue());
		server->SetPublicKeyFile(pickerPublicKeyFile->GetPath());
		server->SetIdentityFile(pickerIdentityFile->GetPath());
#endif
		wxColour colour = colourPicker->GetColour();
		wxString sColour = colour.GetAsString(wxC2S_HTML_SYNTAX);
		server->iSetColour(sColour);
		if (cbGroup->GetValue().IsEmpty())
			cbGroup->SetValue(_("Servers"));
		if (server->GetGroup() != cbGroup->GetValue())
		{
			ctlTree *browser = mainForm->GetBrowser();
			wxTreeItemId oldgroupitem, groupitem, serveritem;
			wxTreeItemIdValue groupcookie;
			bool found;
			int total;
			wxString label, oldgroupname;

			// Duplicate server object
			pgServer *newserver = new pgServer(
			    server->GetName(),
			    server->GetHostAddr(),
			    server->GetDescription(),
			    server->GetService(),
			    server->GetDatabaseName(),
			    server->GetUsername(),
			    server->GetPort(),
			    server->GetStorePwd(),
			    server->GetRolename(),
			    server->GetRestore(),
			    server->GetSSL(),
			    server->GetColour(),
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
			    server->GetGroup(),
			    server->GetSSHTunnel(),
			    server->GetTunnelHost(),
			    server->GetTunnelUserName(),
			    server->GetAuthModePwd(),
			    server->GetTunnelPassword(),
			    server->GetPublicKeyFile(),
			    server->GetIdentityFile(),
			    server->GetTunnelPort());
#else
			    server->GetGroup());
#endif
			newserver->iSetDbRestriction(server->GetDbRestriction().Trim());
			newserver->iSetServiceID(server->GetServiceID().Trim());
			newserver->iSetDiscoveryID(server->GetDiscoveryID().Trim());
			newserver->SetSSLCert(server->GetSSLCert());
			newserver->SetSSLKey(server->GetSSLKey());
			newserver->SetSSLRootCert(server->GetSSLRootCert());
			newserver->SetSSLCrl(server->GetSSLCrl());
			newserver->iSetSSLCompression(server->GetSSLCompression());

			// Drop the old item
			// (will also take care of dropping the pgServer item)
			oldgroupitem = browser->GetItemParent(item);
			oldgroupname = server->GetGroup();
			if (oldgroupname.IsEmpty())
				oldgroupname = _("Servers");
			browser->Delete(item);

			// Move the item
			found = false;
			if (browser->ItemHasChildren(browser->GetRootItem()))
			{
				groupitem = browser->GetFirstChild(browser->GetRootItem(), groupcookie);
				while (!found && groupitem)
				{
					if (browser->GetItemText(groupitem).StartsWith(cbGroup->GetValue()))
						found = true;
					else
						groupitem = browser->GetNextChild(browser->GetRootItem(), groupcookie);
				}
			}

			if (!found)
			{
				groupitem = browser->AppendItem(browser->GetRootItem(), cbGroup->GetValue(), browser->GetItemImage(browser->GetRootItem()));
				browser->SortChildren(browser->GetRootItem());
			}

			serveritem = browser->AppendItem(groupitem, newserver->GetFullName(), newserver->GetIconId(), -1, newserver);
			browser->SortChildren(groupitem);
			browser->Expand(groupitem);
			browser->SelectItem(serveritem);

			// Count the number of items in the old group item
			total = browser->GetChildrenCount(oldgroupitem, false);
			if (total == 0)
				browser->Delete(oldgroupitem);
			else
			{
				label = oldgroupname + wxT(" (") + NumToStr((long)total) + wxT(")");
				browser->SetItemText(oldgroupitem, label);
			}

			// Count the number of items in the new group item
			total = browser->GetChildrenCount(groupitem, false);
			label = cbGroup->GetValue() + wxT(" (") + NumToStr((long)total) + wxT(")");
			browser->SetItemText(groupitem, label);

			// Re-initialize old variables to have their new meanings
			server = newserver;
			item = serveritem;
		}
		server->iSetGroup(cbGroup->GetValue());

		mainForm->execSelChange(server->GetId(), true);
		mainForm->GetBrowser()->SetItemText(item, server->GetFullName());
		mainForm->SetItemBackgroundColour(item, wxColour(server->GetColour()));
		mainForm->StoreServers();
	}

	if (IsModal())
	{
		EndModal(wxID_OK);
		return;
	}
	else
		Destroy();
}


void dlgServer::OnChangeColour(wxColourPickerEvent &ev)
{
	dlgProperty::OnChange(ev);
}


void dlgServer::OnChangeFile(wxFileDirPickerEvent &ev)
{
	dlgProperty::OnChange(ev);
}


void dlgServer::OnChangeRestr(wxCommandEvent &ev)
{
	if (!connection || txtDbRestriction->GetValue().IsEmpty())
		dbRestrictionOk = true;
	else
	{
		wxString sql = wxT("EXPLAIN SELECT 1 FROM pg_database\n")
		               wxT("WHERE datname IN (") + txtDbRestriction->GetValue() + wxT(")");

		wxLogNull nix;
		wxString result = connection->ExecuteScalar(sql);

		dbRestrictionOk = !result.IsEmpty();
	}
	dlgProperty::OnChange(ev);
}


void dlgServer::OnPageSelect(wxNotebookEvent &event)
{
	// to prevent dlgProperty from catching it
}


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


int dlgServer::GoNew()
{
	if (cbSSL->GetValue().IsEmpty())
		return Go(true);
	else
	{
		CheckChange();
		return ShowModal();
	}
}


int dlgServer::Go(bool modal)
{
	chkSSLCompression->Disable();

	cbSSL->Clear();
	cbSSL->Append(wxT(" "));

#ifdef PG_SSL
	cbSSL->Append(_("require"));
	cbSSL->Append(_("prefer"));

	if (pgConn::GetLibpqVersion() > 7.3)
	{
		cbSSL->Append(_("allow"));
		cbSSL->Append(_("disable"));
	}

	if (pgConn::GetLibpqVersion() >= 8.4)
	{
		cbSSL->Append(_("verify-ca"));
		cbSSL->Append(_("verify-full"));
	}

	if (pgConn::GetLibpqVersion() >= 9.2)
	{
		chkSSLCompression->Enable();
	}
#endif

	if (server)
	{
		if (cbDatabase->FindString(server->GetDatabaseName()) < 0)
			cbDatabase->Append(server->GetDatabaseName());
		txtHostAddr->SetValue(server->GetHostAddr());
		txtDescription->SetValue(server->GetDescription());
		txtService->SetValue(server->GetService());
		txtServiceID->SetValue(server->GetServiceID());
		txtPort->SetValue(NumToStr((long)server->GetPort()));
		cbSSL->SetSelection(server->GetSSL());
		cbDatabase->SetValue(server->GetDatabaseName());
		txtUsername->SetValue(server->GetUsername());
		chkStorePwd->SetValue(server->GetStorePwd());
		txtRolename->SetValue(server->GetRolename());
		chkRestore->SetValue(server->GetRestore());
		txtDbRestriction->SetValue(server->GetDbRestriction());
		colourPicker->SetColour(server->GetColour());
		cbGroup->SetValue(server->GetGroup());

		pickerSSLCert->SetPath(server->GetSSLCert());
		pickerSSLKey->SetPath(server->GetSSLKey());
		pickerSSLRootCert->SetPath(server->GetSSLRootCert());
		pickerSSLCrl->SetPath(server->GetSSLCrl());

		chkSSLCompression->SetValue(server->GetSSLCompression());

#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
		chkSSHTunnel->SetValue(server->GetSSHTunnel());
		if(server->GetSSHTunnel())
		{
			chkStorePwd->SetValue(false);
			chkStorePwd->Enable(false);
		}
		txtTunnelPort->SetValue(NumToStr((long)server->GetTunnelPort()));
		txtTunnelHost->SetValue(server->GetTunnelHost());
		txtTunnelUsername->SetValue(server->GetTunnelUserName());
		if (server->GetAuthModePwd())
		{
			radioBtnPassword->SetValue(true);
			radioBtnKeyfile->SetValue(false);
		}
		else
		{
			radioBtnPassword->SetValue(false);
			radioBtnKeyfile->SetValue(true);
		}
		txtTunnelPassword->SetValue(server->GetTunnelPassword());
		pickerPublicKeyFile->SetPath(server->GetPublicKeyFile());
		pickerIdentityFile->SetPath(server->GetIdentityFile());
#endif
		stPassword->Disable();
		txtPassword->Disable();
		if (connection)
		{
			txtHostAddr->Disable();
			txtDescription->Disable();
			txtService->Disable();
			txtServiceID->Disable();
			txtName->Disable();
			cbDatabase->Disable();
			txtPort->Disable();
			cbSSL->Disable();
			txtUsername->Disable();
			chkStorePwd->Disable();
			txtRolename->Disable();
			chkRestore->Disable();
			txtDbRestriction->Disable();
			colourPicker->Disable();
			cbGroup->Disable();
			pickerSSLCert->Disable();
			pickerSSLKey->Disable();
			pickerSSLRootCert->Disable();
			pickerSSLCrl->Disable();
			chkSSLCompression->Disable();
			EnableOK(false);
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
			chkSSHTunnel->Enable(false);
			EnableSSHTunnelControls(false);
		}
		else
		{
			if (server->GetSSHTunnel())
			{
				chkSSHTunnel->Enable(true);
				EnableSSHTunnelControls(true);
				EnableAuthenticationOptions();
			}
#endif
		}
	}
	else
	{
		SetTitle(_("Add server"));
		cbGroup->SetValue(_("Servers"));
		wxString colour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW).GetAsString(wxC2S_HTML_SYNTAX);
		colourPicker->SetColour(colour);
	}

	return dlgProperty::Go(modal);
}


bool dlgServer::GetTryConnect()
{
	return chkTryConnect->GetValue();
}


wxString dlgServer::GetPassword()
{
	return txtPassword->GetValue();
}


pgObject *dlgServer::CreateObject(pgCollection *collection)
{
	wxString name = GetName();
	pgServer *obj = NULL;

#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
	if (chkSSHTunnel->GetValue())
	{
		obj = new pgServer(GetName(), txtHostAddr->GetValue(), txtDescription->GetValue(),
		                   txtService->GetValue(), cbDatabase->GetValue(),
		                   txtUsername->GetValue(), StrToLong(txtPort->GetValue()),
		                   chkTryConnect->GetValue() && chkStorePwd->GetValue(),
		                   txtRolename->GetValue(), chkRestore->GetValue(), cbSSL->GetCurrentSelection(),
		                   colourPicker->GetColourString(), cbGroup->GetValue(),
		                   chkSSHTunnel->GetValue(), txtTunnelHost->GetValue(), txtTunnelUsername->GetValue(),
		                   radioBtnPassword->GetValue(),
		                   txtTunnelPassword->GetValue(), pickerPublicKeyFile->GetPath(),
		                   pickerIdentityFile->GetPath(), StrToLong(txtTunnelPort->GetValue()));
	}
	else
#endif
	{
		obj = new pgServer(GetName(), txtHostAddr->GetValue(), txtDescription->GetValue(),
		                   txtService->GetValue(), cbDatabase->GetValue(),
		                   txtUsername->GetValue(), StrToLong(txtPort->GetValue()),
		                   chkTryConnect->GetValue() && chkStorePwd->GetValue(),
		                   txtRolename->GetValue(), chkRestore->GetValue(), cbSSL->GetCurrentSelection(),
		                   colourPicker->GetColourString(), cbGroup->GetValue());
	}

	obj->iSetDbRestriction(txtDbRestriction->GetValue().Trim());
	obj->iSetServiceID(txtServiceID->GetValue());
	obj->SetSSLCert(pickerSSLCert->GetTextCtrlValue());
	obj->SetSSLKey(pickerSSLKey->GetTextCtrlValue());
	obj->SetSSLRootCert(pickerSSLRootCert->GetTextCtrlValue());
	obj->SetSSLCrl(pickerSSLCrl->GetTextCtrlValue());
	obj->iSetSSLCompression(chkSSLCompression->GetValue());

	return obj;
}


void dlgServer::OnChangeTryConnect(wxCommandEvent &ev)
{
	chkStorePwd->Enable(chkTryConnect->GetValue() && !connection);
	txtPassword->Enable(chkTryConnect->GetValue() && !connection);
	OnChange(ev);
}


void dlgServer::CheckChange()
{
	wxString name = GetName();
	bool enable = true;

	if (server)
	{
		// Get old value
		wxColour colour;
		wxString sColour = wxEmptyString;

		if (colour.Set(server->GetColour()))
			sColour = colour.GetAsString(wxC2S_HTML_SYNTAX);

		// Get new value
		wxString sColour2 = colourPicker->GetColourString();

		enable =  name != server->GetName()
		          || txtHostAddr->GetValue() != server->GetHostAddr()
		          || txtDescription->GetValue() != server->GetDescription()
		          || txtService->GetValue() != server->GetService()
		          || txtServiceID->GetValue() != server->GetServiceID()
		          || StrToLong(txtPort->GetValue()) != server->GetPort()
		          || cbDatabase->GetValue() != server->GetDatabaseName()
		          || txtUsername->GetValue() != server->GetUsername()
		          || cbSSL->GetCurrentSelection() != server->GetSSL()
		          || chkStorePwd->GetValue() != server->GetStorePwd()
		          || txtRolename->GetValue() != server->GetRolename()
		          || chkRestore->GetValue() != server->GetRestore()
		          || txtDbRestriction->GetValue() != server->GetDbRestriction()
		          || sColour != sColour2
		          || cbGroup->GetValue() != server->GetGroup()
		          || pickerSSLCert->GetPath() != server->GetSSLCert()
		          || pickerSSLKey->GetPath() != server->GetSSLKey()
		          || pickerSSLRootCert->GetPath() != server->GetSSLRootCert()
		          || pickerSSLCrl->GetPath() != server->GetSSLCrl()
		          || chkSSLCompression->GetValue() != server->GetSSLCompression();
	}

#ifdef __WXMSW__
	CheckValid(enable, !name.IsEmpty(), _("Please specify address."));
#else
	bool isPipe = (name.IsEmpty() || name.StartsWith(wxT("/")));
	cbSSL->Enable(!isPipe && !connection);
#endif
	CheckValid(enable, !txtDescription->GetValue().IsEmpty(), _("Please specify description."));
	if (txtService->GetValue().IsEmpty())
	{
		CheckValid(enable, StrToLong(txtPort->GetValue()) > 0, _("Please specify port."));
		CheckValid(enable, !txtUsername->GetValue().IsEmpty(), _("Please specify user name"));
	}
	CheckValid(enable, dbRestrictionOk, _("Restriction not valid."));

#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
	if(chkSSHTunnel->GetValue())
	{
		CheckValid(enable, !txtTunnelHost->GetValue().IsEmpty(), _("Please specify ssh tunnel host."));
		CheckValid(enable, !txtTunnelPort->GetValue().IsEmpty(), _("Please specify ssh tunnel port."));
		CheckValid(enable, !txtTunnelUsername->GetValue().IsEmpty(), _("Please specify ssh tunnel user name."));
	}
#endif

	EnableOK(enable && !connection);
}


wxString dlgServer::GetSql()
{
	return wxEmptyString;
}

#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)

void dlgServer::OnCheckSSHTunnel(wxCommandEvent &ev)
{
	if (chkSSHTunnel->IsChecked())
	{
		if(chkStorePwd->GetValue())
		{
			wxMessageBox(_("Database passwords cannot be stored when using SSH tunnelling. The 'Store password' option has been turned off."), _("Stored Password"), wxICON_EXCLAMATION | wxOK, this);
		}
		chkStorePwd->SetValue(false);
		chkStorePwd->Enable(false);
		EnableSSHTunnelControls(true);
		EnableAuthenticationOptions();
	}
	else
	{
		chkStorePwd->Enable();
		EnableSSHTunnelControls(false);
	}

	CheckChange();
}

void dlgServer::OnChangeAuthOption(wxCommandEvent &ev)
{
	EnableAuthenticationOptions();
}

void dlgServer::EnableSSHTunnelControls(const bool &bEnable)
{
	txtTunnelHost->Enable(bEnable);
	txtTunnelUsername->Enable(bEnable);
	pickerPublicKeyFile->Enable(bEnable);
	pickerIdentityFile->Enable(bEnable);
	radioBtnPassword->Enable(bEnable);
	radioBtnKeyfile->Enable(bEnable);
	txtTunnelPassword->Enable(bEnable);
	txtTunnelPort->Enable(bEnable);
}

void dlgServer::EnableAuthenticationOptions()
{
	if (radioBtnPassword->GetValue())
	{
		pickerIdentityFile->Enable(false);
		pickerPublicKeyFile->Enable(false);
	}
	else
	{
		pickerIdentityFile->Enable(true);
		pickerPublicKeyFile->Enable(true);
	}
}
#endif