~ubuntu-branches/debian/sid/filezilla/sid

« back to all changes in this revision

Viewing changes to src/interface/filter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Cécile (Le_Vert)
  • Date: 2008-07-05 21:00:24 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080705210024-mvzp21zlyheschi6
Tags: 3.0.11.1-1
* wxWidgets 2.8 just entered unstable ! Upload to unstable.
* New upstream release.
* Bump Standards-Version to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
#include "inputdialog.h"
11
11
#include "state.h"
12
12
 
13
 
bool CFilterDialog::m_loaded = false;
14
 
std::vector<CFilter> CFilterDialog::m_globalFilters;
15
 
std::vector<CFilterSet> CFilterDialog::m_globalFilterSets;
16
 
unsigned int CFilterDialog::m_globalCurrentFilterSet = 0;
 
13
bool CFilterManager::m_loaded = false;
 
14
std::vector<CFilter> CFilterManager::m_globalFilters;
 
15
std::vector<CFilterSet> CFilterManager::m_globalFilterSets;
 
16
unsigned int CFilterManager::m_globalCurrentFilterSet = 0;
17
17
 
18
18
BEGIN_EVENT_TABLE(CFilterDialog, wxDialogEx)
19
19
EVT_BUTTON(XRCID("wxID_OK"), CFilterDialog::OnOK)
33
33
 
34
34
CFilterCondition::CFilterCondition()
35
35
{
36
 
        type = 0;
 
36
        type = name;
37
37
        condition = 0;
38
38
        pRegEx = 0;
39
39
        matchCase = true;
45
45
        delete pRegEx;
46
46
}
47
47
 
 
48
CFilterCondition::CFilterCondition(const CFilterCondition& cond)
 
49
{
 
50
        pRegEx = 0;
 
51
        *this = cond;
 
52
}
 
53
 
48
54
CFilterCondition& CFilterCondition::operator=(const CFilterCondition& cond)
49
55
{
50
56
        type = cond.type;
72
78
#endif
73
79
}
74
80
 
 
81
bool CFilter::HasConditionOfType(enum t_filterType type) const
 
82
{
 
83
        for (std::vector<CFilterCondition>::const_iterator iter = filters.begin(); iter != filters.end(); iter++)
 
84
        {
 
85
                if (iter->type == type)
 
86
                        return true;
 
87
        }
 
88
 
 
89
        return false;
 
90
}
 
91
 
75
92
CFilterDialog::CFilterDialog()
76
93
{
77
 
        m_currentFilterSet = 0;
78
94
        m_pMainFrame = 0;
79
95
        m_shiftClick = false;
80
 
        
81
 
        LoadFilters();
82
 
        CompileRegexes();
83
 
 
84
 
        if (m_globalFilterSets.empty())
85
 
        {
86
 
                CFilterSet set;
87
 
                set.local.resize(m_filters.size(), false);
88
 
                set.remote.resize(m_filters.size(), false);
89
 
 
90
 
                m_globalFilterSets.push_back(set);
91
 
                m_filterSets.push_back(set);
92
 
        }
93
96
}
94
97
 
95
98
bool CFilterDialog::Create(CMainFrame* parent)
139
142
        CFilterEditDialog dlg;
140
143
        if (!dlg.Create(this, m_filters, m_filterSets))
141
144
                return;
142
 
        
 
145
 
143
146
        if (dlg.ShowModal() != wxID_OK)
144
147
                return;
145
148
 
152
155
 
153
156
void CFilterDialog::SaveFilters()
154
157
{
155
 
        CInterProcessMutex(MUTEX_FILTERS);
 
158
        CInterProcessMutex mutex(MUTEX_FILTERS);
156
159
 
157
160
        wxFileName file(wxGetApp().GetSettingsDir(), _T("filters.xml"));
158
161
        TiXmlElement* pDocument = GetXmlFile(file);
190
193
                        const CFilterCondition& condition = *conditionIter;
191
194
                        TiXmlElement* pCondition = pConditions->InsertEndChild(TiXmlElement("Condition"))->ToElement();
192
195
 
193
 
                        AddTextElement(pCondition, "Type", wxString::Format(_T("%d"), condition.type));
194
 
                        AddTextElement(pCondition, "Condition", wxString::Format(_T("%d"), condition.condition));
 
196
                        AddTextElement(pCondition, "Type", condition.type);
 
197
                        AddTextElement(pCondition, "Condition", condition.condition);
195
198
                        AddTextElement(pCondition, "Value", condition.strValue);
196
199
                }
197
200
        }
226
229
        delete pDocument->GetDocument();
227
230
}
228
231
 
229
 
void CFilterDialog::LoadFilters()
230
 
