2 #include <QStringListModel>
6 #include <QtMaemo5/QMaemo5InformationBox>
9 #include "foldersdialog.h"
13 #include "bookfinder.h"
15 FoldersDialog::FoldersDialog(QWidget *parent): ListWindow(parent)
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);
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()));
30 void FoldersDialog::onAdd()
32 Trace t("FoldersDialog::onAdd");
35 Settings *settings = Settings::instance();
37 settings->value("lastfolderadded", QDir::homePath()).toString();
39 QFileDialog::getExistingDirectory(this, tr("Add Folder"), last);
43 settings->setValue("lastfolderadded", QFileInfo(path).absolutePath());
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);
54 QMaemo5InformationBox::information(this,
55 tr("This folder is already in the library"));
57 (void)QMessageBox::information(this, tr("Dorian"),
58 tr("This folder is already in the library"), QMessageBox::Ok);
59 #endif // Q_WS_MAEMO_5
63 void FoldersDialog::onRemove()
65 Trace t("FoldersDialog::onRemove");
67 QModelIndexList selection = list->selectionModel()->selectedIndexes();
68 if (selection.size() != 1) {
72 QModelIndex selected = selection[0];
73 QString path = list->model()->data(selected).toString();
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());
86 void FoldersDialog::onRefresh()
88 Trace t("FoldersDialog::onRefresh");
90 QModelIndexList selection = list->selectionModel()->selectedIndexes();
91 if (selection.size() != 1) {
96 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
98 QModelIndex selected = selection[0];
99 QString path = list->model()->data(selected).toString();
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());
111 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
115 void FoldersDialog::onRefreshAll()
118 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
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());
132 setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
136 void FoldersDialog::onRefreshDone(int added, int removed)
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);
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);
153 QString msg(tr("Scanning complete\n\n%1, %2.").
154 arg(addedMsg).arg(removedMsg));
155 Trace::trace(QString("FoldersDialog::onRefreshDone: " + msg));
157 QMaemo5InformationBox::information(this, msg);