summaryrefslogtreecommitdiff
path: root/structures.cpp
blob: 1612445c7b33c39f5a3e2b8d3dfc26294a746874 (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
// **************************************************************************
// * 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 <iterator>
#include <stdexcept>
#include <ctime>
#include <zen/i18n.h>

using namespace zen;


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

    assert(false);
    return wxEmptyString;
}


wxString zen::getVariantName(DirectionConfig::Variant var)
{
    switch (var)
    {
        case DirectionConfig::AUTOMATIC:
            return _("<Automatic>");
        case DirectionConfig::MIRROR:
            return _("Mirror ->>");
        case DirectionConfig::UPDATE:
            return _("Update ->");
        case DirectionConfig::CUSTOM:
            return _("Custom");
    }
    return _("Error");
}


DirectionSet zen::extractDirections(const DirectionConfig& cfg)
{
    DirectionSet output;
    switch (cfg.var)
    {
        case DirectionConfig::AUTOMATIC:
            throw std::logic_error("there are no predefined directions for automatic mode!");

        case DirectionConfig::MIRROR:
            output.exLeftSideOnly  = SYNC_DIR_RIGHT;
            output.exRightSideOnly = SYNC_DIR_RIGHT;
            output.leftNewer       = SYNC_DIR_RIGHT;
            output.rightNewer      = SYNC_DIR_RIGHT;
            output.different       = SYNC_DIR_RIGHT;
            output.conflict        = SYNC_DIR_RIGHT;
            break;

        case DirectionConfig::UPDATE:
            output.exLeftSideOnly  = SYNC_DIR_RIGHT;
            output.exRightSideOnly = SYNC_DIR_NONE;
            output.leftNewer       = SYNC_DIR_RIGHT;
            output.rightNewer      = SYNC_DIR_NONE;
            output.different       = SYNC_DIR_RIGHT;
            output.conflict        = SYNC_DIR_NONE;
            break;

        case DirectionConfig::CUSTOM:
            output = cfg.custom;
            break;
    }
    return output;
}


DirectionSet zen::getTwoWaySet()
{
    DirectionSet output;
    output.exLeftSideOnly  = SYNC_DIR_RIGHT;
    output.exRightSideOnly = SYNC_DIR_LEFT;
    output.leftNewer       = SYNC_DIR_RIGHT;
    output.rightNewer      = SYNC_DIR_LEFT;
    output.different       = SYNC_DIR_NONE;
    output.conflict        = SYNC_DIR_NONE;
    return output;
}


