Manage folders.
[dorian] / foldersdialog.cpp
1 #include <QtGui>
2 #include <QStringListModel>
3 #include <QFileInfo>
4
5 #ifdef Q_WS_MAEMO_5
6 #include <QtMaemo5/QMaemo5InformationBox>
7 #endif
8
9 #include "foldersdialog.h"
10 #include "library.h"
11 #include "settings.h"
12 #include "trace.h"
13 #include "bookfinder.h"
14
15 FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
16 {
17     setWindowTitle(tr("Folders"));
18     model = new QStringListModel(Library::instance()->folders(), this);
19     QListView *list = new QListView(this);
20     list->setModel(model);
21     list->setEditTriggers(QAbstractItemView::NoEditTriggers);
22     list->setUniformItemSizes(true);
23     addList(list);
24     addAction(tr("Add folder"), this, SLOT(onAdd()));
25 #ifndef Q_WS_MAEMO_5
26     addAction(tr("Delete folder"), this, SLOT(onDelete()));
27 #endif
28     addAction(tr("Re-scan folders"), this, SLOT(onRefresh()));
29 }
30
31 void FoldersDialog::onAdd()
32 {
33     Trace t("FoldersDialog::onAdd");
34
35     // Get folder name
36     Settings *settings = Settings::instance();
37     QString last = settings->value("lastfolderadded", QDir::homePath()).toString();
38     QString path = QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
39     if (path == "") {
40         return;
41     }
42     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
43     t.trace(path);
44
45     // Add folder to model
46     if (Library::instance()->addFolder(path)) {
47         int rows = model->rowCount();
48         model->insertRows(rows, 1);
49         model->setData(model->index(rows), path);
50         onRefresh();
51     } else {
52 #ifdef Q_WS_MAEMO_5
53         QMaemo5InformationBox::information(this,
54             tr("This folder is already in the library"),
55             QMaemo5InformationBox::DefaultTimeout);
56 #else
57         (void)QMessageBox::information(this, tr("Dorian"),
58             tr("This folder is already in the library"), QMessageBox::Ok);
59 #endif // Q_WS_MAEMO_5
60     }
61 }
62
63 void FoldersDialog::onRemove()
64 {
65 }
66
67 void FoldersDialog::onRefresh()
68 {
69 #ifdef Q_WS_MAEMO_5
70     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
71 #endif
72
73     BookFinder *bookFinder = new BookFinder(this);
74     Library *library = Library::instance();
75     connect(bookFinder, SIGNAL(add(const QString &)),
76             library, SLOT(add(const QString &)));
77     connect(bookFinder, SIGNAL(remove(const QString &)),
78             library, SLOT(remove(const QString &)));
79     connect(bookFinder, SIGNAL(done(int,int)),
80             this, SLOT(onRefreshDone(int, int)));
81     // bookFinder->moveToThread(&bookFinderThread);
82     // bookFinderThread.start();
83
84     (void)QMetaObject::invokeMethod(
85         bookFinder,
86         "find",
87         Q_ARG(QStringList, model->stringList()),
88         Q_ARG(QStringList, library->bookPaths()));
89
90 #ifdef Q_WS_MAEMO_5
91     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
92 #endif
93 }
94
95 void FoldersDialog::onRefreshDone(int added, int removed)
96 {
97     QString addedMsg;
98     QString removedMsg;
99
100     switch (added) {
101     case 0: addedMsg = tr("No books added"); break;
102     case 1: addedMsg = tr("%1 book added").arg(1); break;
103     default: addedMsg = tr("%1 books added").arg(added);
104     }
105
106     switch (removed) {
107     case 0: removedMsg = tr("no books removed"); break;
108     case 1: removedMsg = tr("%1 book removed").arg(1); break;
109     default: removedMsg = tr("%1 books removed").arg(removed);
110     }
111
112     QString msg(tr("Scanning folders complete\n\n%1, %2.").
113                 arg(addedMsg).arg(removedMsg));
114     Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
115 #ifdef Q_WS_MAEMO_5
116     QMaemo5InformationBox::
117             information(this, msg, QMaemo5InformationBox::NoTimeout);
118 #else
119     // FIXME
120 #endif
121 }