summaryrefslogtreecommitdiff
path: root/ui/SyncDialog.cpp
blob: 6d838f05bab301a70b577f225488dd5f441fe9c8 (plain)
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
#include "syncDialog.h"
#include "../library/globalFunctions.h"
#include "../library/resources.h"
#include <wx/msgdlg.h>
#include <wx/stdpaths.h>
#include <wx/ffile.h>
#ifdef FFS_WIN
#include <windows.h>
#include <shlobj.h>
#endif  // FFS_WIN

using namespace std;

SyncDialog::SyncDialog(wxWindow* window,
                       const FileCompareResult& gridDataRef,
                       Configuration& config,
                       bool synchronizationEnabled) :
        SyncDlgGenerated(window),
        gridData(gridDataRef),
        cfg(config)
{
    //make working copy of mainDialog.cfg.syncConfiguration and recycler setting
    localSyncConfiguration = config.syncConfiguration;
    m_checkBoxUseRecycler->SetValue(cfg.useRecycleBin);
    m_checkBoxContinueError->SetValue(cfg.continueOnError);

    //set sync config icons
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    //update preview
    calculatePreview();

    //set icons for this dialog
    m_bpButton18->SetBitmapLabel(*GlobalResources::bitmapStartSync);
    m_bpButton18->SetBitmapDisabled(*GlobalResources::bitmapStartSyncDis);
    m_bitmap13->SetBitmap(*GlobalResources::bitmapLeftOnlyDeact);
    m_bitmap14->SetBitmap(*GlobalResources::bitmapRightOnlyDeact);
    m_bitmap15->SetBitmap(*GlobalResources::bitmapLeftNewerDeact);
    m_bitmap16->SetBitmap(*GlobalResources::bitmapRightNewerDeact);
    m_bitmap17->SetBitmap(*GlobalResources::bitmapDifferentDeact);

    if (synchronizationEnabled)
        m_bpButton18->Enable();
    else
    {
        m_bpButton18->Disable();
        m_button6->SetFocus();
    }

    //set radiobutton
    if (localSyncConfiguration.exLeftSideOnly  == SYNC_DIR_RIGHT &&
            localSyncConfiguration.exRightSideOnly == SYNC_DIR_RIGHT &&
            localSyncConfiguration.leftNewer       == SYNC_DIR_RIGHT &&
            localSyncConfiguration.rightNewer      == SYNC_DIR_RIGHT &&
            localSyncConfiguration.different       == SYNC_DIR_RIGHT)
        m_radioBtn1->SetValue(true);    //one way ->

    else if (localSyncConfiguration.exLeftSideOnly  == SYNC_DIR_RIGHT &&
             localSyncConfiguration.exRightSideOnly == SYNC_DIR_LEFT &&
             localSyncConfiguration.leftNewer       == SYNC_DIR_RIGHT &&
             localSyncConfiguration.rightNewer      == SYNC_DIR_LEFT &&
             localSyncConfiguration.different       == SYNC_DIR_NONE)
        m_radioBtn2->SetValue(true);    //two way <->

    else
        m_radioBtn3->SetValue(true);    //other

    m_bpButton18->SetLabel(_("&Start"));

    //set tooltip for ambivalent category "different"
    adjustToolTips(m_bitmap17, config.compareVar);
}

//#################################################################################################################

SyncDialog::~SyncDialog() {}