{
231
 
        if (m_loaded)
232
 
        {
233
 
                m_filters = m_globalFilters;
234
 
                m_filterSets = m_globalFilterSets;
235
 
                return;
236
 
        }
237
 
        m_loaded = true;
238
 
 
239
 
        CInterProcessMutex(MUTEX_FILTERS);
240
 
 
241
 
        wxFileName file(wxGetApp().GetSettingsDir(), _T("filters.xml"));
242
 
        TiXmlElement* pDocument = GetXmlFile(file);
243
 
        if (!pDocument)
244
 
        {
245
 
                wxString msg = wxString::Format(_("Could not load \"%s\", please make sure the file is valid and can be accessed.\nAny changes made in the Site Manager could not be saved."), file.GetFullPath().c_str());
246
 
                wxMessageBox(msg, _("Error loading xml file"), wxICON_ERROR);
247
 
 
248
 
                return;
249
 
        }
250
 
 
251
 
        TiXmlElement *pFilters = pDocument->FirstChildElement("Filters");
252
 
 
253
 
        if (!pFilters)
254
 
        {
255
 
                delete pDocument->GetDocument();
256
 
                return;
257
 
        }
258
 
 
259
 
        TiXmlElement *pFilter = pFilters->FirstChildElement("Filter");
260
 
        while (pFilter)
261
 
        {
262
 
                CFilter filter;
263
 
                filter.name = GetTextElement(pFilter, "Name");
264
 
                if (filter.name == _T(""))
265
 
                {
266
 
                        pFilter = pFilter->NextSiblingElement("Filter");
267
 
                        continue;
268
 
                }
269
 
 
270
 
                filter.filterFiles = GetTextElement(pFilter, "ApplyToFiles") == _T("1");
271
 
                filter.filterDirs = GetTextElement(pFilter, "ApplyToDirs") == _T("1");
272
 
 
273
 
                wxString type = GetTextElement(pFilter, "MatchType");
274
 
                if (type == _T("Any"))
275
 
                        filter.matchType = CFilter::any;
276
 
                else if (type == _T("None"))
277
 
                        filter.matchType = CFilter::none;
278
 
                else
279
 
                        filter.matchType = CFilter::all;
280
 
                filter.matchCase = GetTextElement(pFilter, "MatchCase") == _T("1");
281
 
 
282
 
                TiXmlElement *pConditions = pFilter->FirstChildElement("Conditions");
283
 
                if (!pConditions)
284
 
                {
285
 
                        pFilter = pFilter->NextSiblingElement("Filter");
286
 
                        continue;
287
 
                }
288
 
                
289
 
                TiXmlElement *pCondition = pConditions->FirstChildElement("Condition");
290
 
                while (pCondition)
291
 
                {
292
 
                        CFilterCondition condition;
293
 
                        condition.type = GetTextElementInt(pCondition, "Type", 0);
294
 
                        condition.condition = GetTextElementInt(pCondition, "Condition", 0);
295
 
                        condition.strValue = GetTextElement(pCondition, "Value");
296
 
                        condition.matchCase = filter.matchCase;
297
 
                        if (condition.strValue == _T(""))
298
 
                        {
299
 
                                pCondition = pCondition->NextSiblingElement("Condition");
300
 
                                continue;
301
 
                        }
302
 
 
303
 
                        // TODO: 64bit filesize
304
 
                        if (condition.type == 1)
305
 
                        {
306
 
                                unsigned long tmp;
307
 
                                condition.strValue.ToULong(&tmp);
308
 
                                condition.value = tmp;
309
 
                        }
310
 
 
311
 
                        filter.filters.push_back(condition);
312
 
 
313
 
                        pCondition = pCondition->NextSiblingElement("Condition");
314
 
                }
315
 
 
316
 
                if (!filter.filters.empty())
317
 
                        m_globalFilters.push_back(filter);
318
 
 
319
 
                pFilter = pFilter->NextSiblingElement("Filter");
320
 
        }
321
 
        m_filters = m_globalFilters;
322
 
 
323
 
        TiXmlElement* pSets = pDocument->FirstChildElement("Sets");
324
 
        if (!pSets)
325
 
        {
326
 
                delete pDocument->GetDocument();
327
 
                return;
328
 
        }
329
 
 
330
 
        for (TiXmlElement* pSet = pSets->FirstChildElement("Set"); pSet; pSet = pSet->NextSiblingElement("Set"))
331
 
        {
332
 
                CFilterSet set;
333
 
                TiXmlElement* pItem = pSet->FirstChildElement("Item");
334
 
                while (pItem)
335
 
                {
336
 
                        wxString local = GetTextElement(pItem, "Local");
337
 
                        wxString remote = GetTextElement(pItem, "Remote");
338
 
                        set.local.push_back(local == _T("1") ? true : false);
339
 
                        set.remote.push_back(remote == _T("1") ? true : false);
340
 
 
341
 
                        pItem = pItem->NextSiblingElement("Item");
342
 
                }
343
 
 
344
 
                if (!m_globalFilterSets.empty())
345
 
                {
346
 
                        set.name = GetTextElement(pSet, "Name");
347
 
                        if (set.name == _T(""))
348
 
                                continue;
349
 
                }
350
 
 
351
 
                if (set.local.size() == m_filters.size())
352
 
                        m_globalFilterSets.push_back(set);
353
 
        }
354
 
        m_filterSets = m_globalFilterSets;
355
 
 
356
 
        wxString attribute = GetTextAttribute(pSets, "Current");
357
 
        unsigned long value;
358
 
        if (attribute.ToULong(&value))
359
 
        {
360
 
                if (value < m_globalFilterSets.size())
361
 
                        m_globalCurrentFilterSet = value;
362
 
        }
363
 
 
364
 
        m_currentFilterSet = m_globalCurrentFilterSet;
365
 
 
366
 
        delete pDocument->GetDocument();
367
 
}
368
 
 
369
232
void CFilterDialog::DisplayFilters()
370
233
{
371
234
        wxCheckListBox* pLocalFilters = XRCCTRL(*this, "ID_LOCALFILTERS", wxCheckListBox);
377
240
        for (unsigned int i = 0; i < m_filters.size(); i++)
378
241
        {
379
242
                const CFilter& filter = m_filters[i];
 
243
 
 
244
                const bool localOnly = filter.HasConditionOfType(attributes) || filter.HasConditionOfType(permissions);
 
245
 
380
246
                pLocalFilters->Append(filter.name);
381
247
                pRemoteFilters->Append(filter.name);
382
248
 
383
249
                pLocalFilters->Check(i, m_filterSets[m_currentFilterSet].local[i]);
384
 
                pRemoteFilters->Check(i, m_filterSets[m_currentFilterSet].remote[i]);
 
250
                pRemoteFilters->Check(i, localOnly ? false : m_filterSets[m_currentFilterSet].remote[i]);
385
251
        }
386
252
}
387
253
 
