Make book info dialog work again.
[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
18 LibraryDialog::LibraryDialog(QWidget *parent):
19         QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
20                 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
21 {
22     setWindowTitle(tr("Library"));
23     list = new QListView(this);
24     sortedLibrary = new SortedLibrary(this);
25     list->setModel(sortedLibrary);
26     list->setSelectionMode(QAbstractItemView::SingleSelection);
27     list->setUniformItemSizes(true);
28 #ifndef Q_WS_MAEMO_5
29     setSizeGripEnabled(true);
30 #endif
31
32     Library *library = Library::instance();
33     QModelIndex current = library->nowReading();
34     select(current);
35
36     QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
37     horizontalLayout->addWidget(list);
38
39     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
40 #ifndef Q_WS_MAEMO_5
41     detailsButton = new QPushButton(tr("Details"), this);
42     readButton = new QPushButton(tr("Read"), this);
43     removeButton = new QPushButton(tr("Delete"), this);
44 #endif
45     addButton = new QPushButton(tr("Add"), this);
46
47 #ifndef Q_WS_MAEMO_5
48     buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
49     buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
50     buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
51 #endif // Q_WS_MAEMO_5
52     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
53
54     horizontalLayout->addWidget(buttonBox);
55
56     connect(Library::instance(), SIGNAL(nowReadingChanged()),
57             this, SLOT(onCurrentBookChanged()));
58     connect(Library::instance(),
59             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
60             this,
61             SLOT(onBookAdded()));
62 #ifndef Q_WS_MAEMO_5
63     connect(list, SIGNAL(itemSelectionChanged()),
64             this, SLOT(onItemSelectionChanged()));
65     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
66     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
67     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
68 #endif
69     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
70 #ifdef Q_WS_MAEMO_5
71     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
72             this, SLOT(onItemActivated(QListWidgetItem *)));
73 #else
74     connect(list, SIGNAL(doubleClicked(const QModelIndex &)),
75             this, SLOT(onItemActivated(const QModelIndex &)));
76 #endif
77
78 #ifndef Q_WS_MAEMO_5
79     onItemSelectionChanged();
80 #endif
81 }
82
83 void LibraryDialog::onAdd()
84 {
85     Library *library = Library::instance();
86
87     // Figure out directory to show
88     QString lastDir = Settings::instance()->value("lastdir").toString();
89     if (lastDir == "") {
90         lastDir = QDir::homePath();
91     }
92
93     // Get book file name
94     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
95                                                 lastDir, "Books (*.epub)");
96     if (path == "") {
97         return;
98     }
99
100     // Save directory selected
101     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
102
103     // Add book to library
104     QModelIndex index = library->find(path);
105     if (index.isValid()) {
106 #ifdef Q_WS_MAEMO_5
107         QMaemo5InformationBox::information(this,
108             tr("This book is already in the library"),
109             QMaemo5InformationBox::DefaultTimeout);
110 #else
111         (void)QMessageBox::information(this, tr("Dorian"),
112             tr("This book is already in the library"), QMessageBox::Ok);
113 #endif // Q_WS_MAEMO_5
114         select(index);
115     }
116     else {
117         library->add(path);
118     }
119 }
120
121 void LibraryDialog::onBookAdded()
122 {
123     Library *library = Library::instance();
124     select(library->index(library->rowCount() - 1));
125 }
126
127 #ifndef Q_WS_MAEMO_5
128
129 void LibraryDialog::onRemove()
130 {
131     qDebug() << "LibraryDialog::onRemove";
132     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
133     if (current.isValid()) {
134         Book *currentBook = Library::instance()->book(current);
135         QString title = currentBook->name();
136         if (QMessageBox::Yes ==
137             QMessageBox::question(this, "Delete book",
138                                   "Delete book \"" + title + "\"?",
139                                   QMessageBox::Yes
140 #ifndef Q_WS_MAEMO_5
141                                   , QMessageBox::No
142 #endif
143                                   )) {
144             Library::instance()->remove(current);
145         }
146     }
147 }
148
149 void LibraryDialog::onRead()
150 {
151     qDebug() << "LibraryDialog::onRead";
152     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
153     if (current.isValid()) {
154         Library::instance()->setNowReading(current);
155     }
156 }
157
158 void LibraryDialog::onDetails()
159 {
160     onItemActivated(list->currentIndex());
161 }
162
163 #endif // Q_WS_MAEMO_5
164
165 void LibraryDialog::onItemActivated(const QModelIndex &index)
166 {
167     qDebug() << "LibraryDialog::onItemActivated";
168     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
169     Book *book = Library::instance()->book(libraryIndex);
170     InfoDialog *info = new InfoDialog(book, this);
171     info->exec();
172 }
173
174 QString LibraryDialog::createItemText(const Book *book)
175 {
176     QString text = book->title + "\n";
177     if (book->creators.size()) {
178         text += book->creators[0];
179         for (int i = 1; i < book->creators.size(); i++) {
180             text += ", " + book->creators[i];
181         }
182     }
183     return text;
184 }
185
186 #ifndef Q_WS_MAEMO_5
187
188 void LibraryDialog::onItemSelectionChanged()
189 {
190 #if 0 // FIXME
191     bool enable = list->selectedItems().size();
192     qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
193     readButton->setEnabled(enable);
194     qDebug() << " readButton";
195     detailsButton->setEnabled(enable);
196     qDebug() << " detailsButton";
197     removeButton->setEnabled(enable);
198     qDebug() << " removeButton";
199 #endif
200 }
201
202 #endif // Q_WS_MAEMO_5
203
204 void LibraryDialog::onCurrentBookChanged()
205 {
206     close();
207 }
208
209 void LibraryDialog::select(const QModelIndex &libraryIndex)
210 {
211     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
212     list->selectionModel()->clearSelection();
213     if (sortedIndex.isValid()) {
214         list->selectionModel()->select(sortedIndex,
215                                        QItemSelectionModel::Select);
216         list->setCurrentIndex(sortedIndex);
217     }
218 }