void SyncDialog::updateConfigIcons(wxBitmapButton* button1,
                                   wxBitmapButton* button2,
                                   wxBitmapButton* button3,
                                   wxBitmapButton* button4,
                                   wxBitmapButton* button5,
                                   const SyncConfiguration& syncConfig)
{
    if (syncConfig.exLeftSideOnly == SYNC_DIR_RIGHT)
    {
        button1->SetBitmapLabel(*GlobalResources::bitmapArrowRightCr);
        button1->SetToolTip(_("Copy from left to right"));
    }
    else if (syncConfig.exLeftSideOnly == SYNC_DIR_LEFT)
    {
        button1->SetBitmapLabel(*GlobalResources::bitmapDeleteLeft);
        button1->SetToolTip(_("Delete files/folders existing on left side only"));
    }
    else if (syncConfig.exLeftSideOnly == SYNC_DIR_NONE)
    {
        button1->SetBitmapLabel(*GlobalResources::bitmapArrowNone);
        button1->SetToolTip(_("Do nothing"));
    }

    if (syncConfig.exRightSideOnly == SYNC_DIR_RIGHT)
    {
        button2->SetBitmapLabel(*GlobalResources::bitmapDeleteRight);
        button2->SetToolTip(_("Delete files/folders existing on right side only"));
    }
    else if (syncConfig.exRightSideOnly == SYNC_DIR_LEFT)
    {
        button2->SetBitmapLabel(*GlobalResources::bitmapArrowLeftCr);
        button2->SetToolTip(_("Copy from right to left"));
    }
    else if (syncConfig.exRightSideOnly == SYNC_DIR_NONE)
    {
        button2->SetBitmapLabel(*GlobalResources::bitmapArrowNone);
        button2->SetToolTip(_("Do nothing"));
    }

    if (syncConfig.leftNewer == SYNC_DIR_RIGHT)
    {
        button3->SetBitmapLabel(*GlobalResources::bitmapArrowRight);
        button3->SetToolTip(_("Copy from left to right overwriting"));
    }
    else if (syncConfig.leftNewer == SYNC_DIR_LEFT)
    {
        button3->SetBitmapLabel(*GlobalResources::bitmapArrowLeft);
        button3->SetToolTip(_("Copy from right to left overwriting"));
    }
    else if (syncConfig.leftNewer == SYNC_DIR_NONE)
    {
        button3->SetBitmapLabel(*GlobalResources::bitmapArrowNone);
        button3->SetToolTip(_("Do nothing"));
    }

    if (syncConfig.rightNewer == SYNC_DIR_RIGHT)
    {
        button4->SetBitmapLabel(*GlobalResources::bitmapArrowRight);
        button4->SetToolTip(_("Copy from left to right overwriting"));
    }
    else if (syncConfig.rightNewer == SYNC_DIR_LEFT)
    {
        button4->SetBitmapLabel(*GlobalResources::bitmapArrowLeft);
        button4->SetToolTip(_("Copy from right to left overwriting"));
    }
    else if (syncConfig.rightNewer == SYNC_DIR_NONE)
    {
        button4->SetBitmapLabel(*GlobalResources::bitmapArrowNone);
        button4->SetToolTip(_("Do nothing"));
    }

    if (syncConfig.different == SYNC_DIR_RIGHT)
    {
        button5->SetBitmapLabel(*GlobalResources::bitmapArrowRight);
        button5->SetToolTip(_("Copy from left to right overwriting"));
    }
    else if (syncConfig.different == SYNC_DIR_LEFT)
    {
        button5->SetBitmapLabel(*GlobalResources::bitmapArrowLeft);
        button5->SetToolTip(_("Copy from right to left overwriting"));
    }
    else if (syncConfig.different == SYNC_DIR_NONE)
    {
        button5->SetBitmapLabel(*GlobalResources::bitmapArrowNone);
        button5->SetToolTip(_("Do nothing"));
    }
}


void SyncDialog::adjustToolTips(wxStaticBitmap* bitmap, const CompareVariant var)
{
    //set tooltip for ambivalent category "different"
    if (var == CMP_BY_TIME_SIZE)
    {
        bitmap->SetToolTip(_("Files that exist on both sides, have same date but different filesizes"));
    }
    else if (var == CMP_BY_CONTENT)
    {
        bitmap->SetToolTip(_("Files that exist on both sides and have different content"));
    }
    else
        assert(false);
}


