Get rid of unused Options menu items on Symbian.
[dorian] / librarydialog.cpp
index 3ec1a98..4312cd4 100644 (file)
@@ -1,12 +1,5 @@
-#include <QtGui>
-#include <QDebug>
-#include <QFileInfo>
 #include <QDir>
-#include <QModelIndex>
-
-#ifdef Q_WS_MAEMO_5
-#include <QtMaemo5/QMaemo5InformationBox>
-#endif
+#include <QtGui>
 
 #include "librarydialog.h"
 #include "library.h"
 #include "platform.h"
 #include "searchresultsdialog.h"
 #include "progressdialog.h"
+#include "settings.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
 {
+    TRACE;
     setWindowTitle(tr("Library"));
+    setAttribute(Qt::WA_DeleteOnClose, true);
 
     // Add actions
 
-#ifndef Q_WS_MAEMO_5
-    addItemAction(tr("Details"), this, SLOT(onDetails()));
-    addItemAction(tr("Read"), this, SLOT(onRead()));
-    addItemAction(tr("Delete"), this, SLOT(onRemove()));
-#endif // ! Q_WS_MAEMO_5
+    sortByTitle = addMenuAction(tr("Sort by title"), this, SLOT(onSortByTitle()));
+    sortByAuthor =
+            addMenuAction(tr("Sort by author"), this, SLOT(onSortByAuthor()));
 
     addAction(tr("Add book"), this, SLOT(onAdd()), "add");
     addAction(tr("Add books from folder"), this, SLOT(onAddFolder()), "folder");
@@ -47,13 +41,11 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     list->setSpacing(1);
     Library *library = Library::instance();
     QModelIndex current = library->nowReading();
-    setSelected(current);
+    setSelected(sortedLibrary->mapFromSource(current));
     addList(list);
 
     progress = new ProgressDialog(tr("Adding books"), this);
 
-    connect(Library::instance(), SIGNAL(nowReadingChanged()),
-            this, SLOT(onCurrentBookChanged()));
     connect(Library::instance(),
             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
             this,
@@ -65,6 +57,15 @@ LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
     searchDialog = new SearchDialog(this);
     connect(Search::instance(), SIGNAL(endSearch()),
             this, SLOT(showSearchResults()));
+
+    // Retrieve default sort criteria
+    switch (Settings::instance()->value("lib/sortby").toInt()) {
+    case SortedLibrary::SortByAuthor:
+        onSortByAuthor();
+        break;
+    default:
+        onSortByTitle();
+    }
 }
 
 void LibraryDialog::onAdd()
@@ -90,15 +91,9 @@ void LibraryDialog::onAdd()
     // Add book to library
     QModelIndex index = library->find(path);
     if (index.isValid()) {
-#ifdef Q_WS_MAEMO_5
-        QMaemo5InformationBox::information(this,
-            tr("This book is already in the library"),
-            QMaemo5InformationBox::DefaultTimeout);
-#else
-        (void)QMessageBox::information(this, tr("Dorian"),
-            tr("This book is already in the library"), QMessageBox::Ok);
-#endif // Q_WS_MAEMO_5
-        setSelected(index);
+        Platform::instance()->information(
+                tr("This book is already in the library"), this);
+        setSelected(sortedLibrary->mapFromSource(index));
     }
     else {
         library->add(path);
@@ -108,54 +103,41 @@ void LibraryDialog::onAdd()
 void LibraryDialog::onBookAdded()
 {
     Library *library = Library::instance();
-    setSelected(library->index(library->rowCount() - 1));
-}
-
-#ifndef Q_WS_MAEMO_5
-
-void LibraryDialog::onRemove()
-{
-    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-    if (current.isValid()) {
-        Book *currentBook = Library::instance()->book(current);
-        QString title = currentBook->name();
-        if (QMessageBox::Yes ==
-            QMessageBox::question(this, tr("Delete book"),
-                tr("Delete book \"%1\" from library?").
-                    arg(currentBook->shortName()),
-                QMessageBox::Yes | QMessageBox::No)) {
-            Library::instance()->remove(current);
-        }
-    }
-}
-
-void LibraryDialog::onRead()
-{
-    qDebug() << "LibraryDialog::onRead";
-    QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
-    if (current.isValid()) {
-        Library::instance()->setNowReading(current);
-    }
+    setSelected(sortedLibrary->
+                mapFromSource(library->index(library->rowCount() - 1)));
 }
 
-void LibraryDialog::onDetails()
-{
-    onItemActivated(list->currentIndex());
-}
-
-#endif // Q_WS_MAEMO_5
-
 void LibraryDialog::onItemActivated(const QModelIndex &index)
 {
-    qDebug() << "LibraryDialog::onItemActivated";
+    TRACE;
     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
     Book *book = Library::instance()->book(libraryIndex);
-    (new InfoDialog(book, this))->exec();
+    int ret = (new InfoDialog(book, this))->exec();
+
+    switch (ret) {
+    case InfoDialog::Read:
+        {
+            QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+            Q_ASSERT(current.isValid());
+            Library::instance()->setNowReading(current);
+            close();
+        }
+        break;
+    case InfoDialog::Delete:
+        {
+            QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
+            Library::instance()->remove(current);
+        }
+        break;
+    default:
+        ;
+    }
 }
 
-QString LibraryDialog::createItemText(const Book *book)
+QString LibraryDialog::createItemText(Book *book)
 {
-    QString text = book->title + "\n";
+    Q_ASSERT(book);
+    QString text = book->shortName() + "\n";
     if (book->creators.size()) {
         text += book->creators[0];
         for (int i = 1; i < book->creators.size(); i++) {
@@ -165,11 +147,6 @@ QString LibraryDialog::createItemText(const Book *book)
     return text;
 }
 
-void LibraryDialog::onCurrentBookChanged()
-{
-    close();
-}
-
 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
 {
     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
@@ -192,7 +169,7 @@ QModelIndex LibraryDialog::selected() const
 
 void LibraryDialog::onAddFolder()
 {
-    Trace t("LibraryDialog::onAddFolder");
+    TRACE;
 
     // Get folder name
     Settings *settings = Settings::instance();
@@ -232,11 +209,7 @@ void LibraryDialog::onAddFromFolderDone(int added)
 
     progress->reset();
     qDebug() << "LibraryDialog::onRefreshDone:" << msg;
-#ifdef Q_WS_MAEMO_5
-    QMaemo5InformationBox::information(this, msg);
-#else
-    // FIXME
-#endif
+    Platform::instance()->information(msg, this);
 }
 
 void LibraryDialog::onAddFromFolder(const QString &path)
@@ -252,10 +225,7 @@ void LibraryDialog::onSearch()
         return;
     }
     progress->setLabelText(tr("Searching Project Gutenberg"));
-    progress->setMinimum(0);
-    progress->setMaximum(0);
-    progress->setValue(0);
-    progress->show();
+    progress->showWait();
     Search::instance()->start(searchDialog->query());
 }
 
@@ -263,14 +233,32 @@ void LibraryDialog::showSearchResults()
 {
     progress->reset();
     QList<Search::Result> results = Search::instance()->results();
-    if (results.count() == 0) {
-        QMessageBox::information(this, tr("Search results"), tr("No books found"));
+    if (results.isEmpty()) {
+        QMessageBox::information(this, tr("Search results"),
+                                 tr("No books found"));
         return;
     }
 
     SearchResultsDialog *dialog = new SearchResultsDialog(results, this);
-    // FIXME
     connect(dialog, SIGNAL(add(const Search::Result &)),
             this, SLOT(onAddSearchResult(const Search::Result &)));
     dialog->show();
 }
+
+void LibraryDialog::onSortByAuthor()
+{
+    TRACE;
+    sortedLibrary->setSortBy(SortedLibrary::SortByAuthor);
+    Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByAuthor);
+    sortByAuthor->setChecked(true);
+    sortByTitle->setChecked(false);
+}
+
+void LibraryDialog::onSortByTitle()
+{
+    TRACE;
+    sortedLibrary->setSortBy(SortedLibrary::SortByTitle);
+    Settings::instance()->setValue("lib/sortby", SortedLibrary::SortByTitle);
+    sortByAuthor->setChecked(false);
+    sortByTitle->setChecked(true);
+}