Initial import.
[dorian] / librarydialog.cpp
1 #include <QtGui>
2 #include <QDebug>
3 #include <QFileInfo>
4 #include <QDir>
5
6 #ifdef Q_WS_MAEMO_5
7 #include <QtMaemo5/QMaemo5InformationBox>
8 #endif
9
10 #include "librarydialog.h"
11 #include "library.h"
12 #include "book.h"
13 #include "infodialog.h"
14
15 LibraryDialog::LibraryDialog(QWidget *parent):
16         QDialog(parent, Qt::Dialog | Qt::WindowTitleHint |
17                 Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint)
18 {
19     setWindowTitle(tr("Library"));
20     list = new QListWidget(this);
21     list->setSelectionMode(QAbstractItemView::SingleSelection);
22 #ifndef Q_WS_MAEMO_5
23     setSizeGripEnabled(true);
24 #endif
25
26     Library *library = Library::instance();
27     for (int i = 0; i < library->size(); i++) {
28         Book *book = library->at(i);
29         (void)new QListWidgetItem(book->cover, createItemText(book), list);
30     }
31     Book *current = library->current();
32     if (library->size() && current) {
33         list->setItemSelected(list->item(library->find(current)), true);
34     }
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, SIGNAL(bookAdded()), this, SLOT(onBookAdded()));
57     connect(library, SIGNAL(currentBookChanged()),
58             this, SLOT(onCurrentBookChanged()));
59 #ifndef Q_WS_MAEMO_5
60     connect(list, SIGNAL(itemSelectionChanged()),
61             this, SLOT(onItemSelectionChanged()));
62     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
63     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
64     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
65 #endif
66     connect(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
67 #ifdef Q_WS_MAEMO_5
68     connect(list, SIGNAL(itemActivated(QListWidgetItem *)),
69             this, SLOT(onItemActivated(QListWidgetItem *)));
70 #endif
71
72 #ifndef Q_WS_MAEMO_5
73     onItemSelectionChanged();
74 #endif
75 }
76
77 void LibraryDialog::onAdd()
78 {
79     Library *library = Library::instance();
80
81     if (lastDir == "") {
82         if (library->size()) {
83             QFileInfo info(library->at(library->size() - 1)->path());
84             lastDir = info.absolutePath();
85         }
86     }
87     if (lastDir == "") {
88         lastDir = QDir::homePath();
89     }
90     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
91                                                 lastDir, "Books (*.epub)");
92     qDebug() << "LibraryDialog::onAdd" << path;
93     if (path == "") {
94         return;
95     }
96
97     lastDir = QFileInfo(path).absolutePath();
98     if (library->find(path) != -1) {
99 #ifdef Q_WS_MAEMO_5
100         QMaemo5InformationBox::information(this,
101             tr("Book is alreadButtony in the library"),
102             QMaemo5InformationBox::DefaultTimeout);
103 #endif
104         // FIXME: Select existing book
105     }
106     else {
107         library->add(path);
108     }
109 }
110
111 void LibraryDialog::onBookAdded()
112 {
113     Library *library = Library::instance();
114     int index = library->size() - 1;
115     Book *book = library->at(index);
116     QListWidgetItem *item = new QListWidgetItem(book->cover,
117                                                 createItemText(book));
118     list->addItem(item);
119     list->setCurrentItem(item);
120 }
121
122 #ifndef Q_WS_MAEMO_5
123
124 void LibraryDialog::onRemove()
125 {
126     qDebug() << "LibraryDialog::onRemove";
127     if (list->selectedItems().isEmpty()) {
128         return;
129     }
130     QListWidgetItem *item = list->selectedItems()[0];
131     int row = list->row(item);
132     QString title = Library::instance()->at(row)->title;
133     if (QMessageBox::Yes ==
134         QMessageBox::question(this, "Delete book", "Delete book " + title,
135                               QMessageBox::Yes, QMessageBox::No)) {
136         list->takeItem(row);
137         Library::instance()->remove(row);
138     }
139 }
140
141 void LibraryDialog::onRead()
142 {
143     qDebug() << "LibraryDialog::onRead";
144     if (list->selectedItems().isEmpty()) {
145         return;
146     }
147     QListWidgetItem *item = list->selectedItems()[0];
148     int row = list->row(item);
149     Library::instance()->setCurrent(row);
150 }
151
152 void LibraryDialog::onDetails()
153 {
154     onItemActivated(list->selectedItems()[0]);
155 }
156
157 #endif // Q_WS_MAEMO_5
158
159 void LibraryDialog::onItemActivated(QListWidgetItem *item)
160 {
161     qDebug() << "LibraryDialog::onItemActivated";
162     int row = list->row(item);
163     Book *book = Library::instance()->at(row);
164     InfoDialog *info = new InfoDialog(book, this);
165     info->exec();
166 }
167
168 QString LibraryDialog::createItemText(const Book *book)
169 {
170     QString text = book->title + "\n";
171     if (book->creators.size()) {
172         text += book->creators[0];
173         for (int i = 1; i < book->creators.size(); i++) {
174             text += ", " + book->creators[i];
175         }
176     }
177     return text;
178 }
179
180 #ifndef Q_WS_MAEMO_5
181
182 void LibraryDialog::onItemSelectionChanged()
183 {
184     bool enable = list->selectedItems().size();
185     qDebug() << "LibraryDialog::onItemSelectionChanged" << enable;
186     readButton->setEnabled(enable);
187     qDebug() << " readButton";
188     detailsButton->setEnabled(enable);
189     qDebug() << " detailsButton";
190     removeButton->setEnabled(enable);
191     qDebug() << " removeButton";
192 }
193
194 #endif // Q_WS_MAEMO_5
195
196 void LibraryDialog::onCurrentBookChanged()
197 {
198     close();
199 }