-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplatform.cpp
executable file
·240 lines (212 loc) · 6.22 KB
/
platform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <QDesktopServices>
#include <QUrl>
#include <QCoreApplication>
#include <QDebug>
#include <qplatformdefs.h>
#include <QDir>
#include <QProcess>
#include <QUuid>
#include <QDesktopServices>
#include <QLocale>
#include <QDateTime>
#if defined(Q_OS_SYMBIAN)
# include <sysutil.h>
# include <f32file.h>
# include <psmclient.h>
# include <psmsettings.h>
#elif defined(MEEGO_EDITION_HARMATTAN)
# include <qmdevicemode.h>
# include <qmsysteminformation.h>
#endif
#include "platform.h"
#include "preferences.h"
#include "trace.h"
#if defined(Q_OS_SYMBIAN)
# define STEPS_BASEDIR "steps"
#elif defined(MEEGO_EDITION_HARMATTAN)
# define STEPS_BASEDIR ".steps"
#else
# error Unknown platform
#endif
#if defined(Q_OS_SYMBIAN)
# define STEPS_OS_NAME "symbian"
#elif defined(MEEGO_EDITION_HARMATTAN)
# define STEPS_OS_NAME "harmattan"
#else
# define STEPS_OS_NAME "unknown"
#endif
/// Minimum free disk space (in K)
const int STEPS_MIN_FREE = 4096;
static Platform *theInstance;
#if defined(Q_OS_SYMBIAN)
class PsmObserver: public MPsmClientObserver {
public:
void PowerSaveModeChangeError(const TInt error) {
qDebug() << "PsmObserver::PowerSaveModeChangeError" << error;
}
void PowerSaveModeChanged(const TPsmsrvMode mode) {
qDebug() << "PsmObserver::PowerSaveModeChanged: To" << (int)mode;
}
};
#endif
Platform::Platform(): QObject() {
#if defined(Q_OS_SYMBIAN)
psmObserver = new PsmObserver;
QT_TRAP_THROWING(psmClient = CPsmClient::NewL(*psmObserver));
#endif
}
Platform::~Platform() {
#if defined(Q_OS_SYMBIAN)
delete psmClient;
delete psmObserver;
#endif
}
Platform *Platform::instance() {
if (!theInstance) {
theInstance = new Platform();
}
return theInstance;
}
void Platform::close() {
delete theInstance;
theInstance = 0;
}
QString Platform::osName() {
return STEPS_OS_NAME;
}
QString Platform::osVersion() {
#if defined(Q_OS_SYMBIAN)
return "3";
#elif defined(MEEGO_EDITION_HARMATTAN)
QString firmwareVersion = MeeGo::QmSystemInformation().valueForKey("/device/sw-release-ver");
if (!firmwareVersion.startsWith("DFL61_HARMATTAN_") || firmwareVersion.length() < 20) {
return "unknown";
}
int majorVersion = firmwareVersion.at(16).digitValue();
int minorVersion = firmwareVersion.at(17).digitValue();
if (majorVersion >= 3) {
return "1.2";
} else if (majorVersion == 2 && minorVersion >= 2) {
return "1.1.1";
} else if (majorVersion == 2) {
return "1.1";
} else if (majorVersion == 1) {
return "1.0";
} else {
return "unknown";
}
#else
return QString();
#endif
}
QString Platform::appVersion() {
return QString(STEPS_VERSION);
}
QUrl Platform::soundUrl(const QString &name) {
QUrl ret;
QString base = QString(STEPS_DATADIR) + "/sounds/" + name;
QString wav = base + ".wav";
QString mp3 = base + ".mp3";
if (QFileInfo(wav).exists()) {
ret = QUrl::fromLocalFile(wav);
} else if (QFileInfo(mp3).exists()) {
ret = QUrl::fromLocalFile(mp3);
} else {
qWarning() << "Platform.soundUrl: No file for" << name << "in" << (QString(STEPS_DATADIR) + "/sounds");
}
return ret;
}
QString Platform::dbPath() {
QString base(QDir::home().absoluteFilePath(STEPS_BASEDIR));
return QDir(base).absoluteFilePath("current.dc");
}
bool Platform::dbFull() {
bool ret = false;
QString base(QDir::home().absoluteFilePath(STEPS_BASEDIR));
#if defined(MEEGO_EDITION_HARMATTAN)
QProcess df;
df.start("/bin/df", QStringList() << "-k" << base);
df.waitForFinished();
QList<QByteArray> lines = df.readAll().split('\n');
if (lines.length() > 1) {
QStringList fields = QString(lines[1]).split(QRegExp("\\s+"));
if (fields.length() > 3) {
ret = fields.at(3).toInt() < STEPS_MIN_FREE;
}
}
#elif defined(Q_OS_SYMBIAN)
TInt drive = base[0].toLower().unicode() - QChar('a').unicode() + EDriveA;
ret = SysUtil::DiskSpaceBelowCriticalLevelL(0, STEPS_MIN_FREE * 1024, drive);
#endif
if (ret) {
qCritical() << "Platform::dbFull: Disk full";
}
return ret;
}
QString Platform::deviceId() {
QString id = Preferences::instance()->value("deviceid").toString();
if (!id.length()) {
id = QUuid::createUuid().toString();
Preferences::instance()->setValue("deviceid", id);
}
return id;
}
void Platform::setSavePower(bool v) {
#if defined(MEEGO_EDITION_HARMATTAN)
if (MeeGo::QmDeviceMode().setPSMState(v? MeeGo::QmDeviceMode::PSMStateOn: MeeGo::QmDeviceMode::PSMStateOff)) {
emit savePowerChanged();
}
#elif defined(Q_OS_SYMBIAN)
psmClient->ChangePowerSaveMode(v? EPsmsrvModePowerSave: EPsmsrvModeNormal);
emit savePowerChanged();
#endif
}
bool Platform::savePower() {
#if defined(MEEGO_EDITION_HARMATTAN)
return MeeGo::QmDeviceMode().getPSMState() == MeeGo::QmDeviceMode::PSMStateOn;
#elif defined(Q_OS_SYMBIAN)
TInt mode;
(void)psmClient->PsmSettings().GetCurrentMode(mode);
return mode == EPsmsrvModePowerSave;
#else
return false;
#endif
}
QString Platform::text(const QString &key) {
QString locale = QLocale::system().name();
QFile resource(":/texts/" + locale + "/" + key);
if (!resource.open(QFile::ReadOnly)) {
resource.setFileName(":/texts/" + locale.left(2) + "/" + key);
if (!resource.open(QFile::ReadOnly)) {
resource.setFileName(":/texts/" + key);
(void)resource.open(QFile::ReadOnly);
}
}
QString ret = QString::fromUtf8(resource.readAll().constData());
resource.close();
return ret;
}
QString Platform::traceFileName() const {
#if defined(Q_OS_SYMBIAN)
return "e:/data/steps.log";
#else
return QDir(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)).absoluteFilePath("steps.log");
#endif
}
void Platform::traceToFile(bool enable) {
Trace::setFileName(enable? traceFileName(): "");
}
void Platform::deleteTraceFile() {
Trace::setFileName("");
QFile(traceFileName()).remove();
}
qint64 Platform::time() {
return QDateTime::currentMSecsSinceEpoch() / 1000;
}
QString Platform::buildDate() {
return __DATE__ " " __TIME__;
}