404
270
 
405
271
        int item = event.GetSelection();
406
272
 
 
273
        const CFilter& filter = m_filters[item];
 
274
        const bool localOnly = filter.HasConditionOfType(attributes) || filter.HasConditionOfType(permissions);
 
275
        if (localOnly && event.GetEventObject() != pLocal)
 
276
        {
 
277
                pRemote->Check(item, false);
 
278
                wxMessageBox(_("Selected filter only works for local files."), _("Cannot select filter"), wxICON_INFORMATION);
 
279
                return;
 
280
        }
 
281
 
 
282
 
407
283
        if (m_shiftClick)
408
284
        {
409
285
                if (event.GetEventObject() == pLocal)
410
 
                        pRemote->Check(item, pLocal->IsChecked(event.GetSelection()));
 
286
                {
 
287
                        if (!localOnly)
 
288
                                pRemote->Check(item, pLocal->IsChecked(event.GetSelection()));
 
289
                }
411
290
                else
412
291
                        pLocal->Check(item, pRemote->IsChecked(event.GetSelection()));
413
292
        }
426
305
        m_filterSets[0].remote[item] = remoteChecked;
427
306
}
428
307
 
429
 
bool CFilterDialog::FilenameFiltered(const wxString& name, bool dir, wxLongLong size, bool local) const
 
308
void CFilterDialog::OnSaveAs(wxCommandEvent& event)
 
309
{
 
310
        CInputDialog dlg;
 
311
        dlg.Create(this, _("Enter name for filterset"), _("Please enter a unique name for this filter set"));
 
312
        if (dlg.ShowModal() != wxID_OK)
 
313
                return;
 
314
 
 
315
        wxString name = dlg.GetValue();
 
316
        if (name == _T(""))
 
317
        {
 
318
                wxMessageBox(_("No name for the filterset given."), _("Cannot save filterset"), wxICON_INFORMATION);
 
319
                return;
 
320
        }
 
321
        wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
 
322
        int pos = pChoice->FindString(name);
 
323
        if (pos != wxNOT_FOUND)
 
324
        {
 
325
                if (wxMessageBox(_("Given filterset name already exists, overwrite filter set?"), _("Filter set already exists"), wxICON_QUESTION | wxYES_NO) != wxYES)
 
326
                        return;
 
327
        }
 
328
 
 
329
        if (pos == wxNOT_FOUND)
 
330
        {
 
331
                pos = m_filterSets.size();
 
332
                m_filterSets.push_back(m_filterSets[0]);
 
333
                pChoice->Append(name);
 
334
        }
 
335
        else
 
336
                m_filterSets[pos] = m_filterSets[0];
 
337
 
 
338
        m_filterSets[pos].name = name;
 
339
 
 
340
        pChoice->SetSelection(pos);
 
341
        m_currentFilterSet = pos;
 
342
}
 
