File: main.cpp

package info (click to toggle)
lomiri-terminal-app 2.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,780 kB
  • sloc: javascript: 415; cpp: 361; python: 160; makefile: 15
file content (254 lines) | stat: -rw-r--r-- 10,135 bytes parent folder | download
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright: 2013 - 2014 Canonical, Ltd
 *
 * This file is part of lomiri-terminal-app
 *
 * lomiri-terminal-app is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * lomiri-terminal-app 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
 *          Riccardo Padovani <rpadovani@ubuntu.com>
 *          David Planella <david.planella@ubuntu.com>
 *          Florian Boucault <florian.boucault@canonical.com>
 */

#include <QApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QtQml>
#include <QLibrary>
#include <QDir>
#include <QProcess>

#include "config.h"
#include "fileio.h"
#include "fonts.h"
#include "shortcuts.h"
#include "standardpaths.h"

#include <QDebug>

QString getNamedArgument(QStringList args, QString name, QString defaultName)
{
    int index = args.indexOf(name);
    return (index != -1) ? args[index + 1] : QString(defaultName);
}
QStringList getProfileFromDir(const QString &path) {
    QDir layoutDir(path);
    layoutDir.setNameFilters(QStringList("*.json"));

    QStringList jsonFiles = layoutDir.entryList();

    QStringList result;
    foreach (QString s, jsonFiles) {
        result.append(s.prepend(path));
    }
    return result;
}

bool sshdRunning() {
    QProcess process;
    QString pgm("pgrep");
    QStringList args = QStringList() << "sshd";
    process.start(pgm, args);
    process.waitForReadyRead();
    return !process.readAllStandardOutput().isEmpty();
}

#define MAKE_SINGLETON_FACTORY(type) \
    static QObject* type##_singleton_factory(QQmlEngine* engine, QJSEngine* scriptEngine) { \
        Q_UNUSED(engine); \
        Q_UNUSED(scriptEngine); \
        return new type(); \
    }

MAKE_SINGLETON_FACTORY(FileIO)
MAKE_SINGLETON_FACTORY(Fonts)
MAKE_SINGLETON_FACTORY(Shortcuts)
MAKE_SINGLETON_FACTORY(StandardPaths)