wxString MainConfiguration::getCompVariantName() const
{
    const CompareVariant firstVariant = firstPair.altCmpConfig.get() ?
                                        firstPair.altCmpConfig->compareVar :
                                        cmpConfig.compareVar; //fallback to main sync cfg

    //test if there's a deviating variant within the additional folder pairs
    for (auto fp = additionalPairs.begin(); fp != additionalPairs.end(); ++fp)
    {
        const CompareVariant thisVariant = fp->altCmpConfig.get() ?
                                           fp->altCmpConfig->compareVar :
                                           cmpConfig.compareVar; //fallback to main sync cfg
        if (thisVariant != firstVariant)
            return _("Multiple...");
    }

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


wxString MainConfiguration::getSyncVariantName() const
{
    const DirectionConfig::Variant firstVariant = firstPair.altSyncConfig.get() ?
                                                  firstPair.altSyncConfig->directionCfg.var :
                                                  syncCfg.directionCfg.var; //fallback to main sync cfg

    //test if there's a deviating variant within the additional folder pairs
    for (auto fp = additionalPairs.begin(); fp != additionalPairs.end(); ++fp)
    {
        const DirectionConfig::Variant thisVariant = fp->altSyncConfig.get() ?
                                                     fp->altSyncConfig->directionCfg.var :
                                                     syncCfg.directionCfg.var;
        if (thisVariant != firstVariant)
            return _("Multiple...");
    }

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


wxString zen::getDescription(CompareFilesResult cmpRes)
{
    switch (cmpRes)
    {
        case FILE_LEFT_SIDE_ONLY:
            return _("File/folder exists on left side only");
        case FILE_RIGHT_SIDE_ONLY:
            return _("File/folder exists on right side only");
        case FILE_LEFT_NEWER:
            return _("Left file is newer");
        case FILE_RIGHT_NEWER:
            return _("Right file is newer");
        case FILE_DIFFERENT:
            return _("Files have different content");
        case FILE_EQUAL:
            return _("Both sides are equal");
        case FILE_DIFFERENT_METADATA:
            return _("Files/folders differ in attributes only");
        case FILE_CONFLICT:
            return _("Conflict/file cannot be categorized");
    }

    assert(false);
    return wxEmptyString;
}


wxString zen::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 zen::getDescription(SyncOperation op)
{
    switch (op)
    {
        case SO_CREATE_NEW_LEFT:
            return _("Copy new file/folder to left");
        case SO_CREATE_NEW_RIGHT:
            return _("Copy new file/folder to right");
        case SO_DELETE_LEFT:
            return _("Delete left file/folder");
        case SO_DELETE_RIGHT:
            return _("Delete right file/folder");
        case SO_OVERWRITE_LEFT:
            return _("Overwrite left file/folder with right one");
        case SO_OVERWRITE_RIGHT:
            return _("Overwrite right file/folder with left one");
        case SO_DO_NOTHING:
            return _("Do nothing");
        case SO_EQUAL:
            return _("Both sides are equal");
        case SO_COPY_METADATA_TO_LEFT:
            return _("Copy file attributes only to left");
        case SO_COPY_METADATA_TO_RIGHT:
            return _("Copy file attributes only to right");
        case SO_UNRESOLVED_CONFLICT: //not used on GUI, but in .csv
            return _("Conflict/file cannot be categorized");
    };

    assert(false);
    return wxEmptyString;
}


wxString zen::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
{
assert_static(std::numeric_limits<zen:: Int64>::is_specialized);
assert_static(std::numeric_limits<zen::UInt64>::is_specialized);


int daysSinceBeginOfWeek(int dayOfWeek) //0-6, 0=Monday, 6=Sunday
{
    assert(0 <= dayOfWeek && dayOfWeek <= 6);
#ifdef FFS_WIN
    DWORD firstDayOfWeek = 0;
    if (::GetLocaleInfo(LOCALE_USER_DEFAULT,                 //__in   LCID Locale,
                        LOCALE_IFIRSTDAYOFWEEK |             // first day of week specifier, 0-6, 0=Monday, 6=Sunday
                        LOCALE_RETURN_NUMBER,                //__in   LCTYPE LCType,
                        reinterpret_cast<LPTSTR>(&firstDayOfWeek),    //__out  LPTSTR lpLCData,
                        sizeof(firstDayOfWeek) / sizeof(TCHAR)) != 0) //__in   int cchData
    {
        assert(firstDayOfWeek <= 6);
        return (dayOfWeek + (7 - firstDayOfWeek)) % 7;
    }
    else //default
#endif
        return dayOfWeek; //let all weeks begin with monday
}

zen::Int64 resolve(size_t value, UnitTime unit, zen::Int64 defaultVal)
{
    const time_t utcTimeNow = ::time(NULL);
    struct tm* localTimeFmt = ::localtime (&utcTimeNow); //utc to local

    switch (unit)
    {
        case UTIME_NONE:
            return defaultVal;

            //        case UTIME_LAST_X_HOURS:
            //           return Int64(utcTimeNow) - Int64(value) * 3600;

        case UTIME_TODAY:
            localTimeFmt->tm_sec  = 0; //0-61
            localTimeFmt->tm_min  = 0; //0-59
            localTimeFmt->tm_hour = 0; //0-23
            return Int64(::mktime(localTimeFmt)); //convert local time back to UTC

        case UTIME_THIS_WEEK:
        {
            localTimeFmt->tm_sec  = 0; //0-61
            localTimeFmt->tm_min  = 0; //0-59
            localTimeFmt->tm_hour = 0; //0-23
            size_t timeFrom = ::mktime(localTimeFmt);

            int dayOfWeek = (localTimeFmt->tm_wday + 6) % 7; //tm_wday := days since Sunday	0-6
            // +6 == -1 in Z_7

            return Int64(timeFrom) - daysSinceBeginOfWeek(dayOfWeek) * 24 * 3600;
        }
        case UTIME_THIS_MONTH:
            localTimeFmt->tm_sec  = 0; //0-61
            localTimeFmt->tm_min  = 0; //0-59
            localTimeFmt->tm_hour = 0; //0-23
            localTimeFmt->tm_mday = 1; //1-31
            return Int64(::mktime(localTimeFmt)); //convert local time back to UTC

        case UTIME_THIS_YEAR:
            localTimeFmt->tm_sec  = 0; //0-61
            localTimeFmt->tm_min  = 0; //0-59
            localTimeFmt->tm_hour = 0; //0-23
            localTimeFmt->tm_mday = 1; //1-31
            localTimeFmt->tm_mon  = 0; //0-11
            return Int64(::mktime(localTimeFmt)); //convert local time back to UTC
    }

    assert(false);
    return utcTimeNow;
}

zen::UInt64 resolve(size_t value, UnitSize unit, zen::UInt64 defaultVal)
{
    double out = value;
    switch (unit)
    {
        case USIZE_NONE:
            return defaultVal;
        case USIZE_BYTE:
            return value;
        case USIZE_KB:
            out *= 1024;
            break;
        case USIZE_MB:
            out *= 1024 * 1024;
            break;
    }
    return out >= to<double>(std::numeric_limits<zen::UInt64>::max()) ? //prevent overflow!!!
           std::numeric_limits<zen::UInt64>::max() :
           zen::UInt64(out);
}
}

void zen::resolveUnits(size_t timeSpan, UnitTime unitTimeSpan,
                       size_t sizeMin,  UnitSize unitSizeMin,
                       size_t sizeMax,  UnitSize unitSizeMax,
                       zen::Int64&  timeFrom,  //unit: UTC time, seconds
                       zen::UInt64& sizeMinBy, //unit: bytes
                       zen::UInt64& sizeMaxBy) //unit: bytes
{
    timeFrom  = resolve(timeSpan, unitTimeSpan, std::numeric_limits<Int64>::min());
    sizeMinBy = resolve(sizeMin,  unitSizeMin, 0U);
    sizeMaxBy = resolve(sizeMax,  unitSizeMax, std::numeric_limits<UInt64>::max());
}


namespace
{
bool sameFilter(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;
}


FilterConfig mergeFilterConfig(const FilterConfig& global, const FilterConfig& local)
{
    FilterConfig out = local;

    //hard filter

    //pragmatism: if both global and local include filter contain data, only local filter is preserved
    if (out.includeFilter == FilterConfig().includeFilter)
        out.includeFilter = global.includeFilter;

    trim(out.excludeFilter, true, false);
    out.excludeFilter = global.excludeFilter + Zstr("\n") + out.excludeFilter;
    trim(out.excludeFilter, true, false);

    //soft filter
    Int64  loctimeFrom;
    UInt64 locSizeMinBy;
    UInt64 locSizeMaxBy;
    resolveUnits(out.timeSpan, out.unitTimeSpan,
                 out.sizeMin,  out.unitSizeMin,
                 out.sizeMax,  out.unitSizeMax,
                 loctimeFrom,   //unit: UTC time, seconds
                 locSizeMinBy,  //unit: bytes
                 locSizeMaxBy); //unit: bytes

    //soft filter
    Int64  glotimeFrom;
    UInt64 gloSizeMinBy;
    UInt64 gloSizeMaxBy;
    resolveUnits(global.timeSpan, global.unitTimeSpan,
                 global.sizeMin,  global.unitSizeMin,
                 global.sizeMax,  global.unitSizeMax,
                 glotimeFrom,
                 gloSizeMinBy,
                 gloSizeMaxBy);

    if (glotimeFrom > loctimeFrom)
    {
        out.timeSpan     = global.timeSpan;
        out.unitTimeSpan = global.unitTimeSpan;
    }
    if (gloSizeMinBy > locSizeMinBy)
    {
        out.sizeMin     = global.sizeMin;
        out.unitSizeMin = global.unitSizeMin;
    }
    if (gloSizeMaxBy < locSizeMaxBy)
    {
        out.sizeMax     = global.sizeMax;
        out.unitSizeMax = global.unitSizeMax;
    }
    return out;
}


inline
bool isEmpty(const FolderPairEnh& fp)
{
    return fp == FolderPairEnh();
}
}


MainConfiguration zen::merge(const std::vector<MainConfiguration>& mainCfgs)
{
    assert(!mainCfgs.empty());
    if (mainCfgs.empty())
        return 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 iterMain = mainCfgs.begin(); iterMain != mainCfgs.end(); ++iterMain)
    {
        std::vector<FolderPairEnh> fpTmp;

        //list non-empty local configurations
        if (!isEmpty(iterMain->firstPair))
            fpTmp.push_back(iterMain->firstPair);
        std::copy_if(iterMain->additionalPairs.begin(), iterMain->additionalPairs.end(), std::back_inserter(fpTmp),
        [](const FolderPairEnh& fp) { return !isEmpty(fp); });

        //move all configuration down to item level
        for (std::vector<FolderPairEnh>::iterator fp = fpTmp.begin(); fp != fpTmp.end(); ++fp)
        {
            if (!fp->altCmpConfig.get())
                fp->altCmpConfig = std::make_shared<CompConfig>(iterMain->cmpConfig);

            if (!fp->altSyncConfig.get())
                fp->altSyncConfig = std::make_shared<SyncConfig>(iterMain->syncCfg);

            fp->localFilter = mergeFilterConfig(iterMain->globalFilter, fp->localFilter);
        }

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

    if (fpMerged.empty())
        return MainConfiguration();

    //optimization: remove redundant configuration

    //########################################################################################################################
    //find out which comparison and synchronization setting are used most often and use them as new "header"
    std::vector<std::pair<CompConfig, int>> cmpCfgStat;
    std::vector<std::pair<SyncConfig, int>> syncCfgStat;
    for (auto fp = fpMerged.begin(); fp != fpMerged.end(); ++fp) //rather inefficient algorithm, but it does not require a less-than operator!
    {
        {
            const CompConfig& cmpCfg = *fp->altCmpConfig;

            auto iter = std::find_if(cmpCfgStat.begin(), cmpCfgStat.end(),
            [&](const std::pair<CompConfig, int>& entry) { return entry.first == cmpCfg; });
            if (iter == cmpCfgStat.end())
                cmpCfgStat.push_back(std::make_pair(cmpCfg, 1));
            else
                ++(iter->second);
        }
        {
            const SyncConfig& syncCfg = *fp->altSyncConfig;

            auto iter = std::find_if(syncCfgStat.begin(), syncCfgStat.end(),
            [&](const std::pair<SyncConfig, int>& entry) { return entry.first == syncCfg; });
            if (iter == syncCfgStat.end())
                syncCfgStat.push_back(std::make_pair(syncCfg, 1));
            else
                ++(iter->second);
        }
    }

    //set most-used comparison and synchronization settions as new header options
    const CompConfig cmpCfgHead = cmpCfgStat.empty() ? CompConfig() :
                                  std::max_element(cmpCfgStat.begin(), cmpCfgStat.end(),
    [](const std::pair<CompConfig, int>& lhs, const std::pair<CompConfig, int>& rhs) { return lhs.second < rhs.second; })->first;

    const SyncConfig syncCfgHead = syncCfgStat.empty() ? SyncConfig() :
                                   std::max_element(syncCfgStat.begin(), syncCfgStat.end(),
    [](const std::pair<SyncConfig, int>& lhs, const std::pair<SyncConfig, int>& rhs) { return lhs.second < rhs.second; })->first;
    //########################################################################################################################

    FilterConfig globalFilter;
    const bool equalFilters = sameFilter(fpMerged);
    if (equalFilters)
        globalFilter = fpMerged[0].localFilter;

    //strip redundancy...
    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->altCmpConfig &&
            *fp->altCmpConfig == cmpCfgHead)
            fp->altCmpConfig.reset();

        if (fp->altSyncConfig &&
            *fp->altSyncConfig == syncCfgHead)
            fp->altSyncConfig.reset();

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

    //final assembly
    zen::MainConfiguration cfgOut;
    cfgOut.cmpConfig = cmpCfgHead;
    cfgOut.syncCfg   = syncCfgHead;
    cfgOut.globalFilter = globalFilter;
    cfgOut.firstPair    = fpMerged[0];
    cfgOut.additionalPairs.assign(fpMerged.begin() + 1, fpMerged.end());

    return cfgOut;
}
bgstack15