From 614793ecd062199e0b1fc4efaa733aa3bf845b67 Mon Sep 17 00:00:00 2001
From: Filippo Scognamiglio <flscogna@gmail.com>
Date: Sat, 4 Oct 2014 16:21:17 +0200
Subject: [PATCH] Added --workdir and --program flags.

---
 app/main.cpp                     | 16 ++++++++++++++--
 app/qml/PreprocessedTerminal.qml |  7 +++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/app/main.cpp b/app/main.cpp
index 10c6b96..0cb4bb6 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -1,12 +1,20 @@
 #include <QtQml/QQmlApplicationEngine>
 #include <QtGui/QGuiApplication>
 
+#include <QQmlContext>
+#include <QStringList>
+
 #include <QtWidgets/QApplication>
 
 #include <QDebug>
 #include <stdlib.h>
 
 
+QString getNamedArgument(QStringList args, QString name) {
+    int index = args.indexOf(name);
+    return (index != -1) ? args[index + 1] : QString("");
+}
+
 int main(int argc, char *argv[])
 {
     setenv("QT_QPA_PLATFORMTHEME", "", 1);
@@ -16,13 +24,18 @@ int main(int argc, char *argv[])
     // Manage command line arguments from the cpp side
     QStringList args = app.arguments();
     if (args.contains("-h") || args.contains("--help")) {
-        qDebug() << "Usage: " + args.at(0) + " [--default-settings] [-h|--help]";
+        qDebug() << "Usage: " + args.at(0) + " [--default-settings] [--workdir <dir>] [--program <prog>] [-h|--help]";
         qDebug() << "    --default-settings  Run cool-old-term with the default settings";
+        qDebug() << "    --workdir <dir>     Change working directory to 'dir'";
+        qDebug() << "    --program <prog>    Run the 'prog' in the new terminal.";
         qDebug() << "    -p|--profile        Run cool-old-term with the given profile.";
         qDebug() << "    -h|--help           Print this help.";
         return 0;
     }
 
+    engine.rootContext()->setContextProperty("workdir", getNamedArgument(args, "--workdir"));
+    engine.rootContext()->setContextProperty("shellProgram", getNamedArgument(args, "--program"));
+
     // Manage import paths
     QStringList importPathList = engine.importPathList();
     importPathList.prepend(QCoreApplication::applicationDirPath() + "/imports/");
@@ -32,4 +45,3 @@ int main(int argc, char *argv[])
 
     return app.exec();
 }
-
diff --git a/app/qml/PreprocessedTerminal.qml b/app/qml/PreprocessedTerminal.qml
index 6ca87ad..97318c5 100644
--- a/app/qml/PreprocessedTerminal.qml
+++ b/app/qml/PreprocessedTerminal.qml
@@ -137,6 +137,13 @@ Item{
         }
         Component.onCompleted: {
             shadersettings.terminalFontChanged.connect(handleFontChange);
+
+            // Retrieve the variable set in main.cpp if arguments are passed.
+            if (shellProgram)
+                ksession.setShellProgram(shellProgram);
+            if (workdir)
+                ksession.initialWorkingDirectory = workdir;
+
             ksession.startShellProgram();
             forceActiveFocus();
         }