343
 
 
344
void CFilterDialog::OnDeleteSet(wxCommandEvent& event)
 
345
{
 
346
        wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
 
347
        int pos = pChoice->GetSelection();
 
348
        if (pos == -1)
 
349
                return;
 
350
 
 
351
        if (!pos)
 
352
        {
 
353
                wxMessageBox(_("This filter set cannot be removed."));
 
354
                return;
 
355
        }
 
356
 
 
357
        m_filterSets[0] = m_filterSets[pos];
 
358
 
 
359
        pChoice->Delete(pos);
 
360
        m_filterSets.erase(m_filterSets.begin() + pos);
 
361
        wxASSERT(!m_filterSets.empty());
 
362
 
 
363
        pChoice->SetSelection(0);
 
364
        m_currentFilterSet = 0;
 
365
}
 
366
 
 
367
void CFilterDialog::OnSetSelect(wxCommandEvent& event)
 
368
{
 
369
        m_currentFilterSet = event.GetSelection();
 
370
        DisplayFilters();
 
371
}
 
372
 
 
373
void CFilterDialog::OnChangeAll(wxCommandEvent& event)
 
374
{
 
375
        bool check = true;
 
376
        if (event.GetId() == XRCID("ID_LOCAL_DISABLEALL") || event.GetId() == XRCID("ID_REMOTE_DISABLEALL"))
 
377
                check = false;
 
378
 
 
379
        bool local;
 
380
        std::vector<bool>* pValues;
 
381
        wxCheckListBox* pListBox;
 
382
        if (event.GetId() == XRCID("ID_LOCAL_ENABLEALL") || event.GetId() == XRCID("ID_LOCAL_DISABLEALL"))
 
383
        {
 
384
                pListBox = XRCCTRL(*this, "ID_LOCALFILTERS", wxCheckListBox);
 
385
                pValues = &m_filterSets[0].local;
 
386
                local = true;
 
387
        }
 
388
        else
 
389
        {
 
390
                pListBox = XRCCTRL(*this, "ID_REMOTEFILTERS", wxCheckListBox);
 
391
                pValues = &m_filterSets[0].remote;
 
392
                local = false;
 
393
        }
 
394
 
 
395
        if (m_currentFilterSet)
 
396
        {
 
397
                m_filterSets[0] = m_filterSets[m_currentFilterSet];
 
398
                m_currentFilterSet = 0;
 
399
                wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
 
400
                pChoice->SetSelection(0);
 
401
        }
 
402
 
 
403
        for (size_t i = 0; i < pListBox->GetCount(); i++)
 
404
        {
 
405
                if (!local && (m_filters[i].HasConditionOfType(attributes) || m_filters[i].HasConditionOfType(permissions)))
 
406
                {
 
407
                        pListBox->Check(i, false);
 
408
                        (*pValues)[i] = false;
 
409
                }
 
410
                else
 
411
                {
 
412
                        pListBox->Check(i, check);
 
413
                        (*pValues)[i] = check;
 
414
                }
 
415
        }
 
416
}
 
417
 
 
418
void CFilterDialog::OnApply(wxCommandEvent& event)
 
419
{
 
420
        m_globalFilters = m_filters;
 
421
        m_globalFilterSets = m_filterSets;
 
422
        m_globalCurrentFilterSet = m_currentFilterSet;
 
423
 
 
424
        SaveFilters();
 
425
 
 
426
        m_pMainFrame->GetState()->NotifyHandlers(STATECHANGE_APPLYFILTER);
 
427
}
 
428
 
 
429
CFilterManager::CFilterManager()
 
430
{
 
431
        m_currentFilterSet = 0;
 
432
        LoadFilters();
 
433
        CompileRegexes();
 
434
 
 
435
        if (m_globalFilterSets.empty())
 
436
        {
 
437
                CFilterSet set;
 
438
                set.local.resize(m_filters.size(), false);
 
439
                set.remote.resize(m_filters.size(), false);
 
440
 
 
441
                m_globalFilterSets.push_back(set);
 
442
                m_filterSets.push_back(set);
 
443
        }
 
444
}
 
445
 
 
446
bool CFilterManager::HasActiveFilters() const
 
447
{
 
448
        wxASSERT(m_globalCurrentFilterSet < m_globalFilterSets.size());
 
449
 
 
450
        const CFilterSet& set = m_globalFilterSets[m_globalCurrentFilterSet];
 
451
        for (unsigned int i = 0; i < m_globalFilters.size(); i++)
 
452
        {
 
453
                if (set.local[i])
 
454
                        return true;
 
455
 
 
456
                if (set.remote[i])
 
457
                        return true;
 
458
        }
 
459
 
 
460
        return false;
 
461
}
 
