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