summaryrefslogtreecommitdiff
path: root/ui/folderPair.h
blob: d20dde76af76784ac5eba106fcacff57f7210609 (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
// **************************************************************************
// * 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)                    *
// **************************************************************************
//
#ifndef FOLDERPAIR_H_INCLUDED
#define FOLDERPAIR_H_INCLUDED

#include "../structures.h"
#include "../shared/dragAndDrop.h"
#include "../library/resources.h"
#include "smallDialogs.h"
#include "settingsDialog.h"
#include <wx/event.h>


namespace FreeFileSync
{
//basic functionality for handling alternate folder pair configuration: change sync-cfg/filter cfg, right-click context menu, button icons...

template <class GuiPanel>
class FolderPairPanelBasic : private wxEvtHandler
{
public:
    typedef boost::shared_ptr<const FreeFileSync::AlternateSyncConfig> AltSyncCfgPtr;


    Zstring getLeftDir() const
    {
        return wxToZ(basicPanel_.m_directoryLeft->GetValue());
    }

    Zstring getRightDir() const
    {
        return wxToZ(basicPanel_.m_directoryRight->GetValue());
    }

    AltSyncCfgPtr getAltSyncConfig() const
    {
        return altSyncConfig;
    }

    FilterConfig getAltFilterConfig() const
    {
        return localFilter;
    }

    void setValues(const Zstring& leftDir,
                   const Zstring& rightDir,
                   AltSyncCfgPtr syncCfg,
                   const FilterConfig& filter)
    {
        altSyncConfig = syncCfg;
        localFilter   = filter;

        //insert directory names
        FreeFileSync::setDirectoryName(zToWx(leftDir),  basicPanel_.m_directoryLeft,  basicPanel_.m_dirPickerLeft);
        FreeFileSync::setDirectoryName(zToWx(rightDir), basicPanel_.m_directoryRight, basicPanel_.m_dirPickerRight);

        refreshButtons();
    }


    void refreshButtons()
    {
        if (altSyncConfig.get())
        {
            basicPanel_.m_bpButtonAltSyncCfg->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("syncConfigSmall")));
            basicPanel_.m_bpButtonAltSyncCfg->SetToolTip(wxString(_("Select alternate synchronization settings")) +  wxT(" ") + globalFunctions::LINE_BREAK +
                    wxT("(") + getVariantName(altSyncConfig->syncConfiguration) + wxT(")"));
        }
        else
        {
            basicPanel_.m_bpButtonAltSyncCfg->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("syncConfigSmallGrey")));
            basicPanel_.m_bpButtonAltSyncCfg->SetToolTip(_("Select alternate synchronization settings"));
        }


        if (getMainConfig().filterIsActive)
        {
            //test for Null-filter
            const bool isNullFilter = NameFilter(localFilter.includeFilter, localFilter.excludeFilter).isNull();
            if (isNullFilter)
            {
                basicPanel_.m_bpButtonLocalFilter->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("filterSmallGrey")));
                basicPanel_.m_bpButtonLocalFilter->SetToolTip(_("No filter selected"));
            }
            else
            {
                basicPanel_.m_bpButtonLocalFilter->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("filterSmall")));
                basicPanel_.m_bpButtonLocalFilter->SetToolTip(_("Filter has been selected"));
            }
        }
        else
        {
            basicPanel_.m_bpButtonLocalFilter->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("filterSmallGrey")));
            basicPanel_.m_bpButtonLocalFilter->SetToolTip(_("Filtering is deactivated"));
        }
    }