462
 
 
463
bool CFilterManager::HasSameLocalAndRemoteFilters() const
 
464
{
 
465
        const CFilterSet& set = m_filterSets[m_currentFilterSet];
 
466
        for (unsigned int i = 0; i < m_filters.size(); i++)
 
467
        {
 
468
                if (set.local[i])
 
469
                {
 
470
                        if (!set.remote[i])
 
471
                                return false;
 
472
                }
 
473
                else if (set.remote[i])
 
474
                        return false;
 
475
        }
 
476
 
 
477
        return true;
 
478
}
 
479
 
 
480
bool CFilterManager::FilenameFiltered(const wxString& name, bool dir, wxLongLong size, bool local, int attributes) const
430
481
{
431
482
        wxASSERT(m_currentFilterSet < m_filterSets.size());
432
483
 
 
484
        const CFilterSet& set = m_filterSets[m_currentFilterSet];
 
485
 
433
486
        // Check active filters
434
487
        for (unsigned int i = 0; i < m_filters.size(); i++)
435
488
        {
436
489
                if (local)
437
490
                {
438
 
                        if (m_filterSets[m_currentFilterSet].local[i])
439
 
                                if (FilenameFilteredByFilter(name, dir, size, i))
 
491
                        if (set.local[i])
 
492
                                if (FilenameFilteredByFilter(name, dir, size, i, attributes))
440
493
                                        return true;
441
494
                }
442
495
                else
443
496
                {
444
 
                        if (m_filterSets[m_currentFilterSet].remote[i])
445
 
                                if (FilenameFilteredByFilter(name, dir, size, i))
 
497
                        if (set.remote[i])
 
498
                                if (FilenameFilteredByFilter(name, dir, size, i, attributes))
446
499
                                        return true;
447
500
                }
448
501
        }
450
503
        return false;
451
504
}
452
505
 
453
 
bool CFilterDialog::FilenameFilteredByFilter(const wxString& name, bool dir, wxLongLong size, unsigned int filterIndex) const
 