void SyncDialog::calculatePreview()
{
    //update preview of bytes to be transferred:
    int objectsToCreate    = 0;
    int objectsToOverwrite = 0;
    int objectsToDelete    = 0;
    double dataToProcess   = 0;
    FreeFileSync::calcTotalBytesToSync(objectsToCreate,
                                       objectsToOverwrite,
                                       objectsToDelete,
                                       dataToProcess,
                                       gridData,
                                       localSyncConfiguration);

    wxString toCreate = globalFunctions::numberToWxString(objectsToCreate);
    wxString toUpdate = globalFunctions::numberToWxString(objectsToOverwrite);
    wxString toDelete = globalFunctions::numberToWxString(objectsToDelete);
    wxString data     = FreeFileSync::formatFilesizeToShortString(dataToProcess);

    globalFunctions::includeNumberSeparator(toCreate);
    globalFunctions::includeNumberSeparator(toUpdate);
    globalFunctions::includeNumberSeparator(toDelete);

    m_textCtrlCreate->SetValue(toCreate);
    m_textCtrlUpdate->SetValue(toUpdate);
    m_textCtrlDelete->SetValue(toDelete);
    m_textCtrlData->SetValue(data);
}


void SyncDialog::OnClose(wxCloseEvent& event)
{
    EndModal(0);
}


void SyncDialog::OnCancel(wxCommandEvent& event)
{
    EndModal(0);
}


void SyncDialog::OnBack(wxCommandEvent& event)
{
    //write configuration to main dialog
    cfg.syncConfiguration = localSyncConfiguration;
    cfg.useRecycleBin     = m_checkBoxUseRecycler->GetValue();
    cfg.continueOnError   = m_checkBoxContinueError->GetValue();

    EndModal(0);
}

void SyncDialog::OnStartSync(wxCommandEvent& event)
{
    //write configuration to main dialog
    cfg.syncConfiguration = localSyncConfiguration;
    cfg.useRecycleBin     = m_checkBoxUseRecycler->GetValue();
    cfg.continueOnError   = m_checkBoxContinueError->GetValue();

    EndModal(StartSynchronizationProcess);
}


void SyncDialog::OnSelectRecycleBin(wxCommandEvent& event)
{
    if (event.IsChecked())
    {
        if (!FreeFileSync::recycleBinExists())
        {
            wxMessageBox(_("It was not possible to initialize the Recycle Bin!\n\nIt's likely that you are not using Windows.\nIf you want this feature included, please contact the author. :)"), _("Error") , wxOK | wxICON_ERROR);
            m_checkBoxUseRecycler->SetValue(false);
        }
    }
}


void SyncDialog::OnSyncLeftToRight(wxCommandEvent& event)
{
    localSyncConfiguration.exLeftSideOnly  = SYNC_DIR_RIGHT;
    localSyncConfiguration.exRightSideOnly = SYNC_DIR_RIGHT;
    localSyncConfiguration.leftNewer       = SYNC_DIR_RIGHT;
    localSyncConfiguration.rightNewer      = SYNC_DIR_RIGHT;
    localSyncConfiguration.different       = SYNC_DIR_RIGHT;

    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();

    //if event is triggered by button
    m_radioBtn1->SetValue(true);
}


void SyncDialog::OnSyncBothSides(wxCommandEvent& event)
{
    localSyncConfiguration.exLeftSideOnly  = SYNC_DIR_RIGHT;
    localSyncConfiguration.exRightSideOnly = SYNC_DIR_LEFT;
    localSyncConfiguration.leftNewer       = SYNC_DIR_RIGHT;
    localSyncConfiguration.rightNewer      = SYNC_DIR_LEFT;
    localSyncConfiguration.different       = SYNC_DIR_NONE;

    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();

    //if event is triggered by button
    m_radioBtn2->SetValue(true);
}


void toggleSyncDirection(SyncDirection& current)
{
    if (current == SYNC_DIR_RIGHT)
        current = SYNC_DIR_LEFT;
    else if (current == SYNC_DIR_LEFT)
        current = SYNC_DIR_NONE;
    else if (current== SYNC_DIR_NONE)
        current = SYNC_DIR_RIGHT;
    else
        assert (false);
}

void SyncDialog::OnExLeftSideOnly( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.exLeftSideOnly);
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();
    //set custom config button
    m_radioBtn3->SetValue(true);
}

void SyncDialog::OnExRightSideOnly( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.exRightSideOnly);
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();
    //set custom config button
    m_radioBtn3->SetValue(true);
}

void SyncDialog::OnLeftNewer( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.leftNewer);
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();
    //set custom config button
    m_radioBtn3->SetValue(true);
}

