-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicturedatabase.cpp
More file actions
330 lines (302 loc) · 9.2 KB
/
Copy pathpicturedatabase.cpp
File metadata and controls
330 lines (302 loc) · 9.2 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
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
/***************************************************************************************************
*
* FILE: picturedatabase.cpp
*
* CREATED: 02-08-2010
*
* AUTHOR: Benjamin Caspari (becaspari@googlemail.com)
*
* PURPOSE: functions to handle a picture database
*
* This program is licensed under the terms of the GPL Version 2
*
* Copyright 2010 by Benjamin Caspari
*
***************************************************************************************************/
#include "picturedatabase.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QDataStream>
#include <QDebug>
#define FILE_ID 0x4D494442
bool PictureDatabase::toFile(const QString &file)
{
qDebug() << ("PictureDatabase::toFile called");
quint32 identifier = FILE_ID;
quint32 majorVersion = 1;
quint32 minorVersion = 0;
quint32 entryCount = this->m_pictureInfo->size();
QFile outFile(file);
if (!outFile.open(QIODevice::WriteOnly)) {
qDebug() << (" unable to open file");
return false;
}
QDataStream out(&outFile);
out << identifier;
out << majorVersion;
out << minorVersion;
out << this->m_name;
out << entryCount;
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
this->m_pictureInfo->at(i)->toStream(out);
}
outFile.close();
qDebug() << ("PictureDatabase::toFile done with success");
return true;
}
bool PictureDatabase::doubleFiles()
{
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
//QFileInfo iFile(this->m_pictureInfo->at(i)->getFile());
for (int k = 0; k < this->m_pictureInfo->size(); k++) {
if (i != k) {
/*QFileInfo kFile(this->m_pictureInfo->at(k)->getFile());
if (kFile.canonicalFilePath() == iFile.canonicalFilePath()) {
return true;
}*/
if (this->m_pictureInfo->at(i)->getFile()
== this->m_pictureInfo->at(k)->getFile()) {
return true;
}
}
}
}
return false;
}
void PictureDatabase::removeDoubleFiles()
{
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
//QFileInfo iFile(this->m_pictureInfo->at(i)->getFile());
for (int k = 0; k < this->m_pictureInfo->size(); k++) {
if (i != k) {
/*QFileInfo kFile(this->m_pictureInfo->at(k)->getFile());
if (kFile.canonicalFilePath() == iFile.canonicalFilePath()) {
this->removeEntry(k);
k--;
}*/
if (this->m_pictureInfo->at(i)->getFile()
== this->m_pictureInfo->at(k)->getFile()) {
this->removeEntry(k);
k--;
if (i > k) {
i--;
}
}
}
}
}
}
void PictureDatabase::clearPictureInfo()
{
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
delete m_pictureInfo->value(i);
}
m_pictureInfo->clear();
}
void PictureDatabase::removeEntry(int index)
{
if ((index >= 0) && (index < this->m_pictureInfo->size())) {
delete this->m_pictureInfo->value(index);
this->m_pictureInfo->remove(index);
}
}
void PictureDatabase::removeEntry(const QString &file)
{
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
if (m_pictureInfo->value(i)->getFile().compare(file, Qt::CaseInsensitive) == 0) {
removeEntry(i);
return;
}
}
}
bool PictureDatabase::fromFile(const QString &file)
{
this->clearPictureInfo();
QFile inFile(file);
if (!inFile.open(QIODevice::ReadOnly)) {
return false;
}
QDataStream in(&inFile);
quint32 identifier = 0;
quint32 majorVersion;
quint32 minorVersion;
QString name;
quint32 entryCount;
in >> identifier;
if (identifier != FILE_ID) {
inFile.close();
return false;
}
in >> majorVersion;
in >> minorVersion;
in >> name;
in >> entryCount;
for (unsigned int i = 0; i < entryCount; i++) {
PictureInfo *newEntry = new PictureInfo;
newEntry->fromStream(in);
this->m_pictureInfo->append(newEntry);
}
inFile.close();
return true;
}
void PictureDatabase::cancelIndexing()
{
while (this->m_indexThread->isRunning()) {
this->m_indexThread->cancel();
}
}
void PictureDatabase::setName(const QString &name)
{
this->m_name = name;
}
QString PictureDatabase::name()
{
return this->m_name;
}
void PictureDatabase::processFiles()
{
this->m_processingWasCanceled = false;
this->m_processRunning = true;
this->m_processThread->processImages(this->m_pictureInfo);
disconnect(this->m_processThread,
SIGNAL(finished()),
this,
SLOT(processThreadFinished()));
connect(this->m_processThread,
SIGNAL(finished()),
this,
SLOT(processThreadFinished()));
disconnect(this->m_processThread,
SIGNAL(complete(float)),
this,
SLOT(processProgressFromThread(float)));
connect(this->m_processThread,
SIGNAL(complete(float)),
this,
SLOT(processProgressFromThread(float)));
}
void PictureDatabase::indexFiles(QString directory,
bool includeSubdirectories)
{
connect(this->m_indexThread,
SIGNAL(finished()),
this,
SLOT(indexThreadFinished()));
this->m_indexRunning = true;
this->m_indexThread->indexDirectory(this->m_pictureInfo,
directory,
includeSubdirectories);
this->removeDoubleFiles();
}
void PictureDatabase::indexFile(const QString &filename)
{
PictureInfo *newEntry = new PictureInfo;
newEntry->setFile(QDir::toNativeSeparators(QDir::cleanPath(filename)));
newEntry->setProcessed(false);
newEntry->setValidFile(true);
this->m_pictureInfo->append(newEntry);
this->removeDoubleFiles();
}
void PictureDatabase::processThreadFinished()
{
this->m_processRunning = false;
emit this->processFinished(this->m_processingWasCanceled);
}
void PictureDatabase::indexThreadFinished()
{
this->m_indexRunning = false;
emit this->indexFinished();
}
bool PictureDatabase::isProcessingRunning()
{
return this->m_processRunning;
}
void PictureDatabase::cancelProcessing()
{
while (this->m_processThread->isRunning()) {
this->m_processThread->cancel();
this->m_processingWasCanceled = true;
}
}
bool PictureDatabase::isIndexingRunning()
{
return this->m_indexRunning;
}
PictureDatabase::PictureDatabase()
{
this->m_pictureInfo = new QVector<PictureInfo*>;
this->m_processThread = new ProcessImagesThread;
this->m_indexThread = new IndexFilesThread;
this->m_processRunning = false;
this->m_indexRunning = false;
this->m_processingWasCanceled = false;
}
PictureDatabase::~PictureDatabase()
{
if (this->m_pictureInfo) {
this->clearPictureInfo();
delete this->m_pictureInfo;
}
if (this->m_processThread) {
delete this->m_processThread;
}
if (this->m_indexThread) {
delete this->m_indexThread;
}
}
void PictureDatabase::processProgressFromThread(float percent)
{
qDebug() << ("PictureDatabase::processProgressFromThread called, "
+ QString::number(percent));
emit this->processProgress(percent);
}
int PictureDatabase::size()
{
return this->m_pictureInfo->size();
}
PictureInfo *PictureDatabase::pictureAt(int index)
{
if ((index >= 0) && (index < this->m_pictureInfo->size())) {
return this->m_pictureInfo->value(index);
}
return 0;
}
bool PictureDatabase::allUpToDate()
{
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
if (this->m_pictureInfo->at(i)->validFile()) {
if (this->m_pictureInfo->at(i)->processed()) {
QFileInfo fileInfo(this->m_pictureInfo->at(i)->getFile());
if (fileInfo.lastModified() != this->m_pictureInfo->at(i)->lastChanged()) {
return false;
}
} else {
return false;
}
}
}
return true;
}
int PictureDatabase::filesNotUpToDate()
{
int result = 0;
for (int i = 0; i < this->m_pictureInfo->size(); i++) {
if (this->m_pictureInfo->at(i)->validFile()) {
if (this->m_pictureInfo->at(i)->processed()) {
QFileInfo fileInfo(this->m_pictureInfo->at(i)->getFile());
if (fileInfo.lastModified() != this->m_pictureInfo->at(i)->lastChanged()) {
//file changed since last analysis
result++;
}
} else {
//file not processed yet
result++;
}
} else {
//not a valid file
result++;
}
}
return result;
}