summaryrefslogtreecommitdiff
path: root/ui/batch_config.cpp
blob: bd82194d26d56b5e5d6f70c36a61221d3e9446ab (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
// **************************************************************************
// * 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 "batch_config.h"
#include <wx/wupdlock.h>
#include <wx+/mouse_move_dlg.h>
#include <wx+/font_size.h>
#include "gui_generated.h"
#include "dir_name.h"
#include "../ui/exec_finished_box.h"
#include "../lib/help_provider.h"
#include "../lib/resources.h"

using namespace zen;
using namespace xmlAccess;


namespace
{
enum ButtonPressed
{
    BUTTON_CANCEL,
    BUTTON_SAVE_AS
};


class BatchDialog : public BatchDlgGenerated
{
public:
    BatchDialog(wxWindow* parent,
                XmlBatchConfig& batchCfg, //in/out
                std::vector<std::wstring>& onCompletionHistory,
                size_t onCompletionHistoryMax);

private:
    virtual void OnHelp        (wxCommandEvent& event) { displayHelpEntry(L"html/Schedule a Batch Job.html", this); }
    virtual void OnClose       (wxCloseEvent&   event) { EndModal(BUTTON_CANCEL); }
    virtual void OnCancel      (wxCommandEvent& event) { EndModal(BUTTON_CANCEL); }
    virtual void OnSaveBatchJob(wxCommandEvent& event);
    virtual void OnErrorPopup (wxCommandEvent& event) { localBatchCfg.handleError = ON_ERROR_POPUP;  updateGui(); }
    virtual void OnErrorIgnore(wxCommandEvent& event) { localBatchCfg.handleError = ON_ERROR_IGNORE; updateGui(); }
    virtual void OnErrorExit  (wxCommandEvent& event) { localBatchCfg.handleError = ON_ERROR_EXIT;   updateGui(); }

    virtual void OnToggleGenerateLogfile(wxCommandEvent& event) { updateGui(); }
    virtual void OnToggleLogfilesLimit  (wxCommandEvent& event) { updateGui(); }

    void updateGui(); //re-evaluate gui after config changes

    void setConfig(const XmlBatchConfig& batchCfg);
    XmlBatchConfig getConfig() const;

    XmlBatchConfig& batchCfgOutRef; //output only!
    XmlBatchConfig localBatchCfg; //a mixture of settings some of which have OWNERSHIP WITHIN GUI CONTROLS! use getConfig() to resolve

    std::unique_ptr<DirectoryName<FolderHistoryBox>> logfileDir; //always bound, solve circular compile-time dependency
};

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

BatchDialog::BatchDialog(wxWindow* parent,
                         XmlBatchConfig& batchCfg,
                         std::vector<std::wstring>& onCompletionHistory,
                         size_t onCompletionHistoryMax) :
    BatchDlgGenerated(parent),
    batchCfgOutRef(batchCfg)
{
#ifdef FFS_WIN
    new zen::MouseMoveWindow(*this); //allow moving main dialog by clicking (nearly) anywhere...; ownership passed to "this"
#endif
    wxWindowUpdateLocker dummy(this); //avoid display distortion
    setRelativeFontSize(*m_staticTextHeader, 1.25);

    m_comboBoxExecFinished->initHistory(onCompletionHistory, onCompletionHistoryMax);

    m_bpButtonHelp  ->SetBitmapLabel(getResourceImage(L"help"));
    m_bitmapBatchJob->SetBitmap     (getResourceImage(L"batch"));

    logfileDir = make_unique<DirectoryName<FolderHistoryBox>>(*this, *m_buttonSelectLogfileDir, *m_logfileDir);

    setConfig(batchCfg);

    Fit(); //child-element widths have changed: image was set
    Layout();

    m_buttonSave->SetFocus();
}


void BatchDialog::updateGui() //re-evaluate gui after config changes
{
    XmlBatchConfig cfg = getConfig(); //resolve parameter ownership: some on GUI controls, others member variables

    m_panelLogfile        ->Enable(m_checkBoxGenerateLogfile->GetValue()); //enabled status is *not* directly dependent from resolved config! (but transitively)
    m_spinCtrlLogfileLimit->Enable(m_checkBoxGenerateLogfile->GetValue() && m_checkBoxLogfilesLimit->GetValue());

    m_toggleBtnErrorIgnore->SetValue(false);
    m_toggleBtnErrorPopup ->SetValue(false);
    m_toggleBtnErrorExit  ->SetValue(false);
    switch (cfg.handleError) //*not* owned by GUI controls
    {
        case ON_ERROR_IGNORE:
            m_toggleBtnErrorIgnore->SetValue(true);
            break;
        case ON_ERROR_POPUP:
            m_toggleBtnErrorPopup->SetValue(true);
            break;
        case ON_ERROR_EXIT:
            m_toggleBtnErrorExit->SetValue(true);
            break;
    }
}


void BatchDialog::setConfig(const XmlBatchConfig& batchCfg)
{
    wxWindowUpdateLocker dummy(this); //avoid display distortion

    localBatchCfg = batchCfg; //contains some parameters not owned by GUI controls

    //transfer parameter ownership to GUI
    m_checkBoxShowProgress->SetValue(batchCfg.showProgress);
    logfileDir->setName(utfCvrtTo<wxString>(batchCfg.logFileDirectory));
    m_comboBoxExecFinished->setValue(batchCfg.mainCfg.onCompletion);

    //map single parameter "logfiles limit" to all three checkboxs and spin ctrl:
    m_checkBoxGenerateLogfile->SetValue(batchCfg.logfilesCountLimit != 0);
    m_checkBoxLogfilesLimit  ->SetValue(batchCfg.logfilesCountLimit >= 0);
    m_spinCtrlLogfileLimit   ->SetValue(batchCfg.logfilesCountLimit >= 0 ? batchCfg.logfilesCountLimit : 100 /*XmlBatchConfig().logfilesCountLimit*/);
    //attention: emits a "change value" event!! => updateGui() called implicitly!

    updateGui(); //re-evaluate gui after config changes
}


XmlBatchConfig BatchDialog::getConfig() const
{
    XmlBatchConfig batchCfg = localBatchCfg;

    //load parameters with ownership within GIU controls...

    //load structure with batch settings "batchCfg"
    batchCfg.showProgress     = m_checkBoxShowProgress->GetValue();
    batchCfg.logFileDirectory = utfCvrtTo<Zstring>(logfileDir->getName());
    batchCfg.mainCfg.onCompletion = m_comboBoxExecFinished->getValue();
    //get single parameter "logfiles limit" from all three checkboxes and spin ctrl:
    batchCfg.logfilesCountLimit = m_checkBoxGenerateLogfile->GetValue() ? (m_checkBoxLogfilesLimit->GetValue() ? m_spinCtrlLogfileLimit->GetValue() : -1) : 0;

    return batchCfg;
}


void BatchDialog::OnSaveBatchJob(wxCommandEvent& event)
{
    batchCfgOutRef = getConfig();
    m_comboBoxExecFinished->addItemHistory(); //a good place to commit current "on completion" history item
    EndModal(BUTTON_SAVE_AS);
}
}


bool zen::customizeBatchConfig(wxWindow* parent,
                               xmlAccess::XmlBatchConfig& batchCfg, //in/out
                               std::vector<std::wstring>& execFinishedhistory,
                               size_t execFinishedhistoryMax)
{
    BatchDialog batchDlg(parent, batchCfg, execFinishedhistory, execFinishedhistoryMax);
    return static_cast<ButtonPressed>(batchDlg.ShowModal()) == BUTTON_SAVE_AS;
}
bgstack15