void SyncDialog::OnRightNewer( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.rightNewer);
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();
    //set custom config button
    m_radioBtn3->SetValue(true);
}

void SyncDialog::OnDifferent( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.different);
    updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
    calculatePreview();
    //set custom config button
    m_radioBtn3->SetValue(true);
}

//###################################################################################################################################


BatchDialog::BatchDialog(wxWindow* window,
                         const Configuration& config,
                         const wxString& leftDir,
                         const wxString& rightDir) :
        BatchDlgGenerated(window)
{
    //make working copy of mainDialog.cfg.syncConfiguration and recycler setting
    localSyncConfiguration = config.syncConfiguration;
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);

    m_checkBoxUseRecycler->SetValue(config.useRecycleBin);
    m_checkBoxContinueError->SetValue(config.continueOnError);

    switch (config.compareVar)
    {
    case CMP_BY_TIME_SIZE:
        m_radioBtnSizeDate->SetValue(true);
        break;
    case CMP_BY_CONTENT:
        m_radioBtnContent->SetValue(true);
        break;
    default:
        assert (false);
    }
    //adjust toolTip
    SyncDialog::adjustToolTips(m_bitmap17, config.compareVar);

    filterIsActive = config.filterIsActive;
    updateFilterButton();

    m_textCtrlInclude->SetValue(config.includeFilter);
    m_textCtrlExclude->SetValue(config.excludeFilter);

    m_directoryPanel1->SetValue(leftDir);
    m_directoryPanel2->SetValue(rightDir);

    //set icons for this dialog
    m_bitmap13->SetBitmap(*GlobalResources::bitmapLeftOnlyDeact);
    m_bitmap14->SetBitmap(*GlobalResources::bitmapRightOnlyDeact);
    m_bitmap15->SetBitmap(*GlobalResources::bitmapLeftNewerDeact);
    m_bitmap16->SetBitmap(*GlobalResources::bitmapRightNewerDeact);
    m_bitmap17->SetBitmap(*GlobalResources::bitmapDifferentDeact);
    m_bitmap8->SetBitmap(*GlobalResources::bitmapInclude);
    m_bitmap9->SetBitmap(*GlobalResources::bitmapExclude);

    m_buttonCreate->SetFocus();
}

BatchDialog::~BatchDialog()
{}


void BatchDialog::updateFilterButton()
{
    if (filterIsActive)
    {
        m_bpButtonFilter->SetBitmapLabel(*GlobalResources::bitmapFilterOn);
        m_bpButtonFilter->SetToolTip(_("Filter active: Press again to deactivate"));

        m_textCtrlInclude->Enable();
        m_textCtrlExclude->Enable();
    }
    else
    {
        m_bpButtonFilter->SetBitmapLabel(*GlobalResources::bitmapFilterOff);
        m_bpButtonFilter->SetToolTip(_("Press button to activate filter"));

        m_textCtrlInclude->Disable();
        m_textCtrlExclude->Disable();
    }
}


void BatchDialog::OnExLeftSideOnly(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.exLeftSideOnly);
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
}

void BatchDialog::OnExRightSideOnly(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.exRightSideOnly);
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
}

void BatchDialog::OnLeftNewer(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.leftNewer);
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
}

void BatchDialog::OnRightNewer(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.rightNewer);
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
}

void BatchDialog::OnDifferent(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.different);
    SyncDialog::updateConfigIcons(m_bpButton5, m_bpButton6, m_bpButton7, m_bpButton8, m_bpButton9, localSyncConfiguration);
}


void BatchDialog::OnFilterButton(wxCommandEvent& event)
{
    filterIsActive = !filterIsActive;
    updateFilterButton();
}


void BatchDialog::OnSelectRecycleBin(wxCommandEvent& event)
{
    if (event.IsChecked())
    {
        if (!FreeFileSync::recycleBinExists())
        {
            wxMessageBox(_("It was not possible to initialize the Recycle Bin!\n\nIt's likely that you are not using Windows.\nIf you want this feature included, please contact the author. :)"), _("Error") , wxOK | wxICON_ERROR);
            m_checkBoxUseRecycler->SetValue(false);
        }
    }
}


