More on model.
[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->book(lastIndex);
87             QFileInfo info(lastBook->path());
88             lastDir = info.absolutePath();
89         }
90     }
91     if (lastDir == "") {
92         lastDir = QDir::homePath();
93     }
94
95     // Get book file name
96     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
97                                                 lastDir, "Books (*.epub)");
98     qDebug() << "LibraryDialog::onAdd" << path;
99     if (path == "") {
100         return;
101     }
102
103     // Add book to library
104     lastDir = QFileInfo(path).absolutePath();
105     if (library->find(path).isValid()) {
106 #ifdef Q_WS_MAEMO_5
107         QMaemo5InformationBox::information(this,
108             tr("This book is already in the library"),
109             QMaemo5InformationBox::DefaultTimeout);
110 #endif
111         // FIXME: Select existing book
112     }
113     else {
114         library->add(path);
115     }
116 }
117
118 void LibraryDialog::onBookAdded()
119 {
120 #if 0
121     Library *library = Library::instance();
122     int index = library->size() - 1;
123     Book *book = library->at(index);
124     QListWidgetItem *item = new QListWidgetItem(book->cover,
125                                                 createItemText(book));
126     list->addItem(item);
127     list->setCurrentItem(item);
128 #endif
129 }
130
131 #ifndef Q_WS_MAEMO_5
132
133 void LibraryDialog::onRemove()
134 {
135     qDebug() << "LibraryDialog::onRemove";
136     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
137     if (current.isValid()) {
138         Book *currentBook = Library::instance()->book(current);
139         QString title = currentBook->name();
140         if (QMessageBox::Yes ==
141             QMessageBox::question(this, "Delete book",
142                                   "Delete book \"" + title + "\"?",
143                                   QMessageBox::Yes
144 #ifndef Q_WS_MAEMO_5
145                                   , QMessageBox::No
146 #endif
147                                   )) {
148             Library::instance()->remove(current);
149         }
150     }
151 }
152
153 void LibraryDialog::onRead()
154 {
155     qDebug() << "LibraryDialog::onRead";
156     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
157     if (current.isValid()) {
158         Library::instance()->setNowReading(current);
159     }
160 }
161
162 void LibraryDialog::onDetails()
163 {
164 #if 0 // FIXME
165     onItemActivated(list->selectedItems()[0]);
166 #endif
167 }
168
169 #endif // Q_WS_MAEMO_5
170
171 void LibraryDialog::onItemActivated(const QModelIndex &index)
172 {
173     qDebug() << "LibraryDialog::onItemActivated";
174 #if 0 // FIXME
175     int row = list->row(item);
176     Book *book = Library::instance()->at(row);
177     InfoDialog *info = new InfoDialog(book, this);
178     info->exec();
179 #endif
180 }
181
182 QString LibraryDialog::createItemText(const Book *book)
183 {
184     QString text = book->title + "\n";
185     if (book->creators.size()) {
186         text += book->creators[0];
187         for (int i = 1; i < book->creators.size(); i++) {
188             text += ", " + book->creators[i];
189         }
190     }
191     return text;
192 }
193
194 #ifndef Q_WS_MAEMO_5
195
196 void LibraryDialog::onItemSelectionChanged()
197 {
198 #if 0 // FIXME
199     bool enable = list->selectedItems().size();
200     qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
201     readButton->setEnabled(enable);
202     qDebug() << " readButton";
203     detailsButton->setEnabled(enable);
204     qDebug() << " detailsButton";
205     removeButton->setEnabled(enable);
206     qDebug() << " removeButton";
207 #endif
208 }
209
210 #endif // Q_WS_MAEMO_5
211
212 void LibraryDialog::onCurrentBookChanged()
213 {
214     close();
215 }