summaryrefslogtreecommitdiff
path: root/ui/SyncDialog.cpp
blob: 58abc753e2705fb493c0c87ae8ef4a9b62188700 (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
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#include "syncDialog.h"
#include "../library/globalFunctions.h"
#include "../library/resources.h"
#include <wx/msgdlg.h>
#include <wx/stdpaths.h>
#include <wx/ffile.h>
#include "../library/customButton.h"
#include "../synchronization.h"
#include "../algorithm.h"
#include <wx/dnd.h>
#include "dragAndDrop.h"

using namespace FreeFileSync;


SyncDialog::SyncDialog(wxWindow* window,
                       const FolderComparison& folderCmpRef,
                       MainConfiguration& config,
                       bool& ignoreErrors) :
        SyncDlgGenerated(window),
        folderCmp(folderCmpRef),
        cfg(config),
        m_ignoreErrors(ignoreErrors)
{
    //make working copy of mainDialog.cfg.syncConfiguration and recycler setting
    localSyncConfiguration = config.syncConfiguration;
    m_checkBoxUseRecycler->SetValue(cfg.useRecycleBin);
    m_checkBoxIgnoreErrors->SetValue(m_ignoreErrors);

    //set sync config icons
    updateConfigIcons(cfg.compareVar, localSyncConfiguration);

    //set icons for this dialog
    m_bitmapLeftOnly->SetBitmap(*GlobalResources::getInstance().bitmapLeftOnly);
    m_bitmapRightOnly->SetBitmap(*GlobalResources::getInstance().bitmapRightOnly);
    m_bitmapLeftNewer->SetBitmap(*GlobalResources::getInstance().bitmapLeftNewer);
    m_bitmapRightNewer->SetBitmap(*GlobalResources::getInstance().bitmapRightNewer);
    m_bitmapDifferent->SetBitmap(*GlobalResources::getInstance().bitmapDifferent);

    bSizer201->Layout(); //wxButtonWithImage size might have changed

    //set radiobutton
    switch (localSyncConfiguration.getVariant())
    {
    case SyncConfiguration::MIRROR:
        m_radioBtn1->SetValue(true);    //one way ->
        break;
    case SyncConfiguration::UPDATE:
        m_radioBtnUpdate->SetValue(true);    //Update ->
        break;
    case SyncConfiguration::TWOWAY:
        m_radioBtn2->SetValue(true);    //two way <->
        break;
    case SyncConfiguration::CUSTOM:
        m_radioBtn3->SetValue(true);    //other
        break;
    }

    Fit();
}

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

SyncDialog::~SyncDialog() {}


void SyncDialog::updateConfigIcons(const FreeFileSync::CompareVariant cmpVar, const FreeFileSync::SyncConfiguration& syncConfig)
{
    updateConfigIcons(cmpVar,
                      syncConfig,
                      m_bpButtonLeftOnly,
                      m_bpButtonRightOnly,
                      m_bpButtonLeftNewer,
                      m_bpButtonRightNewer,
                      m_bpButtonDifferent,
                      m_bitmapLeftOnly,
                      m_bitmapRightOnly,
                      m_bitmapLeftNewer,
                      m_bitmapRightNewer,
                      m_bitmapDifferent);
}


void SyncDialog::updateConfigIcons(const CompareVariant compareVar,
                                   const SyncConfiguration& syncConfig,
                                   wxBitmapButton* buttonLeftOnly,
                                   wxBitmapButton* buttonRightOnly,
                                   wxBitmapButton* buttonLeftNewer,
                                   wxBitmapButton* buttonRightNewer,
                                   wxBitmapButton* buttonDifferent,
                                   wxStaticBitmap* bitmapLeftOnly,
                                   wxStaticBitmap* bitmapRightOnly,
                                   wxStaticBitmap* bitmapLeftNewer,
                                   wxStaticBitmap* bitmapRightNewer,
                                   wxStaticBitmap* bitmapDifferent)
{
    //display only relevant sync options
    switch (compareVar)
    {
    case CMP_BY_TIME_SIZE:
        buttonLeftOnly  ->Show();
        buttonRightOnly ->Show();
        buttonLeftNewer ->Show();
        buttonRightNewer->Show();
        buttonDifferent ->Hide();

        bitmapLeftOnly  ->Show();
        bitmapRightOnly ->Show();
        bitmapLeftNewer ->Show();
        bitmapRightNewer->Show();
        bitmapDifferent ->Hide();
        break;

    case CMP_BY_CONTENT:
        buttonLeftOnly  ->Show();
        buttonRightOnly ->Show();
        buttonLeftNewer ->Hide();
        buttonRightNewer->Hide();
        buttonDifferent ->Show();

        bitmapLeftOnly  ->Show();
        bitmapRightOnly ->Show();
        bitmapLeftNewer ->Hide();
        bitmapRightNewer->Hide();
        bitmapDifferent ->Show();
        break;
    }


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

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

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

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

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


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


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


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

    EndModal(BUTTON_OKAY);
}


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.setVariant(SyncConfiguration::MIRROR);

    updateConfigIcons(cfg.compareVar, localSyncConfiguration);

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


void SyncDialog::OnSyncUpdate(wxCommandEvent& event)
{
    localSyncConfiguration.setVariant(SyncConfiguration::UPDATE);

    updateConfigIcons(cfg.compareVar, localSyncConfiguration);

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


void SyncDialog::OnSyncBothSides(wxCommandEvent& event)
{
    localSyncConfiguration.setVariant(SyncConfiguration::TWOWAY);

    updateConfigIcons(cfg.compareVar, localSyncConfiguration);

    //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(cfg.compareVar, localSyncConfiguration);
    //set custom config button
    m_radioBtn3->SetValue(true);
}


void SyncDialog::OnExRightSideOnly( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.exRightSideOnly);
    updateConfigIcons(cfg.compareVar, localSyncConfiguration);
    //set custom config button
    m_radioBtn3->SetValue(true);
}


void SyncDialog::OnLeftNewer( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.leftNewer);
    updateConfigIcons(cfg.compareVar, localSyncConfiguration);
    //set custom config button
    m_radioBtn3->SetValue(true);
}


