Improve cover image display.
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5 #include <QModelIndex>
6
7 #ifdef Q_WS_MAEMO_5
8 #include <QtMaemo5/QMaemo5InformationBox>
9 #endif
10
11 #include "librarydialog.h"
12 #include "library.h"
13 #include "sortedlibrary.h"
14 #include "book.h"
15 #include "infodialog.h"
16 #include "settings.h"
17 #include "listwindow.h"
18 #include "foldersdialog.h"
19
20 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
21 {
22     setWindowTitle(tr("Library"));
23
24     // Create and add list view
25
26     list = new QListView(this);
27     sortedLibrary = new SortedLibrary(this);
28     list->setModel(sortedLibrary);
29     list->setSelectionMode(QAbstractItemView::SingleSelection);
30     list->setSpacing(1);
31     Library *library = Library::instance();
32     QModelIndex current = library->nowReading();
33     setSelected(current);
34     addList(list);
35
36     // Add actions
37
38 #ifndef Q_WS_MAEMO_5
39     addItemAction(tr("Details"), this, SLOT(onDetails()));
40     addItemAction(tr("Read"), this, SLOT(onRead()));
41     addItemAction(tr("Delete"), this, SLOT(onRemove()));
42 #endif // ! Q_WS_MAEMO_5
43
44     addAction(tr("Add book"), this, SLOT(onAdd()));
45     addAction(tr("Manage folders"), this, SLOT(onShowFolders()));
46
47     connect(Library::instance(), SIGNAL(nowReadingChanged()),
48             this, SLOT(onCurrentBookChanged()));
49     connect(Library::instance(),
50             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
51             this,
52             SLOT(onBookAdded()));
53     connect(list, SIGNAL(activated(const QModelIndex &)),
54             this, SLOT(onItemActivated(const QModelIndex &)));
55 }
56
57 void LibraryDialog::onAdd()
58 {
59     Library *library = Library::instance();
60
61     // Figure out directory to show
62     QString lastDir = Settings::instance()->value("lastdir").toString();
63     if (lastDir == "") {
64         lastDir = QDir::homePath();
65     }
66
67     // Get book file name
68     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
69                                                 lastDir, "Books (*.epub)");
70     if (path == "") {
71         return;
72     }
73
74     // Save directory selected
75     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
76
77     // Add book to library
78     QModelIndex index = library->find(path);
79     if (index.isValid()) {
80 #ifdef Q_WS_MAEMO_5
81         QMaemo5InformationBox::information(this,
82             tr("This book is already in the library"),
83             QMaemo5InformationBox::DefaultTimeout);
84 #else
85         (void)QMessageBox::information(this, tr("Dorian"),
86             tr("This book is already in the library"), QMessageBox::Ok);
87 #endif // Q_WS_MAEMO_5
88         setSelected(index);
89     }
90     else {
91         library->add(path);
92     }
93 }
94
95 void LibraryDialog::onBookAdded()
96 {
97     Library *library = Library::instance();
98     setSelected(library->index(library->rowCount() - 1));
99 }
100
101 #ifndef Q_WS_MAEMO_5
102
103 void LibraryDialog::onRemove()
104 {
105     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
106     if (current.isValid()) {
107         Book *currentBook = Library::instance()->book(current);
108         QString title = currentBook->name();
109         if (QMessageBox::Yes ==
110             QMessageBox::question(this, tr("Delete book"),
111                 tr("Delete book \"%1\" from library?").
112                     arg(currentBook->shortName()),
113                 QMessageBox::Yes | QMessageBox::No)) {
114             Library::instance()->remove(current);
115         }
116     }
117 }
118
119 void LibraryDialog::onRead()
120 {
121     qDebug() << "LibraryDialog::onRead";
122     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
123     if (current.isValid()) {
124         Library::instance()->setNowReading(current);
125     }
126 }
127
128 void LibraryDialog::onDetails()
129 {
130     onItemActivated(list->currentIndex());
131 }
132
133 #endif // Q_WS_MAEMO_5
134
135 void LibraryDialog::onItemActivated(const QModelIndex &index)
136 {
137     qDebug() << "LibraryDialog::onItemActivated";
138     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
139     Book *book = Library::instance()->book(libraryIndex);
140     (new InfoDialog(book, this))->exec();
141 }
142
143 QString LibraryDialog::createItemText(const Book *book)
144 {
145     QString text = book->title + "\n";
146     if (book->creators.size()) {
147         text += book->creators[0];
148         for (int i = 1; i < book->creators.size(); i++) {
149             text += ", " + book->creators[i];
150         }
151     }
152     return text;
153 }
154
155 void LibraryDialog::onCurrentBookChanged()
156 {
157     close();
158 }
159
160 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
161 {
162     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
163     list->selectionModel()->clearSelection();
164     if (sortedIndex.isValid()) {
165         list->selectionModel()->select(sortedIndex,
166                                        QItemSelectionModel::Select);
167         list->setCurrentIndex(sortedIndex);
168     }
169 }
170
171 QModelIndex LibraryDialog::selected() const
172 {
173     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
174     if (selectedItems.size()) {
175         return sortedLibrary->mapToSource(selectedItems[0]);
176     }
177     return QModelIndex();
178 }
179
180 void LibraryDialog::onShowFolders()
181 {
182     FoldersDialog *folders = new FoldersDialog(this);
183     folders->show();
184 }