506
bool CFilterManager::FilenameFilteredByFilter(const wxString& name, bool dir, wxLongLong size, unsigned int filterIndex, int attributes) const
454
507
{
455
508
        wxRegEx regex;
456
509
        const CFilter& filter = m_filters[filterIndex];
459
512
                return false;
460
513
        else if (!dir && !filter.filterFiles)
461
514
                return false;
462
 
    
 
515
 
463
516
        for (std::vector<CFilterCondition>::const_iterator iter = filter.filters.begin(); iter != filter.filters.end(); iter++)
464
517
        {
465
518
                bool match = false;
467
520
 
468
521
                switch (condition.type)
469
522
                {
470
 
                case 0:
 
523
                case (enum t_filterType)::name:
471
524
                        switch (condition.condition)
472
525
                        {
473
526
                        case 0:
530
583
                                        match = true;
531
584
                        }
532
585
                        break;
533
 
                case 1:
 
586
                case (enum t_filterType)::size:
534
587
                        if (size == -1)
535
588
                                continue;
536
589
                        switch (condition.condition)
549
602
                                break;
550
603
                        }
551
604
                        break;
 
605
                case (enum t_filterType)::attributes:
 
606
#ifndef __WXMSW__
 
607
                        continue;
 
608
#else
 
609
                        if (!attributes)
 
610
                                continue;
 
611
 
 
612
                        {
 
613
                                int flag = 0;
 
614
                                switch (condition.condition)
 
615
                                {
 
616
                                case 0:
 
617
                                        flag = FILE_ATTRIBUTE_ARCHIVE;
 
618
                                        break;
 
619
                                case 1:
 
620
                                        flag = FILE_ATTRIBUTE_COMPRESSED;
 
621
                                        break;
 
622
                                case 2:
 
623
                                        flag = FILE_ATTRIBUTE_ENCRYPTED;
 
624
                                        break;
 
625
                                case 3:
 
626
                                        flag = FILE_ATTRIBUTE_HIDDEN;
 
627
                                        break;
 
628
                                case 4:
 
629
                                        flag = FILE_ATTRIBUTE_READONLY;
 
630
                                        break;
 
631
                                case 5:
 
632
                                        flag = FILE_ATTRIBUTE_SYSTEM;
 
633
                                        break;
 
634
                                }
 
635
 
 
636
                                int set = (flag & attributes) ? 1 : 0;
 
637
                                if (set == condition.value)
 
638
                                        match = true;
 
639
                        }
 
640
#endif //__WXMSW__
 
641
                        break;
 
642
                case permissions:
 
643
#ifdef __WXMSW__
 
644
                        continue;
 
645
#else
 
646
                        if (attributes == -1)
 
647
                                continue;
 
648
 
 
649
                        {
 
650
                                int flag = 0;
 
651
                                switch (condition.condition)
 
652
                                {
 
653
                                case 0:
 
654
                                        flag = S_IRUSR;
 
655
                                        break;
 
656
                                case 1:
 
657
                                        flag = S_IWUSR;
 
658
                                        break;
 
659
                                case 2:
 
660
                                        flag = S_IXUSR;
 
661
                                        break;
 
662
                                case 3:
 
663
                                        flag = S_IRGRP;
 
664
                                        break;
 
665
                                case 4:
 
666
                                        flag = S_IWGRP;
 
667
                                        break;
 
668
                                case 5:
 
669
                                        flag = S_IXGRP;
 
670
                                        break;
 
671
                                case 6:
 
672
                                        flag = S_IROTH;
 
673
                                        break;
 
674
                                case 7:
 
675
                                        flag = S_IWOTH;
 
676
                                        break;
 
677
                                case 8:
 
678
                                        flag = S_IXOTH;
 
679
                                        break;
 
680
                                }
 
681
 
 
682
                                int set = (flag & attributes) ? 1 : 0;
 
683
                                if (set == condition.value)
 
684
                                        match = true;
 
685
                        }
 
686
#endif //__WXMSW__
 
687
                        break;
 
688
                default:
 
689
                        wxFAIL_MSG(_T("Unhandled filter type"));
 
690
                        break;
552
691
                }
553
692
                if (match)
554
693
                {
566
705
 
567
706
        if (filter.matchType != CFilter::any)
568
707
                return true;
569
 
        
 
708
 
570
709
        return false;
571
710
}
572
711
 
573
 
bool CFilterDialog::CompileRegexes()
 
712
bool CFilterManager::CompileRegexes()
574
713
{
575
714
        for (unsigned int i = 0; i < m_filters.size(); i++)
576
715
        {
588
727
        return true;
589
728
}
590
729
 
591
 
void CFilterDialog::OnSaveAs(wxCommandEvent& event)
592
 
{
593
 
        CInputDialog dlg;
594
 
        dlg.Create(this, _("Enter name for filterset"), _("Please enter a unique name for this filter set"));
595
 
        if (dlg.ShowModal() != wxID_OK)
596
 
                return;
597
 
 
598
 
        wxString name = dlg.GetValue();
599
 
        wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
600
 
        int pos = pChoice->FindString(name);
601
 
        if (pos != wxNOT_FOUND)
602
 
        {
603
 
                if (wxMessageBox(_("Given filterset name already exists, overwrite filter set?"), _("Filter set already exists"), wxICON_QUESTION | wxYES_NO) != wxYES)
604
 
                        return;
605
 
        }
606
 
 
607
 
        if (pos == wxNOT_FOUND)
608
 
        {
609
 
                pos = m_filterSets.size();
610
 
                m_filterSets.push_back(m_filterSets[0]);
611
 
                pChoice->Append(name);
612
 
        }
613
 
        else
614
 
                m_filterSets[pos] = m_filterSets[0];
615
 
 
616
 
        m_filterSets[pos].name = name;
617
 
 
618
 
        pChoice->SetSelection(pos);
619
 
        m_currentFilterSet = pos;
620
 
}
621
 
 
622
 
void CFilterDialog::OnDeleteSet(wxCommandEvent& event)
623
 
{
624
 
        wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
625
 
        int pos = pChoice->GetSelection();
626
 
        if (pos == -1)
627
 
                return;
628
 
 
629
 
        if (!pos)
630
 
        {
631
 
                wxMessageBox(_("This filter set cannot be removed"));
632
 
                return;
633
 
        }
634
 
 
635
 
        m_filterSets[0] = m_filterSets[pos];
636
 
 
637
 
        pChoice->Delete(pos);
638
 
        m_filterSets.erase(m_filterSets.begin() + pos);
639
 
        wxASSERT(!m_filterSets.empty());
640
 
 
641
 
        pChoice->SetSelection(0);
642
 
        m_currentFilterSet = 0;
643
 
}
644
 
 
645
 
void CFilterDialog::OnSetSelect(wxCommandEvent& event)
646
 
{
647
 
        m_currentFilterSet = event.GetSelection();
648
 
        DisplayFilters();
649
 
}
650
 
 
651
 
void CFilterDialog::OnChangeAll(wxCommandEvent& event)
652
 
{
653
 
        bool check = true;
654
 
        if (event.GetId() == XRCID("ID_LOCAL_DISABLEALL") || event.GetId() == XRCID("ID_REMOTE_DISABLEALL"))
655
 
                check = false;
656
 
 
657
 
        std::vector<bool>* pValues;
658
 
        wxCheckListBox* pListBox;
659
 
        if (event.GetId() == XRCID("ID_LOCAL_ENABLEALL") || event.GetId() == XRCID("ID_LOCAL_DISABLEALL"))
660
 
        {
661
 
                pListBox = XRCCTRL(*this, "ID_LOCALFILTERS", wxCheckListBox);
662
 
                pValues = &m_filterSets[0].local;
663
 
        }
664
 
        else
665
 
        {
666
 
                pListBox = XRCCTRL(*this, "ID_REMOTEFILTERS", wxCheckListBox);
667
 
                pValues = &m_filterSets[0].remote;
668
 
        }
669
 
 
670
 
        if (m_currentFilterSet)
671
 
        {
672
 
                m_filterSets[0] = m_filterSets[m_currentFilterSet];
673
 
                m_currentFilterSet = 0;
674
 
                wxChoice* pChoice = XRCCTRL(*this, "ID_SETS", wxChoice);
675
 
                pChoice->SetSelection(0);
676
 
        }
677
 
 
678
 
        for (size_t i = 0; i < pListBox->GetCount(); i++)
679
 
        {
680
 
                pListBox->Check(i, check);
681
 
                (*pValues)[i] = check;
682
 
        }
683
 
}
684
 
 
685
 
void CFilterDialog::OnApply(wxCommandEvent& event)
686
 
{
687
 
        m_globalFilters = m_filters;
688
 
        m_globalFilterSets = m_filterSets;
689
 
        m_globalCurrentFilterSet = m_currentFilterSet;
690
 
 
691
 
        SaveFilters();
692
 
 
693
 
        m_pMainFrame->GetState()->ApplyCurrentFilter();
694
 
}
695
 
 
696
 
bool CFilterDialog::HasActiveFilters() const
697
 
{
698
 
        wxASSERT(m_currentFilterSet < m_filterSets.size());
699
 
 
700
 
        for (unsigned int i = 0; i < m_filters.size(); i++)
701
 
        {
702
 
                if (m_filterSets[m_currentFilterSet].local[i])
703
 
                        return true;
704
 
 
705
 
                if (m_filterSets[m_currentFilterSet].remote[i])
706
 
                        return true;
707
 
        }
708
 
 
709
 
        return false;
 
730
void CFilterManager::LoadFilters()
 
731
{
 
732
        if (m_loaded)
 
733
        {
 
734
                m_filters = m_globalFilters;
 
735
                m_filterSets = m_globalFilterSets;
 
736
                m_currentFilterSet = m_globalCurrentFilterSet;
 
737
                return;
 
738
        }
 
739
        m_loaded = true;
 
740
 
 
741
        CInterProcessMutex mutex(MUTEX_FILTERS);
 
742
 
 
743
        wxFileName file(wxGetApp().GetSettingsDir(), _T("filters.xml"));
 
744
        if (!file.FileExists())
 
745
        {
 
746
                wxFileName defaults(wxGetApp().GetResourceDir(), _T("defaultfilters.xml"));
 
747
                if (defaults.FileExists())
 
748
                {
 
749
                        TiXmlElement* pDocument = GetXmlFile(defaults);
 
750
                        if (pDocument)
 
751
                                SaveXmlFile(file, pDocument);
 
752
                }
 
753
        }
 
754
 
 
755
        TiXmlElement* pDocument = GetXmlFile(file);
 
756
        if (!pDocument)
 
757
        {
 
758
                wxString msg = wxString::Format(_("Could not load \"%s\", please make sure the file is valid and can be accessed.\nAny changes made in the Site Manager could not be saved."), file.GetFullPath().c_str());
 
759
                wxMessageBox(msg, _("Error loading xml file"), wxICON_ERROR);
 
760
 
 
761
                return;
 
762
        }
 
763
 
 
764
        TiXmlElement *pFilters = pDocument->FirstChildElement("Filters");
 
765
 
 
766
        if (!pFilters)
 
767
        {
 
768
                delete pDocument->GetDocument();
 
769
                return;
 
770
        }
 
771
 
 
772
        TiXmlElement *pFilter = pFilters->FirstChildElement("Filter");
 
773
        while (pFilter)
 
774
        {
 
775
                CFilter filter;
 
776
                filter.name = GetTextElement(pFilter, "Name");
 
777
                if (filter.name == _T(""))
 
778
                {
 
779
                        pFilter = pFilter->NextSiblingElement("Filter");
 
780
                        continue;
 
781
                }
 
782
 
 
783
                filter.filterFiles = GetTextElement(pFilter, "ApplyToFiles") == _T("1");
 
784
                filter.filterDirs = GetTextElement(pFilter, "ApplyToDirs") == _T("1");
 
785
 
 
786
                wxString type = GetTextElement(pFilter, "MatchType");
 
787
                if (type == _T("Any"))
 
788
                        filter.matchType = CFilter::any;
 
789
                else if (type == _T("None"))
 
790
                        filter.matchType = CFilter::none;
 
791
                else
 
792
                        filter.matchType = CFilter::all;
 
793
                filter.matchCase = GetTextElement(pFilter, "MatchCase") == _T("1");
 
794
 
 
795
                TiXmlElement *pConditions = pFilter->FirstChildElement("Conditions");
 
796
                if (!pConditions)
 
797
                {
 
798
                        pFilter = pFilter->NextSiblingElement("Filter");
 
799
                        continue;
 
800
                }
 
801
 
 
802
                TiXmlElement *pCondition = pConditions->FirstChildElement("Condition");
 
803
                while (pCondition)
 
804
                {
 
805
                        CFilterCondition condition;
 
806
                        int type = GetTextElementInt(pCondition, "Type", 0);
 
807
                        if (type < 0 || type >= filterType_size)
 
808
                        {
 
809
                                pCondition = pCondition->NextSiblingElement("Condition");
 
810
                                continue;
 
811
                        }
 
812
                        condition.type = (enum t_filterType)type;
 
813
                        condition.condition = GetTextElementInt(pCondition, "Condition", 0);
 
814
                        condition.strValue = GetTextElement(pCondition, "Value");
 
815
                        condition.matchCase = filter.matchCase;
 
816
                        if (condition.strValue == _T(""))
 
817
                        {
 
818
                                pCondition = pCondition->NextSiblingElement("Condition");
 
819
                                continue;
 
820
                        }
 
821
 
 
822
                        // TODO: 64bit filesize
 
823
                        if (condition.type == size)
 
824
                        {
 
825
                                unsigned long tmp;
 
826
                                condition.strValue.ToULong(&tmp);
 
827
                                condition.value = tmp;
 
828
                        }
 
829
                        else if (condition.type == attributes || condition.type == permissions)
 
830
                        {
 
831
                                if (condition.strValue == _T("0"))
 
832
                                        condition.value = 0;
 
833
                                else
 
834
                                        condition.value = 1;
 
835
                        }
 
836
 
 
837
                        filter.filters.push_back(condition);
 
838
 
 
839
                        pCondition = pCondition->NextSiblingElement("Condition");
 
840
                }
 
841
 
 
842
                if (!filter.filters.empty())
 
843
                        m_globalFilters.push_back(filter);
 
844
 
 
845
                pFilter = pFilter->NextSiblingElement("Filter");
 
846
        }
 
847
        m_filters = m_globalFilters;
 
848
 
 
849
        TiXmlElement* pSets = pDocument->FirstChildElement("Sets");
 
850
        if (!pSets)
 
851
        {
 
852
                delete pDocument->GetDocument();
 
853
                return;
 
854
        }
 
855
 
 
856
        for (TiXmlElement* pSet = pSets->FirstChildElement("Set"); pSet; pSet = pSet->NextSiblingElement("Set"))
 
857
        {
 
858
                CFilterSet set;
 
859
                TiXmlElement* pItem = pSet->FirstChildElement("Item");
 
860
                while (pItem)
 
861
                {
 
862
                        wxString local = GetTextElement(pItem, "Local");
 
863
                        wxString remote = GetTextElement(pItem, "Remote");
 
864
                        set.local.push_back(local == _T("1") ? true : false);
 
865
                        set.remote.push_back(remote == _T("1") ? true : false);
 
866
 
 
867
                        pItem = pItem->NextSiblingElement("Item");
 
868
                }
 
869
 
 
870
                if (!m_globalFilterSets.empty())
 
871
                {
 
872
                        set.name = GetTextElement(pSet, "Name");
 
873
                        if (set.name == _T(""))
 
874
                                continue;
 
875
                }
 
876
 
 
877
                if (set.local.size() == m_filters.size())
 
878
                        m_globalFilterSets.push_back(set);
 
879
        }
 
880
        m_filterSets = m_globalFilterSets;
 
881
 
 
882
        wxString attribute = GetTextAttribute(pSets, "Current");
 
883
        unsigned long value;
 
884
        if (attribute.ToULong(&value))
 
885
        {
 
886
                if (value < m_globalFilterSets.size())
 
887
                        m_globalCurrentFilterSet = value;
 
888
        }
 
889
 
 
890
        m_currentFilterSet = m_globalCurrentFilterSet;
 
891
 
 
892
        delete pDocument->GetDocument();
710
893
}