Fix forward navigation control on Linux.
[dorian] / platform.cpp
1 #include <QtGlobal>
2 #include <QDir>
3 #include <QtGui>
4
5 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
6 #   include <unistd.h>
7 #endif
8
9 #ifdef Q_OS_SYMBIAN
10 #   include <eikenv.h>
11 #   include <eikappui.h>
12 #   include <aknenv.h>
13 #   include <aknappui.h>
14 #endif
15
16 #include "platform.h"
17 #include "trace.h"
18
19 #if defined(Q_OS_WIN32) || defined(Q_OS_SYMBIAN)
20 #   define DORIAN_BASE "dorian"
21 #else
22 #   define DORIAN_BASE ".dorian"
23 #endif
24
25 #if defined(Q_WS_MAC)
26 #   define DORIAN_ICON_PREFIX ":/icons/mac/"
27 #elif defined(Q_OS_SYMBIAN)
28 #   define DORIAN_ICON_PREFIX ":/icons/symbian/"
29 #elif defined(Q_WS_MAEMO_5)
30 #   define DORIAN_ICON_PREFIX ":/icons/maemo/"
31 #else
32 #   define DORIAN_ICON_PREFIX ":/icons/"
33 #endif
34
35 static const char *DORIAN_VERSION =
36 #include "pkg/version.txt"
37 ;
38
39 #define DORIAN_LOG "dorian.txt"
40
41 #ifdef Q_WS_MAEMO_5
42 #   include <QtMaemo5/QMaemo5InformationBox>
43 #else
44 #   include <QMessageBox>
45 #endif
46
47 static Platform *theInstance;
48
49 Platform *Platform::instance()
50 {
51     if (!theInstance) {
52         theInstance = new Platform();
53     }
54     return theInstance;
55 }
56
57 void Platform::close()
58 {
59     delete theInstance;
60     theInstance = 0;
61 }
62
63 QString Platform::dbPath()
64 {
65     QString base(QDir::home().absoluteFilePath(DORIAN_BASE));
66     return QDir(base).absoluteFilePath("books.db");
67 }
68
69 QString Platform::icon(const QString &name, const QString &extension)
70 {
71     QString iconName = QString(DORIAN_ICON_PREFIX) + name + extension;
72     if (QFile(iconName).exists()) {
73         return iconName;
74     } else {
75         return QString(":/icons/") + name + extension;
76     }
77 }
78
79 void Platform::restart(char *argv[])
80 {
81 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
82     extern char **environ;
83     execve(argv[0], argv, environ);
84 #else
85     Q_UNUSED(argv);
86 #endif
87 }
88
89 QString Platform::version()
90 {
91     return QString(DORIAN_VERSION);
92 }
93
94 QString Platform::downloadDir()
95 {
96 #ifdef Q_OS_SYMBIAN
97     if (QDir("E:/").exists()) {
98         return "E:/Books";
99     }
100     return "C:/Books";
101 #else
102     return QDir::home().absoluteFilePath("Books");
103 #endif
104 }
105
106 QString Platform::defaultFont()
107 {
108 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_X11)
109     return QString("Serif");
110 #elif defined(Q_WS_MAC)
111     return QString("Hoefler Text");
112 #elif defined Q_WS_S60
113     return QString("Nokia Sans S60");
114 #else
115     return QString("Times New Roman");
116 #endif
117 }
118
119 void Platform::information(const QString &label, QWidget *parent)
120 {
121 #ifdef Q_WS_MAEMO_5
122     QMaemo5InformationBox::information(parent, label,
123                                        QMaemo5InformationBox::DefaultTimeout);
124 #else
125     (void)QMessageBox::information(parent, QObject::tr("Dorian"), label,
126                                    QMessageBox::Ok);
127 #endif
128 }
129
130 void Platform::showBusy(QWidget *w, bool isBusy)
131 {
132 #ifdef Q_WS_MAEMO_5
133     w->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, isBusy);
134 #else
135     Q_UNUSED(w);
136     Q_UNUSED(isBusy);
137 #endif
138 }
139
140 QString Platform::traceFileName()
141 {
142     return QDir::home().absoluteFilePath(DORIAN_LOG);
143 }
144
145 int Platform::defaultZoom()
146 {
147     return 150;
148 }
149
150 QString Platform::defaultOrientation()
151 {
152 #ifdef Q_OS_SYMBIAN
153     return QString("portrait");
154 #else
155     return QString("landscape");
156 #endif
157 }
158
159 void Platform::setOrientation(QWidget *widget, const QString &orientation)
160 {
161     TRACE;
162     qDebug() << "To" << orientation;
163
164     Q_UNUSED(widget);
165
166 #if defined(Q_OS_SYMBIAN)
167     CAknAppUi *appUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
168     if (!appUi) {
169         qCritical() << "Platform::setOrientation: Couldn't get AppUi pointer";
170         return;
171     }
172 #endif
173
174     if (orientation == "portrait") {
175 #if defined(Q_WS_MAEMO_5)
176         widget->setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
177         widget->setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
178 #elif defined(Q_OS_SYMBIAN)
179         TRAPD(error,
180               appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait););
181 #endif
182     } else {
183 #if defined(Q_WS_MAEMO_5)
184         widget->setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
185         widget->setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
186 #elif defined(Q_OS_SYMBIAN)
187         TRAPD(error,
188               appUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape););
189 #endif
190     }
191 }
192
193 int Platform::softKeyHeight()
194 {
195 #if defined(Q_OS_SYMBIAN)
196     return 62;
197 #else
198     return 0;
199 #endif
200 }
201
202 int Platform::toolBarIconHeight()
203 {
204 #if defined(Q_OS_SYMBIAN)
205     return 60;
206 #elif defined(Q_WS_X11)  && !defined(Q_WS_MAEMO_5)
207     return 40;
208 #else
209     return 0;
210 #endif
211 }
212
213 QSize Platform::size()
214 {
215     return QApplication::desktop()->geometry().size();
216 }
217
218 QSize Platform::availableSize()
219 {
220     QSize s = QApplication::desktop()->availableGeometry().size();
221 #if defined(Q_OS_SYMBIAN)
222     // Work around a Qt bug on Symbian which sometimes forgets to reduce the
223     // available height by the soft key area height
224     if (s.height() == 548) {
225         s.setHeight(s.height() - softKeyHeight());
226     }
227 #endif
228     return s;
229 }