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/xsession.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 7 deletions(-) (limited to 'src/xsession.cpp') 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(); +} -- cgit v1.2.3-55-g7522