Add context dependent actions to LibraryDialog. Make folder delete work on non-Maemo.
[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     addItemAction(tr("Details"), this, SLOT(onDetails()));
41     addItemAction(tr("Read"), this, SLOT(onRead()));
42     addItemAction(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 }
57
58 void LibraryDialog::onAdd()
59 {
60     Library *library = Library::instance();
61
62     // Figure out directory to show
63     QString lastDir = Settings::instance()->value("lastdir").toString();
64     if (lastDir == "") {
65         lastDir = QDir::homePath();
66     }
67
68     // Get book file name
69     QString path = QFileDialog::getOpenFileName(this, tr("Add Book"),
70                                                 lastDir, "Books (*.epub)");
71     if (path == "") {
72         return;
73     }
74
75     // Save directory selected
76     Settings::instance()->setValue("lastdir", QFileInfo(path).absolutePath());
77
78     // Add book to library
79     QModelIndex index = library->find(path);
80     if (index.isValid()) {
81 #ifdef Q_WS_MAEMO_5
82         QMaemo5InformationBox::information(this,
83             tr("This book is already in the library"),
84             QMaemo5InformationBox::DefaultTimeout);
85 #else
86         (void)QMessageBox::information(this, tr("Dorian"),
87             tr("This book is already in the library"), QMessageBox::Ok);
88 #endif // Q_WS_MAEMO_5
89         setSelected(index);
90     }
91     else {
92         library->add(path);
93     }
94 }
95
96 void LibraryDialog::onBookAdded()
97 {
98     Library *library = Library::instance();
99     setSelected(library->index(library->rowCount() - 1));
100 }
101
102 #ifndef Q_WS_MAEMO_5
103
104 void LibraryDialog::onRemove()
105 {
106     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
107     if (current.isValid()) {
108         Book *currentBook = Library::instance()->book(current);
109         QString title = currentBook->name();
110         if (QMessageBox::Yes ==
111             QMessageBox::question(this, tr("Delete book"),
112                 tr("Delete book \"%1\" from library?").
113                     arg(currentBook->shortName()),
114                 QMessageBox::Yes | QMessageBox::No)) {
115             Library::instance()->remove(current);
116         }
117     }
118 }
119
120 void LibraryDialog::onRead()
121 {
122     qDebug() << "LibraryDialog::onRead";
123     QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
124     if (current.isValid()) {
125         Library::instance()->setNowReading(current);
126     }
127 }
128
129 void LibraryDialog::onDetails()
130 {
131     onItemActivated(list->currentIndex());
132 }
133
134 #endif // Q_WS_MAEMO_5
135
136 void LibraryDialog::onItemActivated(const QModelIndex &index)
137 {
138     qDebug() << "LibraryDialog::onItemActivated";
139     QModelIndex libraryIndex = sortedLibrary->mapToSource(index);
140     Book *book = Library::instance()->book(libraryIndex);
141     (new InfoDialog(book, this))->exec();
142 }
143
144 QString LibraryDialog::createItemText(const Book *book)
145 {
146     QString text = book->title + "\n";
147     if (book->creators.size()) {
148         text += book->creators[0];
149         for (int i = 1; i < book->creators.size(); i++) {
150             text += ", " + book->creators[i];
151         }
152     }
153     return text;
154 }
155
156 void LibraryDialog::onCurrentBookChanged()
157 {
158     close();
159 }
160
161 void LibraryDialog::setSelected(const QModelIndex &libraryIndex)
162 {
163     QModelIndex sortedIndex = sortedLibrary->mapFromSource(libraryIndex);
164     list->selectionModel()->clearSelection();
165     if (sortedIndex.isValid()) {
166         list->selectionModel()->select(sortedIndex,
167                                        QItemSelectionModel::Select);
168         list->setCurrentIndex(sortedIndex);
169     }
170 }
171
172 QModelIndex LibraryDialog::selected() const
173 {
174     QModelIndexList selectedItems = list->selectionModel()->selectedIndexes();
175     if (selectedItems.size()) {
176         return sortedLibrary->mapToSource(selectedItems[0]);
177     }
178     return QModelIndex();
179 }
180
181 void LibraryDialog::onShowFolders()
182 {
183     FoldersDialog *folders = new FoldersDialog(this);
184     folders->show();
185 }