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