summaryrefslogtreecommitdiff
path: root/ui/sync_cfg.cpp
blob: d0d32c18a88023f165b1d97b81da76b1bd58e45b (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
// **************************************************************************
// * 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) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "sync_cfg.h"
#include <memory>
#include <zen/format_unit.h>
#include <wx/wupdlock.h>
#include <wx+/mouse_move_dlg.h>
#include <wx+/rtl.h>
#include <wx+/no_flicker.h>
#include <wx+/choice_enum.h>
#include <wx+/image_tools.h>
#include <wx+/font_size.h>
#include <wx+/std_button_order.h>
#include "gui_generated.h"
#include "exec_finished_box.h"
#include "dir_name.h"
#include "../file_hierarchy.h"
#include "../lib/resources.h"

using namespace zen;
using namespace xmlAccess;


class SyncCfgDialog : public SyncCfgDlgGenerated
{
public:
    SyncCfgDialog(wxWindow* parent,
                  CompareVariant compareVar,
                  SyncConfig&    syncCfg,
                  xmlAccess::OnGuiError* handleError,     //
                  ExecWhenFinishedCfg* execWhenFinished); //optional input parameter

private:
    virtual void OnSyncTwoWay(wxCommandEvent& event) { directionCfg.var = DirectionConfig::TWOWAY; updateGui(); }
    virtual void OnSyncMirror(wxCommandEvent& event) { directionCfg.var = DirectionConfig::MIRROR; updateGui(); }
    virtual void OnSyncUpdate(wxCommandEvent& event) { directionCfg.var = DirectionConfig::UPDATE; updateGui(); }
    virtual void OnSyncCustom(wxCommandEvent& event) { directionCfg.var = DirectionConfig::CUSTOM; updateGui(); }

    virtual void OnToggleDetectMovedFiles(wxCommandEvent& event) { directionCfg.detectMovedFiles = !directionCfg.detectMovedFiles; updateGui(); }

    virtual void OnSyncTwoWayDouble(wxMouseEvent& event);
    virtual void OnSyncMirrorDouble(wxMouseEvent& event);
    virtual void OnSyncUpdateDouble(wxMouseEvent& event);
    virtual void OnSyncCustomDouble(wxMouseEvent& event);

    virtual void OnExLeftSideOnly (wxCommandEvent& event);
    virtual void OnExRightSideOnly(wxCommandEvent& event);
    virtual void OnLeftNewer      (wxCommandEvent& event);
    virtual void OnRightNewer     (wxCommandEvent& event);
    virtual void OnDifferent      (wxCommandEvent& event);
    virtual void OnConflict       (wxCommandEvent& event);

    virtual void OnClose (wxCloseEvent&   event) { EndModal(ReturnSyncConfig::BUTTON_CANCEL); }
    virtual void OnCancel(wxCommandEvent& event) { EndModal(ReturnSyncConfig::BUTTON_CANCEL); }
    virtual void OnApply (wxCommandEvent& event);

    virtual void OnParameterChange(wxCommandEvent& event) { updateGui(); }

    virtual void OnDeletionPermanent  (wxCommandEvent& event) { handleDeletion = DELETE_PERMANENTLY;   updateGui(); }
    virtual void OnDeletionRecycler   (wxCommandEvent& event) { handleDeletion = DELETE_TO_RECYCLER;   updateGui(); }
    virtual void OnDeletionVersioning (wxCommandEvent& event) { handleDeletion = DELETE_TO_VERSIONING; updateGui(); }

    virtual void OnErrorPopup (wxCommandEvent& event) { onGuiError = ON_GUIERROR_POPUP;  updateGui(); }
    virtual void OnErrorIgnore(wxCommandEvent& event) { onGuiError = ON_GUIERROR_IGNORE; updateGui(); }

    struct Config
    {
        SyncConfig syncCfg;
        xmlAccess::OnGuiError onGuiError;
        std::wstring onCompletion;
    };
    void setConfig(const Config& cfg);
    Config getConfig() const;

    void updateGui();

    //parameters with ownership NOT within GUI controls!
    DirectionConfig directionCfg;
    DeletionPolicy handleDeletion; //use Recycler, delete permanently or move to user-defined location
    OnGuiError onGuiError;

    //output data
    SyncConfig&            outSyncCfg;
    xmlAccess::OnGuiError* outOptOnGuiError;
    ExecWhenFinishedCfg*   outOptExecWhenFinished;

    CompareVariant compareVar_;
    DirectoryName<FolderHistoryBox> versioningFolder;

    EnumDescrList<VersioningStyle> enumVersioningStyle;
};


void updateConfigIcons(const DirectionConfig& directionCfg,
                       wxBitmapButton* buttonLeftOnly,
                       wxBitmapButton* buttonRightOnly,
                       wxBitmapButton* buttonLeftNewer,
                       wxBitmapButton* buttonRightNewer,
                       wxBitmapButton* buttonDifferent,
                       wxBitmapButton* buttonConflict)
{
    if (directionCfg.var != DirectionConfig::TWOWAY) //automatic mode needs no sync-directions
    {
        const DirectionSet dirCfg = extractDirections(directionCfg);

        switch (dirCfg.exLeftSideOnly)
        {
            case SyncDirection::RIGHT:
                buttonLeftOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_create_right")));
                buttonLeftOnly->SetToolTip(getSyncOpDescription(SO_CREATE_NEW_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonLeftOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_delete_left")));
                buttonLeftOnly->SetToolTip(getSyncOpDescription(SO_DELETE_LEFT));
                break;
            case SyncDirection::NONE:
                buttonLeftOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_none")));
                buttonLeftOnly->SetToolTip(getSyncOpDescription(SO_DO_NOTHING));
                break;
        }

        switch (dirCfg.exRightSideOnly)
        {
            case SyncDirection::RIGHT:
                buttonRightOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_delete_right")));
                buttonRightOnly->SetToolTip(getSyncOpDescription(SO_DELETE_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonRightOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_create_left")));
                buttonRightOnly->SetToolTip(getSyncOpDescription(SO_CREATE_NEW_LEFT));
                break;
            case SyncDirection::NONE:
                buttonRightOnly->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_none")));
                buttonRightOnly->SetToolTip(getSyncOpDescription(SO_DO_NOTHING));
                break;
        }

        switch (dirCfg.leftNewer)
        {
            case SyncDirection::RIGHT:
                buttonLeftNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_right")));
                buttonLeftNewer->SetToolTip(getSyncOpDescription(SO_OVERWRITE_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonLeftNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_left")));
                buttonLeftNewer->SetToolTip(getSyncOpDescription(SO_OVERWRITE_LEFT));
                break;
            case SyncDirection::NONE:
                buttonLeftNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_none")));
                buttonLeftNewer->SetToolTip(getSyncOpDescription(SO_DO_NOTHING));
                break;
        }

        switch (dirCfg.rightNewer)
        {
            case SyncDirection::RIGHT:
                buttonRightNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_right")));
                buttonRightNewer->SetToolTip(getSyncOpDescription(SO_OVERWRITE_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonRightNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_left")));
                buttonRightNewer->SetToolTip(getSyncOpDescription(SO_OVERWRITE_LEFT));
                break;
            case SyncDirection::NONE:
                buttonRightNewer->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_none")));
                buttonRightNewer->SetToolTip(getSyncOpDescription(SO_DO_NOTHING));
                break;
        }

        switch (dirCfg.different)
        {
            case SyncDirection::RIGHT:
                buttonDifferent->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_right")));
                buttonDifferent->SetToolTip(getSyncOpDescription(SO_OVERWRITE_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonDifferent->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_left")));
                buttonDifferent->SetToolTip(getSyncOpDescription(SO_OVERWRITE_LEFT));
                break;
            case SyncDirection::NONE:
                buttonDifferent->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_none")));
                buttonDifferent->SetToolTip(getSyncOpDescription(SO_DO_NOTHING));
                break;
        }

        switch (dirCfg.conflict)
        {
            case SyncDirection::RIGHT:
                buttonConflict->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_right")));
                buttonConflict->SetToolTip(getSyncOpDescription(SO_OVERWRITE_RIGHT));
                break;
            case SyncDirection::LEFT:
                buttonConflict->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"so_update_left")));
                buttonConflict->SetToolTip(getSyncOpDescription(SO_OVERWRITE_LEFT));
                break;
            case SyncDirection::NONE:
                buttonConflict->SetBitmapLabel(mirrorIfRtl(getResourceImage(L"cat_conflict"))); //silent dependency to algorithm.cpp::Redetermine!!!
                buttonConflict->SetToolTip(_("Leave as unresolved conflict"));
                break;
        }
    }
}


SyncCfgDialog::SyncCfgDialog(wxWindow* parent,
                             CompareVariant compareVar,
                             SyncConfig&    syncCfg,
                             xmlAccess::OnGuiError* handleError,
                             ExecWhenFinishedCfg* execWhenFinished) :
    SyncCfgDlgGenerated(parent),
    handleDeletion(DELETE_TO_RECYCLER), //
    onGuiError(ON_GUIERROR_POPUP),      //dummy init
    outSyncCfg(syncCfg),
    outOptOnGuiError(handleError),
    outOptExecWhenFinished(execWhenFinished),
    compareVar_(compareVar),
    versioningFolder(*m_panelVersioning, *m_buttonSelectDirVersioning, *m_versioningFolder/*, m_staticTextResolvedPath*/)
{
#ifdef ZEN_WIN
    new zen::MouseMoveWindow(*this); //allow moving main dialog by clicking (nearly) anywhere...; ownership passed to "this"
#endif
    setStandardButtonOrder(*bSizerStdButtons, StdButtons().setAffirmative(m_buttonOK).setCancel(m_buttonCancel));

    //set icons for this dialog
    m_bitmapLeftOnly  ->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_left_only"  ))));
    m_bitmapRightOnly ->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_right_only" ))));
    m_bitmapLeftNewer ->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_left_newer" ))));
    m_bitmapRightNewer->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_right_newer"))));
    m_bitmapDifferent ->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_different"  ))));
    m_bitmapConflict  ->SetBitmap(mirrorIfRtl(greyScale(getResourceImage(L"cat_conflict"   ))));
    m_bitmapDatabase  ->SetBitmap(getResourceImage(L"database"));

    m_toggleBtnTwoWay->SetLabel(L"<- " + _("Two way") + L" ->");
    m_toggleBtnMirror->SetLabel(         _("Mirror")  + L" ->>");
    m_toggleBtnUpdate->SetLabel(         _("Update")  + L" ->");

    setRelativeFontSize(*m_toggleBtnTwoWay, 1.25);
    setRelativeFontSize(*m_toggleBtnMirror, 1.25);
    setRelativeFontSize(*m_toggleBtnUpdate, 1.25);
    setRelativeFontSize(*m_toggleBtnCustom, 1.25);

    enumVersioningStyle.
    add(VER_STYLE_REPLACE,       _("Replace"),    _("Move files and replace if existing")).
    add(VER_STYLE_ADD_TIMESTAMP, _("Time stamp"), _("Append a timestamp to each file name"));

    //hide controls for optional parameters
    if (!handleError && !execWhenFinished) //currently either both or neither are bound!
    {
        bSizerExtraConfig->Show(false);
        Layout();
    }

    if (execWhenFinished)
        m_comboBoxExecFinished->initHistory(*execWhenFinished->history, execWhenFinished->historyMax);

    Config newCfg = { syncCfg,
                      handleError ?* handleError : ON_GUIERROR_POPUP,
                      execWhenFinished ?* execWhenFinished->command : std::wstring()
                    };
    setConfig(newCfg);

    Fit();

    m_buttonOK->SetFocus();
}

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

void SyncCfgDialog::setConfig(const Config& cfg)
{
    directionCfg   = cfg.syncCfg.directionCfg;  //make working copy; ownership *not* on GUI
    handleDeletion = cfg.syncCfg.handleDeletion;

    versioningFolder.setName(utfCvrtTo<wxString>(cfg.syncCfg.versioningDirectory));
    setEnumVal(enumVersioningStyle, *m_choiceVersioningStyle,  cfg.syncCfg.versioningStyle);

    ////map single parameter "version limit" to both checkbox and spin ctrl:
    //m_checkBoxVersionsLimit->SetValue(cfg.syncCfg.versionCountLimit >= 0);
    //m_spinCtrlVersionsLimit->SetValue(cfg.syncCfg.versionCountLimit >= 0 ? cfg.syncCfg.versionCountLimit : 10 /*SyncConfig().versionCountLimit*/);

    onGuiError = cfg.onGuiError;

    m_comboBoxExecFinished->setValue(cfg.onCompletion);

    updateGui();
}


SyncCfgDialog::Config SyncCfgDialog::getConfig() const
{
    Config output;

    //write configuration to main dialog
    output.syncCfg.directionCfg        = directionCfg;
    output.syncCfg.handleDeletion      = handleDeletion;
    output.syncCfg.versioningDirectory = utfCvrtTo<Zstring>(versioningFolder.getName());
    output.syncCfg.versioningStyle     = getEnumVal(enumVersioningStyle, *m_choiceVersioningStyle),

                   //get single parameter "version limit" from both checkbox and spin ctrl:
                   //   output.syncCfg.versionCountLimit   = m_checkBoxVersionsLimit->GetValue() ? m_spinCtrlVersionsLimit->GetValue() : -1;

                   output.onGuiError = onGuiError;

    output.onCompletion = m_comboBoxExecFinished->getValue();
    return output;
}


void SyncCfgDialog::updateGui()
{
    wxWindowUpdateLocker dummy(this); //avoid display distortion
    wxWindowUpdateLocker dummy2(m_panelVersioning); //avoid display distortion
    wxWindowUpdateLocker dummy3(m_bpButtonLeftOnly);
    wxWindowUpdateLocker dummy4(m_bpButtonRightOnly);
    wxWindowUpdateLocker dummy5(m_bpButtonLeftNewer);
    wxWindowUpdateLocker dummy6(m_bpButtonRightNewer);
    wxWindowUpdateLocker dummy7(m_bpButtonDifferent);
    wxWindowUpdateLocker dummy8(m_bpButtonConflict);

    const Config cfg = getConfig(); //resolve parameter ownership: some on GUI controls, others member variables

    updateConfigIcons(cfg.syncCfg.directionCfg,
                      m_bpButtonLeftOnly,
                      m_bpButtonRightOnly,
                      m_bpButtonLeftNewer,
                      m_bpButtonRightNewer,
                      m_bpButtonDifferent,
                      m_bpButtonConflict);

    //selecting "detect move files" does not always make sense:
    m_checkBoxDetectMove->Enable(detectMovedFilesSelectable(directionCfg));
    m_checkBoxDetectMove->SetValue(detectMovedFilesEnabled(directionCfg)); //parameter NOT owned by checkbox!

    //display only relevant sync options
    m_bitmapDatabase     ->Show(cfg.syncCfg.directionCfg.var == DirectionConfig::TWOWAY);
    sbSizerSyncDirections->Show(cfg.syncCfg.directionCfg.var != DirectionConfig::TWOWAY);

    switch (compareVar_) //sbSizerSyncDirections->Show resets child sizers!
    {
        case CMP_BY_TIME_SIZE:
            bSizerDifferent ->Show(false);
            break;

        case CMP_BY_CONTENT:
            bSizerLeftNewer ->Show(false);
            bSizerRightNewer->Show(false);
            break;
    }
    bSizerConfig->Layout(); //[!]

    //update toggle buttons -> they have no parameter-ownership at all!
    m_staticTextAutomatic->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
    m_staticTextMirror   ->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
    m_staticTextUpdate   ->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));
    m_staticTextCustom   ->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT));

    m_toggleBtnTwoWay->SetValue(false);
    m_toggleBtnMirror->SetValue(false);
    m_toggleBtnUpdate->SetValue(false);
    m_toggleBtnCustom->SetValue(false);

    switch (cfg.syncCfg.directionCfg.var)
    {
        case DirectionConfig::TWOWAY:
            m_toggleBtnTwoWay->SetValue(true);
            m_staticTextAutomatic->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
            break;
        case DirectionConfig::MIRROR:
            m_toggleBtnMirror->SetValue(true);
            m_staticTextMirror->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
            break;
        case DirectionConfig::UPDATE:
            m_toggleBtnUpdate->SetValue(true);
            m_staticTextUpdate->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
            break;
        case DirectionConfig::CUSTOM:
            m_toggleBtnCustom->SetValue(true);
            m_staticTextCustom->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
            break;
    }

    m_toggleBtnPermanent ->SetValue(false);
    m_toggleBtnRecycler  ->SetValue(false);
    m_toggleBtnVersioning->SetValue(false);
    switch (cfg.syncCfg.handleDeletion)
    {
        case DELETE_PERMANENTLY:
            m_toggleBtnPermanent->SetValue(true);
            break;
        case DELETE_TO_RECYCLER:
            m_toggleBtnRecycler->SetValue(true);
            break;
        case DELETE_TO_VERSIONING:
            m_toggleBtnVersioning->SetValue(true);
            break;
    }

    const bool versioningSelected = cfg.syncCfg.handleDeletion == DELETE_TO_VERSIONING;
    bSizerVersioningNamingConvention->Show(versioningSelected);
    bSizerVersioningStyle           ->Show(versioningSelected);
    m_panelVersioning               ->Show(versioningSelected);

    if (versioningSelected)
    {
        updateTooltipEnumVal(enumVersioningStyle, *m_choiceVersioningStyle);

        const std::wstring pathSep = utfCvrtTo<std::wstring>(FILE_NAME_SEPARATOR);
        switch (cfg.syncCfg.versioningStyle)
        {
            case VER_STYLE_REPLACE:
                setText(*m_staticTextNamingCvtPart1, pathSep + _("Folder") + pathSep + _("File") + L".doc");
                setText(*m_staticTextNamingCvtPart2Bold, L"");
                setText(*m_staticTextNamingCvtPart3, L"");
                break;

            case VER_STYLE_ADD_TIMESTAMP:
                setText(*m_staticTextNamingCvtPart1, pathSep + _("Folder") + pathSep + _("File") + L".doc ");
                setText(*m_staticTextNamingCvtPart2Bold, _("YYYY-MM-DD hhmmss"));
                setText(*m_staticTextNamingCvtPart3, L".doc");
                break;
        }
    }

    //m_spinCtrlVersionsLimit->Enable(m_checkBoxVersionsLimit->GetValue()); //enabled status is *not* directly dependent from resolved config! (but transitively)

    m_toggleBtnErrorIgnore->SetValue(false);
    m_toggleBtnErrorPopup ->SetValue(false);
    switch (cfg.onGuiError)
    {
        case ON_GUIERROR_IGNORE:
            m_toggleBtnErrorIgnore->SetValue(true);
            break;
        case ON_GUIERROR_POPUP:
            m_toggleBtnErrorPopup->SetValue(true);
            break;
    }

    Layout();
    bSizerNamingConvention->Layout(); //[!] not handled by previous "Layout"
    Refresh(); //removes a few artifacts when toggling display of versioning folder
    GetSizer()->SetSizeHints(this); //this works like a charm for GTK2 with window resizing problems!!! (includes call to Fit())
}


void SyncCfgDialog::OnApply(wxCommandEvent& event)
{
    const Config cfg = getConfig();

    //write configuration to main dialog
    outSyncCfg = cfg.syncCfg;

    if (outOptOnGuiError)
        *outOptOnGuiError = cfg.onGuiError;

    if (outOptExecWhenFinished)
    {
        *outOptExecWhenFinished->command = cfg.onCompletion;
        //a good place to commit current "on completion" history item
        m_comboBoxExecFinished->addItemHistory();
    }

    EndModal(ReturnSyncConfig::BUTTON_OKAY);
}


void SyncCfgDialog::OnSyncTwoWayDouble(wxMouseEvent& event)
{
    wxCommandEvent dummy;
    OnSyncTwoWay(dummy);
    OnApply(dummy);
}

void SyncCfgDialog::OnSyncMirrorDouble(wxMouseEvent& event)
{
    wxCommandEvent dummy;
    OnSyncMirror(dummy);
    OnApply(dummy);
}

void SyncCfgDialog::OnSyncUpdateDouble(wxMouseEvent& event)
{
    wxCommandEvent dummy;
    OnSyncUpdate(dummy);
    OnApply(dummy);
}

void SyncCfgDialog::OnSyncCustomDouble(wxMouseEvent& event)
{
    wxCommandEvent dummy;
    OnSyncCustom(dummy);
    OnApply(dummy);
}

namespace
{
void toggleSyncDirection(SyncDirection& current)
{
    switch (current)
    {
        case SyncDirection::RIGHT:
            current = SyncDirection::LEFT;
            break;
        case SyncDirection::LEFT:
            current = SyncDirection::NONE;
            break;
        case SyncDirection::NONE:
            current = SyncDirection::RIGHT;
            break;
    }
}


void pressCustomDir(DirectionConfig& directionCfg, SyncDirection& syncdir)
{
    switch (directionCfg.var)
    {
        case DirectionConfig::TWOWAY:
            assert(false);
            break;
        case DirectionConfig::MIRROR:
        case DirectionConfig::UPDATE:
            directionCfg.custom = extractDirections(directionCfg);
            directionCfg.var = DirectionConfig::CUSTOM;
            toggleSyncDirection(syncdir);
            break;
        case DirectionConfig::CUSTOM:
            toggleSyncDirection(syncdir);

            //some config optimization: if custom settings happen to match "mirror" or "update", just switch variant
            const DirectionSet setMirror = []
            {
                DirectionConfig mirrorCfg;
                mirrorCfg.var = DirectionConfig::MIRROR;
                return extractDirections(mirrorCfg);
            }();

            const DirectionSet setUpdate = []
            {
                DirectionConfig updateCfg;
                updateCfg.var = DirectionConfig::UPDATE;
                return extractDirections(updateCfg);
            }();

            const DirectionSet currentSet = extractDirections(directionCfg);
            if (currentSet == setMirror)
                directionCfg.var = DirectionConfig::MIRROR;
            else if (currentSet == setUpdate)
                directionCfg.var = DirectionConfig::UPDATE;
            break;
    }
}
}

void SyncCfgDialog::OnExLeftSideOnly(wxCommandEvent& event )
{
    pressCustomDir(directionCfg, directionCfg.custom.exLeftSideOnly);
    updateGui();
}


void SyncCfgDialog::OnExRightSideOnly(wxCommandEvent& event )
{
    pressCustomDir(directionCfg, directionCfg.custom.exRightSideOnly);
    updateGui();
}


void SyncCfgDialog::OnLeftNewer(wxCommandEvent& event )
{
    pressCustomDir(directionCfg, directionCfg.custom.leftNewer);
    updateGui();
}


void SyncCfgDialog::OnRightNewer(wxCommandEvent& event )
{
    pressCustomDir(directionCfg, directionCfg.custom.rightNewer);
    updateGui();
}


void SyncCfgDialog::OnDifferent(wxCommandEvent& event )
{
    pressCustomDir(directionCfg, directionCfg.custom.different);
    updateGui();
}


void SyncCfgDialog::OnConflict(wxCommandEvent& event)
{
    pressCustomDir(directionCfg, directionCfg.custom.conflict);
    updateGui();
}


ReturnSyncConfig::ButtonPressed zen::showSyncConfigDlg(wxWindow* parent,
                                                       CompareVariant compareVar,
                                                       SyncConfig&    syncCfg,
                                                       xmlAccess::OnGuiError* handleError,    //
                                                       ExecWhenFinishedCfg* execWhenFinished) //optional input parameter
{
    SyncCfgDialog syncDlg(parent,
                          compareVar,
                          syncCfg,
                          handleError,
                          execWhenFinished);

    return static_cast<ReturnSyncConfig::ButtonPressed>(syncDlg.ShowModal());
}
bgstack15