summaryrefslogtreecommitdiffstats
path: root/src/xsession.cpp
diff options
context:
space:
mode:
authorNils Schwabe2014-03-03 15:14:20 +0100
committerNils Schwabe2014-03-03 15:14:20 +0100
commit030d91e628a2ecd5634376c9c4d903a1fa0fb423 (patch)
tree227029e3b48deb1d4d01ce497d9ec3b8d47f7a61 /src/xsession.cpp
parentInitial commit (diff)
downloadvmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.tar.gz
vmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.tar.xz
vmchooser2-030d91e628a2ecd5634376c9c4d903a1fa0fb423.zip
added files from vmchooser1
Diffstat (limited to 'src/xsession.cpp')
-rw-r--r--src/xsession.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/xsession.cpp b/src/xsession.cpp
new file mode 100644
index 0000000..f4cc3d9
--- /dev/null
+++ b/src/xsession.cpp
@@ -0,0 +1,149 @@
+#include <QDir>
+#include <QSettings>
+#include <QLocale>
+#include <QApplication>
+#include <QProcess>
+
+#include "xsession.h"
+
+void XSession::init(const QString& name, const QString& exec,
+ const QString& comment, const QString& icon) {
+ this->name_ = name;
+ this->exec_ = exec;
+ this->comment_ = comment;
+ this->icon_ = icon;
+}
+
+bool XSession::init(const QString& filename) {
+ QSettings settings(filename, QSettings::IniFormat);
+ settings.setIniCodec("UTF-8");
+
+ settings.beginGroup("Desktop Entry");
+
+ if (settings.value("NoDisplay").toString().compare("true") == 0 ||
+ settings.value("Hidden").toString().compare("true") == 0) {
+ return false;
+ }
+
+ if (!settings.contains("Exec")) {
+ return false;
+ }
+ QString exec(settings.value("Exec").toString());
+
+ QString locale(QLocale::system().name());
+ QString language(locale.split("_").at(0));
+ 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 {
+ return false;
+ }
+
+ QString comment;
+ if (settings.contains("Comment[" + locale + "]")) {
+ comment = settings.value("Comment[" + locale + "]").toString();
+ } else if (settings.contains("Comment[" + language + "]")) {
+ comment = settings.value("Comment[" + language + "]").toString();
+ } else {
+ comment = settings.value("Comment").toString();
+ }
+
+ QString icon(settings.value("Icon").toString());
+ if (QDir::isRelativePath(icon)) {
+ // icons with relative paths are too complicated to find
+ // see http://freedesktop.org/wiki/Specifications/icon-theme-spec
+ // let's just ignore them
+ icon = QString();
+ }
+
+ this->name_ = name;
+ this->exec_ = exec;
+ this->comment_ = comment;
+ this->icon_ = icon;
+
+ _process = new QProcess();
+
+ return true;
+}
+
+bool XSession::isActive() const {
+ return true;
+}
+
+bool XSession::isValid() const {
+ return true;
+}
+
+bool XSession::isLocked() const {
+ return false;
+}
+
+int XSession::priority() const {
+ return 0;
+}
+
+QString XSession::icon() const {
+ QString icon(this->icon_);
+
+ if (icon.isEmpty()) {
+ if (this->exec_.contains("kde", Qt::CaseInsensitive)) {
+ icon = "kde";
+ } else if (this->exec_.contains("gnome", Qt::CaseInsensitive)) {
+ icon = "gnome";
+ } else if (this->exec_.contains("term", Qt::CaseInsensitive)) {
+ icon = "term";
+ } else if (this->exec_.contains("xfce", Qt::CaseInsensitive)) {
+ icon = "xfce";
+ } else if (this->exec_.contains("lxde", Qt::CaseInsensitive)) {
+ icon = "lxde";
+ } else {
+ icon = "linux";
+ }
+ }
+
+ return icon;
+}
+
+bool XSession::run() const {
+ _process->start(this->exec_);
+ QObject::connect(_process, SIGNAL(finished(int, QProcess::ExitStatus)), QApplication::instance(), SLOT(quit()));
+ if (_process->state() == QProcess::Starting || QProcess::Running)
+ return true;
+ else
+ return false;
+}
+
+int XSession::type() const {
+ return Session::XSESSION;
+}
+
+QList<Session*> XSession::readSessions(const QString& path) {
+ QList<Session*> retval;
+ foreach (QFileInfo fi, QDir(path).entryInfoList(QStringList("*.desktop"))) {
+ if (fi.baseName().compare("default") == 0) {
+ continue;
+ }
+ XSession* session = new XSession;
+ if (session->init(fi.absoluteFilePath())) {
+ retval.append(session);
+ }
+ }
+ return retval;
+}
+
+bool XSession::operator<(const Session& other) const {
+ int p0 = this->priority();
+ int p1 = other.priority();
+
+ if (p0 < p1) return true;
+ if (p0 == p1) {
+ QString d0 = this->shortDescription();
+ QString d1 = other.shortDescription();
+ return d0.localeAwareCompare(d1) < 0;
+ }
+ return false;
+}