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