protected:
    FolderPairPanelBasic(GuiPanel& basicPanel) : //takes reference on basic panel to be enhanced
        basicPanel_(basicPanel)
    {
        //register events for removal of alternate configuration
        basicPanel_.m_bpButtonAltSyncCfg->Connect(wxEVT_RIGHT_DOWN, wxCommandEventHandler(FolderPairPanelBasic::OnAltSyncCfgRemove), NULL, this);
        basicPanel_.m_bpButtonLocalFilter->Connect(wxEVT_RIGHT_DOWN, wxCommandEventHandler(FolderPairPanelBasic::OnLocalFilterCfgRemove), NULL, this);

        basicPanel_.m_bpButtonAltSyncCfg-> Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FolderPairPanelBasic::OnAltSyncCfg), NULL, this);
        basicPanel_.m_bpButtonLocalFilter->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(FolderPairPanelBasic::OnLocalFilterCfg), NULL, this);

        basicPanel_.m_bpButtonRemovePair->SetBitmapLabel(GlobalResources::getInstance().getImageByName(wxT("removeFolderPair")));
    }

    virtual void OnLocalFilterCfgRemoveConfirm(wxCommandEvent& event)
    {
        localFilter = FilterConfig();
        refreshButtons();
    }

    virtual void OnAltSyncCfgRemoveConfirm(wxCommandEvent& event)
    {
        altSyncConfig.reset();
        refreshButtons();
    }


private:
    void OnLocalFilterCfgRemove(wxCommandEvent& event)
    {
        const int menuId = 1234;
        contextMenu.reset(new wxMenu); //re-create context menu
        contextMenu->Append(menuId, _("Remove local filter settings"));
        contextMenu->Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(FolderPairPanelBasic::OnLocalFilterCfgRemoveConfirm), NULL, this);

        if (NameFilter(localFilter.includeFilter, localFilter.excludeFilter).isNull())
            contextMenu->Enable(menuId, false); //disable menu item, if clicking wouldn't make sense anyway

        basicPanel_.PopupMenu(contextMenu.get()); //show context menu
    }

    void OnAltSyncCfgRemove(wxCommandEvent& event)
    {
        const int menuId = 1234;
        contextMenu.reset(new wxMenu); //re-create context menu
        contextMenu->Append(menuId, _("Remove alternate settings"));
        contextMenu->Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(FolderPairPanelBasic::OnAltSyncCfgRemoveConfirm), NULL, this);

        if (!altSyncConfig.get())
            contextMenu->Enable(menuId, false); //disable menu item, if clicking wouldn't make sense anyway

        basicPanel_.PopupMenu(contextMenu.get()); //show context menu
    }

    virtual MainConfiguration getMainConfig()   const = 0;
    virtual wxWindow* getParentWindow() = 0;

    virtual void OnAltSyncCfgChange() {};

    void OnAltSyncCfg(wxCommandEvent& event)
    {
        const MainConfiguration mainCfg = getMainConfig();
        const AlternateSyncConfig syncConfigMain(mainCfg.syncConfiguration,
                mainCfg.handleDeletion,
                mainCfg.customDeletionDirectory);

        AlternateSyncConfig altSyncCfg = altSyncConfig.get() ? *altSyncConfig : syncConfigMain;
        SyncCfgDialog* syncDlg = new SyncCfgDialog(getParentWindow(),
                mainCfg.compareVar,
                altSyncCfg.syncConfiguration,
                altSyncCfg.handleDeletion,
                altSyncCfg.customDeletionDirectory,
                NULL);
        if (syncDlg->ShowModal() == SyncCfgDialog::BUTTON_APPLY)
        {
            altSyncConfig.reset(new AlternateSyncConfig(altSyncCfg));
            refreshButtons();

            OnAltSyncCfgChange();
        }
    }

    virtual void OnLocalFilterCfgChange() {};

    void OnLocalFilterCfg(wxCommandEvent& event)
    {
        FilterConfig localFiltTmp = localFilter;

        if (showFilterDialog(false, //is local filter dialog
                             localFiltTmp.includeFilter,
                             localFiltTmp.excludeFilter,
                             getMainConfig().filterIsActive) == DefaultReturnCode::BUTTON_OKAY)
        {
            localFilter = localFiltTmp;
            refreshButtons();

            OnLocalFilterCfgChange();
        }
    }

    GuiPanel& basicPanel_; //panel to be enhanced by this template

    //alternate configuration attached to it
    AltSyncCfgPtr altSyncConfig; //optional: present if non-NULL
    FilterConfig  localFilter;

    std::auto_ptr<wxMenu> contextMenu;
};
}


#endif // FOLDERPAIR_H_INCLUDED


bgstack15