From ee5759a157eb3c5c7d0c343aa6ac066f6c14b3f0 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 26 Jun 2018 18:31:57 +0200 Subject: support for xsessions priority and renaming --- src/globals.cpp | 2 +- src/globals.h | 1 + src/xsession.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/xsession.h | 21 ++++++++++- 4 files changed, 125 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 d0f1c75..daf2a52 100644 --- a/src/globals.h +++ b/src/globals.h @@ -27,6 +27,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..6c8aec6 100644 --- a/src/xsession.cpp +++ b/src/xsession.cpp @@ -5,8 +5,18 @@ #include #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 XSession::readSessions(const QString& path) { + // load the priorities from the config file + loadXSessionsConfig(); + QList retval; foreach (QFileInfo fi, QDir(path).entryInfoList(QStringList("*.desktop"))) { if (fi.baseName().compare("default") == 0) { @@ -167,3 +200,66 @@ bool XSession::containsKeywords(const QList& 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]////[+-] + // - 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(); + // if 0, parsing failed, default to high priority to send them to the bottom of the list + if (priority == 0) { + qWarning() << "Failed to parse priority on line" << idx; + priority = 1000; + } + + 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 #include #include +#include #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_*/ -- cgit v1.2.3-55-g7522 From 9a4448dd31c6c14fd611087f4f02059388659652 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 27 Jun 2018 10:43:59 +0200 Subject: fix indentation --- src/xsession.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xsession.cpp b/src/xsession.cpp index 6c8aec6..9895470 100644 --- a/src/xsession.cpp +++ b/src/xsession.cpp @@ -44,7 +44,7 @@ bool XSession::init(const QString& filename) { QString language(locale.split("_").at(0)); QString defaultName(""); if (settings.contains("Name")) - defaultName = settings.value("Name").toString(); + defaultName = settings.value("Name").toString(); QString name; if (settings.contains("Name[" + locale + "]")) { name = settings.value("Name[" + locale + "]").toString(); -- cgit v1.2.3-55-g7522 From f66f73e6ac1ad39895dd29f408edaca1bcb03d81 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Wed, 27 Jun 2018 10:44:38 +0200 Subject: keep 0 as default priority use < 0 priorities in config file to bump them --- src/xsession.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/xsession.cpp b/src/xsession.cpp index 9895470..d2c0ad8 100644 --- a/src/xsession.cpp +++ b/src/xsession.cpp @@ -240,11 +240,6 @@ void XSession::loadXSessionsConfig() { priorityLength = third - second - 1; } int priority = line.mid(second + 1, priorityLength).toInt(); - // if 0, parsing failed, default to high priority to send them to the bottom of the list - if (priority == 0) { - qWarning() << "Failed to parse priority on line" << idx; - priority = 1000; - } QRegularExpression::PatternOptions opts = 0; if (flags.contains("i")) -- cgit v1.2.3-55-g7522