Warn if selected book is already in the library. Make book info working
authorAkos Polster <polster@nolove.pipacs.com>
Tue, 20 Jul 2010 08:06:59 +0000 (10:06 +0200)
committerAkos Polster <polster@nolove.pipacs.com>
Tue, 20 Jul 2010 08:06:59 +0000 (10:06 +0200)
again.

infodialog.cpp
library.cpp
librarydialog.cpp
librarydialog.h
mainwindow.cpp
mainwindow.h
pkg/changelog

index 07e05c5..6bd9e01 100644 (file)
@@ -36,7 +36,9 @@ void InfoDialog::onRemoveBook()
 {
     QString title = book->name();
     if (QMessageBox::Yes ==
-        QMessageBox::question(this, "Delete book", "Delete book " + title,
+        QMessageBox::question(this,
+                              "Delete book",
+                              "Delete book \"" + title + "\"",
                               QMessageBox::Yes
 #ifndef Q_WS_MAEMO_5
                               , QMessageBox::No
index e5f2b0c..dadbbc4 100644 (file)
@@ -36,8 +36,6 @@ int Library::rowCount(const QModelIndex &parent) const
 
 QVariant Library::data(const QModelIndex &index, int role) const
 {
-    qDebug() << "Library::data, row" << index.row() << "role" << role;
-
     if (!index.isValid()) {
         return QVariant();
     }
@@ -52,7 +50,9 @@ QVariant Library::data(const QModelIndex &index, int role) const
 
 Book *Library::book(const QModelIndex &index)
 {
-    if (index.isValid()) {
+    if (index.isValid() &&
+        (index.row() >= 0) &&
+        (index.row() < mBooks.size())) {
         return mBooks[index.row()];
     } else {
         return 0;
@@ -122,23 +122,20 @@ bool Library::add(QString path)
 
 void Library::remove(const QModelIndex &index)
 {
-    if (!index.isValid()) {
+    Book *toRemove = book(index);
+    if (!toRemove) {
         return;
     }
     int row = index.row();
-    if ((row < 0) || (row >= mBooks.size())) {
-        return;
-    }
     beginRemoveRows(QModelIndex(), row, row);
-    Book *book = mBooks[row];
     mBooks.removeAt(row);
     save();
     endRemoveRows();
-    if (book == mNowReading) {
+    if (toRemove == mNowReading) {
         mNowReading = 0;
         emit nowReadingChanged();
     }
-    delete book;
+    delete toRemove;
 }
 
 QModelIndex Library::nowReading() const
@@ -148,14 +145,7 @@ QModelIndex Library::nowReading() const
 
 void Library::setNowReading(const QModelIndex index)
 {
-    if (index.isValid()) {
-        int row = index.row();
-        if ((row >= 0) && (row < mBooks.size())) {
-            mNowReading = mBooks[row];
-        }
-    } else {
-        mNowReading = 0;
-    }
+    mNowReading = book(index);
     save();
     emit nowReadingChanged();
 }
index a14c67e..8a5cf74 100644 (file)
@@ -13,6 +13,7 @@
 #include "sortedlibrary.h"
 #include "book.h"
 #include "infodialog.h"
+#include "settings.h"
 
 LibraryDialog::LibraryDialog(QWidget *parent):
         QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
@@ -55,7 +56,7 @@ LibraryDialog::LibraryDialog(QWidget *parent):
 
     horizontalLayout->addWidget(buttonBox);
 
-    connect(Library::instance(), SIGNAL(currentBookChanged()),
+    connect(Library::instance(), SIGNAL(nowReadingChanged()),
             this, SLOT(onCurrentBookChanged()));
 #ifndef Q_WS_MAEMO_5
     connect(list, SIGNAL(itemSelectionChanged()),
@@ -80,14 +81,7 @@ void LibraryDialog::onAdd()
     Library *library = Library::instance();
 
     // Figure out directory to show
-    if (lastDir == "") {
-        if (library->rowCount()) {
-            QModelIndex lastIndex = library->index(library->rowCount() - 1);
-            Book *lastBook = library->book(lastIndex);
-            QFileInfo info(lastBook->path());
-            lastDir = info.absolutePath();
-        }
-    }
+    QString lastDir = Settings::instance()->value("lastdir").toString();
     if (lastDir == "") {
         lastDir = QDir::homePath();
     }
@@ -95,19 +89,23 @@ void LibraryDialog::onAdd()
     // Get book file name
     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
                                                 lastDir, "Books (*.epub)");
-    qDebug() << "LibraryDialog::onAdd" << path;
     if (path == "") {
         return;
     }
 
+    // Save directory selected
+    Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
+
     // Add book to library
-    lastDir = QFileInfo(path).absolutePath();
     if (library->find(path).isValid()) {
 #ifdef Q_WS_MAEMO_5
         QMaemo5InformationBox::information(this,
             tr("This book is already in the library"),
             QMaemo5InformationBox::DefaultTimeout);
-#endif
+#else
+        (void)QMessageBox::information(this, tr("Dorian"),
+            tr("This book is already in the library"), QMessageBox::Ok);
+#endif // Q_WS_MAEMO_5
         // FIXME: Select existing book
     }
     else {
index 4c5ce6f..95d8579 100644 (file)
@@ -27,7 +27,6 @@ public:
     QPushButton *readButton;
 #endif // Q_WS_MAEMO_5
     QPushButton *addButton;
-    QString lastDir;
 
 public slots:
     void onAdd();
index 59550c5..56ef2e2 100755 (executable)
@@ -34,7 +34,7 @@ const Qt::WindowFlags WIN_BIG_FLAGS =
 const int WIN_BIG_TIMER = 3000;
 
 MainWindow::MainWindow(QWidget *parent):
-        QMainWindow(parent), view(0), book(0), isFullscreen(false)
+        QMainWindow(parent), view(0), isFullscreen(false)
 {
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
@@ -153,6 +153,7 @@ void MainWindow::showFullScreen()
 
 void MainWindow::setCurrentBook(const QModelIndex &current)
 {
+    mCurrent = current;
     if (current.isValid()) {
         Book *book = Library::instance()->book(current);
         view->setBook(book);
@@ -185,8 +186,9 @@ void MainWindow::showSettings()
 
 void MainWindow::showInfo()
 {
-    if (book) {
-        InfoDialog *info = new InfoDialog(book, this);
+    if (mCurrent.isValid()) {
+        InfoDialog *info =
+            new InfoDialog(Library::instance()->book(mCurrent), this);
         info->exec();
     }
 }
@@ -199,6 +201,7 @@ void MainWindow::showDevTools()
 
 void MainWindow::showBookmarks()
 {
+    Book *book = Library::instance()->book(mCurrent);
     if (book) {
         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
         int ret = bookmarks->exec();
@@ -264,6 +267,7 @@ void MainWindow::onChapterLoaded(int index)
 {
     bool enablePrevious = false;
     bool enableNext = false;
+    Book *book = Library::instance()->book(mCurrent);
     if (book) {
         if (index > 0) {
             enablePrevious = true;
index 6ac81d0..1959965 100755 (executable)
@@ -59,7 +59,7 @@ private:
     QToolBar *toolBar;
     QDialog *settings;
     DevTools *devTools;
-    Book *book;
+    QModelIndex mCurrent;
     Qt::WindowFlags normalFlags;
     TranslucentButton *restoreButton;
     bool isFullscreen;
index ed564a3..1465ad2 100644 (file)
@@ -1,6 +1,7 @@
 dorian (0.0.10-1) unstable; urgency=low
 
   * Turn library into proper model
+  * Show message if selected book is already in the library
 
  -- Akos Polster <akos@pipacs.com>  Fri, 16 Jul 2010 20:00:00 +0200