void BatchDialog::OnChangeCompareVar(wxCommandEvent& event)
{
    CompareVariant var;
    if (m_radioBtnSizeDate->GetValue())
        var = CMP_BY_TIME_SIZE;
    else if (m_radioBtnContent->GetValue())
        var = CMP_BY_CONTENT;
    else
    {
        assert(false);
        var = CMP_BY_TIME_SIZE;
    }

    //set tooltip for ambivalent category "different"
    SyncDialog::adjustToolTips(m_bitmap17, var);
}


void BatchDialog::OnClose(wxCloseEvent&   event)
{
    EndModal(0);
}


void BatchDialog::OnCancel(wxCommandEvent& event)
{
    EndModal(0);
}


void BatchDialog::OnCreateBatchJob(wxCommandEvent& event)
{
    if (m_directoryPanel1->GetValue().IsEmpty() || m_directoryPanel2->GetValue().IsEmpty())
    {
        wxMessageBox(_("Please select both left and right directories!"), _("Information"));
        return;
    }

    //check if directories exist if loaded by config file
    if (!wxDirExists(FreeFileSync::getFormattedDirectoryName(m_directoryPanel1->GetValue())))
    {
        wxMessageBox(_("Left directory does not exist. Please select a new one!"), _("Warning"), wxICON_WARNING);
        return;
    }
    else if (!wxDirExists(FreeFileSync::getFormattedDirectoryName(m_directoryPanel2->GetValue())))
    {
        wxMessageBox(_("Right directory does not exist. Please select a new one!"), _("Warning"), wxICON_WARNING);
        return;
    }

    //get a filename
#ifdef FFS_WIN
    wxString fileName = _("SyncJob.lnk"); //proposal
    wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, fileName, wxString(_("Shell link")) + wxT(" (*.lnk)|*.lnk"), wxFD_SAVE);
#elif defined FFS_LINUX
    wxString fileName = _("SyncJob.sh");  //proposal
    wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, fileName, wxString(_("Shell script")) + wxT(" (*.sh)|*.sh"), wxFD_SAVE);
#else
    assert(false);
#endif

    if (filePicker->ShowModal() == wxID_OK)
    {
        fileName = filePicker->GetPath();
        if (wxFileExists(fileName))
        {
            wxMessageDialog* messageDlg = new wxMessageDialog(this, wxString(wxT("\"")) + fileName + wxT("\"") + _(" already exists. Overwrite?"), _("Warning") , wxOK | wxCANCEL);

            if (messageDlg->ShowModal() != wxID_OK)
                return;
        }

        //create batch file
        if (createBatchFile(fileName))
            EndModal(batchFileCreated);
        else
            wxMessageBox(wxString(_("Could not create file ")) + wxT("\"") + fileName + wxT("\""), _("Error"), wxOK | wxICON_ERROR);
    }
}


wxString getFormattedSyncDirection(const SyncDirection direction)
{
    if (direction == SYNC_DIR_RIGHT)
        return wxChar('R');
    else if (direction == SYNC_DIR_LEFT)
        return wxChar('L');
    else if (direction == SYNC_DIR_NONE)
        return wxChar('N');
    else
    {
        assert (false);
        return wxEmptyString;
    }
}


