-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.cpp
82 lines (73 loc) · 2.25 KB
/
scanner.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
#include "scanner.h"
#include "dbmanager.h"
#include <QDebug>
#include <QDirIterator>
#include <QFileInfo>
#include <QImage>
#include <QtWidgets>
Scanner::Scanner(QObject *parent, QString db_path) : QThread(parent) {
this->f_running = false;
this->parent = parent;
this->db_path = db_path;
catalog_id = -1;
with_thumbs = true;
}
void Scanner::setCatalogName(QString cname) { this->catalog_name = cname; }
void Scanner::setPath(QString path) { this->scan_path = path; }
bool Scanner::running() { return this->f_running; }
void Scanner::setCatalogId(int id) { catalog_id = id; }
void Scanner::withThumbs(bool state) { with_thumbs = state; }
void Scanner::run() {
QDir dir(this->scan_path);
if (!dir.exists()) {
QMessageBox box;
box.setText(tr("Directory does not exist"));
box.setStandardButtons(QMessageBox::Ok);
box.setIcon(QMessageBox::Warning);
box.setWindowTitle("Warning");
box.exec();
this->stop();
return;
}
qDebug() << "Into process directory";
qDebug() << this->scan_path;
this->processDirectory(this->scan_path);
this->stop();
}
void Scanner::processDirectory(QString path) {
DBManager *db = new DBManager(db_path);
if (catalog_id == -1) {
catalog_id = db->createCatalog(catalog_name, path, "");
}
QDir dir(path);
QDirIterator it(dir, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filename = it.next();
QFileInfo info(filename);
QString suffix = info.completeSuffix().toUpper();
QByteArray x;
if (with_thumbs && (suffix == "JPG" || suffix == "PNG" || suffix == "JPEG")) {
QBuffer buffer(&x);
QImage img(info.absoluteFilePath());
QImage scaled = img.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
buffer.open(QIODevice::WriteOnly);
scaled.save(&buffer, "JPG");
if (buffer.isOpen()) {
buffer.close();
}
}
int parent = db->findParent(catalog_id, info.absolutePath());
if (info.isDir()) {
emit setProgressFilename(filename);
}
if (db->dirEntryExists(info.absoluteFilePath())) {
continue;
}
db->createDirEntry(info.completeBaseName(), info.absolutePath(), info.absoluteFilePath(), info.size(), x, info.isDir(),
parent, catalog_id);
}
delete db;
catalog_id = -1;
emit setProgressFilename("finished");
}
void Scanner::stop() { this->f_running = false; }