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