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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
//
#include "mainDialog.h"
#include "resources.h"
#include "../shared/customButton.h"
#include "../shared/standardPaths.h"
#include "functions.h"
#include <wx/msgdlg.h>
#include <wx/wupdlock.h>
#include "watcher.h"
#include <wx/utils.h>
#include "xmlProcessing.h"
#include "trayMenu.h"
#include "../shared/fileHandling.h"
#include "xmlFreeFileSync.h"
#include "../shared/systemConstants.h"
#include "../shared/stringConv.h"
#include "../shared/staticAssert.h"
#include "../shared/buildInfo.h"
#include "../shared/helpProvider.h"
using namespace FreeFileSync;
MainDialog::MainDialog(wxDialog *dlg,
const wxString& cfgFilename)
: MainDlgGenerated(dlg)
{
wxWindowUpdateLocker dummy(this); //avoid display distortion
m_bpButtonRemoveTopFolder->Hide();
m_panelMainFolder->Layout();
m_bpButtonAddFolder->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("addFolderPair")));
m_bpButtonRemoveTopFolder->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("removeFolderPair")));
m_buttonStart->setBitmapFront(GlobalResources::getInstance().getImageByName(wxT("startRed")));
//register key event
Connect(wxEVT_CHAR_HOOK, wxKeyEventHandler(MainDialog::OnKeyPressed), NULL, this);
//prepare drag & drop
dragDropOnFolder.reset(new FreeFileSync::DragDropOnDlg(m_panelMainFolder, m_dirPickerMain, m_txtCtrlDirectoryMain));
//load config values
xmlAccess::XmlRealConfig newConfig;
bool startWatchingImmediately = false;
if (cfgFilename.empty())
try
{
RealtimeSync::readRealOrBatchConfig(lastConfigFileName(), newConfig);
}
catch (const xmlAccess::XmlError& error)
{
if (wxFileExists(lastConfigFileName())) //show error only if it's a parsing problem
{
if (error.getSeverity() == xmlAccess::XmlError::WARNING)
wxMessageBox(error.show(), _("Warning"), wxOK | wxICON_WARNING);
else
wxMessageBox(error.show(), _("Error"), wxOK | wxICON_ERROR);
}
}
else
try
{
RealtimeSync::readRealOrBatchConfig(cfgFilename, newConfig);
startWatchingImmediately = true;
}
catch (const xmlAccess::XmlError& error)
{
if (error.getSeverity() == xmlAccess::XmlError::WARNING)
wxMessageBox(error.show(), _("Warning"), wxOK | wxICON_WARNING);
else
wxMessageBox(error.show(), _("Error"), wxOK | wxICON_ERROR);
}
setConfiguration(newConfig);
Fit();
Center();
if (startWatchingImmediately) //start watch mode directly
{
wxCommandEvent dummy2(wxEVT_COMMAND_BUTTON_CLICKED);
this->OnStart(dummy2);
}
else
m_buttonStart->SetFocus(); //don't "steal" focus if program is running from sys-tray"
}
MainDialog::~MainDialog()
{
//save current configuration
const xmlAccess::XmlRealConfig currentCfg = getConfiguration();
try //write config to XML
{
writeRealConfig(currentCfg, lastConfigFileName());
}
catch (const xmlAccess::XmlError& error)
{
wxMessageBox(error.show().c_str(), _("Error"), wxOK | wxICON_ERROR);
}
}
void MainDialog::OnClose(wxCloseEvent &event)
{
Destroy();
}
void MainDialog::OnQuit(wxCommandEvent &event)
{
Destroy();
}
const wxString& MainDialog::lastConfigFileName()
{
static wxString instance = FreeFileSync::getConfigDir() + wxT("LastRun.ffs_real");
return instance;
}
void MainDialog::OnShowHelp(wxCommandEvent& event)
{
#ifdef FFS_WIN
FreeFileSync::displayHelpEntry(wxT("html\\advanced\\RealtimeSync.html"));
#elif defined FFS_LINUX
FreeFileSync::displayHelpEntry(wxT("html/advanced/RealtimeSync.html"));
#endif
}
void MainDialog::OnMenuAbout(wxCommandEvent& event)
{
//build information
wxString build = __TDATE__;
#if wxUSE_UNICODE
build += wxT(" - Unicode");
#else
build += wxT(" - ANSI");
#endif //wxUSE_UNICODE
//compile time info about 32/64-bit build
if (Utility::is64BitBuild)
build += wxT(" x64");
else
build += wxT(" x86");
assert_static(Utility::is32BitBuild || Utility::is64BitBuild);
wxString buildFormatted = _("(Build: %x)");
buildFormatted.Replace(wxT("%x"), build);
wxMessageDialog* aboutDlg = new wxMessageDialog(this, wxString(wxT("RealtimeSync")) + wxT("\n\n") + buildFormatted, _("About"), wxOK);
aboutDlg->ShowModal();
}
void MainDialog::OnKeyPressed(wxKeyEvent& event)
{
const int keyCode = event.GetKeyCode();
if (keyCode == WXK_ESCAPE)
Destroy();
event.Skip();
}
void MainDialog::OnStart(wxCommandEvent& event)
{
xmlAccess::XmlRealConfig currentCfg = getConfiguration();
Hide();
wxWindowDisabler dummy; //avoid unwanted re-entrance in the following process
switch (RealtimeSync::startDirectoryMonitor(currentCfg))
{
case RealtimeSync::QUIT:
{
Destroy();
return;
}
break;
case RealtimeSync::RESUME:
break;
}
Show();
}
void MainDialog::OnSaveConfig(wxCommandEvent& event)
{
const wxString defaultFileName = wxT("Realtime.ffs_real");
wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, defaultFileName, wxString(_("RealtimeSync configuration")) + wxT(" (*.ffs_real)|*.ffs_real"), wxFD_SAVE);
if (filePicker->ShowModal() == wxID_OK)
{
const wxString newFileName = filePicker->GetPath();
if (wxFileExists(newFileName))
{
wxMessageDialog* messageDlg = new wxMessageDialog(this, wxString(_("File already exists. Overwrite?")) + wxT(" \"") + newFileName + wxT("\""), _("Warning") , wxOK | wxCANCEL);
if (messageDlg->ShowModal() != wxID_OK)
{
OnSaveConfig(event); //retry
return;
}
}
const xmlAccess::XmlRealConfig currentCfg = getConfiguration();
try //write config to XML
{
writeRealConfig(currentCfg, newFileName);
}
catch (const FreeFileSync::FileError& error)
{
wxMessageBox(error.show().c_str(), _("Error"), wxOK | wxICON_ERROR);
}
}
}
void MainDialog::loadConfig(const wxString& filename)
{
xmlAccess::XmlRealConfig newConfig;
try
{
RealtimeSync::readRealOrBatchConfig(filename, newConfig);
}
catch (const xmlAccess::XmlError& error)
{
if (error.getSeverity() == xmlAccess::XmlError::WARNING)
wxMessageBox(error.show(), _("Warning"), wxOK | wxICON_WARNING);
else
{
wxMessageBox(error.show(), _("Error"), wxOK | wxICON_ERROR);
return;
}
}
setConfiguration(newConfig);
}
void MainDialog::OnLoadConfig(wxCommandEvent& event)
{
wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, wxEmptyString,
wxString(_("RealtimeSync configuration")) + wxT(" (*.ffs_real;*.ffs_batch)|*.ffs_real;*.ffs_batch"),
wxFD_OPEN);
if (filePicker->ShowModal() == wxID_OK)
loadConfig(filePicker->GetPath());
}
void MainDialog::setConfiguration(const xmlAccess::XmlRealConfig& cfg)
{
//clear existing folders
m_txtCtrlDirectoryMain->ChangeValue(wxEmptyString);
m_dirPickerMain->SetPath(wxEmptyString);
clearAddFolders();
if (!cfg.directories.empty())
{
//fill top folder
m_txtCtrlDirectoryMain->SetValue(*cfg.directories.begin());
const Zstring dirFormatted = FreeFileSync::getFormattedDirectoryName(wxToZ(*cfg.directories.begin()));
if (dirExists(dirFormatted))
m_dirPickerMain->SetPath(zToWx(dirFormatted));
//fill additional folders
addFolder(std::vector<wxString>(cfg.directories.begin() + 1, cfg.directories.end()));
}
//fill commandline
m_textCtrlCommand->SetValue(cfg.commandline);
//set delay
m_spinCtrlDelay->SetValue(static_cast<int>(cfg.delay));
}
xmlAccess::XmlRealConfig MainDialog::getConfiguration()
{
xmlAccess::XmlRealConfig output;
output.directories.push_back(m_txtCtrlDirectoryMain->GetValue());
for (std::vector<FolderPanel*>::const_iterator i = additionalFolders.begin(); i != additionalFolders.end(); ++i)
output.directories.push_back((*i)->m_txtCtrlDirectory->GetValue());
output.commandline = m_textCtrlCommand->GetValue();
output.delay = m_spinCtrlDelay->GetValue();;
return output;
}
void MainDialog::OnAddFolder(wxCommandEvent& event)
{
const wxString topFolder = m_txtCtrlDirectoryMain->GetValue();
//clear existing top folder first
RealtimeSync::setDirectoryName(wxEmptyString, m_txtCtrlDirectoryMain, m_dirPickerMain);
std::vector<wxString> newFolders;
newFolders.push_back(topFolder.c_str());
addFolder(newFolders, true); //add pair in front of additonal pairs
}
void MainDialog::OnRemoveFolder(wxCommandEvent& event)
{
//find folder pair originating the event
const wxObject* const eventObj = event.GetEventObject();
for (std::vector<FolderPanel*>::const_iterator i = additionalFolders.begin(); i != additionalFolders.end(); ++i)
{
if (eventObj == static_cast<wxObject*>((*i)->m_bpButtonRemoveFolder))
{
removeAddFolder(i - additionalFolders.begin());
return;
}
}
}
void MainDialog::OnRemoveTopFolder(wxCommandEvent& event)
{
if (additionalFolders.size() > 0)
{
const wxString topDir = (*additionalFolders.begin())->m_txtCtrlDirectory->GetValue().c_str();
RealtimeSync::setDirectoryName(topDir, m_txtCtrlDirectoryMain, m_dirPickerMain);
removeAddFolder(0); //remove first of additional folders
}
}
#ifdef FFS_WIN
static const size_t MAX_ADD_FOLDERS = 8;
#elif defined FFS_LINUX
static const size_t MAX_ADD_FOLDERS = 6;
#endif
void MainDialog::addFolder(const std::vector<wxString>& newFolders, bool addFront)
{
if (newFolders.size() == 0)
return;
wxWindowUpdateLocker dummy(this); //avoid display distortion
int folderHeight = 0;
for (std::vector<wxString>::const_iterator i = newFolders.begin(); i != newFolders.end(); ++i)
{
//add new folder pair
FolderPanel* newFolder = new FolderPanel(m_scrolledWinFolders);
newFolder->m_bpButtonRemoveFolder->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("removeFolderPair")));
//get size of scrolled window
folderHeight = newFolder->GetSize().GetHeight();
if (addFront)
{
bSizerFolders->Insert(0, newFolder, 0, wxEXPAND, 5);
additionalFolders.insert(additionalFolders.begin(), newFolder);
}
else
{
bSizerFolders->Add(newFolder, 0, wxEXPAND, 5);
additionalFolders.push_back(newFolder);
}
//register events
newFolder->m_bpButtonRemoveFolder->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainDialog::OnRemoveFolder), NULL, this );
//insert directory name
RealtimeSync::setDirectoryName(*i, newFolder->m_txtCtrlDirectory, newFolder->m_dirPicker);
}
//set size of scrolled window
const size_t additionalRows = std::min(additionalFolders.size(), MAX_ADD_FOLDERS); //up to MAX_ADD_FOLDERS additional folders shall be shown
m_scrolledWinFolders->SetMinSize(wxSize( -1, folderHeight * static_cast<int>(additionalRows)));
//adapt delete top folder pair button
m_bpButtonRemoveTopFolder->Show();
m_panelMainFolder->Layout();
//update controls
m_scrolledWinFolders->Fit(); //adjust scrolled window size
m_scrolledWinFolders->Layout(); //fix small layout problem
m_panelMain->Layout(); //adjust stuff inside scrolled window
Fit(); //adapt dialog size
}
void MainDialog::removeAddFolder(const int pos)
{
wxWindowUpdateLocker dummy(this); //avoid display distortion
if (0 <= pos && pos < int(additionalFolders.size()))
{
//remove folder pairs from window
FolderPanel* dirToDelete = additionalFolders[pos];
const int folderHeight = dirToDelete->GetSize().GetHeight();
bSizerFolders->Detach(dirToDelete); //Remove() does not work on Window*, so do it manually
dirToDelete->Destroy(); //
additionalFolders.erase(additionalFolders.begin() + pos); //remove last element in vector
//set size of scrolled window
const size_t additionalRows = std::min(additionalFolders.size(), MAX_ADD_FOLDERS); //up to MAX_ADD_FOLDERS additional folders shall be shown
m_scrolledWinFolders->SetMinSize(wxSize( -1, folderHeight * static_cast<int>(additionalRows)));
//adapt delete top folder pair button
if (additionalFolders.size() == 0)
{
m_bpButtonRemoveTopFolder->Hide();
m_panelMainFolder->Layout();
}
//update controls
m_scrolledWinFolders->Fit(); //adjust scrolled window size
m_panelMain->Layout(); //adjust stuff inside scrolled window
Fit(); //adapt dialog size
}
}
void MainDialog::clearAddFolders()
{
wxWindowUpdateLocker dummy(this); //avoid display distortion
additionalFolders.clear();
bSizerFolders->Clear(true);
m_bpButtonRemoveTopFolder->Hide();
m_panelMainFolder->Layout();
m_scrolledWinFolders->SetMinSize(wxSize(-1, 0));
Fit(); //adapt dialog size
}
|