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