Make library full screen on Maemo. Show busy indicator while loading
[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): QMainWindow(parent)
19 {
20 #ifdef Q_WS_MAEMO_5
21     setAttribute(Qt::WA_Maemo5StackedWindow, true);
22 #endif
23     setWindowTitle(tr("Library"));
24
25     QFrame *frame = new QFrame(this);
26     setCentralWidget(frame);
27     QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
28     frame->setLayout(horizontalLayout);
29
30     list = new QListView(this);
31     sortedLibrary = new SortedLibrary(this);
32     list->setModel(sortedLibrary);
33     list->setSelectionMode(QAbstractItemView::SingleSelection);
34     list->setUniformItemSizes(true);
35
36     Library *library = Library::instance();
37     QModelIndex current = library->nowReading();
38     setSelected(current);
39     horizontalLayout->addWidget(list);
40
41 #ifndef Q_WS_MAEMO_5
42     QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical);
43     detailsButton = new QPushButton(tr("Details"), this);
44     readButton = new QPushButton(tr("Read"), this);
45     removeButton = new QPushButton(tr("Delete"), this);
46     addButton = new QPushButton(tr("Add"), this);
47
48     buttonBox->addButton(detailsButton, QDialogButtonBox::ActionRole);
49     buttonBox->addButton(readButton, QDialogButtonBox::AcceptRole);
50     buttonBox->addButton(removeButton, QDialogButtonBox::ActionRole);
51     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
52     horizontalLayout->addWidget(buttonBox);
53 #else
54     QAction *addBookAction = menuBar()->addAction(tr("Add book"));
55 #endif // Q_WS_MAEMO_5
56
57     connect(Library::instance(), SIGNAL(nowReadingChanged()),
58             this, SLOT(onCurrentBookChanged()));
59     connect(Library::instance(),
60             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
61             this,
62             SLOT(onBookAdded()));
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(addButton, SIGNAL(clicked()), this, SLOT(onAdd()));
69     connect(detailsButton, SIGNAL(clicked()), this, SLOT(onDetails()));
70     connect(readButton, SIGNAL(clicked()), this, SLOT(onRead()));
71     connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
72     connect(list->selectionModel(),
73             SIGNAL(selectionChanged(const QItemSelection &,
74                                     const QItemSelection &)),
75             this, SLOT(onItemSelectionChanged()));
76     onItemSelectionChanged();
77 #else
78     connect(addBookAction, SIGNAL(triggered()), this, SLOT(onAdd()));
79 #endif // !Q_WS_MAEMO_5
80 }
81
82 void LibraryDialog::onAdd()
83 {
84     Library *library = Library::instance();
85
86     // Figure out directory to show
87     QString lastDir = Settings::instance()->value("lastdir").toString();
88     if (lastDir == "") {
89         lastDir = QDir::homePath();
90     }
91
92     // Get book file name
93     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
94                                                 lastDir, "Books (*.epub)");
95     if (path == "") {
96         return;
97     }
98
99     // Save directory selected
100     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
101
102     // Add book to library
103     QModelIndex index = library->find(path);
104     if (index.isValid()) {
105 #ifdef Q_WS_MAEMO_5
106         QMaemo5InformationBox::information(this,
107             tr("This book is already in the library"),
108             QMaemo5InformationBox::DefaultTimeout);
109 #else
110         (void)QMessageBox::information(this, tr("Dorian"),
111             tr("This book is already in the library"), QMessageBox::Ok);
112 #endif // Q_WS_MAEMO_5
113         setSelected(index);
114     }
115     else {
116         library->add(path);
117     }
118 }
119
120 void LibraryDialog::onBookAdded()
121 {
122     Library *library = Library::instance();
123     setSelected(library->index(library->rowCount() - 1));
124 }
125
126 #ifndef Q_WS_MAEMO_5
127
128 void LibraryDialog::onRemove()
129 {
130     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
131     if (current.isValid()) {
132         Book *currentBook = Library::instance()->book(current);
133         QString title = currentBook->name();
134         if (QMessageBox::Yes ==
135             QMessageBox::question(this, "Delete book",
136                                   "Delete book \"" + title + "\"?",
137                                   QMessageBox::Yes | QMessageBox::No)) {
138             Library::instance()->remove(current);
139         }
140     }
141 }
142
143 void LibraryDialog::onRead()
144 {
145     qDebug() << "LibraryDialog::onRead";
146     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
147     if (current.isValid()) {
148         Library::instance()->setNowReading(current);
149     }
150 }
151
152 void LibraryDialog::onDetails()
153 {
154     onItemActivated(list->currentIndex());
155 }
156
157 #endif // Q_WS_MAEMO_5
158
159 void LibraryDialog::onItemActivated(const QModelIndex &index)
160 {
161     qDebug() << "LibraryDialog::onItemActivated";
162     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
163     Book *book = Library::instance()->book(libraryIndex);
164     (new InfoDialog(book, this))->exec();
165 }
166
167 QString LibraryDialog::createItemText(const Book *book)
168 {
169     QString text = book->title + "\n";
170     if (book->creators.size()) {
171         text += book->creators[0];
172         for (int i = 1; i < book->creators.size(); i++) {
173             text += ", " + book->creators[i];
174         }
175     }
176     return text;
177 }
178
179 #ifndef Q_WS_MAEMO_5
180
181 void LibraryDialog::onItemSelectionChanged()
182 {
183     bool enable = selected().isValid();
184     readButton->setEnabled(enable);
185     detailsButton->setEnabled(enable);
186     removeButton->setEnabled(enable);
187 }
188
189 #endif // Q_WS_MAEMO_5
190
191 void LibraryDialog::onCurrentBookChanged()
192 {
193     close();
194 }
195
196 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
197 {
198     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
199     list->selectionModel()->clearSelection();
200     if (sortedIndex.isValid()) {
201         list->selectionModel()->select(sortedIndex,
202                                        QItemSelectionModel::Select);
203         list->setCurrentIndex(sortedIndex);
204     }
205 }
206
207 QModelIndex LibraryDialog::selected() const
208 {
209     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
210     if (selectedItems.size()) {
211         return sortedLibrary->mapToSource(selectedItems[0]);
212     }
213     return QModelIndex();
214 }
215
216 void LibraryDialog::closeEvent(QCloseEvent *event)
217 {
218 #ifdef Q_WS_MAEMO_5
219     menuBar()->clear();
220 #endif
221     event->accept();
222 }