/
sailfishapp_priv.cpp
158 lines (130 loc) · 4.48 KB
/
sailfishapp_priv.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
/**
*
* Copyright (C) 2013 - 2014 Jolla Ltd.
* Contact: Thomas Perl <thomas.perl@jollamobile.com>
* All rights reserved.
*
* This file is part of libsailfishapp
*
* You may use this file under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
**/
#include "sailfishapp_priv.h"
#include <QCoreApplication>
#include <QGuiApplication>
#include <QQmlEngine>
#include <QString>
#include <QFileInfo>
#include <QDir>
#include <QLocale>
#include <QTranslator>
#include <QQuickView>
#include <MDesktopEntry>
#include <QStandardPaths>
static QString applicationPath()
{
QString argv0 = QCoreApplication::arguments()[0];
if (argv0.startsWith(QChar('/'))) {
// First, try argv[0] if it's an absolute path (needed for booster)
return argv0;
} else {
// If that doesn't give an absolute path, use /proc-based detection
return QCoreApplication::applicationFilePath();
}
}
static QString customAppName;
namespace SailfishAppPriv
{
void _PrivateAPI_DoNotUse_setAppName(QString appName)
{
// Set custom app name - used by the launcher
customAppName = appName;
}
QString appName()
{
if (!customAppName.isNull()) {
return customAppName;
}
QFileInfo exe = QFileInfo(applicationPath());
return exe.fileName();
}
QString dataDir()
{
QFileInfo exe = QFileInfo(applicationPath());
// "/usr/bin/<appname>" --> "/usr/share/<appname>/"
QString path = exe.absolutePath();
int i = path.lastIndexOf(QChar('/')) + 1;
return path.replace(i, path.length() - i, "share/") + appName();
}
QGuiApplication *
configureApp(QGuiApplication *app)
{
// We need to set the appName, so the following features will use
// the right application name for the data directory:
//
// - QStandardPaths (::DataLocation, ::CacheLocation)
// - QSettings (with default organization/application name)
// - Qt Quick Local Storage (QQmlEngine::offlineStoragePath())
QString name = appName();
MDesktopEntry entry(QStandardPaths::locate(
QStandardPaths::ApplicationsLocation, appName() + ".desktop"));
if (entry.isSandboxed()) {
const auto section = QStringLiteral("X-Sailjail");
QString organizationName = entry.value(section, "OrganizationName");
if (organizationName.isEmpty())
organizationName = name;
app->setOrganizationName(organizationName);
app->setOrganizationDomain(organizationName);
QString applicationName = entry.value(section, "ApplicationName");
app->setApplicationName(
applicationName.isEmpty() ? name : applicationName);
} else {
app->setOrganizationName(name);
app->setOrganizationDomain(name);
app->setApplicationName(name);
}
// Automatic i18n support. Translations are supposed to be named
// "<appname>-<lang>.qm" in "/usr/share/<appname>/translations/"
QString translations = QDir(dataDir()).filePath("translations");
if (QDir(translations).exists()) {
QTranslator *translator = new QTranslator();
// Firstly try to load "<appname>-<lang>.qm" then "<appname>.qm"
translator->load(QLocale::system(), name, "-", translations, ".qm");
app->installTranslator(translator);
}
return app;
}
QQuickView *
configureView(QQuickView *view)
{
if (!view) {
qWarning("View is NULL while trying to configure view");
return NULL;
}
MDesktopEntry entry(QStandardPaths::locate(
QStandardPaths::ApplicationsLocation, appName() + ".desktop"));
if (entry.isValid()) {
view->setTitle(entry.name());
}
QObject::connect(view->engine(), &QQmlEngine::quit,
qApp, &QGuiApplication::quit);
if (_PrivateAPI_DoNotUse_onViewCreated) {
_PrivateAPI_DoNotUse_onViewCreated(view);
}
return view;
}
}