~larryprice/+junk/terminal-helper

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
/*
 * Copyright: 2013 - 2014 Canonical, Ltd
 *
 * This file is part of ubuntu-terminal-app
 *
 * ubuntu-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.
 *
 * ubuntu-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>
 */

#include "fileio.h"
#include <QApplication>
#include <QtQuick/QQuickView>
#include <QtQml/QtQml>
#include <QLibrary>
#include <QDir>
#include <QDebug>

QString getNamedArgument(const QStringList& args, const QString& name, const QString& defaultName) {
  int index = args.indexOf(name);
  return index != -1 ? args[index + 1] : QString(defaultName);
}

QStringList getProfileFromDir(const QString &path) {
    QDir layoutDir(path, "*.json");

    QStringList result;
    for (const auto& s: layoutDir.entryList()) {
        result.append(path + s);
    }
    return result;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QQuickView view;
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    FileIO fileIO;
    view.engine()->rootContext()->setContextProperty("fileIO", &fileIO);

    // Set up import paths
    QStringList importPathList = view.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/");
    view.engine()->setImportPathList(importPathList);

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

        for(const auto& 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", "xterm", 0);

    QStringList args = a.arguments();
    if (args.length() < 2) {
        qDebug() << "usage: " + args.at(0) + " command [-- args]";
        return 1;
    }

    view.engine()->rootContext()->setContextProperty("command", args[1]);

    auto argsIdx = args.indexOf("--");
    QStringList arguments;
    if (argsIdx != -1)
    {
      arguments = args.mid(argsIdx+1);
    }
    view.engine()->rootContext()->setContextProperty("args", arguments);

    // Dynamic folder home
    view.engine()->rootContext()->setContextProperty("workdir", "$HOME");

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

    QString qmlfile;
    for(const auto& path: paths) {
      QFileInfo fi(path + "/qml/ubuntu-terminal-app.qml");
      qDebug() << "Trying to load QML from:" << path + "/qml/ubuntu-terminal-app.qml";
      if (fi.exists()) {
        qmlfile = path +  "/qml/ubuntu-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);
    for(const auto& path: configLocations) {
        QString fullPath = path + "/com.ubuntu.terminal/Layouts/";
        qDebug() << "Retrieving keyboard profiles from folder: " << fullPath;
        keyboardLayouts << getProfileFromDir(fullPath);
    }

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

    qDebug() << "using main qml file from:" << qmlfile;
    view.setSource(QUrl::fromLocalFile(qmlfile));
    view.show();

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

    return a.exec();
}