[go: up one dir, main page]

Menu

[14870f]: / rules.cpp  Maximize  Restore  History

Download this file

251 lines (231 with data), 8.5 kB

  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
/////////////////////////////////////////////////////////////////////////////////
// Author: Steven Lamerton
// Copyright: Copyright (C) 2007-2009 Steven Lamerton
// License: GNU GPL 2 (See readme for more info)
/////////////////////////////////////////////////////////////////////////////////
#include <wx/regex.h>
#include <wx/filename.h>
#include <wx/fileconf.h>
#include <wx/listctrl.h>
#include <wx/combobox.h>
#include <wx/msgdlg.h>
#include "rules.h"
#include "toucan.h"
#include "path.h"
#include "basicfunctions.h"
#include "forms/frmmain.h"
Rules::Rules(const wxString &name, bool loadfromfile) : m_Name(name){
if(loadfromfile){
TransferFromFile();
}
m_Normalised = false;
}
bool Rules::IsEmpty(){
if(GetExcludedFiles().Count() == 0 && GetExcludedFolders().Count() == 0 && GetIncludedLocations().Count() == 0){
return true;
}
return false;
}
bool Rules::ShouldExclude(wxString strName, bool blIsDir){
//If there are no rules then return false
if(IsEmpty()){
return false;
}
//If this is the first time we have run then we need to expand the rules if they have variables in them
if(!m_Normalised){
for(unsigned int i = 0; i < m_ExcludedFiles.GetCount(); i++){
m_ExcludedFiles.Item(i) = Path::Normalise(m_ExcludedFiles.Item(i));
}
for(unsigned int i = 0; i < m_ExcludedFolders.GetCount(); i++){
m_ExcludedFolders.Item(i) = Path::Normalise(m_ExcludedFolders.Item(i));
}
for(unsigned int i = 0; i < m_IncludedLocations.GetCount(); i++){
m_IncludedLocations.Item(i) = Path::Normalise(m_IncludedLocations.Item(i));
}
m_Normalised = true;
}
//If there are any matches for inclusions then immediately retun as no other options need to be checked
for(unsigned int r = 0; r < GetIncludedLocations().Count(); r++){
wxString strInclusion = GetIncludedLocations().Item(r);
//If it is a regex then regex match it
if(strInclusion.Left(1) == wxT("*")){
wxRegEx regMatch;
regMatch.Compile(strInclusion.Right(strInclusion.Length() -1), wxRE_ICASE| wxRE_EXTENDED);
if(regMatch.IsValid()){
if(regMatch.Matches(strName)){
return false;
}
}
}
//Otherwise plain text match
else{
if(strName.Lower().Find(strInclusion.Lower()) != wxNOT_FOUND){
return false;
}
}
}
//Always check the directory exclusions as a file that is in an excluded directory should be excluded
for(unsigned int j = 0; j < GetExcludedFolders().Count(); j++){
wxString strFolderExclusion = GetExcludedFolders().Item(j);
//If it is a regex then regex match it
if(strFolderExclusion.Left(1) == wxT("*")){
wxRegEx regMatch;
regMatch.Compile(strFolderExclusion.Right(strFolderExclusion.Length() - 1), wxRE_ICASE| wxRE_EXTENDED);
if(regMatch.IsValid()){
if(regMatch.Matches(strName)){
return true;
}
}
}
//Otherwise plain text match
else{
if(strName.Lower().Find(strFolderExclusion.Lower()) != wxNOT_FOUND){
return true;
}
}
}
//It is a file so run the extra checks as well
if(!blIsDir){
for(unsigned int j = 0; j < GetExcludedFiles().Count(); j++){
wxString strExclusion = GetExcludedFiles().Item(j);
//Check to see if it is a filesize based exclusion
if(strExclusion.Left(1) == wxT("<") || strExclusion.Left(1) == wxT(">")){
if(strExclusion.Right(2) == wxT("kB") || strExclusion.Right(2) == wxT("MB") || strExclusion.Right(2) == wxT("GB")){
//We can now be sure that this is a size exclusion
wxFileName flName(strName);
wxString strFileSize = flName.GetHumanReadableSize();
strFileSize.Replace(wxT(" "), wxT(""));
double dFileSize = GetInPB(strFileSize);
wxString strSize = strExclusion.Right(strExclusion.Length() - 1);
double dExclusionSize = GetInPB(strSize);
if(strExclusion.Left(1) == wxT("<")){
if(dFileSize < dExclusionSize){
return true;
}
}
if(strExclusion.Left(1) == wxT(">")){
if(dFileSize > dExclusionSize){
return true;
}
}
}
}
//Check to see if it is a date, but NOT a size
if(strExclusion.Left(1) == wxT("<") && strExclusion.Right(1) != wxT("B")){
wxFileName flName(strName);
wxDateTime date;
date.ParseDate(strExclusion.Right(strExclusion.Length() - 1));
if(flName.GetModificationTime().IsEarlierThan(date)){
return true;
}
}
//Other date direction
else if(strExclusion.Left(1) == wxT(">") && strExclusion.Right(1) != wxT("B")){
wxFileName flName(strName);
wxDateTime date;
date.ParseDate(strExclusion.Right(strExclusion.Length() - 1));
if(flName.GetModificationTime().IsLaterThan(date)){
return true;
}
}
//Check to see if it is a regex
else if(strExclusion.Left(1) == wxT("*")){
//Check to see if there is a match in the filename, including any regex bits
wxRegEx regMatch;
regMatch.Compile(strExclusion.Right(strExclusion.Length() - 1), wxRE_ICASE| wxRE_EXTENDED);
if(regMatch.IsValid()){
if(regMatch.Matches(strName)){
return true;
}
}
}
//Else plain text match it
else{
if(strName.Lower().Find(strExclusion.Lower()) != wxNOT_FOUND){
return true;
}
}
}
}
return false;
}
bool Rules::TransferFromFile(){
bool error = false;
wxString stemp;
if(!wxGetApp().m_Rules_Config->Exists(GetName())){
return false;
}
if(wxGetApp().m_Rules_Config->Read(GetName() + wxT("/FilesToInclude"), &stemp)) SetIncludedLocations(StringToArrayString(stemp, wxT("|")));
else error = true;
if(wxGetApp().m_Rules_Config->Read(GetName() + wxT("/FilesToExclude"), &stemp)) SetExcludedFiles(StringToArrayString(stemp, wxT("|")));
else error = true;
if(wxGetApp().m_Rules_Config->Read(GetName() + wxT("/FoldersToExclude"), &stemp)) SetExcludedFolders(StringToArrayString(stemp, wxT("|")));
else error = true;
if(error){
wxMessageBox(_("There was an error reading from the rules file"), _("Error"), wxICON_ERROR);
return false;
}
return true;
}
bool Rules::TransferToFile(){
bool error = false;
wxGetApp().m_Rules_Config->DeleteGroup(GetName());
if(!wxGetApp().m_Rules_Config->Write(GetName() + wxT("/FilesToInclude"), ArrayStringToString(GetIncludedLocations(), wxT("|")))) error = true;
if(!wxGetApp().m_Rules_Config->Write(GetName() + wxT("/FilesToExclude"), ArrayStringToString(GetExcludedFiles(), wxT("|")))) error = true;
if(!wxGetApp().m_Rules_Config->Write(GetName() + wxT("/FoldersToExclude"), ArrayStringToString(GetExcludedFolders(), wxT("|")))) error = true;
wxGetApp().m_Rules_Config->Flush();
if(error){
wxMessageBox(_("There was an error saving to the rules file, \nplease check it is not set as read only or in use"), _("Error"), wxICON_ERROR);
return false;
}
return true;
}
bool Rules::TransferFromForm(frmMain *window){
if(!window){
return false;
}
Clear();
for(int i = 0; i < window->m_RulesList->GetItemCount(); i++){
wxListItem itemcol1, itemcol2;
itemcol1.m_itemId = i;
itemcol1.m_col = 0;
itemcol1.m_mask = wxLIST_MASK_TEXT;
window->m_RulesList->GetItem(itemcol1);
itemcol2.m_itemId = i;
itemcol2.m_col = 1;
itemcol2.m_mask = wxLIST_MASK_TEXT;
window->m_RulesList->GetItem(itemcol2);
if(itemcol2.m_text == _("File to Exclude")){
m_ExcludedFiles.Add(itemcol1.m_text);
}
else if(itemcol2.m_text == _("Folder to Exclude")){
m_ExcludedFolders.Add(itemcol1.m_text);
}
else if(itemcol2.m_text == _("Location to Include")){
m_IncludedLocations.Add(itemcol1.m_text);
}
}
return true;
}
bool Rules::TransferToForm(frmMain *window){
if(!window){
return false;
}
window->m_Rules_Name->SetStringSelection(GetName());
for(unsigned int i = 0; i < m_ExcludedFiles.Count(); i++){
int pos = window->m_RulesList->InsertItem(window->m_RulesList->GetItemCount(), wxT("Test"));
window->m_RulesList->SetItem(pos, 0, m_ExcludedFiles.Item(i));
window->m_RulesList->SetItem(pos, 1, _("File to Exclude"));
}
for(unsigned int i = 0; i < m_ExcludedFolders.Count(); i++){
int pos = window->m_RulesList->InsertItem(window->m_RulesList->GetItemCount(), wxT("Test"));
window->m_RulesList->SetItem(pos, 0, m_ExcludedFolders.Item(i));
window->m_RulesList->SetItem(pos, 1, _("Folder to Exclude"));
}
for(unsigned int i = 0; i < m_IncludedLocations.GetCount(); i++){
int pos = window->m_RulesList->InsertItem(window->m_RulesList->GetItemCount(), wxT("Test"));
window->m_RulesList->SetItem(pos, 0, m_IncludedLocations.Item(i));
window->m_RulesList->SetItem(pos, 1, _("Locations to Include"));
}
return true;
}