Improve traces. Simplify folder management.
[dorian] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QDir>
4 #include <QApplication>
5 #include <QFileInfo>
6 #include <QStringList>
7
8 #ifdef Q_WS_MAEMO_5
9 #   include <QtMaemo5/QMaemo5InformationBox>
10 #   include <QtDBus>
11 #   include <QtGui/QX11Info>
12 #   include <X11/Xlib.h>
13 #   include <X11/Xatom.h>
14 #   include <mce/mode-names.h>
15 #   include <mce/dbus-names.h>
16 #endif // Q_WS_MAEMO_5
17
18 #include "bookview.h"
19 #include "book.h"
20 #include "library.h"
21 #include "infodialog.h"
22 #include "librarydialog.h"
23 #include "devtools.h"
24 #include "mainwindow.h"
25 #include "settingswindow.h"
26 #include "bookmarksdialog.h"
27 #include "settings.h"
28 #include "chaptersdialog.h"
29 #include "fullscreenwindow.h"
30 #include "trace.h"
31 #include "bookfinder.h"
32 #include "progress.h"
33 #include "dyalog.h"
34
35 #ifdef DORIAN_TEST_MODEL
36 #include "modeltest.h"
37 #endif
38
39 #ifdef Q_WS_MAC
40 #   define ICON_PREFIX ":/icons/mac/"
41 #else
42 #   define ICON_PREFIX ":/icons/"
43 #endif
44
45 const int PROGRESS_HEIGHT = 17;
46 const char *DORIAN_VERSION =
47 #include "pkg/version.txt"
48 ;
49
50 MainWindow::MainWindow(QWidget *parent):
51     BookWindow(parent), view(0), preventBlankingTimer(-1)
52 {
53     Trace t("MainWindow::MainWindow");
54 #ifdef Q_WS_MAEMO_5
55     setAttribute(Qt::WA_Maemo5StackedWindow, true);
56 #endif
57     setWindowTitle("Dorian");
58
59     // Central widget. Must be an intermediate, because the book view widget
60     // can be re-parented later
61     QFrame *central = new QFrame(this);
62     QVBoxLayout *layout = new QVBoxLayout(central);
63     layout->setMargin(0);
64     central->setLayout(layout);
65     setCentralWidget(central);
66
67     // Book view
68     view = new BookView(central);
69     view->show();
70     layout->addWidget(view);
71
72     // Progress
73     progress = new Progress(central);
74
75     // Tool bar
76     setUnifiedTitleAndToolBarOnMac(true);
77     settings = new QDialog(this);
78     toolBar = addToolBar("controls");
79     toolBar->setMovable(false);
80     toolBar->setFloatable(false);
81     toolBar->toggleViewAction()->setVisible(false);
82 #if defined(Q_WS_X11) && !defined(Q_WS_MAEMO_5)
83     toolBar->setIconSize(QSize(42, 42));
84 #endif
85
86     previousAction = addToolBarAction(view, SLOT(goPrevious()), "previous");
87     nextAction = addToolBarAction(view, SLOT(goNext()), "next");
88     chaptersAction = addToolBarAction(this, SLOT(showChapters()), "chapters");
89     bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()), "bookmarks");
90
91 #ifdef Q_WS_MAEMO_5
92     infoAction = menuBar()->addAction(tr("Book details"));
93     connect(infoAction, SIGNAL(triggered()), this, SLOT(showInfo()));
94     libraryAction = menuBar()->addAction(tr("Library"));
95     connect(libraryAction, SIGNAL(triggered()), this, SLOT(showLibrary()));
96     settingsAction = menuBar()->addAction(tr("Settings"));
97     connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
98     devToolsAction = menuBar()->addAction(tr("Developer"));
99     connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
100     QAction *aboutAction = menuBar()->addAction(tr("About"));
101     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
102 #else
103     infoAction = addToolBarAction(this, SLOT(showInfo()), "document-properties");
104     libraryAction = addToolBarAction(this, SLOT(showLibrary()),
105                                      "system-file-manager");
106     settingsAction = addToolBarAction(this, SLOT(showSettings()),
107                                       "preferences-system");
108     devToolsAction = addToolBarAction(this, SLOT(showDevTools()), "developer");
109     addToolBarAction(this, SLOT(about()), "about");
110 #endif // Q_WS_MAEMO_5
111
112     QFrame *frame = new QFrame(toolBar);
113     frame->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
114     toolBar->addWidget(frame);
115
116     fullScreenAction = addToolBarAction(this, SLOT(showBig()), "view-fullscreen");
117
118     // Handle model changes
119     connect(Library::instance(), SIGNAL(nowReadingChanged()),
120             this, SLOT(onCurrentBookChanged()));
121
122     // Load book on command line, or load last read book, or load default book
123     Library *library = Library::instance();
124     if (QCoreApplication::arguments().size() == 2) {
125         QString path = QCoreApplication::arguments()[1];
126         library->add(path);
127         QModelIndex index = library->find(path);
128         if (index.isValid()) {
129             library->setNowReading(index);
130         }
131     }
132     else {
133         QModelIndex index = library->nowReading();
134         if (index.isValid()) {
135             library->setNowReading(index);
136         }
137         else {
138             if (!library->rowCount()) {
139                 library->add(":/books/2 B R 0 2 B.epub");
140             }
141             library->setNowReading(library->index(0));
142         }
143     }
144
145     // Handle loading book parts
146     connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
147     connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
148
149     // Handle progress
150     connect(view, SIGNAL(progress(qreal)), progress, SLOT(setProgress(qreal)));
151
152     // Shadow window for full screen
153     fullScreenWindow = new FullScreenWindow(this);
154     connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
155
156     // Handle settings changes
157     Settings *settings = Settings::instance();
158     connect(settings, SIGNAL(valueChanged(const QString &)),
159             this, SLOT(onSettingsChanged(const QString &)));
160     settings->setValue("orientation", settings->value("orientation"));
161     settings->setValue("lightson", settings->value("lightson"));
162     settings->setValue("usevolumekeys", settings->value("usevolumekeys"));
163
164 #ifdef DORIAN_TEST_MODEL
165     (void)new ModelTest(Library::instance(), this);
166 #endif
167 }
168
169 MainWindow::~MainWindow()
170 {
171 }
172
173 void MainWindow::onCurrentBookChanged()
174 {
175     setCurrentBook(Library::instance()->nowReading());
176 }
177
178 void MainWindow::showRegular()
179 {
180     Trace t("MainWindow::showRegular");
181     fullScreenWindow->hide();
182     fullScreenWindow->leaveChildren();
183     progress->setGeometry(0, 0, geometry().width(), PROGRESS_HEIGHT);
184     QList<QWidget *> otherChildren;
185     otherChildren.append(progress);
186     takeChildren(view, otherChildren);
187     progress->flash();
188 }
189
190 void MainWindow::showBig()
191 {
192     Trace t("MainWindow::showBig");
193     leaveChildren();
194     QList<QWidget *> otherChildren;
195     otherChildren.append(progress);
196     progress->setGeometry(0, 0, QApplication::desktop()->screenGeometry().width(),
197                           PROGRESS_HEIGHT);
198     fullScreenWindow->takeChildren(view, otherChildren);
199     fullScreenWindow->showFullScreen();
200     progress->flash();
201 }
202
203 void MainWindow::setCurrentBook(const QModelIndex &current)
204 {
205     mCurrent = current;
206     Book *book = Library::instance()->book(current);
207     view->setBook(book);
208     setWindowTitle(book? book->shortName(): tr("Dorian"));
209 }
210
211 QAction *MainWindow::addToolBarAction(const QObject *receiver,
212                                       const char *member,
213                                       const QString &name)
214 {
215     return toolBar->
216         addAction(QIcon(ICON_PREFIX + name + ".png"), "", receiver, member);
217 }
218
219 void MainWindow::showLibrary()
220 {
221     (new LibraryDialog(this))->show();
222 }
223
224 void MainWindow::showSettings()
225 {
226     (new SettingsWindow(this))->show();
227 }
228
229 void MainWindow::showInfo()
230 {
231     if (mCurrent.isValid()) {
232         (new InfoDialog(Library::instance()->book(mCurrent), this))->exec();
233     }
234 }
235
236 void MainWindow::showDevTools()
237 {
238     (new DevTools())->exec();
239 }
240
241 void MainWindow::showBookmarks()
242 {
243     Book *book = Library::instance()->book(mCurrent);
244     if (book) {
245         BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
246         bookmarks->setWindowModality(Qt::WindowModal);
247         connect(bookmarks, SIGNAL(addBookmark()), this, SLOT(onAddBookmark()));
248         connect(bookmarks, SIGNAL(goToBookmark(int)),
249                 this, SLOT(onGoToBookmark(int)));
250         bookmarks->show();
251     }
252 }
253
254 void MainWindow::closeEvent(QCloseEvent *event)
255 {
256     Trace t("MainWindow::closeEvent");
257     view->setLastBookmark();
258     event->accept();
259 }
260
261 void MainWindow::onSettingsChanged(const QString &key)
262 {
263 #ifdef Q_WS_MAEMO_5
264     if (key == "orientation") {
265         QString value = Settings::instance()->value(key).toString();
266         qDebug() << "MainWindow::onSettingsChanged: orientation" << value;
267         if (value == "portrait") {
268             setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
269             setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
270         }
271         else {
272             setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
273             setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
274         }
275     } else if (key == "lightson") {
276         bool enable = Settings::instance()->value(key, false).toBool();
277         qDebug() << "MainWindow::onSettingsChanged: lightson:" << enable;
278         killTimer(preventBlankingTimer);
279         if (enable) {
280             preventBlankingTimer = startTimer(29 * 1000);
281         }
282     } else if (key == "usevolumekeys") {
283         bool value = Settings::instance()->value(key).toBool();
284         qDebug() << "MainWindow::onSettingsChanged: usevolumekeys" << value;
285         grabZoomKeys(value);
286         fullScreenWindow->grabZoomKeys(value);
287     }
288 #else
289     Q_UNUSED(key);
290 #endif // Q_WS_MAEMO_5
291 }
292
293 void MainWindow::onPartLoadStart()
294 {
295     Trace t("MainWindow::onPartLoadStart");
296 #ifdef Q_WS_MAEMO_5
297     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
298 #endif
299 }
300
301 void MainWindow::onPartLoadEnd(int index)
302 {
303     Trace t("MainWindow::onPartLoadEnd");
304     bool enablePrevious = false;
305     bool enableNext = false;
306     Book *book = Library::instance()->book(mCurrent);
307     if (book) {
308         if (index > 0) {
309             enablePrevious = true;
310         }
311         if (index < (book->parts.size() - 1)) {
312             enableNext = true;
313         }
314     }
315 #ifdef Q_WS_MAEMO_5
316     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
317     previousAction->setIcon(QIcon(enablePrevious?
318         ":/icons/previous.png" : ":/icons/previous-disabled.png"));
319     nextAction->setIcon(QIcon(enableNext?
320         ":/icons/next.png": ":/icons/next-disabled.png"));
321 #endif // Q_WS_MAEMO_5
322     previousAction->setEnabled(enablePrevious);
323     nextAction->setEnabled(enableNext);
324 }
325
326 void MainWindow::onAddBookmark()
327 {
328     Trace t("MainWindow:onAddBookmark");
329     view->addBookmark();
330 }
331
332 void MainWindow::onGoToBookmark(int index)
333 {
334     Trace t("MainWindow::onGoToBookmark");
335     Book *book = Library::instance()->book(mCurrent);
336     view->goToBookmark(book->bookmarks()[index]);
337 }
338
339 void MainWindow::showChapters()
340 {
341     Book *book = Library::instance()->book(mCurrent);
342     if (book) {
343         ChaptersDialog *chapters = new ChaptersDialog(book, this);
344         chapters->setWindowModality(Qt::WindowModal);
345         connect(chapters, SIGNAL(goToChapter(int)),
346                 this, SLOT(onGoToChapter(int)));
347         chapters->show();
348     }
349 }
350
351 void MainWindow::onGoToChapter(int index)
352 {
353     Trace t("MainWindow::onGoToChapter");
354
355     Book *book = Library::instance()->book(mCurrent);
356     if (book) {
357         int partIndex = book->partFromChapter(index);
358         if (partIndex != -1) {
359             view->goToBookmark(Book::Bookmark(partIndex, 0));
360         }
361     }
362 }
363
364 void MainWindow::timerEvent(QTimerEvent *event)
365 {
366     if (event->timerId() == preventBlankingTimer) {
367 #ifdef Q_WS_MAEMO_5
368         QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
369                            MCE_REQUEST_IF, QDBusConnection::systemBus());
370         mce.call(MCE_PREVENT_BLANK_REQ);
371 #endif // Q_WS_MAEMO_5
372         qDebug() << "MainWindow::timerEvent: Prevent display blanking";
373     }
374 }
375
376 void MainWindow::resizeEvent(QResizeEvent *e)
377 {
378     progress->setGeometry(QRect(0, 0, e->size().width(), PROGRESS_HEIGHT));
379     QMainWindow::resizeEvent(e);
380 }
381
382 void MainWindow::about()
383 {
384     Dyalog *aboutDialog = new Dyalog(this);
385     aboutDialog->setWindowTitle(tr("About Dorian"));
386     QLabel *label = new QLabel(aboutDialog);
387     label->setTextFormat(Qt::RichText);
388     label->setOpenExternalLinks(true);
389     label->setText(tr("<b>Dorian %1</b><br><br>Copyright &copy; 2010 by "
390         "Akos Polster &lt;akos@pipacs.com&gt;<br>"
391         "Licensed under GNU General Public License, Version 3<br>"
392         "Source code: <a href='https://garage.maemo.org/projects/dorian/'>"
393         "garage.maemo.org/projects/dorian</a>").arg(DORIAN_VERSION));
394     aboutDialog->addWidget(label);
395     aboutDialog->show();
396 }