void SyncDialog::OnRightNewer( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.rightNewer);
    updateConfigIcons(cfg.compareVar, localSyncConfiguration);
    //set custom config button
    m_radioBtn3->SetValue(true);
}


void SyncDialog::OnDifferent( wxCommandEvent& event )
{
    toggleSyncDirection(localSyncConfiguration.different);
    updateConfigIcons(cfg.compareVar, localSyncConfiguration);
    //set custom config button
    m_radioBtn3->SetValue(true);
}
//###################################################################################################################################


class BatchFolderPairPanel : public BatchFolderPairGenerated
{
public:
    BatchFolderPairPanel(wxWindow* parent) :
            BatchFolderPairGenerated(parent),
            dragDropOnLeft(m_panelLeft, m_dirPickerLeft, m_directoryLeft),
            dragDropOnRight(m_panelRight, m_dirPickerRight, m_directoryRight) {}

private:
    //support for drag and drop
    DragDropOnDlg dragDropOnLeft;
    DragDropOnDlg dragDropOnRight;
};

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


class BatchFileDropEvent : public wxFileDropTarget
{
public:
    BatchFileDropEvent(BatchDialog* dlg) :
            batchDlg(dlg) {}

    virtual bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames)
    {
        if (!filenames.IsEmpty())
        {
            const wxString droppedFileName = filenames[0];

            xmlAccess::XmlType fileType = xmlAccess::getXmlType(droppedFileName);

            //test if ffs batch file has been dropped
            if (fileType == xmlAccess::XML_BATCH_CONFIG)
                batchDlg->loadBatchFile(droppedFileName);
            else
            {
                wxString errorMessage = _("%x is not a valid FreeFileSync batch file!");
                errorMessage.Replace(wxT("%x"), wxString(wxT("\"")) + droppedFileName + wxT("\""), false);
                wxMessageBox(errorMessage, _("Error"), wxOK | wxICON_ERROR);
            }
        }
        return false;
    }

private:
    BatchDialog* batchDlg;
};


BatchDialog::BatchDialog(wxWindow* window, const xmlAccess::XmlBatchConfig& batchCfg) :
        BatchDlgGenerated(window)
{
    init();
    loadBatchCfg(batchCfg);
}


BatchDialog::BatchDialog(wxWindow* window, const wxString& filename) :
        BatchDlgGenerated(window)
{
    init();
    loadBatchFile(filename);
}


