Retrieve metadata when adding book to library. Show progress while scanning folders...
[dorian] / model / bookfinder.cpp
1 #include <QStringList>
2 #include <QDir>
3 #include <QFileInfoList>
4
5 #include "bookfinder.h"
6 #include "trace.h"
7
8 BookFinder::BookFinder(QObject *parent): QObject(parent)
9 {
10 }
11
12 void BookFinder::find(const QStringList &directories, const QStringList &books)
13 {
14     Trace t("BookFinder::find");
15     QStringList booksFound;
16     int added = 0;
17     int removed = 0;
18
19     foreach (QString path, directories) {
20         QStringList filters(QString("*.epub"));
21         QFileInfoList entries =
22                 QDir(path).entryInfoList(filters, QDir::Files | QDir::Readable);
23         foreach (QFileInfo entry, entries) {
24             booksFound.append(entry.absoluteFilePath());
25         }
26     }
27
28     int toAdd = 0;
29     foreach (QString found, booksFound) {
30         if (!books.contains(found)) {
31             toAdd++;
32         }
33     }
34     emit beginAdd(toAdd);
35
36     foreach (QString found, booksFound) {
37         if (!books.contains(found)) {
38             t.trace(QString("New book ") + found);
39             emit add(found);
40             added++;
41         }
42     }
43
44     foreach (QString book, books) {
45         QFileInfo bookInfo = QFileInfo(book);
46         QString bookDir = bookInfo.absolutePath();
47         QString bookPath = bookInfo.absoluteFilePath();
48         foreach (QString dirName, directories) {
49             t.trace(bookDir + " vs. " + QDir(dirName).absolutePath());
50             if (bookDir == QDir(dirName).absolutePath()) {
51                 if (!booksFound.contains(bookPath)) {
52                     t.trace(QString("Deleted book ") + bookPath);
53                     removed++;
54                     emit remove(bookPath);
55                 }
56                 break;
57             }
58         }
59     }
60
61     emit done(added, removed);
62 }