summaryrefslogtreecommitdiff
path: root/plugins/CopyEngine/Ultracopier/Filters.cpp
blob: c7b1526764bc49bdba576c737387ea8a1ec7767c (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
#include "Filters.h"
#include "ui_Filters.h"
#include "../../../cpp11addition.h"

#include <QRegularExpression>

Filters::Filters(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Filters)
{
    ui->setupUi(this);
}

Filters::~Filters()
{
    delete ui;
}

void Filters::setFilters(std::vector<std::string> includeStrings,std::vector<std::string> includeOptions,std::vector<std::string> excludeStrings,std::vector<std::string> excludeOptions)
{
    if(includeStrings.size()!=includeOptions.size() || excludeStrings.size()!=excludeOptions.size())
        return;
    Filters_rules new_item;

    include.clear();
    unsigned int index=0;
    while(index<(unsigned int)includeStrings.size())
    {
        new_item.search_text=includeStrings.at(index);
        std::vector<std::string> options=stringsplit(includeOptions.at(index),';');
        new_item.need_match_all=false;
        new_item.search_type=SearchType_rawText;
        new_item.apply_on=ApplyOn_fileAndFolder;

        if(vectorcontainsAtLeastOne(options,std::string("SearchType_simpleRegex")))
            new_item.search_type=SearchType_simpleRegex;
        if(vectorcontainsAtLeastOne(options,std::string("SearchType_perlRegex")))
            new_item.search_type=SearchType_perlRegex;
        if(vectorcontainsAtLeastOne(options,std::string("ApplyOn_file")))
            new_item.apply_on=ApplyOn_file;
        if(vectorcontainsAtLeastOne(options,std::string("ApplyOn_folder")))
            new_item.apply_on=ApplyOn_folder;
        if(vectorcontainsAtLeastOne(options,std::string("need_match_all")))
            new_item.need_match_all=true;

        if(convertToRegex(new_item))
            include.push_back(new_item);

        index++;
    }

    exclude.clear();
    index=0;
    while(index<excludeStrings.size())
    {
        new_item.search_text=excludeStrings.at(index);
        std::vector<std::string> options=stringsplit(excludeOptions.at(index),';');
        new_item.need_match_all=false;
        new_item.search_type=SearchType_rawText;
        new_item.apply_on=ApplyOn_fileAndFolder;

        if(vectorcontainsAtLeastOne(options,std::string("SearchType_simpleRegex")))
            new_item.search_type=SearchType_simpleRegex;
        if(vectorcontainsAtLeastOne(options,std::string("SearchType_perlRegex")))
            new_item.search_type=SearchType_perlRegex;
        if(vectorcontainsAtLeastOne(options,std::string("ApplyOn_file")))
            new_item.apply_on=ApplyOn_file;
        if(vectorcontainsAtLeastOne(options,std::string("ApplyOn_folder")))
            new_item.apply_on=ApplyOn_folder;
        if(vectorcontainsAtLeastOne(options,std::string("need_match_all")))
            new_item.need_match_all=true;

        if(convertToRegex(new_item))
            exclude.push_back(new_item);

        index++;
    }

    reShowAll();
}

void Filters::reShowAll()
{
    ui->inclusion->clear();
    unsigned int index=0;
    while(index<(unsigned int)include.size())
    {
        std::string entryShow=include.at(index).search_text+" (";
        std::vector<std::string> optionsToShow;
        switch(include.at(index).search_type)
        {
            case SearchType_rawText:
                optionsToShow.push_back(tr("Raw text").toStdString());
            break;
            case SearchType_simpleRegex:
                optionsToShow.push_back(tr("Simplified regex").toStdString());
            break;
            case SearchType_perlRegex:
                optionsToShow.push_back(tr("Perl's regex").toStdString());
            break;
            default:
            break;
        }
        switch(include.at(index).apply_on)
        {
            case ApplyOn_file:
                optionsToShow.push_back(tr("Only on file").toStdString());
            break;
            case ApplyOn_folder:
                optionsToShow.push_back(tr("Only on folder").toStdString());
            break;
            default:
            break;
        }
        if(include.at(index).need_match_all)
            optionsToShow.push_back(tr("Full match").toStdString());
        entryShow+=stringimplode(optionsToShow,",");
        entryShow+=")";
        ui->inclusion->addItem(new QListWidgetItem(QString::fromStdString(entryShow)));
        index++;
    }
    ui->exclusion->clear();
    index=0;
    while(index<(unsigned int)exclude.size())
    {
        std::string entryShow=exclude.at(index).search_text+" (";
        std::vector<std::string> optionsToShow;
        switch(exclude.at(index).search_type)
        {
            case SearchType_rawText:
                optionsToShow.push_back(tr("Raw text").toStdString());
            break;
            case SearchType_simpleRegex:
                optionsToShow.push_back(tr("Simplified regex").toStdString());
            break;
            case SearchType_perlRegex:
                optionsToShow.push_back(tr("Perl's regex").toStdString());
            break;
            default:
            break;
        }
        switch(exclude.at(index).apply_on)
        {
            case ApplyOn_file:
                optionsToShow.push_back(tr("Only on file").toStdString());
            break;
            case ApplyOn_folder:
                optionsToShow.push_back(tr("Only on folder").toStdString());
            break;
            default:
            break;
        }
        if(exclude.at(index).need_match_all)
            optionsToShow.push_back(tr("Full match").toStdString());
        entryShow+=stringimplode(optionsToShow,",");
        entryShow+=")";
        ui->exclusion->addItem(new QListWidgetItem(QString::fromStdString(entryShow)));
        index++;
    }
}

std::vector<Filters_rules> Filters::getInclude() const
{
    return include;
}

std::vector<Filters_rules> Filters::getExclude() const
{
    return exclude;
}

void Filters::newLanguageLoaded()
{
    ui->retranslateUi(this);
    reShowAll();
}

void Filters::updateFilters()
{
    std::vector<std::string> includeStrings,includeOptions,excludeStrings,excludeOptions;
    unsigned int index=0;
    while(index<(unsigned int)include.size())
    {
        includeStrings.push_back(include.at(index).search_text);
        std::vector<std::string> optionsToShow;

        switch(include.at(index).search_type)
        {
            case SearchType_rawText:
                optionsToShow.push_back("SearchType_rawText");
            break;
            case SearchType_simpleRegex:
                optionsToShow.push_back("SearchType_simpleRegex");
            break;
            case SearchType_perlRegex:
                optionsToShow.push_back("SearchType_perlRegex");
            break;
            default:
            break;
        }
        switch(include.at(index).apply_on)
        {
            case ApplyOn_file:
                optionsToShow.push_back("ApplyOn_file");
            break;
            case ApplyOn_fileAndFolder:
                optionsToShow.push_back("ApplyOn_fileAndFolder");
            break;
            case ApplyOn_folder:
                optionsToShow.push_back("ApplyOn_folder");
            break;
            default:
            break;
        }
        if(include.at(index).need_match_all)
            optionsToShow.push_back(tr("Full match").toStdString());
        includeOptions.push_back(stringimplode(optionsToShow,";"));
        index++;
    }
    index=0;
    while(index<(unsigned int)exclude.size())
    {
        excludeStrings.push_back(exclude.at(index).search_text);
        std::vector<std::string> optionsToShow;

        switch(exclude.at(index).search_type)
        {
            case SearchType_rawText:
                optionsToShow.push_back("SearchType_rawText");
            break;
            case SearchType_simpleRegex:
                optionsToShow.push_back("SearchType_simpleRegex");
            break;
            case SearchType_perlRegex:
                optionsToShow.push_back("SearchType_perlRegex");
            break;
            default:
            break;
        }
        switch(exclude.at(index).apply_on)
        {
            case ApplyOn_file:
                optionsToShow.push_back("ApplyOn_file");
            break;
            case ApplyOn_fileAndFolder:
                optionsToShow.push_back("ApplyOn_fileAndFolder");
            break;
            case ApplyOn_folder:
                optionsToShow.push_back("ApplyOn_folder");
            break;
            default:
            break;
        }
        if(exclude.at(index).need_match_all)
            optionsToShow.push_back(tr("Full match").toStdString());
        excludeOptions.push_back(stringimplode(optionsToShow,";"));
        index++;
    }
    emit sendNewFilters(includeStrings,includeOptions,excludeStrings,excludeOptions);
    emit haveNewFilters();
}

bool Filters::convertToRegex(Filters_rules &item)
{
    bool isValid=!item.search_text.empty();
    if(isValid)
    {
        std::regex regex;
        std::string tempString;
        if(item.search_type==SearchType_rawText)
        {
            tempString=QRegularExpression::escape(QString::fromStdString(item.search_text)).toStdString();
            if(tempString.find('/') != std::string::npos || tempString.find('\\') != std::string::npos)
                isValid=false;
        }
        else if(item.search_type==SearchType_simpleRegex)
        {
            tempString=QRegularExpression::escape(QString::fromStdString(item.search_text)).toStdString();
            stringreplaceAll(tempString,"\\*","[^\\\\/]*");
        }
        else if(item.search_type==SearchType_perlRegex)
        {
            tempString=item.search_text;
            if(stringStartWith(tempString,'^') && stringEndsWith(tempString,'$'))
            {
                item.need_match_all=true;
                if(stringStartWith(tempString,'^'))
                    tempString=tempString.substr(1,tempString.size()-1);
                if(stringEndsWith(tempString,'$'))
                    tempString=tempString.substr(0,tempString.size()-1);
                item.search_text=tempString;
            }
        }
        if(isValid)
        {
            if(item.need_match_all==true)
                tempString="^"+tempString+"$";
            regex=std::regex(tempString);
            //isValid=regex.isValid();
            item.regex=regex;
            return true;
        }
        else
            return false;
    }
    return false;
}

void Filters::on_remove_exclusion_clicked()
{
    bool removedEntry=false;
    int index=0;
    while(index<ui->exclusion->count())
    {
        if(ui->exclusion->item(index)->isSelected())
        {
            delete ui->exclusion->item(index);
            exclude.erase(exclude.cbegin()+index);
            removedEntry=true;
        }
        else
            index++;
    }
    if(removedEntry)
    {
        reShowAll();
        updateFilters();
    }
}

void Filters::on_remove_inclusion_clicked()
{
    bool removedEntry=false;
    int index=0;
    while(index<ui->inclusion->count())
    {
        if(ui->inclusion->item(index)->isSelected())
        {
            delete ui->inclusion->item(index);
            include.erase(include.cbegin()+index);
            removedEntry=true;
        }
        else
            index++;
    }
    if(removedEntry)
    {
        reShowAll();
        updateFilters();
    }
}

void Filters::on_add_exclusion_clicked()
{
    FilterRules dialog(this);
    dialog.exec();
    if(dialog.getIsValid())
    {
        Filters_rules new_item;
        new_item.apply_on=dialog.get_apply_on();
        new_item.need_match_all=dialog.get_need_match_all();
        new_item.search_text=dialog.get_search_text();
        new_item.search_type=dialog.get_search_type();
        exclude.push_back(new_item);
        reShowAll();
        updateFilters();
    }
}

void Filters::on_buttonBox_clicked(QAbstractButton *button)
{
    if(ui->buttonBox->buttonRole(button)==QDialogButtonBox::RejectRole)
        reject();
}

void Filters::on_add_inclusion_clicked()
{
    FilterRules dialog(this);
    dialog.exec();
    if(dialog.getIsValid())
    {
        Filters_rules new_item;
        new_item.apply_on=dialog.get_apply_on();
        new_item.need_match_all=dialog.get_need_match_all();
        new_item.search_text=dialog.get_search_text();
        new_item.search_type=dialog.get_search_type();
        if(convertToRegex(new_item))
            include.push_back(new_item);
        reShowAll();
        updateFilters();
    }
}

void Filters::on_edit_exclusion_clicked()
{
    bool editedEntry=false;
    int index=0;
    while(index<ui->exclusion->count())
    {
        if(ui->exclusion->item(index)->isSelected())
        {
            FilterRules dialog(this);
            dialog.set_apply_on(exclude.at(index).apply_on);
            dialog.set_need_match_all(exclude.at(index).need_match_all);
            dialog.set_search_text(exclude.at(index).search_text);
            dialog.set_search_type(exclude.at(index).search_type);
            dialog.exec();
            if(dialog.getIsValid())
            {
                exclude[index].apply_on=dialog.get_apply_on();
                exclude[index].need_match_all=dialog.get_need_match_all();
                exclude[index].search_text=dialog.get_search_text();
                exclude[index].search_type=dialog.get_search_type();
                if(!convertToRegex(exclude[index]))
                    exclude.erase(exclude.cbegin()+index);
                editedEntry=true;
            }
        }
        index++;
    }
    if(editedEntry)
    {
        reShowAll();
        updateFilters();
    }
}

void Filters::on_edit_inclusion_clicked()
{
    bool editedEntry=false;
    int index=0;
    while(index<ui->inclusion->count())
    {
        if(ui->inclusion->item(index)->isSelected())
        {
            FilterRules dialog(this);
            dialog.set_apply_on(exclude.at(index).apply_on);
            dialog.set_need_match_all(exclude.at(index).need_match_all);
            dialog.set_search_text(exclude.at(index).search_text);
            dialog.set_search_type(exclude.at(index).search_type);
            dialog.exec();
            if(dialog.getIsValid())
            {
                exclude[index].apply_on=dialog.get_apply_on();
                exclude[index].need_match_all=dialog.get_need_match_all();
                exclude[index].search_text=dialog.get_search_text();
                exclude[index].search_type=dialog.get_search_type();
                if(!convertToRegex(exclude[index]))
                    exclude.erase(exclude.cbegin()+index);
                editedEntry=true;
            }
        }
        index++;
    }
    if(editedEntry)
    {
        reShowAll();
        updateFilters();
    }
}