More naming fixes.
[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     foreach (QString found, booksFound) {
29         if (!books.contains(found)) {
30             t.trace(QString("New book ") + found);
31             emit add(found);
32             added++;
33         }
34     }
35
36     foreach (QString book, books) {
37         QFileInfo bookInfo = QFileInfo(book);
38         QString bookDir = bookInfo.absolutePath();
39         QString bookPath = bookInfo.absoluteFilePath();
40         foreach (QString dirName, directories) {
41             t.trace(bookDir + " vs. " + QDir(dirName).absolutePath());
42             if (bookDir == QDir(dirName).absolutePath()) {
43                 if (!booksFound.contains(bookPath)) {
44                     t.trace(QString("Deleted book ") + bookPath);
45                     removed++;
46                     emit remove(bookPath);
47                 }
48                 break;
49             }
50         }
51     }
52
53     emit done(added, removed);
54 }