void BatchDialog::init()
{
    //prepare drag & drop for loading of *.ffs_batch files
    SetDropTarget(new BatchFileDropEvent(this));

    dragDropOnLogfileDir.reset(new DragDropOnDlg(m_panelLogging, m_dirPickerLogfileDir, m_textCtrlLogfileDir));

    //set icons for this dialog
    m_bitmapLeftOnly->SetBitmap(*GlobalResources::getInstance().bitmapLeftOnly);
    m_bitmapRightOnly->SetBitmap(*GlobalResources::getInstance().bitmapRightOnly);
    m_bitmapLeftNewer->SetBitmap(*GlobalResources::getInstance().bitmapLeftNewer);
    m_bitmapRightNewer->SetBitmap(*GlobalResources::getInstance().bitmapRightNewer);
    m_bitmapDifferent->SetBitmap(*GlobalResources::getInstance().bitmapDifferent);
    m_bitmap8->SetBitmap(*GlobalResources::getInstance().bitmapInclude);
    m_bitmap9->SetBitmap(*GlobalResources::getInstance().bitmapExclude);
    m_bitmap27->SetBitmap(*GlobalResources::getInstance().bitmapBatch);
}


xmlAccess::OnError BatchDialog::getSelectionHandleError()
{
    switch (m_choiceHandleError->GetSelection())
    {
    case 0:
        return xmlAccess::ON_ERROR_POPUP;
    case 1:
        return xmlAccess::ON_ERROR_IGNORE;
    case 2:
        return xmlAccess::ON_ERROR_EXIT;
    default:
        assert(false);
        return xmlAccess::ON_ERROR_POPUP;
    }
}


void updateToolTip(wxChoice* choiceHandleError, const xmlAccess::OnError value)
{
    switch (value)
    {
    case xmlAccess::ON_ERROR_POPUP:
        choiceHandleError->SetToolTip(_("Show popup on errors or warnings"));
        break;
    case xmlAccess::ON_ERROR_IGNORE:
        choiceHandleError->SetToolTip(_("Hide all error and warning messages"));
        break;
    case xmlAccess::ON_ERROR_EXIT:
        choiceHandleError->SetToolTip(_("Exit immediately and set returncode < 0"));
        break;
    default:
        assert(false);
        choiceHandleError->SetToolTip(wxEmptyString);
    }
}


void BatchDialog::setSelectionHandleError(const xmlAccess::OnError value)
{
    m_choiceHandleError->Clear();
    m_choiceHandleError->Append(_("Show popup"));
    m_choiceHandleError->Append(_("Ignore errors"));
    m_choiceHandleError->Append(_("Exit with RC < 0"));

    switch (value)
    {
    case xmlAccess::ON_ERROR_POPUP:
        m_choiceHandleError->SetSelection(0);
        break;
    case xmlAccess::ON_ERROR_IGNORE:
        m_choiceHandleError->SetSelection(1);
        break;
    case xmlAccess::ON_ERROR_EXIT:
        m_choiceHandleError->SetSelection(2);
        break;
    default:
        assert(false);
        m_choiceHandleError->SetSelection(0);
    }

    updateToolTip(m_choiceHandleError, getSelectionHandleError());
}


void BatchDialog::OnChangeErrorHandling(wxCommandEvent& event)
{
    updateToolTip(m_choiceHandleError, getSelectionHandleError());
}


void BatchDialog::OnExLeftSideOnly(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.exLeftSideOnly);
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
}


void BatchDialog::OnExRightSideOnly(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.exRightSideOnly);
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
}


void BatchDialog::OnLeftNewer(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.leftNewer);
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
}


void BatchDialog::OnRightNewer(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.rightNewer);
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
}


void BatchDialog::OnDifferent(wxCommandEvent& event)
{
    toggleSyncDirection(localSyncConfiguration.different);
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
}


void BatchDialog::OnCheckFilter(wxCommandEvent& event)
{
    updateVisibleTabs();
}


void BatchDialog::OnCheckLogging(wxCommandEvent& event)
{
    updateVisibleTabs();
}


void BatchDialog::updateVisibleTabs()
{
    showNotebookpage(m_panelFilter, _("Filter"), m_checkBoxFilter->GetValue());
    showNotebookpage(m_panelLogging, _("Logging"), m_checkBoxSilent->GetValue());
}


