1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // frmBackupGlobals.cpp - Backup globals dialogue
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 // wxWindows headers
13 #include <wx/wx.h>
14 #include <wx/settings.h>
15 
16 
17 // App headers
18 #include "pgAdmin3.h"
19 #include "frm/frmMain.h"
20 #include "frm/frmBackupGlobals.h"
21 #include "utils/sysLogger.h"
22 #include "schema/pgSchema.h"
23 #include "schema/pgTable.h"
24 
25 // Icons
26 #include "images/backup.pngc"
27 
28 
29 #define nbNotebook              CTRL_NOTEBOOK("nbNotebook")
30 #define txtFilename             CTRL_TEXT("txtFilename")
31 #define btnFilename             CTRL_BUTTON("btnFilename")
32 #define cbRolename              CTRL_COMBOBOX("cbRolename")
33 #define chkVerbose              CTRL_CHECKBOX("chkVerbose")
34 #define chkForceQuoteForIdent   CTRL_CHECKBOX("chkForceQuoteForIdent")
35 
36 
BEGIN_EVENT_TABLE(frmBackupGlobals,ExternProcessDialog)37 BEGIN_EVENT_TABLE(frmBackupGlobals, ExternProcessDialog)
38 	EVT_TEXT(XRCID("txtFilename"),          frmBackupGlobals::OnChange)
39 	EVT_BUTTON(XRCID("btnFilename"),        frmBackupGlobals::OnSelectFilename)
40 	EVT_BUTTON(wxID_OK,                     frmBackupGlobals::OnOK)
41 	EVT_CLOSE(                              ExternProcessDialog::OnClose)
42 END_EVENT_TABLE()
43 
44 
45 
46 frmBackupGlobals::frmBackupGlobals(frmMain *form, pgObject *obj) : ExternProcessDialog(form)
47 {
48 	object = obj;
49 
50 	SetFont(settings->GetSystemFont());
51 	LoadResource(form, wxT("frmBackupGlobals"));
52 	RestorePosition();
53 
54 	SetTitle(object->GetTranslatedMessage(BACKUPGLOBALS));
55 
56 	if (object->GetConnection()->EdbMinimumVersion(8, 0))
57 		backupExecutable = edbBackupAllExecutable;
58 	else if (object->GetConnection()->GetIsGreenplum())
59 		backupExecutable = gpBackupAllExecutable;
60 	else
61 		backupExecutable = pgBackupAllExecutable;
62 
63 	wxString val;
64 	settings->Read(wxT("frmBackupGlobals/LastFile"), &val, wxEmptyString);
65 	txtFilename->SetValue(val);
66 
67 	pgServer *server;
68 	if (object->GetMetaType() == PGM_SERVER)
69 		server = (pgServer *)object;
70 	else
71 		server = object->GetDatabase()->GetServer();
72 
73 	bool roles_supported = pgAppMinimumVersion(backupExecutable, 8, 4) && server->GetConnection()->BackendMinimumVersion(8, 1);
74 	cbRolename->Enable(roles_supported);
75 
76 	if (roles_supported)
77 	{
78 		// Collect the available rolenames
79 		pgSetIterator set(server->GetConnection(),
80 		                  wxT("SELECT DISTINCT rolname\n")
81 		                  wxT("FROM pg_roles db\n")
82 		                  wxT("ORDER BY rolname"));
83 
84 		cbRolename->Append(wxEmptyString);
85 
86 		while(set.RowsLeft())
87 			cbRolename->Append(set.GetVal(wxT("rolname")));
88 
89 		cbRolename->SetValue(server->GetRolename());
90 	}
91 
92 	if (!server->GetPasswordIsStored())
93 		environment.Add(wxT("PGPASSWORD=") + server->GetPassword());
94 
95 	// Pass the SSL mode via the environment
96 	environment.Add(wxT("PGSSLMODE=") + server->GetConnection()->GetSslModeName());
97 
98 	if (server->GetSSLRootCert() != wxEmptyString)
99 		environment.Add(wxT("PGSSLROOTCERT=") + server->GetSSLRootCert());
100 
101 	if (server->GetSSLCert() != wxEmptyString)
102 		environment.Add(wxT("PGSSLCERT=") + server->GetSSLCert());
103 
104 	if (server->GetSSLKey() != wxEmptyString)
105 		environment.Add(wxT("PGSSLKEY=") + server->GetSSLKey());
106 
107 	if (server->GetSSLCrl() != wxEmptyString)
108 		environment.Add(wxT("PGSSLCRL=") + server->GetSSLCrl());
109 
110 	// Icon
111 	SetIcon(*backup_png_ico);
112 
113 	txtMessages = CTRL_TEXT("txtMessages");
114 	// Note that under GTK+, SetMaxLength() function may only be used with single line text controls.
115 	// (see http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html#wxtextctrlsetmaxlength)
116 #ifndef __WXGTK__
117 	txtMessages->SetMaxLength(0L);
118 #endif
119 	btnOK->Disable();
120 
121 	if (!pgAppMinimumVersion(backupExecutable, 9, 1))
122 	{
123 		chkForceQuoteForIdent->Disable();
124 	}
125 
126 	wxCommandEvent ev;
127 	OnChange(ev);
128 }
129 
130 
~frmBackupGlobals()131 frmBackupGlobals::~frmBackupGlobals()
132 {
133 	SavePosition();
134 }
135 
136 
GetHelpPage() const137 wxString frmBackupGlobals::GetHelpPage() const
138 {
139 	wxString page;
140 	page = wxT("pg/app-pg-dumpall");
141 	return page;
142 }
143 
144 
OnSelectFilename(wxCommandEvent & ev)145 void frmBackupGlobals::OnSelectFilename(wxCommandEvent &ev)
146 {
147 	wxString title, prompt, FilenameOnly;
148 
149 	title  = _("Select output file");
150 #ifdef __WXMSW__
151 	prompt = _("Query files (*.sql)|*.sql|All files (*.*)|*.*");
152 #else
153 	prompt = _("Query files (*.sql)|*.sql|All files (*)|*");
154 #endif
155 
156 	wxFileName::SplitPath(txtFilename->GetValue(), NULL, NULL, &FilenameOnly, NULL);
157 	wxFileDialog file(this, title, ::wxPathOnly(txtFilename->GetValue()), FilenameOnly, prompt, wxFD_SAVE);
158 
159 	if (file.ShowModal() == wxID_OK)
160 	{
161 		txtFilename->SetValue(file.GetPath());
162 		OnChange(ev);
163 	}
164 }
165 
166 
OnChange(wxCommandEvent & ev)167 void frmBackupGlobals::OnChange(wxCommandEvent &ev)
168 {
169 	if (!process && !done)
170 		btnOK->Enable(!txtFilename->GetValue().IsEmpty());
171 }
172 
GetCmd(int step)173 wxString frmBackupGlobals::GetCmd(int step)
174 {
175 	wxString cmd = getCmdPart1();
176 
177 	return cmd + getCmdPart2();
178 }
179 
180 
GetDisplayCmd(int step)181 wxString frmBackupGlobals::GetDisplayCmd(int step)
182 {
183 	wxString cmd = getCmdPart1();
184 
185 	return cmd + getCmdPart2();
186 }
187 
188 
getCmdPart1()189 wxString frmBackupGlobals::getCmdPart1()
190 {
191 	pgServer *server;
192 	if (object->GetMetaType() == PGM_SERVER)
193 		server = (pgServer *)object;
194 	else
195 		server = object->GetDatabase()->GetServer();
196 
197 	wxString cmd = backupExecutable;
198 
199 	if (!server->GetName().IsEmpty())
200 		cmd += wxT(" --host ") + server->GetName();
201 
202 	cmd +=  wxT(" --port ") + NumToStr((long)server->GetPort())
203 	        +  wxT(" --username ") + commandLineCleanOption(qtIdent(server->GetUsername()))
204 	        +  wxT(" --database ") + commandLineCleanOption(qtIdent(server->GetDatabaseName()));
205 
206 	if (!cbRolename->GetValue().IsEmpty())
207 		cmd += wxT(" --role ") + commandLineCleanOption(qtIdent(cbRolename->GetValue()));
208 
209 	if (pgAppMinimumVersion(backupExecutable, 8, 4))
210 		cmd += wxT(" --no-password ");
211 
212 	return cmd;
213 }
214 
215 
getCmdPart2()216 wxString frmBackupGlobals::getCmdPart2()
217 {
218 	wxString cmd;
219 
220 	if (settings->GetIgnoreVersion())
221 		cmd.Append(wxT(" --ignore-version"));
222 	if (chkVerbose->GetValue())
223 		cmd.Append(wxT(" --verbose"));
224 	if (chkForceQuoteForIdent->GetValue())
225 		cmd.Append(wxT(" --quote-all-identifiers"));
226 
227 	cmd.Append(wxT(" --file \"") + txtFilename->GetValue() + wxT("\""));
228 
229 	cmd.Append(wxT(" --globals-only"));
230 
231 	return cmd;
232 }
233 
234 
Go()235 void frmBackupGlobals::Go()
236 {
237 	txtFilename->SetFocus();
238 	Show(true);
239 }
240 
OnOK(wxCommandEvent & ev)241 void frmBackupGlobals::OnOK(wxCommandEvent &ev)
242 {
243 	if (!done)
244 	{
245 		if (processedFile == txtFilename->GetValue())
246 		{
247 			if (wxMessageBox(_("Are you sure you wish to run a backup to this file again?"), _("Repeat backup?"), wxICON_QUESTION | wxYES_NO) != wxYES)
248 				return;
249 		}
250 		else if (wxFile::Exists(txtFilename->GetValue()))
251 		{
252 			wxString msg;
253 			msg.Printf(_("The file: \n\n%s\n\nalready exists. Do you want to overwrite it?"), txtFilename->GetValue().c_str());
254 			if (wxMessageBox(msg, _("Overwrite file?"), wxICON_WARNING | wxYES_NO) != wxYES)
255 				return;
256 		}
257 
258 		processedFile = txtFilename->GetValue();
259 	}
260 
261 	settings->Write(wxT("frmBackupGlobals/LastFile"), txtFilename->GetValue());
262 	ExternProcessDialog::OnOK(ev);
263 }
264 
backupGlobalsFactory(menuFactoryList * list,wxMenu * mnu,ctlMenuToolbar * toolbar)265 backupGlobalsFactory::backupGlobalsFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
266 {
267 	mnu->Append(id, _("&Backup globals..."), _("Creates a backup of the global database objects"));
268 }
269 
270 
StartDialog(frmMain * form,pgObject * obj)271 wxWindow *backupGlobalsFactory::StartDialog(frmMain *form, pgObject *obj)
272 {
273 	frmBackupGlobals *frm = new frmBackupGlobals(form, obj);
274 	frm->Go();
275 	return 0;
276 }
277 
278 
CheckEnable(pgObject * obj)279 bool backupGlobalsFactory::CheckEnable(pgObject *obj)
280 {
281 	if (!obj)
282 		return false;
283 
284 	if (obj->GetMetaType() == PGM_SERVER)
285 		if (!((pgServer *)obj)->GetConnected())
286 			return false;
287 
288 	if (obj->GetConnection() && obj->GetConnection()->EdbMinimumVersion(8, 0))
289 		return obj->CanBackupGlobals() && !edbBackupAllExecutable.IsEmpty() && pgAppMinimumVersion(edbBackupAllExecutable, 8, 3);
290 	else if (obj->GetConnection() && obj->GetConnection()->GetIsGreenplum())
291 		return obj->CanBackupGlobals() && !gpBackupAllExecutable.IsEmpty() && pgAppMinimumVersion(gpBackupAllExecutable, 8, 3);
292 	else
293 		return obj->CanBackupGlobals() && !pgBackupAllExecutable.IsEmpty() && pgAppMinimumVersion(pgBackupAllExecutable, 8, 3);
294 }
295 
296