wxString BatchDialog::getCommandlineArguments()
{
    wxString output;

    output+= wxString(wxT("-")) + GlobalResources::paramCompare + wxT(" ");
    if (m_radioBtnSizeDate->GetValue())
        output+= GlobalResources::valueSizeDate;
    else if (m_radioBtnContent->GetValue())
        output+= GlobalResources::valueContent;
    else
        assert(false);

    output+= wxString(wxT(" -")) + GlobalResources::paramSync + wxT(" ") +
             getFormattedSyncDirection(localSyncConfiguration.exLeftSideOnly) +
             getFormattedSyncDirection(localSyncConfiguration.exRightSideOnly) +
             getFormattedSyncDirection(localSyncConfiguration.leftNewer) +
             getFormattedSyncDirection(localSyncConfiguration.rightNewer) +
             getFormattedSyncDirection(localSyncConfiguration.different);

    if (filterIsActive)
    {
        output+= wxString(wxT(" -")) + GlobalResources::paramInclude + wxT(" ") +
                 wxT("\"") + m_textCtrlInclude->GetValue() + wxT("\"");

        output+= wxString(wxT(" -")) + GlobalResources::paramExclude + wxT(" ") +
                 wxT("\"") + m_textCtrlExclude->GetValue() + wxT("\"");
    }

    if (m_checkBoxUseRecycler->GetValue())
        output+= wxString(wxT(" -")) + GlobalResources::paramRecycler;

    if (m_checkBoxContinueError->GetValue())
        output+= wxString(wxT(" -")) + GlobalResources::paramContinueError;

    if (m_checkBoxSilent->GetValue())
        output+= wxString(wxT(" -")) + GlobalResources::paramSilent;

    wxString leftDir  = FreeFileSync::getFormattedDirectoryName(m_directoryPanel1->GetValue());
    wxString rightDir = FreeFileSync::getFormattedDirectoryName(m_directoryPanel2->GetValue());

    output+= wxString(wxT(" \"")) + wxDir(leftDir).GetName() + wxT("\""); //directory WITHOUT trailing path separator
    output+= wxString(wxT(" \"")) + wxDir(rightDir).GetName() + wxT("\""); //needed since e.g. "C:\" isn't parsed correctly by commandline

    return output;
}


template <typename T>
struct CleanUp
{
    CleanUp(T* element) : m_element(element) {}

    ~CleanUp()
    {
        m_element->Release();
    }

    T* m_element;
};


bool BatchDialog::createBatchFile(const wxString& filename)
{
#ifdef FFS_WIN
    //create shell link (instead of batch file) for full Unicode support
    HRESULT hResult = E_FAIL;
    IShellLink* pShellLink = NULL;

    if (FAILED(CoCreateInstance(CLSID_ShellLink,       //class identifier
                                NULL,                  //object isn't part of an aggregate
                                CLSCTX_INPROC_SERVER,  //context for running executable code
                                IID_IShellLink,        //interface identifier
                                (void**)&pShellLink))) //pointer to storage of interface pointer
        return false;
    CleanUp<IShellLink> cleanOnExit(pShellLink);

    wxString freeFileSyncExe = wxStandardPaths::Get().GetExecutablePath();
    if (FAILED(pShellLink->SetPath(freeFileSyncExe.c_str())))
        return false;

    if (FAILED(pShellLink->SetArguments(getCommandlineArguments().c_str())))
        return false;

    if (FAILED(pShellLink->SetIconLocation(freeFileSyncExe.c_str(), 1))) //second icon from executable file is used
        return false;

    if (FAILED(pShellLink->SetDescription(_("FreeFileSync Batch Job"))))
        return false;

    IPersistFile*  pPersistFile = NULL;
    if (FAILED(pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile)))
        return false;
    CleanUp<IPersistFile> cleanOnExit2(pPersistFile);

    //pPersistFile->Save accepts unicode input only
#ifdef _UNICODE
    hResult = pPersistFile->Save(filename.c_str(), TRUE);
#else
    WCHAR wszTemp [MAX_PATH];
    if (MultiByteToWideChar(CP_ACP, 0, filename.c_str(), -1, wszTemp, MAX_PATH) == 0)
        return false;

    hResult = pPersistFile->Save(wszTemp, TRUE);
#endif
    if (FAILED(hResult))
        return false;

    return true;

//############################################################################
#elif defined FFS_LINUX
    //create shell script
    wxFFile output(filename, wxT("w"));
    if (output.IsOpened())
    {
        wxString outputString;
        outputString+= wxT("#!/bin/bash\n"); //shell script identifier
        outputString+= wxString(wxT("\"")) + wxStandardPaths::Get().GetExecutablePath() + wxT("\" ");
        outputString+= getCommandlineArguments() + wxT("\n");

        if (!output.Write(outputString))
            return false;

        //for linux the batch file needs the executable flag
        output.Close();
        wxExecute(wxString(wxT("chmod +x ")) + filename);
        return true;
    }
    else
        return false;
#else
    adapt this!
#endif  // FFS_LINUX
}
bgstack15