This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
/
frmConfig.cpp
451 lines (360 loc) · 12.6 KB
/
frmConfig.cpp
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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID: $Id$
// Copyright (C) 2002 - 2010, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// frmConfig.cpp - Configuration tool
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
#ifdef __WXMSW__
#include <io.h>
#include <fcntl.h>
#endif
#include <wx/imaglist.h>
#include "ctl/ctlMenuToolbar.h"
#include "frm/frmConfig.h"
#include "frm/frmHint.h"
#include "frm/frmMainConfig.h"
#include "frm/frmHbaConfig.h"
#include "frm/frmPgpassConfig.h"
#include "frm/frmMain.h"
#include "frm/frmAbout.h"
#include "utils/utffile.h"
#include "db/pgConn.h"
#include "db/pgSet.h"
#include "frm/menu.h"
#include "utils/pgfeatures.h"
#include "images/file_open.xpm"
#include "images/file_save.xpm"
#include "images/edit_undo.xpm"
#include "images/delete.xpm"
#include "images/help.xpm"
#include "images/hint2.xpm"
#include "images/checked.xpm"
#include "images/unchecked.xpm"
#include "images/query_execute.xpm"
BEGIN_EVENT_TABLE(frmConfig, pgFrame)
EVT_CLOSE( frmConfig::OnClose)
EVT_MENU(MNU_OPEN, frmConfig::OnOpen)
EVT_MENU(MNU_SAVE, frmConfig::OnSave)
EVT_MENU(MNU_SAVEAS, frmConfig::OnSaveAs)
EVT_MENU(MNU_EXECUTE, frmConfig::OnExecute)
EVT_MENU(MNU_HELP, frmConfig::OnHelp)
EVT_MENU(MNU_HINT, frmConfig::OnHint)
END_EVENT_TABLE()
wxImageList *configImageList=0;
frmConfig::frmConfig(frmMain *parent, const wxString& title, pgConn *_conn)
: pgFrame(parent, title)
{
mainForm = parent;
conn=_conn;
SetStatusBarPane(-1);
}
frmConfig::frmConfig(const wxString& title, const wxString &configFile)
: pgFrame(NULL, title)
{
mainForm = 0;
conn=0;
lastPath = configFile;
SetStatusBarPane(-1);
}
frmConfig::~frmConfig()
{
if (conn)
delete conn;
if (mainForm)
mainForm->RemoveFrame(this);
}
void frmConfig::InitFrame(const wxChar *frameName)
{
dlgName = frameName;
if (!configImageList)
{
configImageList = new wxImageList(16, 16, true, 2);
configImageList->Add(wxIcon(unchecked_xpm));
configImageList->Add(wxIcon(checked_xpm));
}
menuFactories = new menuFactoryList();
wxWindowBase::SetFont(settings->GetSystemFont());
toolBar = new ctlMenuToolbar(this, -1, wxDefaultPosition, wxDefaultSize, wxTB_FLAT | wxTB_NODIVIDER);
toolBar->SetToolBitmapSize(wxSize(16, 16));
fileMenu = new wxMenu();
editMenu = new wxMenu();
helpMenu=new wxMenu();
recentFileMenu = new wxMenu();
fileMenu->Append(MNU_OPEN, _("&Open...\tCtrl-O"), _("Open a query file"));
toolBar->AddTool(MNU_OPEN, _("Open"), wxBitmap(file_open_xpm), _("Open file"), wxITEM_NORMAL);
fileMenu->Append(MNU_SAVE, _("&Save\tCtrl-S"), _("Save current file"));
toolBar->AddTool(MNU_SAVE, _("Save"), wxBitmap(file_save_xpm), _("Save file"), wxITEM_NORMAL);
fileMenu->Append(MNU_SAVEAS, _("Save &as..."), _("Save file under new name"));
fileMenu->AppendSeparator();
toolBar->AddSeparator();
// File Menu
fileMenu->Append(MNU_EXECUTE, _("Reload server"), _("Reload Server to apply configuration changes."));
toolBar->AddTool(MNU_EXECUTE, _("Reload server"), wxBitmap(query_execute_xpm), _("Reload Server to apply configuration changes."), wxITEM_NORMAL);
fileMenu->AppendSeparator();
toolBar->AddSeparator();
fileMenu->Append(MNU_RECENT, _("&Recent files"), recentFileMenu);
fileMenu->Append(MNU_EXIT, _("E&xit\tCtrl-W"), _("Exit configuration tool"));
editMenu->Append(MNU_UNDO, _("&Undo\tCtrl-Z"), _("Undo last action"), wxITEM_NORMAL);
toolBar->AddTool(MNU_UNDO, _("Undo"), wxBitmap(edit_undo_xpm), _("Undo last action"), wxITEM_NORMAL);
editMenu->AppendSeparator();
editMenu->Append(MNU_DELETE, _("&Delete\tDEL"), _("Delete current row"), wxITEM_NORMAL);
toolBar->AddTool(MNU_DELETE, _("Delete"), wxBitmap(delete_xpm), _("Delete current row"), wxITEM_NORMAL);
toolBar->AddSeparator();
helpMenu->Append(MNU_CONTENTS, _("&Help"), _("Open the helpfile."));
helpMenu->Append(MNU_HINT, _("Hints"), _("Display helpful hints on current object."));
toolBar->AddTool(MNU_HINT, _("Hints"), wxBitmap(hint2_xpm), _("Display helpful hints on current object."));
helpMenu->Append(MNU_HELP, _("&Configuration Help\tF1"), _("Display help on configuration options."));
helpMenu->AppendSeparator();
new bugReportFactory(menuFactories, helpMenu, 0);
aboutFactory *af=new aboutFactory(menuFactories, helpMenu, 0);
menuBar = new wxMenuBar();
menuBar->Append(fileMenu, _("&File"));
menuBar->Append(editMenu, _("&Edit"));
menuBar->Append(helpMenu, _("&Help"));
SetMenuBar(menuBar);
#ifdef __WXMAC__
wxApp::s_macAboutMenuItemId = af->GetId();
#else
(void)af;
#endif
menuFactories->RegisterMenu(this, wxCommandEventHandler(pgFrame::OnAction));
menuFactories->CheckMenu(0, menuBar, toolBar);
wxAcceleratorEntry entries[3];
entries[0].Set(wxACCEL_CTRL, (int)'O', MNU_OPEN);
entries[1].Set(wxACCEL_CTRL, (int)'S', MNU_SAVE);
entries[2].Set(wxACCEL_NORMAL, WXK_F1, MNU_HELP);
wxAcceleratorTable accel(3, entries);
SetAcceleratorTable(accel);
toolBar->AddTool(MNU_HELP, _("Help"), wxBitmap(help_xpm), _("Display help on configuration options."), wxITEM_NORMAL);
toolBar->Realize();
UpdateRecentFiles();
fileMenu->Enable(MNU_SAVE, false);
editMenu->Enable(MNU_UNDO, false);
toolBar->EnableTool(MNU_SAVE, false);
toolBar->EnableTool(MNU_UNDO, false);
if (conn)
{
toolBar->EnableTool(MNU_OPEN, false);
if (!conn->HasFeature(FEATURE_RELOAD_CONF))
{
fileMenu->Enable(MNU_EXECUTE, false);
toolBar->EnableTool(MNU_EXECUTE, false);
}
}
else
{
fileMenu->Enable(MNU_EXECUTE, false);
toolBar->EnableTool(MNU_EXECUTE, false);
}
SetToolBar(toolBar);
statusBar = CreateStatusBar();
}
void frmConfig::Go()
{
Show();
}
void frmConfig::DoOpen(const wxString &fn)
{
wxCommandEvent ev;
OnOpen(ev);
}
void frmConfig::OnClose(wxCloseEvent& event)
{
if (CheckChanged(event.CanVeto()) && event.CanVeto())
{
event.Veto();
return;
}
Destroy();
}
void frmConfig::OnHelp(wxCommandEvent& event)
{
DisplayHelp(GetHelpPage(), HELP_POSTGRESQL);
}
void frmConfig::OnHint(wxCommandEvent& event)
{
DisplayHint(true);
}
void frmConfig::OnBugreport(wxCommandEvent& event)
{
DisplayHelp(wxT("bugreport"), HELP_PGADMIN);
}
void frmConfig::OnExecute(wxCommandEvent& event)
{
wxMessageDialog dlg(this, _("Are you sure you want trigger the server to reload its configuration?"),
_("Reload server configuration"), wxICON_EXCLAMATION | wxYES_NO |wxNO_DEFAULT);
if (dlg.ShowModal() == wxID_YES)
{
if (conn->ExecuteVoid(wxT("SELECT pg_reload_conf()")))
{
}
}
}
void frmConfig::OnOpen(wxCommandEvent& event)
{
if (CheckChanged(true))
return;
#ifdef __WXMSW__
wxFileDialog dlg(this, _("Open configuration file"), lastDir, wxT(""),
_("Configuration files (*.conf)|*.conf|All files (*.*)|*.*"), wxFD_OPEN);
#else
wxFileDialog dlg(this, _("Open configuration file"), lastDir, wxT(""),
_("Configuration files (*.conf)|*.conf|All files (*)|*"), wxFD_OPEN);
#endif
if (dlg.ShowModal() == wxID_OK)
{
lastFilename=dlg.GetFilename();
lastDir = dlg.GetDirectory();
lastPath = dlg.GetPath();
OpenLastFile();
UpdateRecentFiles();
}
}
void frmConfig::OnSave(wxCommandEvent& event)
{
if (!fileMenu->IsEnabled(MNU_SAVE))
return;
if (conn)
{
wxMessageDialog msg(this, _("If a malformed configuration is written to the server, it might show problems when restarting or reloading.\nAre you sure the configuration is correct?"),
_("Writing configuration file to server"), wxICON_EXCLAMATION|wxYES_NO|wxNO_DEFAULT);
if (msg.ShowModal() != wxID_YES)
return;
}
WriteFile(conn);
}
void frmConfig::OnSaveAs(wxCommandEvent& event)
{
#ifdef __WXMSW__
wxFileDialog *dlg=new wxFileDialog(this, _("Save configuration file as"), lastDir, lastFilename,
_("Configuration files (*.conf)|*.conf|All files (*.*)|*.*"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
#else
wxFileDialog *dlg=new wxFileDialog(this, _("Save configuration file as"), lastDir, lastFilename,
_("Configuration files (*.conf)|*.conf|All files (*)|*"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
#endif
if (dlg->ShowModal() == wxID_OK)
{
lastFilename=dlg->GetFilename();
lastDir = dlg->GetDirectory();
lastPath = dlg->GetPath();
WriteFile();
if (!changed)
UpdateRecentFiles();
}
delete dlg;
}
void frmConfig::OpenLastFile()
{
wxUtfFile file(lastPath, wxFile::read, wxFONTENCODING_SYSTEM);
if (file.IsOpened())
{
#ifdef __WXMSW__
_setmode(file.fd(), _O_BINARY);
#endif
wxString buffer;
file.Read(buffer);
file.Close();
DisplayFile(buffer);
statusBar->SetStatusText(wxString::Format(_(" Configuration read from %s"), lastPath.c_str()));
fileMenu->Enable(MNU_SAVE, false);
editMenu->Enable(MNU_UNDO, false);
toolBar->EnableTool(MNU_SAVE, false);
toolBar->EnableTool(MNU_UNDO, false);
}
}
bool frmConfig::CheckChanged(bool canVeto)
{
if (changed && settings->GetAskSaveConfirmation())
{
wxMessageDialog msg(this, _("The configuration is modified.\nDo you want to save changes?"),
GetTitle(),
wxYES_NO|wxNO_DEFAULT|wxICON_EXCLAMATION|
(canVeto ? wxCANCEL : 0));
switch (msg.ShowModal())
{
case wxID_YES:
WriteFile(conn);
return changed;
case wxID_CANCEL:
return true;
}
}
return false;
}
bool frmConfig::DoWriteFile(const wxChar *str, pgConn *conn)
{
bool done=false;
wxString buffer;
if (filetype != wxTextFileType_Unix)
{
buffer = wxTextBuffer::Translate(str, filetype);
str = buffer;
}
if (conn)
{
done = conn->ExecuteVoid(
wxT("SELECT pg_file_unlink('") + serverFileName + wxT(".bak');\n")
wxT("SELECT pg_file_write('") + serverFileName + wxT(".tmp', ")
+ conn->qtDbString(str) + wxT(", false);\n")
wxT("SELECT pg_file_rename('") + serverFileName + wxT(".tmp', '")
+ serverFileName + wxT("', '")
+ serverFileName + wxT(".bak');"));
if (!done)
conn->ExecuteVoid(
wxT("SELECT pg_file_unlink('") + serverFileName + wxT(".tmp')"));
}
else
{
wxUtfFile file(lastPath, wxFile::write, wxFONTENCODING_SYSTEM);
if (file.IsOpened())
{
#ifdef __WXMSW__
_setmode(file.fd(), _O_BINARY);
#endif
file.Write(str);
file.Close();
done=true;
}
}
if (done)
{
if (conn)
statusBar->SetStatusText(wxString::Format(_(" Configuration written to %s"), conn->GetHost().c_str()));
else
statusBar->SetStatusText(wxString::Format(_(" Configuration written to %s"), lastPath.c_str()));
}
return done;
}
void frmConfig::DisplayHint(bool force)
{
wxString str=GetHintString();
if (str.IsEmpty())
{
if (!force)
return;
str = _("No configuration setting detected that appears doubtful.");
}
wxMessageBox(str, _("Backend Configuration Hints"), wxICON_EXCLAMATION);
}
frmConfig *frmConfig ::Create(const wxString &title, const wxString &configFile, tryMode mode)
{
frmConfig *frm=0;
if (wxFile::Exists(configFile))
{
if (mode == HBAFILE ||configFile.Right(11) == wxT("pg_hba.conf"))
frm = new frmHbaConfig(title, configFile);
else if (mode == MAINFILE || configFile.Right(15) == wxT("postgresql.conf"))
frm = new frmMainConfig(title, configFile);
else if (mode == PGPASSFILE || configFile.Right(11) == wxT("pgpass.conf") || configFile.Right(7) == wxT(".pgpass"))
frm = new frmPgpassConfig(title, configFile);
// unknown config file!
}
if (frm)
frm->Go();
return frm;
}