Introduce ListWidget.
[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 #include "listwindow.h"
18
19 LibraryDialog::LibraryDialog(QWidget *parent): ListWindow(parent)
20 {
21     setWindowTitle(tr("Library"));
22
23     // Create and add list view
24
25     list = new QListView(this);
26     sortedLibrary = new SortedLibrary(this);
27     list->setModel(sortedLibrary);
28     list->setSelectionMode(QAbstractItemView::SingleSelection);
29     list->setSpacing(1);
30     list->setUniformItemSizes(true);
31     Library *library = Library::instance();
32     QModelIndex current = library->nowReading();
33     setSelected(current);
34     addList(list);
35
36     // Add actions
37
38 #ifndef Q_WS_MAEMO_5
39     addAction(tr("Details"), this, SLOT(onDetails()));
40     addAction(tr("Read"), this, SLOT(onRead()));
41     addAction(tr("Delete"), this, SLOT(onRemove()));
42 #endif // ! Q_WS_MAEMO_5
43
44     addAction(tr("Add"), this, SLOT(onAdd()));
45     addAction(tr("Folders"), this, SLOT(onShowFolders()));
46
47     connect(Library::instance(), SIGNAL(nowReadingChanged()),
48             this, SLOT(onCurrentBookChanged()));
49     connect(Library::instance(),
50             SIGNAL(rowsInserted(const QModelIndex &, int, int)),
51             this,
52             SLOT(onBookAdded()));
53     connect(list, SIGNAL(activated(const QModelIndex &)),
54             this, SLOT(onItemActivated(const QModelIndex &)));
55 #ifndef Q_WS_MAEMO_5
56     connect(list->selectionModel(),
57             SIGNAL(selectionChanged(const QItemSelection &,
58                                     const QItemSelection &)),
59             this, SLOT(onItemSelectionChanged()));
60     onItemSelectionChanged();
61 #endif // !Q_WS_MAEMO_5
62 }
63
64 void LibraryDialog::onAdd()
65 {
66     Library *library = Library::instance();
67
68     // Figure out directory to show
69     QString lastDir = Settings::instance()->value("lastdir").toString();
70     if (lastDir == "") {
71         lastDir = QDir::homePath();
72     }
73
74     // Get book file name
75     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
76                                                 lastDir, "Books (*.epub)");
77     if (path == "") {
78         return;
79     }
80
81     // Save directory selected
82     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
83
84     // Add book to library
85     QModelIndex index = library->find(path);
86     if (index.isValid()) {
87 #ifdef Q_WS_MAEMO_5
88         QMaemo5InformationBox::information(this,
89             tr("This book is already in the library"),
90             QMaemo5InformationBox::DefaultTimeout);
91 #else
92         (void)QMessageBox::information(this, tr("Dorian"),
93             tr("This book is already in the library"), QMessageBox::Ok);
94 #endif // Q_WS_MAEMO_5
95         setSelected(index);
96     }
97     else {
98         library->add(path);
99     }
100 }
101
102 void LibraryDialog::onBookAdded()
103 {
104     Library *library = Library::instance();
105     setSelected(library->index(library->rowCount() - 1));
106 }
107
108 #ifndef Q_WS_MAEMO_5
109
110 void LibraryDialog::onRemove()
111 {
112     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
113     if (current.isValid()) {
114         Book *currentBook = Library::instance()->book(current);
115         QString title = currentBook->name();
116         if (QMessageBox::Yes ==
117             QMessageBox::question(this, tr("Delete book"),
118                 tr("Delete book \"%1\" from library?").
119                     arg(currentBook->shortName()),
120                 QMessageBox::Yes | QMessageBox::No)) {
121             Library::instance()->remove(current);
122         }
123     }
124 }
125
126 void LibraryDialog::onRead()
127 {
128     qDebug() << "LibraryDialog::onRead";
129     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
130     if (current.isValid()) {
131         Library::instance()->setNowReading(current);
132     }
133 }
134
135 void LibraryDialog::onDetails()
136 {
137     onItemActivated(list->currentIndex());
138 }
139
140 #endif // Q_WS_MAEMO_5
141
142 void LibraryDialog::onItemActivated(const QModelIndex &index)
143 {
144     qDebug() << "LibraryDialog::onItemActivated";
145     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
146     Book *book = Library::instance()->book(libraryIndex);
147     (new InfoDialog(book, this))->exec();
148 }
149
150 QString LibraryDialog::createItemText(const Book *book)
151 {
152     QString text = book->title + "\n";
153     if (book->creators.size()) {
154         text += book->creators[0];
155         for (int i = 1; i < book->creators.size(); i++) {
156             text += ", " + book->creators[i];
157         }
158     }
159     return text;
160 }
161
162 #ifndef Q_WS_MAEMO_5
163
164 void LibraryDialog::onItemSelectionChanged()
165 {
166 #if 0 // FIXME: API missing from ListWindow
167     bool enable = selected().isValid();
168     readButton->setEnabled(enable);
169     detailsButton->setEnabled(enable);
170     removeButton->setEnabled(enable);
171 #endif
172 }
173
174 #endif // Q_WS_MAEMO_5
175
176 void LibraryDialog::onCurrentBookChanged()
177 {
178     close();
179 }
180
181 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
182 {
183     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
184     list->selectionModel()->clearSelection();
185     if (sortedIndex.isValid()) {
186         list->selectionModel()->select(sortedIndex,
187                                        QItemSelectionModel::Select);
188         list->setCurrentIndex(sortedIndex);
189     }
190 }
191
192 QModelIndex LibraryDialog::selected() const
193 {
194     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
195     if (selectedItems.size()) {
196         return sortedLibrary->mapToSource(selectedItems[0]);
197     }
198     return QModelIndex();
199 }
200
201 void LibraryDialog::onShowFolders()
202 {
203 }