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