Add feature to remove folder from library. Experiment with context menus on Maemo.
[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     addItemAction(tr("Re-scan"), this, SLOT(onRefresh()));
26     addItemAction(tr("Remove"), this, SLOT(onRemove()));
27     addAction(tr("Re-scan all folders"), this, SLOT(onRefreshAll()));
28 }
29
30 void FoldersDialog::onAdd()
31 {
32     Trace t("FoldersDialog::onAdd");
33
34     // Get folder name
35     Settings *settings = Settings::instance();
36     QString last =
37             settings->value("lastfolderadded", QDir::homePath()).toString();
38     QString path =
39             QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
40     if (path == "") {
41         return;
42     }
43     settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
44     t.trace(path);
45
46     // Add folder to model
47     if (Library::instance()->addFolder(path)) {
48         int rows = model->rowCount();
49         model->insertRows(rows, 1);
50         model->setData(model->index(rows), path);
51         onRefresh();
52     } else {
53 #ifdef Q_WS_MAEMO_5
54         QMaemo5InformationBox::information(this,
55             tr("This folder is already in the library"));
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     Trace t("FoldersDialog::onRemove");
66
67     QModelIndexList selection = list->selectionModel()->selectedIndexes();
68     if (selection.size() != 1) {
69         return;
70     }
71
72     QModelIndex selected = selection[0];
73     QString path = list->model()->data(selected).toString();
74     t.trace(path);
75
76     if (QMessageBox::Yes ==
77         QMessageBox::question(this, tr("Remove folder"),
78             tr("Remove folder \"%1\" from library?").arg(path),
79             QMessageBox::Yes | QMessageBox::No)) {
80         if (Library::instance()->removeFolder(path)) {
81             model->removeRow(selected.row());
82         }
83     }
84 }
85
86 void FoldersDialog::onRefresh()
87 {
88     Trace t("FoldersDialog::onRefresh");
89
90     QModelIndexList selection = list->selectionModel()->selectedIndexes();
91     if (selection.size() != 1) {
92         return;
93     }
94
95 #ifdef Q_WS_MAEMO_5
96     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
97 #endif
98     QModelIndex selected = selection[0];
99     QString path = list->model()->data(selected).toString();
100     t.trace(path);
101     BookFinder *bookFinder = new BookFinder(this);
102     Library *library = Library::instance();
103     connect(bookFinder, SIGNAL(add(const QString &)),
104             library, SLOT(add(const QString &)));
105     connect(bookFinder, SIGNAL(remove(const QString &)),
106             library, SLOT(remove(const QString &)));
107     connect(bookFinder, SIGNAL(done(int,int)),
108             this, SLOT(onRefreshDone(int, int)));
109     bookFinder->find(QStringList(path), library->bookPaths());
110 #ifdef Q_WS_MAEMO_5
111     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
112 #endif
113 }
114
115 void FoldersDialog::onRefreshAll()
116 {
117 #ifdef Q_WS_MAEMO_5
118     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
119 #endif
120
121     BookFinder *bookFinder = new BookFinder(this);
122     Library *library = Library::instance();
123     connect(bookFinder, SIGNAL(add(const QString &)),
124             library, SLOT(add(const QString &)));
125     connect(bookFinder, SIGNAL(remove(const QString &)),
126             library, SLOT(remove(const QString &)));
127     connect(bookFinder, SIGNAL(done(int,int)),
128             this, SLOT(onRefreshDone(int, int)));
129     bookFinder->find(model->stringList(), library->bookPaths());
130
131 #ifdef Q_WS_MAEMO_5
132     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
133 #endif
134 }
135
136 void FoldersDialog::onRefreshDone(int added, int removed)
137 {
138     QString addedMsg;
139     QString removedMsg;
140
141     switch (added) {
142     case 0: addedMsg = tr("No books added"); break;
143     case 1: addedMsg = tr("%1 book added").arg(1); break;
144     default: addedMsg = tr("%1 books added").arg(added);
145     }
146
147     switch (removed) {
148     case 0: removedMsg = tr("no books removed"); break;
149     case 1: removedMsg = tr("%1 book removed").arg(1); break;
150     default: removedMsg = tr("%1 books removed").arg(removed);
151     }
152
153     QString msg(tr("Scanning complete\n\n%1, %2.").
154                 arg(addedMsg).arg(removedMsg));
155     Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
156 #ifdef Q_WS_MAEMO_5
157     QMaemo5InformationBox::information(this, msg);
158 #else
159     // FIXME
160 #endif
161 }