picturemodel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /****************************************************************************
  2. ** Artriculate: Art comes tumbling down
  3. ** Copyright (C) 2016 Chaos Reins
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ****************************************************************************/
  18. #include "picturemodel.h"
  19. #include <QDir>
  20. #include <QDebug>
  21. #include <QCoreApplication>
  22. #include <QSettings>
  23. #include <QThread>
  24. #include <QImageReader>
  25. #include <QMimeDatabase>
  26. #include <QElapsedTimer>
  27. #include <QStandardPaths>
  28. #include <QtSql/QSqlDatabase>
  29. #include <QtSql/QSqlError>
  30. #include <QtSql/QSqlQuery>
  31. #include <QtSql/QSqlDriver>
  32. namespace {
  33. QSqlDatabase openDBConnection(const QString &connectionName) {
  34. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
  35. QFileInfo dbFile(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first() + "/" + qApp->applicationName() + ".db");
  36. QDir().mkpath(dbFile.absolutePath());
  37. db.setDatabaseName(dbFile.absoluteFilePath());
  38. if (!db.open()) {
  39. qDebug() << "Failed to open the database:" << dbFile.absoluteFilePath();
  40. qDebug() << "Error:" << db.lastError().text();
  41. qApp->exit(-1);
  42. }
  43. return db;
  44. }
  45. QString stripDbHostileCharacters(QString path) {
  46. return path.replace(QString("/"), QString(""));
  47. }
  48. inline int offsetHash(int hash) { return hash + 1; }
  49. }
  50. struct ArtPiece {
  51. ArtPiece() : refCount(0) { /**/ }
  52. QString path;
  53. QSize size;
  54. int refCount;
  55. };
  56. struct FSNode {
  57. FSNode(const QString& rname, const FSNode *pparent = nullptr);
  58. QString qualify() const;
  59. const QString name;
  60. const FSNode *parent;
  61. };
  62. struct FSLeafNode : public FSNode {
  63. using FSNode::FSNode;
  64. QSize size;
  65. };
  66. FSNode::FSNode(const QString& rname, const FSNode *pparent)
  67. : name(rname),
  68. parent(pparent)
  69. {
  70. }
  71. QString FSNode::qualify() const {
  72. QString qualifiedPath;
  73. const FSNode *node = this;
  74. while(node->parent != nullptr) {
  75. qualifiedPath = "/" + node->name + qualifiedPath;
  76. node = node->parent;
  77. }
  78. qualifiedPath = node->name + qualifiedPath;
  79. return qualifiedPath;
  80. }
  81. class FSNodeTree : public QObject
  82. {
  83. Q_OBJECT
  84. public:
  85. FSNodeTree(const QString& path);
  86. virtual ~FSNodeTree();
  87. void addModelNode(const FSNode* parentNode);
  88. int fileCount() const { return files.length(); }
  89. QVector<FSLeafNode*> files;
  90. public slots:
  91. void populate(bool useDatabaseBackend);
  92. signals:
  93. void countChanged();
  94. private:
  95. void dumpTreeToDb();
  96. QStringList extensions;
  97. QString rootDir;
  98. };
  99. FSNodeTree::FSNodeTree(const QString& path)
  100. : QObject(nullptr),
  101. rootDir(path)
  102. {
  103. QMimeDatabase mimeDatabase;
  104. foreach(const QByteArray &m, QImageReader::supportedMimeTypes()) {
  105. foreach(const QString &suffix, mimeDatabase.mimeTypeForName(m).suffixes())
  106. extensions.append(suffix);
  107. }
  108. if (extensions.isEmpty()) {
  109. qFatal("Your Qt install has no image format support");
  110. }
  111. }
  112. FSNodeTree::~FSNodeTree()
  113. {
  114. QSet<const FSNode*> nodes;
  115. foreach(const FSNode *node, files) {
  116. while(node) {
  117. nodes << node;
  118. node = node->parent;
  119. }
  120. }
  121. qDeleteAll(nodes.values());
  122. }
  123. void FSNodeTree::addModelNode(const FSNode* parentNode)
  124. {
  125. // TODO: Check for symlink recursion
  126. QDir parentDir(parentNode->qualify());
  127. foreach(const QString &currentDir, parentDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
  128. const FSNode *dir = new FSNode(currentDir, parentNode);
  129. addModelNode(dir);
  130. }
  131. foreach(const QString &currentFile, parentDir.entryList(QDir::Files)) {
  132. QString extension = currentFile.mid(currentFile.length() - 3);
  133. if (!extensions.contains(extension))
  134. continue;
  135. FSLeafNode *file = new FSLeafNode(currentFile, parentNode);
  136. const QString fullPath = file->qualify();
  137. QSize size = QImageReader(fullPath).size();
  138. bool rational = false;
  139. if (size.isValid()) {
  140. file->size = size;
  141. qreal ratio = qreal(size.width())/size.height();
  142. if ((ratio < 0.01) || (ratio > 100)) {
  143. qDebug() << "Image" << fullPath << "has excessive ratio" << ratio << "excluded";
  144. } else {
  145. rational = true;
  146. }
  147. } else {
  148. qDebug() << "Discarding" << fullPath << "due to invalid size";
  149. }
  150. if (rational) {
  151. files << file;
  152. emit countChanged();
  153. } else {
  154. delete file;
  155. }
  156. }
  157. }
  158. void FSNodeTree::populate(bool useDatabaseBackend)
  159. {
  160. QElapsedTimer timer;
  161. timer.start();
  162. QDir currentDir(rootDir);
  163. if (!currentDir.exists()) {
  164. qDebug() << "Being told to watch a non existent directory:" << rootDir;
  165. }
  166. addModelNode(new FSNode(rootDir));
  167. qDebug() << "Completed building file tree containing:" << files.length() << "images after:" << timer.elapsed() << "ms";
  168. if (useDatabaseBackend) {
  169. qDebug() << "No database found; dumping tree to db" << rootDir;
  170. timer.restart();
  171. dumpTreeToDb();
  172. qDebug() << "Completed database dump after:" << timer.elapsed() << "ms";
  173. }
  174. }
  175. void FSNodeTree::dumpTreeToDb()
  176. {
  177. QSqlDatabase db = openDBConnection("write");
  178. QSqlQuery q("", db);
  179. if (!q.exec(QString("create table %1 (path varchar, width integer, height integer)").arg(::stripDbHostileCharacters(rootDir)))) {
  180. qDebug() << "Failed to init DB with:" << q.lastError().text();
  181. return;
  182. }
  183. qDebug() << "Database supports transactions" << QSqlDatabase::database().driver()->hasFeature(QSqlDriver::Transactions);
  184. // Turns out SQLITE has a 999 variable limit by default
  185. // Arch shieleded me from this
  186. int varLimitPerWave = 999;
  187. int varCountPerItem = 3;
  188. int itemCountPerWave = varLimitPerWave/varCountPerItem;
  189. int waveCount = files.length()/itemCountPerWave;
  190. const int waveTail = files.length()%itemCountPerWave;
  191. if (waveTail > 0) {
  192. waveCount += 1;
  193. }
  194. qDebug() << "About to drop" << files.length() << "files to DB";
  195. qDebug() << "This will require" << waveCount << "separate DB transactions";
  196. for (int wave = 0; wave < waveCount; wave++)
  197. {
  198. int itemCount = itemCountPerWave;
  199. if ((waveTail > 0) && (wave == waveCount - 1)) {
  200. itemCount = waveTail;
  201. }
  202. QString insertQuery = QString("INSERT INTO %1 (path, width, height) VALUES ").arg(::stripDbHostileCharacters(rootDir));
  203. QString insertQueryValues("(?, ?, ?),");
  204. insertQuery.reserve(insertQuery.size() + insertQueryValues.size()*itemCount);
  205. for(int i = 0; i < itemCount; i++) {
  206. insertQuery.append(insertQueryValues);
  207. }
  208. insertQuery = insertQuery.replace(insertQuery.length()-1, 1, ";");
  209. db.transaction();
  210. QSqlQuery query("", db);
  211. if (!query.prepare(insertQuery)) {
  212. qDebug() << "Query preperation failed with" << query.lastError().text();
  213. return;
  214. }
  215. for(int i = wave*itemCountPerWave; i < (wave*itemCountPerWave + itemCount); i++) {
  216. const FSLeafNode *node = files.at(i);
  217. query.addBindValue(node->qualify());
  218. query.addBindValue(node->size.width());
  219. query.addBindValue(node->size.height());
  220. }
  221. query.exec();
  222. if (db.commit()) {
  223. qDebug() << "SQL transaction succeeded";
  224. } else {
  225. qDebug() << "SQL transaction failed";
  226. }
  227. QSqlError err = query.lastError();
  228. if (err.type() != QSqlError::NoError) {
  229. qDebug() << "Database dump of content tree failed with" << err.text();
  230. } else {
  231. qDebug() << "Successfully finished adding wave" << wave << "to DB" << rootDir;
  232. }
  233. }
  234. }
  235. class PictureModel::PictureModelPrivate {
  236. public:
  237. PictureModelPrivate(PictureModel* p);
  238. ~PictureModelPrivate();
  239. FSNodeTree *fsTree;
  240. bool useDatabaseBackend;
  241. bool assumeLinearAccess = false;
  242. void cacheIndex(int index);
  243. void retireCachedIndex(int index);
  244. int itemCount();
  245. QHash<int, ArtPiece*> artwork;
  246. private:
  247. PictureModel *parent;
  248. int collectionSize;
  249. QString artPath;
  250. void createFSTree(const QString &path);
  251. QThread scanningThread;
  252. };
  253. PictureModel::PictureModelPrivate::PictureModelPrivate(PictureModel* p)
  254. : fsTree(nullptr),
  255. parent(p)
  256. {
  257. QSettings settings;
  258. useDatabaseBackend = settings.value("useDatabaseBackend", true).toBool();
  259. settings.setValue("useDatabaseBackend", useDatabaseBackend);
  260. artPath = settings.value("artPath", QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).first()).toString();
  261. settings.setValue("artPath", artPath);
  262. if (useDatabaseBackend) {
  263. QSqlDatabase db = openDBConnection("read");
  264. QStringList tables = db.tables();
  265. if (tables.contains(::stripDbHostileCharacters(artPath), Qt::CaseInsensitive)) {
  266. QString queryString = "SELECT COUNT(*) FROM " % ::stripDbHostileCharacters(artPath) % ";";
  267. QSqlQuery query(queryString, db);
  268. query.next();
  269. collectionSize = query.value(0).toInt();
  270. QMetaObject::invokeMethod(parent, "countChanged");
  271. qDebug() << "Using existing database entry for" << artPath;
  272. } else {
  273. qDebug() << "No database found; creating file tree" << artPath;
  274. createFSTree(artPath);
  275. }
  276. } else {
  277. createFSTree(artPath);
  278. }
  279. };
  280. void PictureModel::PictureModelPrivate::createFSTree(const QString &path)
  281. {
  282. fsTree = new FSNodeTree(path);
  283. connect(fsTree, &FSNodeTree::countChanged, parent, &PictureModel::countChanged);
  284. fsTree->moveToThread(&scanningThread);
  285. scanningThread.start();
  286. QMetaObject::invokeMethod(fsTree, "populate", Qt::QueuedConnection, Q_ARG(bool, useDatabaseBackend));
  287. }
  288. PictureModel::PictureModelPrivate::~PictureModelPrivate()
  289. {
  290. if (fsTree) {
  291. scanningThread.quit();
  292. scanningThread.wait(5000);
  293. delete fsTree;
  294. fsTree = nullptr;
  295. }
  296. }
  297. int PictureModel::PictureModelPrivate::itemCount() {
  298. return fsTree ? fsTree->fileCount() : collectionSize;
  299. };
  300. void PictureModel::PictureModelPrivate::cacheIndex(int index)
  301. {
  302. int hashIndex = ::offsetHash(index);
  303. if (artwork.contains(hashIndex)) {
  304. artwork[hashIndex]->refCount++;
  305. return;
  306. }
  307. QString queryString = "SELECT path, width, height FROM " % ::stripDbHostileCharacters(artPath) % " LIMIT 1 OFFSET " % QString::number(index) % ";";
  308. QSqlDatabase db = QSqlDatabase::database("read", true);
  309. QSqlQuery query(queryString, db);
  310. query.next();
  311. ArtPiece *art = new ArtPiece;
  312. art->path = query.value(0).toString();
  313. art->size = QSize(query.value(1).toInt(), query.value(2).toInt());
  314. art->refCount++;
  315. artwork[hashIndex] = art;
  316. }
  317. void PictureModel::PictureModelPrivate::retireCachedIndex(int index)
  318. {
  319. int hashIndex = ::offsetHash(index);
  320. artwork[hashIndex]->refCount--;
  321. if (assumeLinearAccess || artwork[hashIndex]->refCount < 1) {
  322. delete artwork[hashIndex];
  323. artwork.remove(hashIndex);
  324. }
  325. }
  326. PictureModel::PictureModel(QObject *parent)
  327. : QAbstractListModel(parent),
  328. d(new PictureModelPrivate(this)) { /**/ }
  329. PictureModel::~PictureModel()
  330. {
  331. delete d;
  332. d = nullptr;
  333. }
  334. int PictureModel::rowCount(const QModelIndex &parent) const
  335. {
  336. Q_UNUSED(parent)
  337. return d->itemCount();
  338. }
  339. QVariant PictureModel::data(const QModelIndex &index, int role) const
  340. {
  341. if (d->assumeLinearAccess) {
  342. requestIndex(index.row());
  343. }
  344. // What the fuck; Qt queries item 0 before we substantiate it
  345. // I get to offset my hash by 1 or loss a piece of art
  346. if (index.row() <= 0 || index.row() >= d->itemCount()) {
  347. switch (role) {
  348. case SizeRole:
  349. return QSize(1222,900);
  350. case NameRole:
  351. return "Qt logo";
  352. case PathRole:
  353. default:
  354. return QString("qrc:///qt_logo_green_rgb.png");
  355. }
  356. }
  357. if (d->fsTree) {
  358. switch (role) {
  359. case SizeRole:
  360. return d->fsTree->files.at(index.row())->size;
  361. case NameRole:
  362. return d->fsTree->files.at(index.row())->name;
  363. case PathRole:
  364. default:
  365. return QUrl::fromLocalFile(d->fsTree->files.at(index.row())->qualify());
  366. }
  367. } else {
  368. int hashIndex = ::offsetHash(index.row());
  369. switch (role) {
  370. case SizeRole: {
  371. return d->artwork[hashIndex]->size;
  372. }
  373. case NameRole:
  374. return d->artwork[hashIndex]->path;
  375. case PathRole:
  376. default:
  377. return QUrl::fromLocalFile(d->artwork[hashIndex]->path);
  378. }
  379. }
  380. return QVariant();
  381. }
  382. int PictureModel::requestIndex(int index) const
  383. {
  384. if (index == -1) {
  385. index = d->itemCount() == 0 ? 0 : qrand() % d->itemCount();
  386. }
  387. if (!d->fsTree) {
  388. d->cacheIndex(index);
  389. }
  390. return index;
  391. }
  392. void PictureModel::retireIndex(int index) const
  393. {
  394. if (!d->fsTree) {
  395. d->retireCachedIndex(index);
  396. }
  397. }
  398. void PictureModel::assumeLinearAccess()
  399. {
  400. d->assumeLinearAccess = true;
  401. }
  402. QHash<int, QByteArray> PictureModel::roleNames() const
  403. {
  404. QHash<int, QByteArray> roles;
  405. roles[NameRole] = "name";
  406. roles[PathRole] = "path";
  407. roles[SizeRole] = "size";
  408. return roles;
  409. }
  410. #include "moc/picturemodel.moc"