-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalsearch.cpp
219 lines (180 loc) · 6.1 KB
/
globalsearch.cpp
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
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QProgressDialog>
#include <QPushButton>
#include "filetypes.h"
#include "globalsearch.h"
#include "ui_globalsearch.h"
#include "listviewstrings.h"
#include "../Lib/supportfunctions.h"
// TODO: Control-shift-G returns current file if latest backup is chosen
GlobalSearch::GlobalSearch(QWidget *parent) :
QDialog(parent),
ui(new Ui::Search)
{
getSelectionResponse = "" ;
ui->setupUi(this);
clearResults() ;
ui->listSearchResults->setModel(strings.getModel()) ;
QPushButton* okBtn = ui->buttonBox->button(QDialogButtonBox::Ok);
okBtn->setAutoDefault(true);
okBtn->setDefault(true);
QPushButton* caBtn = ui->buttonBox->button(QDialogButtonBox::Cancel);
caBtn->setAutoDefault(false);
caBtn->setDefault(false);
}
GlobalSearch::~GlobalSearch()
{
delete ui;
}
void GlobalSearch::setSearch(ImportFilter *importfilter, QString& path, bool currentfiles, QString& searchtext)
{
this->path=path ;
this->currentfiles=currentfiles ;
this->searchtext=searchtext ;
this->importfilter = importfilter ;
}
void GlobalSearch::addString(QString resulttext, QString resulthint)
{
strings.addString(resulttext, resulthint) ;
entries++ ;
}
QString& GlobalSearch::getSelection()
{
int index = ui->listSearchResults->currentIndex().row() ;
if (index>=0)
getSelectionResponse = strings.hintAt(index) ;
else
getSelectionResponse = "" ;
return getSelectionResponse ;
}
QString& GlobalSearch::getSelectionFileName()
{
int index = ui->listSearchResults->currentIndex().row() ;
if (index>=0)
getSelectionFilenameResponse = strings.stringAt(index) ;
else
getSelectionFilenameResponse = "" ;
return getSelectionFilenameResponse ;
}
int GlobalSearch::exec()
{
clearResults() ;
QStringList directories ;
parseDirectory(path, directories, "") ;
int isz=directories.size() ;
QIcon icon(":icon-image.png");
QProgressDialog progress("Easy Notepad Global Search", "Abort Search", 0, isz, this);
progress.setAccessibleName("Easy Notepad Global Search Progress") ;
progress.setMinimumDuration(0);
progress.setWindowIcon(icon) ;
progress.setWindowModality(Qt::WindowModal);
progress.show() ;
for (int i=0; i<isz; i++) {
// Update progress bar and allow progress bar to update
progress.setValue(i) ;
qApp->processEvents();
if (progress.wasCanceled())
break ;
QApplication::processEvents() ;
QStringList files ;
QString directory = directories.at(i) ;
QString folderpath = path + "/" + directory ;
if (currentfiles) {
parseDirectory(folderpath, files, TXT, false, true) ;
parseDirectory(folderpath, files, ENC, false, false) ;
for (int j=0, jsz=files.size(); j<jsz; j++) {
QString file = files.at(j) ;
QString filepath = folderpath + "/" + file ;
if (searchInFile(file, filepath, searchtext) ) {
addString(directory.replace(ENF,"") + ": " + file.replace(TXT,"").replace(ENC,""), filepath) ;
}
}
} else {
QStringList backups ;
parseDirectory(folderpath, backups, "") ;
for (int j=0, jsz=backups.size(); j<jsz; j++) {
QString backup = backups.at(j) ;
QString backuppath = folderpath + "/" + backup ;
parseDirectory(backuppath, files, BAK, true, true) ;
parseDirectory(backuppath, files, ENB, true, false) ;
for (int k=0, ksz=files.size(); k<ksz; k++) {
QString file = files.at(k) ;
QString filepath = backuppath + "/" + file ;
if (searchInFile(file, filepath, searchtext)) {
addString(directory.replace(ENF,"") + ": " + backup.replace(BAK,"").replace(ENB,"") + " (" + parseBackupDate(files.at(k)) + ")" , filepath) ;
}
}
}
}
}
progress.setValue(isz) ;
progress.cancel() ;
if (entries>0) {
ui->listSearchResults->setFocus() ;
return QDialog::exec() ;
} else {
return true ;
}
}
// Used by EasyNotepad
QString& GlobalSearch::parseBackupDate(QString backupdate)
{
static QString parsedBackupDate ;
parsedBackupDate = backupdate ;
if (backupdate.length()>=8) {
int d, m, y ;
y = backupdate.left(4).toInt() ;
m = backupdate.mid(4,2).toInt() ;
d = backupdate.mid(6,2).toInt() ;
QDate date ;
date.setDate(y, m, d) ;
if (date.isValid()) parsedBackupDate = date.toString("dd MMM yyyy") ;
}
return parsedBackupDate ;
}
//
// Private Slots
//
void GlobalSearch::on_listSearchResults_clicked(const QModelIndex &index)
{
Q_UNUSED(index) ;
// TODO: store index so that serc form can be queried for the ID
//selectedindex = ui->listSearchResults->currentIndex().row() ;
}
//
// Private Functions
//
void GlobalSearch::clearResults()
{
ui->listSearchResults->reset() ;
strings.clearStrings() ;
selectedindex = -1 ;
entries = 0 ;
}
bool GlobalSearch::searchInFile(QString filename, QString filepath, QString text)
{
text.replace("\n", "") ;
text.replace("\r", "") ;
text.replace("\t","") ;
text.replace(" ","") ;
text = deAccent(text) ;
text.replace(" ", "") ;
text.replace("\t", "") ;
QRegularExpression re( ".*(" + text + ").*" ) ;
QString testfile = deAccent(filename) ;
testfile.replace(" ", "") ;
testfile.replace("\t", "") ;
QRegularExpressionMatch match = re.match(testfile, QRegularExpression::CaseInsensitiveOption) ;
if (match.hasMatch()) return true ;
QString filetext ;
if (!importfilter->LoadFile(filepath, filetext)) return false ;
filetext = deAccent(filetext) ;
filetext.replace("\n", "") ;
filetext.replace("\r", "") ;
filetext.replace("\t", "") ;
filetext.replace(" ", "") ;
match = re.match(filetext, QRegularExpression::CaseInsensitiveOption) ;
return (match.hasMatch()) ;
}