summaryrefslogtreecommitdiff
path: root/structures.cpp
blob: 4dde40e04cd1c3a5e7210740d5481d143bbda026 (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
// **************************************************************************
// * 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-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#include "structures.h"
#include <wx/intl.h>
//#include <stdexcept>

using namespace ffs3;


Zstring ffs3::standardExcludeFilter()
{
#ifdef FFS_WIN
    static Zstring exclude(wxT("\
\\System Volume Information\\\n\
\\RECYCLER\\\n\
\\RECYCLED\\\n\
\\$Recycle.Bin\\")); //exclude Recycle Bin
#elif defined FFS_LINUX
    static Zstring exclude; //exclude nothing
#endif
    return exclude;
}


wxString ffs3::getVariantName(CompareVariant var)
{
    switch (var)
    {
    case CMP_BY_CONTENT:
        return _("File content");
    case CMP_BY_TIME_SIZE:
        return _("File size and date");
    }

    assert(false);
    return wxEmptyString;
}


wxString ffs3::getVariantName(const SyncConfiguration& syncCfg)
{
    switch (getVariant(syncCfg))
    {
    case SyncConfiguration::AUTOMATIC:
        return _("<Automatic>");
    case SyncConfiguration::MIRROR:
        return _("Mirror ->>");
    case SyncConfiguration::UPDATE:
        return _("Update ->");
    case SyncConfiguration::CUSTOM:
        return _("Custom");
    }
    return _("Error");
}


void ffs3::setTwoWay(SyncConfiguration& syncCfg) //helper method used by <Automatic> mode fallback to overwrite old with newer files
{
    syncCfg.automatic = false;
    syncCfg.exLeftSideOnly  = SYNC_DIR_RIGHT;
    syncCfg.exRightSideOnly = SYNC_DIR_LEFT;
    syncCfg.leftNewer       = SYNC_DIR_RIGHT;
    syncCfg.rightNewer      = SYNC_DIR_LEFT;
    syncCfg.different       = SYNC_DIR_NONE;
    syncCfg.conflict        = SYNC_DIR_NONE;
}


SyncConfiguration::Variant ffs3::getVariant(const SyncConfiguration& syncCfg)
{
    if (syncCfg.automatic == true)
        return SyncConfiguration::AUTOMATIC;  //automatic mode

    if (    syncCfg.exLeftSideOnly  == SYNC_DIR_RIGHT &&
            syncCfg.exRightSideOnly == SYNC_DIR_RIGHT &&
            syncCfg.leftNewer       == SYNC_DIR_RIGHT &&
            syncCfg.rightNewer      == SYNC_DIR_RIGHT &&
            syncCfg.different       == SYNC_DIR_RIGHT &&
            syncCfg.conflict        == SYNC_DIR_RIGHT)
        return SyncConfiguration::MIRROR;    //one way ->

    else if (syncCfg.exLeftSideOnly  == SYNC_DIR_RIGHT &&
             syncCfg.exRightSideOnly == SYNC_DIR_NONE  &&
             syncCfg.leftNewer       == SYNC_DIR_RIGHT &&
             syncCfg.rightNewer      == SYNC_DIR_NONE  &&
             syncCfg.different       == SYNC_DIR_RIGHT  &&
             syncCfg.conflict        == SYNC_DIR_NONE)
        return SyncConfiguration::UPDATE;    //Update ->
    else
        return SyncConfiguration::CUSTOM;    //other
}


void ffs3::setVariant(SyncConfiguration& syncCfg, const SyncConfiguration::Variant var)
{
    switch (var)
    {
    case SyncConfiguration::AUTOMATIC:
        syncCfg.automatic = true;
        break;
    case SyncConfiguration::MIRROR:
        syncCfg.automatic = false;
        syncCfg.exLeftSideOnly  = SYNC_DIR_RIGHT;
        syncCfg.exRightSideOnly = SYNC_DIR_RIGHT;
        syncCfg.leftNewer       = SYNC_DIR_RIGHT;
        syncCfg.rightNewer      = SYNC_DIR_RIGHT;
        syncCfg.different       = SYNC_DIR_RIGHT;
        syncCfg.conflict        = SYNC_DIR_RIGHT;
        break;
    case SyncConfiguration::UPDATE:
        syncCfg.automatic = false;
        syncCfg.exLeftSideOnly  = SYNC_DIR_RIGHT;
        syncCfg.exRightSideOnly = SYNC_DIR_NONE;
        syncCfg.leftNewer       = SYNC_DIR_RIGHT;
        syncCfg.rightNewer      = SYNC_DIR_NONE;
        syncCfg.different       = SYNC_DIR_RIGHT;
        syncCfg.conflict        = SYNC_DIR_NONE;
        break;
    case SyncConfiguration::CUSTOM:
        assert(false);
        break;
    }
}



wxString MainConfiguration::getSyncVariantName()
{
    const SyncConfiguration firstSyncCfg =
        firstPair.altSyncConfig.get() ?
        firstPair.altSyncConfig->syncConfiguration :
        syncConfiguration; //fallback to main sync cfg

    const SyncConfiguration::Variant firstVariant = getVariant(firstSyncCfg);

    //test if there's a deviating variant within the additional folder pairs
    for (std::vector<FolderPairEnh>::const_iterator i = additionalPairs.begin(); i != additionalPairs.end(); ++i)
    {
        const SyncConfiguration::Variant thisVariant =
            i->altSyncConfig.get() ?
            getVariant(i->altSyncConfig->syncConfiguration) :
            getVariant(syncConfiguration);

        if (thisVariant != firstVariant)
            return _("Multiple...");
    }

    //seems to be all in sync...
    return getVariantName(firstSyncCfg);
}


wxString ffs3::getDescription(CompareFilesResult cmpRes)
{
    switch (cmpRes)
    {
    case FILE_LEFT_SIDE_ONLY:
        return _("Files/folders that exist on left side only");
    case FILE_RIGHT_SIDE_ONLY:
        return _("Files/folders that exist on right side only");
    case FILE_LEFT_NEWER:
        return _("Files that exist on both sides, left one is newer");
    case FILE_RIGHT_NEWER:
        return _("Files that exist on both sides, right one is newer");
    case FILE_DIFFERENT:
        return _("Files that have different content");
    case FILE_EQUAL:
        return _("Files that are equal on both sides");
    case FILE_DIFFERENT_METADATA:
        return _("Files/folders that differ in attributes only");
    case FILE_CONFLICT:
        return _("Conflicts/files that cannot be categorized");
    }

    assert(false);
    return wxEmptyString;
}


wxString ffs3::getSymbol(CompareFilesResult cmpRes)
{
    switch (cmpRes)
    {
    case FILE_LEFT_SIDE_ONLY:
        return wxT("<|");
    case FILE_RIGHT_SIDE_ONLY:
        return wxT("|>");
    case FILE_LEFT_NEWER:
        return wxT("<<");
    case FILE_RIGHT_NEWER:
        return wxT(">>");
    case FILE_DIFFERENT:
        return wxT("!=");
    case FILE_EQUAL:
        return wxT("'=="); //added quotation mark to avoid error in Excel cell when exporting to *.cvs
    case FILE_CONFLICT:
    case FILE_DIFFERENT_METADATA:
        return wxT("\\/\\->");
    }

    assert(false);
    return wxEmptyString;
}


wxString ffs3::getDescription(SyncOperation op)
{
    switch (op)
    {
    case SO_CREATE_NEW_LEFT:
        return _("Copy from right to left");
    case SO_CREATE_NEW_RIGHT:
        return _("Copy from left to right");
    case SO_DELETE_LEFT:
        return _("Delete files/folders existing on left side only");
    case SO_DELETE_RIGHT:
        return _("Delete files/folders existing on right side only");
    case SO_OVERWRITE_LEFT:
        return _("Copy from right to left overwriting");
    case SO_OVERWRITE_RIGHT:
        return _("Copy from left to right overwriting");
    case SO_DO_NOTHING:
        return _("Do nothing");
    case SO_EQUAL:
        return _("Files that are equal on both sides");
    case SO_COPY_METADATA_TO_LEFT:
        return _("Copy attributes only from right to left");
    case SO_COPY_METADATA_TO_RIGHT:
        return _("Copy attributes only from left to right");
    case SO_UNRESOLVED_CONFLICT:
        return _("Conflicts/files that cannot be categorized");
    };

    assert(false);
    return wxEmptyString;
}


wxString ffs3::getSymbol(SyncOperation op)
{
    switch (op)
    {
    case SO_CREATE_NEW_LEFT:
        return wxT("*-");
    case SO_CREATE_NEW_RIGHT:
        return wxT("-*");
    case SO_DELETE_LEFT:
        return wxT("D-");
    case SO_DELETE_RIGHT:
        return wxT("-D");
    case SO_OVERWRITE_LEFT:
    case SO_COPY_METADATA_TO_LEFT:
        return wxT("<-");
    case SO_OVERWRITE_RIGHT:
    case SO_COPY_METADATA_TO_RIGHT:
        return wxT("->");
    case SO_DO_NOTHING:
        return wxT(" -");
    case SO_EQUAL:
        return wxT("'=="); //added quotation mark to avoid error in Excel cell when exporting to *.cvs
    case SO_UNRESOLVED_CONFLICT:
        return wxT("\\/\\->");
    };

    assert(false);
    return wxEmptyString;
}


namespace
{
bool sameFilterConfig(const std::vector<FolderPairEnh>& folderPairs)
{
    if (folderPairs.empty())
        return true;

    for (std::vector<FolderPairEnh>::const_iterator fp = folderPairs.begin(); fp != folderPairs.end(); ++fp)
        if (!(fp->localFilter == folderPairs[0].localFilter))
            return false;

    return true;
}
}


ffs3::MainConfiguration ffs3::merge(const std::vector<MainConfiguration>& mainCfgs)
{
    assert(!mainCfgs.empty());
    if (mainCfgs.empty())
        return ffs3::MainConfiguration();

    if (mainCfgs.size() == 1) //mergeConfigFilesImpl relies on this!
        return mainCfgs[0];   //

    //merge folder pair config
    std::vector<FolderPairEnh> fpMerged;
    for (std::vector<MainConfiguration>::const_iterator i = mainCfgs.begin(); i != mainCfgs.end(); ++i)
    {
        std::vector<FolderPairEnh> fpTmp;
        fpTmp.push_back(i->firstPair);
        fpTmp.insert(fpTmp.end(), i->additionalPairs.begin(), i->additionalPairs.end());

        //move all configuration to item level
        for (std::vector<FolderPairEnh>::iterator fp = fpTmp.begin(); fp != fpTmp.end(); ++fp)
        {
            if (!fp->altSyncConfig.get())
                fp->altSyncConfig.reset(
                    new AlternateSyncConfig(i->syncConfiguration,
                                            i->handleDeletion,
                                            i->customDeletionDirectory));

            //pragmatism: if both global and local include filter contain data, only local filter is preserved
            if (fp->localFilter.includeFilter == FilterConfig().includeFilter)
                fp->localFilter.includeFilter = i->globalFilter.includeFilter;

            fp->localFilter.excludeFilter.Trim(true, false);
            fp->localFilter.excludeFilter = i->globalFilter.excludeFilter + Zstr("\n") + fp->localFilter.excludeFilter;
            fp->localFilter.excludeFilter.Trim(true, false);
        }

        fpMerged.insert(fpMerged.end(), fpTmp.begin(), fpTmp.end());
    }

    //optimization: remove redundant configuration
    FilterConfig newGlobalFilter;

    const bool sameLocalFilter = sameFilterConfig(fpMerged);
    if (sameLocalFilter)
        newGlobalFilter = fpMerged[0].localFilter;

    for (std::vector<FolderPairEnh>::iterator fp = fpMerged.begin(); fp != fpMerged.end(); ++fp)
    {
        //if local config matches output global config we don't need local one
        if (    fp->altSyncConfig &&
                *fp->altSyncConfig ==
                AlternateSyncConfig(mainCfgs[0].syncConfiguration,
                                    mainCfgs[0].handleDeletion,
                                    mainCfgs[0].customDeletionDirectory))
            fp->altSyncConfig.reset();

        if (sameLocalFilter) //use global filter in this case
            fp->localFilter = FilterConfig();
    }


    //final assembly
    ffs3::MainConfiguration cfgOut = mainCfgs[0];
    cfgOut.globalFilter            = newGlobalFilter;
    cfgOut.firstPair               = fpMerged[0];
    cfgOut.additionalPairs.assign(fpMerged.begin() + 1, fpMerged.end());

    return cfgOut;
}
bgstack15