int main(int argc, char *argv[])
{
    QLoggingCategory::setFilterRules ("qt.xkb.compose=false");

    QApplication a(argc, argv);
    QQmlApplicationEngine engine;

    qmlRegisterSingletonType<FileIO>("Terminal", 0, 1, "FileIO", FileIO_singleton_factory);
    qmlRegisterSingletonType<Fonts>("Terminal", 0, 1, "Fonts", Fonts_singleton_factory);
    qmlRegisterSingletonType<Shortcuts>("Terminal", 0, 1, "Shortcuts", Shortcuts_singleton_factory);
    qmlRegisterSingletonType<StandardPaths>("Terminal", 0, 1, "StandardPaths", StandardPaths_singleton_factory);

    // Set up import paths
    QStringList importPathList = engine.importPathList();
    // Prepend the location of the plugin in the build dir,
    // so that Qt Creator finds it there, thus overriding the one installed
    // in the sistem if there is one
    importPathList.prepend(QCoreApplication::applicationDirPath() + "/../plugin/");

    // Setup useful environmental variables.
    if (!importPathList.empty()){
        QString cs, kbl;

        foreach (QString pwd, importPathList) {
            cs  = pwd + "/QMLTermWidget/color-schemes";
            kbl = pwd + "/QMLTermWidget/kb-layouts";
            if (QDir(cs).exists()) break;
        }

        setenv("KB_LAYOUT_DIR",kbl.toUtf8().constData(),1);
        setenv("COLORSCHEMES_DIR",cs.toUtf8().constData(),1);
    }
    setenv("TERM", QString("xterm").toUtf8().data(), 0);


    QStringList args = a.arguments();
    if (args.contains("-h") || args.contains("--help")) {
        qDebug() << "usage: " + args.at(0) + " [-h|--help] [-I <path>]";
        qDebug() << "    --forceAuth <true|false> Force authentication on or off.";
        qDebug() << "    -h|--help     Print this help.";
        qDebug() << "    -I <path>     Give a path for an additional QML import directory. May be used multiple times.";
        qDebug() << "    -q <qmlfile>  Give an alternative location for the main qml file.";
        qDebug() << "    --ssh     Run a ssh session on local host instead of default bash.";
        return 0;
    }

    // Desktop doesn't have yet Unity8 and so no unity greeter either. Consequently it doesn't
    // also have any "PIN code" or "Password" extra authentication. Don't require any extra
    // authentication there by default
    if (!qgetenv("QT_QPA_PLATFORM").contains(QByteArrayLiteral("ubuntumirclient"))) {
        qDebug() << Q_FUNC_INFO << "Running on non-MIR desktop, not requiring authentication by default";
        engine.rootContext()->setContextProperty("noAuthentication", QVariant(true));
    } else {
        engine.rootContext()->setContextProperty("noAuthentication", QVariant(false));
    }

    QString qmlfile;
    for (int i = 0; i < args.count(); i++) {
        if (args.at(i) == "-I" && args.count() > i + 1) {
            QString addedPath = args.at(i+1);
            if (addedPath.startsWith('.')) {
                addedPath = addedPath.right(addedPath.length() - 1);
                addedPath.prepend(QDir::currentPath());
            }
            importPathList.append(addedPath);
        } else if (args.at(i) == "-q" && args.count() > i + 1) {
            qmlfile = args.at(i+1);
        } else if (args.at(i) == "--forceAuth" && args.count() > i + 1) {
            QString value = args.at(i+1);
            if (value == "true") {
                qDebug() << Q_FUNC_INFO << "Forcing authentication on";
                engine.rootContext()->setContextProperty("noAuthentication", QVariant(false));
            } else if (value == "false") {
                qDebug() << Q_FUNC_INFO << "Forcing authentication off";
                engine.rootContext()->setContextProperty("noAuthentication", QVariant(true));
            } else {
                qWarning() << Q_FUNC_INFO << "Invalid forceAuth option '" << value << "', keeping default";
            }
        }
    }

    if (args.contains(QLatin1String("-testability")) || getenv("QT_LOAD_TESTABILITY")) {
        QLibrary testLib(QLatin1String("qttestability"));
        if (testLib.load()) {
            typedef void (*TasInitialize)(void);
            TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
            if (initFunction) {
                initFunction();
            } else {
                qCritical("Library qttestability resolve failed!");
            }
        } else {
            qCritical("Library qttestability load failed!");
        }
    }

    engine.rootContext()->setContextProperty("applicationPid", QCoreApplication::applicationPid());

    if (args.contains("--ssh")) {
        bool sshIsAvailable = sshdRunning();
        engine.rootContext()->setContextProperty("sshRequired", QVariant(true));
        engine.rootContext()->setContextProperty("sshIsAvailable", sshIsAvailable);
        if (sshIsAvailable) {
            engine.rootContext()->setContextProperty("noAuthentication", QVariant(false));
            engine.rootContext()->setContextProperty("sshUser", qgetenv("USER"));
        }
    } else {
        engine.rootContext()->setContextProperty("sshRequired", QVariant(false));
        engine.rootContext()->setContextProperty("sshIsAvailable", QVariant(false));
        engine.rootContext()->setContextProperty("sshUser", "");
    }

    engine.setImportPathList(importPathList);

    QStringList keyboardLayouts;
    // load the qml file
    if (qmlfile.isEmpty()) {
        QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
        paths.prepend(QDir::currentPath());
        paths.prepend(QCoreApplication::applicationDirPath());

        foreach (const QString &path, paths) {
            QFileInfo fi(path + "/qml/lomiri-terminal-app.qml");
            qDebug() << "Trying to load QML from:" << path + "/qml/lomiri-terminal-app.qml";
            if (fi.exists()) {
                qmlfile = path +  "/qml/lomiri-terminal-app.qml";
                break;
            }
        }
    }

    // Look for default layouts
    QDir keybLayoutDir = QFileInfo(qmlfile).dir();
    const QString &layoutsDir("KeyboardRows/Layouts");

    if (keybLayoutDir.cd(layoutsDir)) {
        QString keybLayoutPath = keybLayoutDir.canonicalPath() + "/";
        qDebug() << "Retrieving default keyboard profiles from folder: " << keybLayoutPath;

        QStringList kLayouts = getProfileFromDir(keybLayoutPath);

        if (kLayouts.isEmpty()) {
            qDebug() << "No default keyboard profile found.";
        } else {
            keyboardLayouts << kLayouts;
        }
    } else {
        qDebug() << "Not able to locate default keyboard profiles folder:" << keybLayoutDir.canonicalPath() + "/" + layoutsDir;
    }

    // Look for user-defined layouts
    QStringList configLocations = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
    foreach (const QString &path, configLocations) {
        QString fullPath = path + "/lomiri-terminal-app/Layouts/";
        qDebug() << "Retrieving keyboard profiles from folder: " << fullPath;
        keyboardLayouts << getProfileFromDir(fullPath);
    }

    engine.rootContext()->setContextProperty("keyboardLayouts", keyboardLayouts);

    QCoreApplication::setApplicationName("terminal.ubports");
    // Unset organization to skip an extra folder component
    QCoreApplication::setOrganizationName(QString());
    // Get Qtlabs.settings to use a sane path
    QCoreApplication::setOrganizationDomain(QCoreApplication::applicationName());

    engine.rootContext()->setContextProperty("i18nDirectory", I18N_DIRECTORY);

    qDebug() << "using main qml file from:" << qmlfile;
    engine.load(QUrl::fromLocalFile(qmlfile));

    // Connect the quit signal
    QObject::connect((QObject*) &engine, SIGNAL(quit()), (QObject*) &a, SLOT(quit()));

    return a.exec();
}