void BatchDialog::showNotebookpage(wxWindow* page, const wxString& pageName, bool show)
{
    int windowPosition = -1;
    for (size_t i = 0; i < m_notebookSettings->GetPageCount(); ++i)
        if (    static_cast<wxWindow*>(m_notebookSettings->GetPage(i)) ==
                static_cast<wxWindow*>(page))
        {
            windowPosition = i;
            break;
        }

    if (show)
    {
        if (windowPosition == -1)
            m_notebookSettings->AddPage(page, pageName, false);
    }
    else
    {
        if (windowPosition != -1)
        {
            //do not delete currently selected tab!!
            if (m_notebookSettings->GetCurrentPage() == m_notebookSettings->GetPage(windowPosition))
                m_notebookSettings->ChangeSelection(0);

            m_notebookSettings->RemovePage(windowPosition);
        }
    }
}


void BatchDialog::OnSelectRecycleBin(wxCommandEvent& event)
{
    if (m_checkBoxUseRecycler->GetValue())
    {
        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);
        }
    }
}


CompareVariant BatchDialog::getCurrentCompareVar()
{
    if (m_radioBtnSizeDate->GetValue())
        return CMP_BY_TIME_SIZE;
    else if (m_radioBtnContent->GetValue())
        return CMP_BY_CONTENT;
    else
    {
        assert(false);
        return CMP_BY_TIME_SIZE;
    }
}


void BatchDialog::updateConfigIcons(const FreeFileSync::CompareVariant cmpVar, const FreeFileSync::SyncConfiguration& syncConfig)
{
    SyncDialog::updateConfigIcons(cmpVar,
                                  syncConfig,
                                  m_bpButtonLeftOnly,
                                  m_bpButtonRightOnly,
                                  m_bpButtonLeftNewer,
                                  m_bpButtonRightNewer,
                                  m_bpButtonDifferent,
                                  m_bitmapLeftOnly,
                                  m_bitmapRightOnly,
                                  m_bitmapLeftNewer,
                                  m_bitmapRightNewer,
                                  m_bitmapDifferent);
}


void BatchDialog::OnChangeCompareVar(wxCommandEvent& event)
{
    updateConfigIcons(getCurrentCompareVar(), localSyncConfiguration);
    Fit();
}


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


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


void BatchDialog::OnSaveBatchJob(wxCommandEvent& event)
{
    //get a filename
    const wxString defaultFileName = proposedBatchFileName.empty() ? wxT("SyncJob.ffs_batch") : proposedBatchFileName;

    wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, defaultFileName, wxString(_("FreeFileSync batch file")) + wxT(" (*.ffs_batch)|*.ffs_batch"), wxFD_SAVE);
    if (filePicker->ShowModal() == wxID_OK)
    {
        const wxString newFileName = filePicker->GetPath();
        if (FreeFileSync::fileExists(newFileName.c_str()))
        {
            wxMessageDialog* messageDlg = new wxMessageDialog(this, wxString(_("File already exists. Overwrite?")) + wxT(" \"") + newFileName + wxT("\""), _("Warning") , wxOK | wxCANCEL);

            if (messageDlg->ShowModal() != wxID_OK)
            {
                OnSaveBatchJob(event); //retry
                return;
            }
        }

        //create batch file
        if (saveBatchFile(newFileName))
            EndModal(BATCH_FILE_SAVED);
    }
}


void BatchDialog::OnLoadBatchJob(wxCommandEvent& event)
{
    wxFileDialog* filePicker = new wxFileDialog(this, wxEmptyString, wxEmptyString, wxEmptyString, wxString(_("FreeFileSync batch file")) + wxT(" (*.ffs_batch)|*.ffs_batch"), wxFD_OPEN);;
    if (filePicker->ShowModal() == wxID_OK)
        loadBatchFile(filePicker->GetPath());
}


