summaryrefslogtreecommitdiff
path: root/ui/gridView.cpp
blob: ed950c1522c95e92f70ad102f7836bd226d7ee3b (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
#include "gridView.h"
#include "sorting.h"

using FreeFileSync::GridView;


GridView::StatusInfo GridView::update(
    const bool includeLeftOnly,
    const bool includeRightOnly,
    const bool includeLeftNewer,
    const bool includeRightNewer,
    const bool includeDifferent,
    const bool includeEqual,
    const bool hideFiltered)
{
    StatusInfo output;
    output.existsLeftOnly   = false;
    output.existsRightOnly  = false;
    output.existsLeftNewer  = false;
    output.existsRightNewer = false;
    output.existsDifferent  = false;
    output.existsEqual      = false;

    output.filesOnLeftView    = 0;
    output.foldersOnLeftView  = 0;
    output.filesOnRightView   = 0;
    output.foldersOnRightView = 0;

    refView.clear();

    for (FolderComparison::const_iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
    {
        const FileComparison& fileCmp = j->fileCmp;

        RefIndex newEntry;
        newEntry.folderIndex = j - folderCmp.begin();

        for (FileComparison::const_iterator i = fileCmp.begin(); i != fileCmp.end(); ++i)
        {
            //hide filtered row, if corresponding option is set
            if (hideFiltered && !i->selectedForSynchronization)
                continue;

            //process UI filter settings
            switch (i->cmpResult)
            {
            case FILE_LEFT_SIDE_ONLY:
                output.existsLeftOnly = true;
                if (!includeLeftOnly) continue;
                break;
            case FILE_RIGHT_SIDE_ONLY:
                output.existsRightOnly = true;
                if (!includeRightOnly) continue;
                break;
            case FILE_LEFT_NEWER:
                output.existsLeftNewer = true;
                if (!includeLeftNewer) continue;
                break;
            case FILE_RIGHT_NEWER:
                output.existsRightNewer = true;
                if (!includeRightNewer) continue;
                break;
            case FILE_DIFFERENT:
                output.existsDifferent = true;
                if (!includeDifferent) continue;
                break;
            case FILE_EQUAL:
                output.existsEqual = true;
                if (!includeEqual) continue;
                break;
            default:
                assert (false);
            }

            //calculate total number of bytes for each side
            if (i->fileDescrLeft.objType == FileDescrLine::TYPE_FILE)
            {
                output.filesizeLeftView += i->fileDescrLeft.fileSize;
                ++output.filesOnLeftView;
            }
            else if (i->fileDescrLeft.objType == FileDescrLine::TYPE_DIRECTORY)
                ++output.foldersOnLeftView;

            if (i->fileDescrRight.objType == FileDescrLine::TYPE_FILE)
            {
                output.filesizeRightView += i->fileDescrRight.fileSize;
                ++output.filesOnRightView;
            }
            else if (i->fileDescrRight.objType == FileDescrLine::TYPE_DIRECTORY)
                ++output.foldersOnRightView;


            newEntry.rowIndex = i - fileCmp.begin();
            refView.push_back(newEntry);
        }

//        //add some empty line after each folder pair
//        RefIndex emptyLine;
//        emptyLine.folderIndex = -1;
//        emptyLine.rowIndex    = 0;
//        refView.push_back(emptyLine);
    }

    return output;
}


void GridView::viewRefToFolderRef(const std::set<int>& viewRef, FolderCompRef& output)
{
    output.clear();
    for (int i = 0; i < int(folderCmp.size()); ++i)
        output.push_back(std::set<int>());      //avoid copy by value for full set<int>

    for (std::set<int>::iterator i = viewRef.begin(); i != viewRef.end(); ++i)
    {
        const unsigned int folder = refView[*i].folderIndex;
        const unsigned int row    = refView[*i].rowIndex;

        output[folder].insert(row);
    }
}


unsigned int GridView::elementsTotal() const
{
    unsigned int total = 0;
    for (FolderComparison::const_iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
        total += j->fileCmp.size();

    return total;
}


template <typename CompareFct>
void bubbleSort(FreeFileSync::FolderComparison& folderCmp, CompareFct compare)
{
    for (int i = folderCmp.size() - 2; i >= 0; --i)
    {
        bool swapped = false;
        for (int j = 0; j <= i; ++j)
            if (compare(folderCmp[j + 1], folderCmp[j]))
            {
                std::swap(folderCmp[j + 1].syncPair, folderCmp[j].syncPair);
                folderCmp[j + 1].fileCmp.swap(folderCmp[j].fileCmp);

                swapped = true;
            }

        if (!swapped)
            return;
    }
}


void GridView::sortView(const SortType type, const bool onLeft, const bool ascending)
{
    using namespace FreeFileSync;

    if (type == SORT_BY_DIRECTORY)
    {
        //specialization: use own sorting function based on vector<FileCompareLine>::swap()
        //bubble sort is no performance issue since number of folder pairs should be "small"
        if      (ascending  &&  onLeft) bubbleSort(folderCmp, sortByDirectory<ASCENDING,  SORT_ON_LEFT>);
        else if (ascending  && !onLeft) bubbleSort(folderCmp, sortByDirectory<ASCENDING,  SORT_ON_RIGHT>);
        else if (!ascending &&  onLeft) bubbleSort(folderCmp, sortByDirectory<DESCENDING, SORT_ON_LEFT>);
        else if (!ascending && !onLeft) bubbleSort(folderCmp, sortByDirectory<DESCENDING, SORT_ON_RIGHT>);

        //then sort by relative name
        GridView::sortView(SORT_BY_REL_NAME, onLeft, ascending);
        return;
    }


    for (FolderComparison::iterator j = folderCmp.begin(); j != folderCmp.end(); ++j)
    {
        FileComparison& fileCmp = j->fileCmp;

        switch (type)
        {
        case SORT_BY_REL_NAME:
            if      ( ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByRelativeName<ASCENDING,  SORT_ON_LEFT>);
            else if ( ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByRelativeName<ASCENDING,  SORT_ON_RIGHT>);
            else if (!ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByRelativeName<DESCENDING, SORT_ON_LEFT>);
            else if (!ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByRelativeName<DESCENDING, SORT_ON_RIGHT>);
            break;
        case SORT_BY_FILENAME:
            if      ( ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileName<ASCENDING,  SORT_ON_LEFT>);
            else if ( ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileName<ASCENDING,  SORT_ON_RIGHT>);
            else if (!ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileName<DESCENDING, SORT_ON_LEFT>);
            else if (!ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileName<DESCENDING, SORT_ON_RIGHT>);
            break;
        case SORT_BY_FILESIZE:
            if      ( ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileSize<ASCENDING,  SORT_ON_LEFT>);
            else if ( ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileSize<ASCENDING,  SORT_ON_RIGHT>);
            else if (!ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileSize<DESCENDING, SORT_ON_LEFT>);
            else if (!ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByFileSize<DESCENDING, SORT_ON_RIGHT>);
            break;
        case SORT_BY_DATE:
            if      ( ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByDate<ASCENDING,  SORT_ON_LEFT>);
            else if ( ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByDate<ASCENDING,  SORT_ON_RIGHT>);
            else if (!ascending &&  onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByDate<DESCENDING, SORT_ON_LEFT>);
            else if (!ascending && !onLeft) std::sort(fileCmp.begin(), fileCmp.end(), sortByDate<DESCENDING, SORT_ON_RIGHT>);
            break;
        case SORT_BY_CMP_RESULT:
            if      ( ascending) std::sort(fileCmp.begin(), fileCmp.end(), sortByCmpResult<ASCENDING>);
            else if (!ascending) std::sort(fileCmp.begin(), fileCmp.end(), sortByCmpResult<DESCENDING>);
            break;
        default:
            assert(false);
        }
    }
}

bgstack15