/
findutils.cpp
153 lines (118 loc) · 3.7 KB
/
findutils.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
#include "findutils.h"
#include "defines.h"
#include <QDir>
static const char * LOCATE_BIN = "locate";
static const char * UPDATEDB_BIN = "updatedb";
static QString APP_HOME = QDir::homePath() + "/." + APP_NAME;
static QString DB_PATH = APP_HOME + "/mlocate.db";
static const char * IGNORED_FOLDERS = "\".git .bzr .hg .svn\"";
FindUtils::FindUtils()
{
// Connect the process signals
locate_process = new QProcess(this);
connect(locate_process, SIGNAL(finished(int)),
this, SLOT(locate_process_finished()));
updatedb_process = new QProcess(this);
connect(updatedb_process, SIGNAL(finished(int)),
this, SLOT(updatedb_process_finished()));
statistics_process = new QProcess(this);
connect(statistics_process, SIGNAL(finished(int)),
this, SLOT(statistics_process_finished()));
create_db_path();
}
void FindUtils::updatedb()
{
QStringList arguments;
arguments << "--require-visibility" << "0"
<< "--add-prunenames" << IGNORED_FOLDERS
<< "--output" << DB_PATH;
updatedb_process->start(UPDATEDB_BIN, arguments);
}
void FindUtils::create_db_path()
{
QDir helper;
helper.mkpath(APP_HOME);
}
void FindUtils::locate(const QString & q)
{
QString query = q.trimmed();
if(query.isEmpty())
return;
QStringList filters = query.split(" ");
QString key = filters.at(0);
filters.removeAt(0);
search_filters = filters;
/*
if(search_key.compare(key) == 0) {
// the search key hasn't changed, there is no need
// to call locate again, we only need to filter the results
puts("trying to search for the same key again");
locate_process_finished();
return;
}
*/
search_results.clear();
search_key = key;
QStringList arguments;
arguments << "-d" << DB_PATH << key;
locate_process->start(LOCATE_BIN, arguments);
locate_process->waitForFinished();
}
void FindUtils::statistics()
{
QStringList arguments;
arguments << "-S" << "-d" << DB_PATH;
statistics_process->start(LOCATE_BIN, arguments);
}
void FindUtils::updatedb_process_finished()
{
emit updatedb_finished();
}
void FindUtils::locate_process_finished()
{
QByteArray output = locate_process->readAllStandardOutput().trimmed();
QList<QByteArray> matches = output.split('\n');
filter_results(matches);
emit locate_finished(search_results);
}
void FindUtils::filter_results(QList<QByteArray> & matches)
{
for(int r = 0; r < matches.size(); r++) {
// for each result returned by locate
QString line = matches.at(r);
if(line.size() == 0)
continue;
bool matches = true;
for(int f = 0; f < search_filters.size(); f++) {
// check that contains all the filters
QString filter = search_filters.at(f);
if(!line.contains(filter, Qt::CaseInsensitive)) {
matches = false;
break;
}
}
// Ignore the ones that don't have all the filter words
if(matches) {
search_results << line;
}
}
}
void FindUtils::statistics_process_finished()
{
QByteArray output = statistics_process->readAllStandardOutput().trimmed();
QList<QByteArray> rows = output.split('\n');
/* output of 'locate -S -d {DB}' is
Database {DB}:
M directories
N files
XXX bytes in file names
YYY bytes used to store database
*/
if(rows.size() > 3) {
QString directories_line = rows.at(1).data();
QString files_line = rows.at(2).data();
emit statistics_finished(
QString("Searching %1 - %2").arg(directories_line).arg(files_line)
);
}
}