6 # include <QtGui/QX11Info>
8 # include <X11/Xatom.h>
9 # include <mce/mode-names.h>
10 # include <mce/dbus-names.h>
11 #endif // Q_WS_MAEMO_5
16 #include "infodialog.h"
17 #include "librarydialog.h"
19 #include "mainwindow.h"
20 #include "settingswindow.h"
21 #include "bookmarksdialog.h"
23 #include "chaptersdialog.h"
24 #include "fullscreenwindow.h"
26 #include "bookfinder.h"
29 #include "translucentbutton.h"
31 #include "progressdialog.h"
32 #include "sortedlibrary.h"
34 #ifdef DORIAN_TEST_MODEL
35 # include "modeltest.h"
38 MainWindow::MainWindow(QWidget *parent):
39 AdopterWindow(parent), view(0), preventBlankingTimer(-1)
43 setAttribute(Qt::WA_Maemo5StackedWindow, true);
46 // Central widget. Must be an intermediate, because the book view widget
47 // might be re-parented later
48 QFrame *central = new QFrame(this);
49 QVBoxLayout *layout = new QVBoxLayout(central);
51 central->setLayout(layout);
52 setCentralWidget(central);
55 view = new BookView(this);
59 chaptersAction = addToolBarAction(this, SLOT(showChapters()),
60 "chapters", tr("Chapters"), true);
61 bookmarksAction = addToolBarAction(this, SLOT(showBookmarks()),
62 "bookmarks", tr("Bookmarks"), true);
63 libraryAction = addToolBarAction(this, SLOT(showLibrary()),
64 "library", tr("Library"), false);
67 settingsAction = menuBar()->addAction(tr("Settings"));
68 connect(settingsAction, SIGNAL(triggered()), this, SLOT(showSettings()));
69 devToolsAction = menuBar()->addAction(tr("Developer"));
70 connect(devToolsAction, SIGNAL(triggered()), this, SLOT(showDevTools()));
72 settingsAction = addToolBarAction(this, SLOT(showSettings()),
73 "preferences-system", tr("Settings"));
74 devToolsAction = addToolBarAction(this, SLOT(showDevTools()),
75 "developer", tr("Developer"));
78 rotateAction = addToolBarAction(this, SLOT(rotate()),
79 "rotate", tr("Rotate"), true);
81 fullScreenAction = addToolBarAction(this, SLOT(showBig()),
82 "view-fullscreen", tr("Full screen"), true);
84 #if defined(Q_WS_MAEMO_5)
85 QAction *aboutAction = menuBar()->addAction(tr("About"));
86 connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
88 addToolBarAction(this, SLOT(about()), "about", tr("About"));
89 #endif // defined(Q_WS_MAEMO_5)
91 #if defined(Q_OS_SYMBIAN)
92 (void)addToolBarAction(this, SLOT(close()), "", tr("Exit"), false);
93 #endif // defined(Q_OS_SYMBIAN)
96 prev = new TranslucentButton("back", this);
97 next = new TranslucentButton("forward", this);
98 prog = new Progress(this);
100 // Handle model changes
101 connect(Library::instance(), SIGNAL(nowReadingChanged()),
102 this, SLOT(onCurrentBookChanged()));
104 // Load library, upgrade it if needed
105 libraryProgress = new ProgressDialog(tr("Upgrading library"), this);
106 Library *library = Library::instance();
107 connect(library, SIGNAL(beginUpgrade(int)),
108 this, SLOT(onBeginUpgrade(int)));
109 connect(library, SIGNAL(upgrading(const QString &)),
110 this, SLOT(onUpgrading(const QString &)));
111 connect(library, SIGNAL(endUpgrade()), this, SLOT(onEndUpgrade()));
113 // Handle loading book parts
114 connect(view, SIGNAL(partLoadStart(int)), this, SLOT(onPartLoadStart()));
115 connect(view, SIGNAL(partLoadEnd(int)), this, SLOT(onPartLoadEnd(int)));
118 connect(view, SIGNAL(progress(qreal)), prog, SLOT(setProgress(qreal)));
120 // Shadow window for full screen reading
121 fullScreenWindow = new FullScreenWindow();
122 connect(fullScreenWindow, SIGNAL(restore()), this, SLOT(showRegular()));
124 // Handle settings changes
125 connect(Settings::instance(), SIGNAL(valueChanged(const QString &)),
126 this, SLOT(onSettingsChanged(const QString &)));
128 // Handle book view buttons
129 connect(next, SIGNAL(triggered()), this, SLOT(goToNextPage()));
130 connect(prev, SIGNAL(triggered()), this, SLOT(goToPreviousPage()));
132 #ifdef DORIAN_TEST_MODEL
133 (void)new ModelTest(Library::instance(), this);
137 MainWindow::~MainWindow()
139 delete fullScreenWindow;
142 void MainWindow::initialize()
145 Library *library = Library::instance();
147 // Show in regular (non full-screen) mode
150 // Upgrade library if needed, then load it
154 // Load book on command line, or load last read book, or load default book
155 if (QCoreApplication::arguments().size() == 2) {
156 QString path = QCoreApplication::arguments()[1];
158 QModelIndex index = library->find(path);
159 if (index.isValid()) {
160 library->setNowReading(index);
163 QModelIndex index = library->nowReading();
164 if (index.isValid()) {
165 library->setNowReading(index);
167 if (!library->rowCount()) {
168 library->add(":/books/2BR02B.epub");
170 SortedLibrary sorted;
171 library->setNowReading(sorted.mapToSource(sorted.index(0, 0)));
176 void MainWindow::onCurrentBookChanged()
179 setCurrentBook(Library::instance()->nowReading());
182 void MainWindow::showRegular()
186 view->setLastBookmark();
187 fullScreenWindow->leaveBookView();
188 takeBookView(view, prog, prev, next);
190 fullScreenWindow->hide();
192 view->scheduleRestoreLastBookmark();
195 void MainWindow::showBig()
199 view->setLastBookmark();
201 fullScreenWindow->takeBookView(view, prog, prev, next);
203 #if defined(Q_WS_MAEMO_5)
204 fullScreenWindow->raise();
208 fullScreenWindow->showFullScreen();
209 view->scheduleRestoreLastBookmark();
212 void MainWindow::setCurrentBook(const QModelIndex ¤t)
215 Book *book = Library::instance()->book(current);
217 setWindowTitle(book? book->shortName(): tr("Dorian"));
220 void MainWindow::showLibrary()
222 (new LibraryDialog(this))->show();
225 void MainWindow::showSettings()
227 (new SettingsWindow(this))->show();
230 void MainWindow::rotate()
232 QString current = Settings::instance()->value("orientation",
233 Platform::instance()->defaultOrientation()).toString();
234 QString target = (current == "landscape")? "portrait": "landscape";
235 view->setLastBookmark();
236 Settings::instance()->setValue("orientation", target);
239 void MainWindow::showDevTools()
241 (new DevTools(this))->exec();
244 void MainWindow::showBookmarks()
246 Book *book = Library::instance()->book(mCurrent);
248 BookmarksDialog *bookmarks = new BookmarksDialog(book, this);
249 connect(bookmarks, SIGNAL(addBookmark(const QString &)),
250 this, SLOT(onAddBookmark(const QString &)));
251 connect(bookmarks, SIGNAL(goToBookmark(int)),
252 this, SLOT(onGoToBookmark(int)));
257 void MainWindow::onSettingsChanged(const QString &key)
260 qDebug() << "Key" << key;
262 if (key == "orientation") {
263 QString value = Settings::instance()->value(key,
264 Platform::instance()->defaultOrientation()).toString();
265 qDebug() << "Value: orientation" << value;
266 Platform::instance()->setOrientation(this, value);
267 Platform::instance()->setOrientation(fullScreenWindow, value);
270 #if defined(Q_WS_MAEMO_5)
271 else if (key == "lightson") {
272 bool enable = Settings::instance()->value(key, false).toBool();
273 killTimer(preventBlankingTimer);
275 preventBlankingTimer = startTimer(29 * 1000);
278 #endif // defined(Q_WS_MAEMO_5)
281 void MainWindow::onPartLoadStart()
284 Platform::instance()->showBusy(this, true);
287 void MainWindow::onPartLoadEnd(int index)
290 bool enablePrevious = false;
291 bool enableNext = false;
292 Book *book = Library::instance()->book(mCurrent);
295 enablePrevious = true;
297 if (index < (book->parts.size() - 1)) {
301 Platform::instance()->showBusy(this, false);
304 void MainWindow::onAddBookmark(const QString ¬e)
307 view->addBookmark(note);
308 Platform::instance()->information(tr("Bookmarked current position"), this);
311 void MainWindow::onGoToBookmark(int index)
314 Book *book = Library::instance()->book(mCurrent);
315 view->goToBookmark(book->bookmarks()[index]);
318 void MainWindow::showChapters()
320 Book *book = Library::instance()->book(mCurrent);
322 ChaptersDialog *chapters = new ChaptersDialog(book, this);
323 connect(chapters, SIGNAL(goToChapter(int)),
324 this, SLOT(onGoToChapter(int)));
329 void MainWindow::onGoToChapter(int index)
333 Book *book = Library::instance()->book(mCurrent);
336 int partIndex = book->partFromChapter(index, fragment);
337 if (partIndex != -1) {
338 view->goToPart(partIndex, fragment);
343 void MainWindow::timerEvent(QTimerEvent *event)
345 if (event->timerId() == preventBlankingTimer) {
347 QDBusInterface mce(MCE_SERVICE, MCE_REQUEST_PATH,
348 MCE_REQUEST_IF, QDBusConnection::systemBus());
349 mce.call(MCE_PREVENT_BLANK_REQ);
350 #endif // Q_WS_MAEMO_5
351 qDebug() << "MainWindow::timerEvent: Prevent display blanking";
353 AdopterWindow::timerEvent(event);
356 void MainWindow::about()
358 Dyalog *aboutDialog = new Dyalog(this, false);
359 aboutDialog->setWindowTitle(tr("About Dorian"));
360 QString version = Platform::instance()->version();
361 #if !defined(Q_WS_MAEMO_5) && !defined(Q_OS_SYMBIAN)
362 QLabel *icon = new QLabel(aboutDialog);
363 QPixmap pixmap(Platform::instance()->icon("dorian"));
364 icon->setPixmap(pixmap);
365 aboutDialog->addWidget(icon);
367 QLabel *label = new QLabel(aboutDialog);
368 label->setTextFormat(Qt::RichText);
369 label->setOpenExternalLinks(true);
370 label->setWordWrap(true);
371 label->setText(tr("<b>Dorian %1</b><br><br>"
372 "Copyright © 2010-2011 by "
373 "Akos Polster <akos@pipacs.com><br><br>"
374 "Licensed under GNU General Public License, Version 3<br><br>"
375 "<a href='http://dorian.garage.maemo.org/'>"
376 "dorian.garage.maemo.org</a><br><br>"
378 aboutDialog->addWidget(label);
379 aboutDialog->addStretch();
383 void MainWindow::goToNextPage()
390 void MainWindow::goToPreviousPage()
394 view->goPreviousPage();
397 void MainWindow::onBeginUpgrade(int total)
399 libraryProgress->setVisible(total > 0);
400 libraryProgress->setWindowTitle(tr("Upgrading library"));
401 libraryProgress->setMaximum(total);
404 void MainWindow::onUpgrading(const QString &path)
406 libraryProgress->setLabelText(tr("Upgrading %1").
407 arg(QFileInfo(path).fileName()));
408 libraryProgress->setValue(libraryProgress->value() + 1);
411 void MainWindow::onEndUpgrade()
413 libraryProgress->hide();
414 libraryProgress->reset();
417 void MainWindow::onBeginLoad(int total)
419 libraryProgress->setVisible(total > 0);
420 libraryProgress->setWindowTitle(tr("Loading library"));
421 libraryProgress->setMaximum(total);
424 void MainWindow::onLoading(const QString &path)
426 libraryProgress->setLabelText(tr("Loading %1").
427 arg(QFileInfo(path).fileName()));
428 libraryProgress->setValue(libraryProgress->value() + 1);
431 void MainWindow::onEndLoad()
433 libraryProgress->hide();
434 libraryProgress->reset();