summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Rettberg2018-07-26 17:16:12 +0200
committerSimon Rettberg2018-07-26 17:16:12 +0200
commit25a457eaf54c9ff371481f1a871846918baf9826 (patch)
treeac6defd9c1982574aa8459eac5925f44e3d1ab96 /src
parentCleanup code style (C++11, casts, etc) (diff)
parentkeep 0 as default priority (diff)
downloadvmchooser2-25a457eaf54c9ff371481f1a871846918baf9826.tar.gz
vmchooser2-25a457eaf54c9ff371481f1a871846918baf9826.tar.xz
vmchooser2-25a457eaf54c9ff371481f1a871846918baf9826.zip
Merge branch 'master' of git.openslx.org:openslx-ng/vmchooser2
Diffstat (limited to 'src')
-rw-r--r--src/globals.cpp2
-rw-r--r--src/globals.h1
-rw-r--r--src/xsession.cpp105
-rw-r--r--src/xsession.h21
4 files changed, 120 insertions, 9 deletions
diff --git a/src/globals.cpp b/src/globals.cpp
index ac14274..d4f2350 100644
--- a/src/globals.cpp
+++ b/src/globals.cpp
@@ -16,8 +16,8 @@ QString g_runVmScript(VMCHOOSER_SCRIPTS_PATH "/vmchooser-run_virt");
const QString SESSION_START_SCRIPT(VMCHOOSER_SESSION_START_SCRIPT);
const QString CONFIG_FILE_GLOBAL("/opt/openslx/vmchooser/config/vmchooser.conf");
-
const QString CONFIG_FILE_USER(userPath + "/vmchooser.conf");
+const QString CONFIG_FILE_XSESSIONS("/opt/openslx/vmchooser/config/xsessions.conf");
const QString PREVIOUS_SESSION_USER(userPath + "/vmchooser2.ini");
bool g_debugMode = false;
diff --git a/src/globals.h b/src/globals.h
index 707182e..4b38377 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -29,6 +29,7 @@ extern const QString SESSION_START_SCRIPT;
extern const QString CONFIG_FILE_GLOBAL;
extern const QString CONFIG_FILE_USER;
+extern const QString CONFIG_FILE_XSESSIONS;
extern const QString PREVIOUS_SESSION_USER;
extern QString g_currentPoolName;
diff --git a/src/xsession.cpp b/src/xsession.cpp
index 4017111..d2c0ad8 100644
--- a/src/xsession.cpp
+++ b/src/xsession.cpp
@@ -5,8 +5,18 @@
#include <QProcess>
#include "xsession.h"
+#include "globals.h"
#include "sessionsiconholder.h"
+struct DisplayConfig {
+ QChar type;
+ QRegularExpression regex;
+ int priority;
+ QString nameChange;
+};
+
+static QList< DisplayConfig > priorityList;
+
void XSession::init(const QString& name, const QString& exec,
const QString& comment, const QString& icon) {
this->name_ = name;
@@ -30,16 +40,18 @@ bool XSession::init(const QString& filename) {
return false;
}
QString exec(settings.value("Exec").toString());
-
QString locale(QLocale::system().name());
QString language(locale.split("_").at(0));
+ QString defaultName("");
+ if (settings.contains("Name"))
+ defaultName = settings.value("Name").toString();
QString name;
if (settings.contains("Name[" + locale + "]")) {
name = settings.value("Name[" + locale + "]").toString();
} else if (settings.contains("Name[" + language + "]")) {
name = settings.value("Name[" + language + "]").toString();
- } else if (settings.contains("Name")) {
- name = settings.value("Name").toString();
+ } else if (!defaultName.isEmpty()) {
+ name = defaultName;
} else {
return false;
}
@@ -61,6 +73,28 @@ bool XSession::init(const QString& filename) {
icon = QString();
}
+ // check for xsession priority/name change
+ for (auto &entry : priorityList) {
+ if ((entry.type == 't' && entry.regex.match(defaultName).hasMatch()) ||
+ (entry.type == 'e' && entry.regex.match(exec).hasMatch()) ||
+ (entry.type == 'f' && entry.regex.match(filename).hasMatch())) {
+ this->priority_ = entry.priority;
+ if (entry.nameChange.isEmpty())
+ continue;
+ // check if we have a +/- to append/prepend the given text
+ // TODO support localized name changes...
+ QChar pend = entry.nameChange.at(0);
+ if (pend == '+') {
+ name.append(entry.nameChange.mid(1));
+ continue;
+ } else if (pend == '-') {
+ name.prepend(entry.nameChange.mid(1));
+ } else {
+ name = entry.nameChange;
+ }
+ }
+ }
+
this->name_ = name;
this->exec_ = exec;
this->comment_ = comment;
@@ -84,10 +118,6 @@ bool XSession::isLocked() const {
return false;
}
-int XSession::priority() const {
- return 0;
-}
-
QIcon XSession::icon() const {
QIcon retIcon;
if (!this->icon_.isEmpty()) {
@@ -129,6 +159,9 @@ int XSession::type() const {
}
QList<Session*> XSession::readSessions(const QString& path) {
+ // load the priorities from the config file
+ loadXSessionsConfig();
+
QList<Session*> retval;
foreach (QFileInfo fi, QDir(path).entryInfoList(QStringList("*.desktop"))) {
if (fi.baseName().compare("default") == 0) {
@@ -167,3 +200,61 @@ bool XSession::containsKeywords(const QList<QString>& keywords) const {
return true;
}
+void XSession::loadXSessionsConfig() {
+ int idx = 0;
+ QFile inputFile(CONFIG_FILE_XSESSIONS);
+ if (!inputFile.open(QIODevice::ReadOnly))
+ return;
+ QTextStream in(&inputFile);
+ in.setCodec("UTF-8");
+ while (!in.atEnd()) {
+ QString line = in.readLine();
+ idx++;
+ if (line.isEmpty())
+ continue;
+ // start parsing line, the expected format is:
+ // [tef]/<regex>/<modifiers>/<priority>/[+-]<text>
+ // - t, e, f denotes to match against xsession's title, exec command, filename, respectively.
+ // - regex to match
+ // - modifiers to use when matching (e.g. insensitive)
+ // - priority to set for the xsession
+ // - text to replace the matched title with, only valid in combination with 't'. Optional +/- to append/prepend.
+ QChar matchType = line.at(0);
+ if (!QString("tef").contains(matchType))
+ continue;
+ QChar delim = line.at(1);
+ int first = find(delim, 2, line);
+ if (first == -1)
+ continue;
+ int second = find(delim, first + 1, line);
+ if (second == -1)
+ continue;
+ int third = find(delim, second + 1, line);
+
+ QString regex = line.mid(2, first - 2).replace(QString("\\") + delim, QString(delim));
+ QString flags = line.mid(first + 1, second - first - 1);
+ QString nameChanges("");
+ int priorityLength = line.length() - second;
+ if (third != -1) {
+ nameChanges = line.mid(third + 1);
+ priorityLength = third - second - 1;
+ }
+ int priority = line.mid(second + 1, priorityLength).toInt();
+
+ QRegularExpression::PatternOptions opts = 0;
+ if (flags.contains("i"))
+ opts |= QRegularExpression::CaseInsensitiveOption;
+ QRegularExpression re(regex, opts);
+ if (!re.isValid()) {
+ qWarning() << "Invalid regex:" << regex << "on line" << idx;
+ continue;
+ }
+ DisplayConfig config;
+ config.type = matchType;
+ config.regex = re;
+ config.nameChange = nameChanges;
+ config.priority = priority;
+ priorityList.append(config);
+ }
+ inputFile.close();
+}
diff --git a/src/xsession.h b/src/xsession.h
index 27e431e..fcef5d4 100644
--- a/src/xsession.h
+++ b/src/xsession.h
@@ -6,6 +6,7 @@
#include <QDomDocument>
#include <QDir>
#include <QProcess>
+#include <QRegularExpression>
#include "session.h"
class XSession : public Session {
@@ -17,7 +18,6 @@ class XSession : public Session {
bool isActive() const;
bool isLocked() const;
bool isValid() const;
- int priority() const;
SectionType section() const {
return SECTION_XSESSION;
@@ -35,6 +35,10 @@ class XSession : public Session {
return this->exec_;
}
+ int priority() const {
+ return this->priority_;
+ }
+
QIcon icon() const;
bool run() const;
@@ -52,8 +56,23 @@ class XSession : public Session {
QString exec_;
QString comment_;
QString icon_;
+ int priority_;
QProcess *_process;
+
+ static void loadXSessionsConfig();
+
+ static inline int find(QChar c, int start, const QString& line)
+ {
+ do {
+ int next = line.indexOf(c, start);
+ if (next == -1)
+ return -1;
+ if (line.at(next - 1) != '\\')
+ return next;
+ start = next + 1;
+ } while (true);
+ }
};
#endif /*VMCHOOSER_XSESSION_H_*/