bool BatchDialog::saveBatchFile(const wxString& filename)
{
    xmlAccess::XmlBatchConfig batchCfg;

    //load structure with basic settings "mainCfg"
    batchCfg.mainCfg.compareVar        = getCurrentCompareVar();
    batchCfg.mainCfg.syncConfiguration = localSyncConfiguration;
    batchCfg.mainCfg.filterIsActive    = m_checkBoxFilter->GetValue();
    batchCfg.mainCfg.includeFilter     = m_textCtrlInclude->GetValue();
    batchCfg.mainCfg.excludeFilter     = m_textCtrlExclude->GetValue();
    batchCfg.mainCfg.useRecycleBin     = m_checkBoxUseRecycler->GetValue();
    batchCfg.handleError               = getSelectionHandleError();

    for (unsigned int i = 0; i < localFolderPairs.size(); ++i)
    {
        FolderPair newPair;
        newPair.leftDirectory  = localFolderPairs[i]->m_directoryLeft->GetValue().c_str();
        newPair.rightDirectory = localFolderPairs[i]->m_directoryRight->GetValue().c_str();

        batchCfg.directoryPairs.push_back(newPair);
    }

    //load structure with batch settings "batchCfg"
    batchCfg.silent = m_checkBoxSilent->GetValue();
    batchCfg.logFileDirectory = m_textCtrlLogfileDir->GetValue();

    //write config to XML
    try
    {
        xmlAccess::writeBatchConfig(filename, batchCfg);
    }
    catch (const FileError& error)
    {
        wxMessageBox(error.show().c_str(), _("Error"), wxOK | wxICON_ERROR);
        return false;
    }

    SetTitle(wxString(_("Create a batch job")) + wxT(" - ") + filename);
    proposedBatchFileName = filename; //may be used on next save

    return true;
}


void BatchDialog::loadBatchFile(const wxString& filename)
{
    //load XML settings
    xmlAccess::XmlBatchConfig batchCfg;  //structure to receive gui settings
    try
    {
        batchCfg = xmlAccess::readBatchConfig(filename);
    }
    catch (const FileError& error)
    {
        wxMessageBox(error.show().c_str(), _("Error"), wxOK | wxICON_ERROR);
        return;
    }

    SetTitle(wxString(_("Create a batch job")) + wxT(" - ") + filename);
    proposedBatchFileName = filename; //may be used on next save

    this->loadBatchCfg(batchCfg);
}


void BatchDialog::loadBatchCfg(const xmlAccess::XmlBatchConfig& batchCfg)
{
    //make working copy of mainDialog.cfg.syncConfiguration and recycler setting
    localSyncConfiguration = batchCfg.mainCfg.syncConfiguration;

    m_checkBoxUseRecycler->SetValue(batchCfg.mainCfg.useRecycleBin);
    setSelectionHandleError(batchCfg.handleError);

    switch (batchCfg.mainCfg.compareVar)
    {
    case CMP_BY_TIME_SIZE:
        m_radioBtnSizeDate->SetValue(true);
        break;
    case CMP_BY_CONTENT:
        m_radioBtnContent->SetValue(true);
        break;
    }

    updateConfigIcons(batchCfg.mainCfg.compareVar, batchCfg.mainCfg.syncConfiguration);

    m_checkBoxFilter->SetValue(batchCfg.mainCfg.filterIsActive);
    m_textCtrlInclude->SetValue(batchCfg.mainCfg.includeFilter);
    m_textCtrlExclude->SetValue(batchCfg.mainCfg.excludeFilter);

    m_checkBoxSilent->SetValue(batchCfg.silent);
    m_textCtrlLogfileDir->SetValue(batchCfg.logFileDirectory);

    //remove existing folder pairs
    localFolderPairs.clear();
    bSizerFolderPairs->Clear(true);

    //add folder pairs
    int scrWindowHeight = 0;
    for (std::vector<FolderPair>::const_iterator i = batchCfg.directoryPairs.begin(); i != batchCfg.directoryPairs.end(); ++i)
    {
        BatchFolderPairPanel* newPair = new BatchFolderPairPanel(m_scrolledWindow6);
        newPair->m_directoryLeft->SetValue(i->leftDirectory.c_str());
        newPair->m_directoryRight->SetValue(i->rightDirectory.c_str());

        bSizerFolderPairs->Add( newPair, 0, wxEXPAND, 5);
        localFolderPairs.push_back(newPair);

        if (i == batchCfg.directoryPairs.begin())
            scrWindowHeight = newPair->GetSize().GetHeight();
    }
    //set size of scrolled window
    int pairCount = std::min(localFolderPairs.size(), size_t(3)); //up to 3 additional pairs shall be shown
    m_scrolledWindow6->SetMinSize(wxSize( -1, scrWindowHeight * pairCount));

    updateVisibleTabs();

    m_scrolledWindow6->Layout(); //needed
    m_panelOverview->Layout(); //needed

    Fit(); //needed
    Centre();
    m_buttonSave->SetFocus();
}


/*
#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"
#include <shlobj.h>
#endif  // FFS_WIN

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

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

    T* m_element;
};


bool BatchDialog::createBatchFile(const wxString& filename)
{
    //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;
}
*/
bgstack15