| 1 | #include "dekkodserviceplugin.h" |
| 2 | #include <QCoreApplication> |
| 3 | #include <QDir> |
| 4 | #include <QFile> |
| 5 | #include <QProcess> |
| 6 | #include <QRegExp> |
| 7 | #include <QSettings> |
| 8 | #include <SnapStandardPaths.h> |
| 9 | |
| 10 | DekkodService::DekkodService(QObject *parent): ServicePlugin(parent) |
| 11 | { |
| 12 | m_service = QStringLiteral("dekkod"); |
| 13 | m_serviceFile = QString("%1/.config/systemd/user/%2.service").arg(QDir::homePath(), m_service); |
| 14 | } |
| 15 | |
| 16 | QString DekkodService::pluginId() const |
| 17 | { |
| 18 | return QStringLiteral("dekkod-service"); |
| 19 | } |
| 20 | |
| 21 | QString DekkodService::location() const |
| 22 | { |
| 23 | return QStringLiteral("Dekko::Service"); |
| 24 | } |
| 25 | |
| 26 | QString DekkodService::i18n() const |
| 27 | { |
| 28 | return QString(); |
| 29 | } |
| 30 | |
| 31 | void DekkodService::start() |
| 32 | { |
| 33 | if (newVersion()) { |
| 34 | qDebug() << "[DekkodService] Stopping service for version upgrade"; |
| 35 | stopService(); |
| 36 | } |
| 37 | |
| 38 | if (!serviceRunning()) { |
| 39 | // The service is started on login, so actually it should already be running. |
| 40 | // Overwrite service file to make sure thats not causing the problem. |
| 41 | // see also issue #114 |
| 42 | qDebug() << "[DekkodService] Installing service file"; |
| 43 | installServiceFile(); |
| 44 | |
| 45 | qDebug() << "[DekkodService] Starting dekkod service"; |
| 46 | startService(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void DekkodService::stop() |
| 51 | { |
| 52 | // if (serviceRunning()) { |
| 53 | // stopService(); |
| 54 | // } |
| 55 | |
| 56 | // if (serviceFileInstalled()) { |
| 57 | // removeServiceFile(); |
| 58 | // } |
| 59 | } |
| 60 | |
| 61 | bool DekkodService::serviceFileInstalled() const |
| 62 | { |
| 63 | return QFile(m_serviceFile).exists(); |
| 64 | } |
| 65 | |
| 66 | bool DekkodService::installServiceFile() const |
| 67 | { |
| 68 | QFile f(m_serviceFile); |
| 69 | QDir parent = QFileInfo(f).dir(); |
| 70 | |
| 71 | if (!parent.mkpath(".")) { |
| 72 | qDebug() << "[DekkodService] Cannot create systemd user service directory"; |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | if (!f.open(QFile::WriteOnly | QFile::Truncate)) { |
| 77 | qDebug() << "[DekkodService] Cannot create service file"; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | QString appDir = QCoreApplication::applicationDirPath(); |
| 82 | appDir.replace(QRegExp("dekko2.dekkoproject/[^/]+/"), "dekko2.dekkoproject/current/"); |
| 83 | |
| 84 | f.write("[Unit]\n"); |
| 85 | f.write("Description=Dekkod Service\n"); |
| 86 | |
| 87 | f.write("\n[Service]\n"); |
| 88 | f.write("ExecStart=" + appDir.toUtf8() + "/" + m_service.toUtf8() + "\n"); |
| 89 | f.write("WorkingDirectory=" + appDir.toUtf8() + "\n"); |
| 90 | f.write("Environment=\"LD_LIBRARY_PATH=" + appDir.toUtf8() + "/../:$LD_LIBRARY_PATH\"\n"); |
| 91 | f.write("Environment=\"DEKKO_PLUGINS=" + appDir.toUtf8() + "/plugins\"\n"); |
| 92 | f.write("Environment=\"QMF_PLUGINS=" + appDir.toUtf8() + "/../qmf/plugins5\"\n"); |
| 93 | f.write("Environment=\"QMF_DATA=" + QDir::homePath().toUtf8() + "/.cache/dekko2.dekkoproject\"\n"); |
| 94 | f.write("Restart=on-failure\n"); |
| 95 | |
| 96 | f.write("\n[Install]\n"); |
| 97 | f.write("WantedBy=graphical-session.target\n"); |
| 98 | |
| 99 | f.close(); |
| 100 | |
| 101 | qDebug() << "[DekkodService] should enable service"; |
| 102 | int ret1 = QProcess::execute("systemctl", {"--user", "daemon-reload"}); |
| 103 | int ret2 = QProcess::execute("systemctl", {"--user", "enable", m_service}); |
| 104 | |
| 105 | return ret1 == 0 && ret2 == 0; |
| 106 | } |
| 107 | |
| 108 | bool DekkodService::removeServiceFile() const |
| 109 | { |
| 110 | if (serviceFileInstalled()) { |
| 111 | int ret = QProcess::execute("systemctl", {"--user", "disable", "--now", m_service}); |
| 112 | return QFile(m_serviceFile).remove() && ret == 0; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool DekkodService::serviceRunning() const |
| 118 | { |
| 119 | QProcess p; |
| 120 | p.start("systemctl", {"--user", "status", m_service}); |
| 121 | p.waitForFinished(); |
| 122 | QByteArray output = p.readAll(); |
| 123 | qDebug() << output; |
| 124 | return output.contains("active (running)"); |
| 125 | } |
| 126 | |
| 127 | bool DekkodService::startService() |
| 128 | { |
| 129 | qDebug() << "[DekkodService] should start service"; |
| 130 | int ret = QProcess::execute("systemctl", {"--user", "start", m_service}); |
| 131 | return ret == 0; |
| 132 | } |
| 133 | |
| 134 | bool DekkodService::restartService() |
| 135 | { |
| 136 | qDebug() << "[DekkodService] should restart service"; |
| 137 | int ret = QProcess::execute("systemctl", {"--user", "restart", m_service}); |
| 138 | return ret == 0; |
| 139 | } |
| 140 | |
| 141 | bool DekkodService::stopService() |
| 142 | { |
| 143 | qDebug() << "[DekkodService] should stop service"; |
| 144 | int ret = QProcess::execute("systemctl", {"--user", "stop", m_service}); |
| 145 | return ret == 0; |
| 146 | } |
| 147 | |
| 148 | bool DekkodService::newVersion() |
| 149 | { |
| 150 | static const QString path = SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation) + QStringLiteral("/dekkod/settings.ini"); |
| 151 | QSettings settings(path, QSettings::IniFormat); |
| 152 | if (!settings.contains(QStringLiteral("version"))) { |
| 153 | settings.setValue(QStringLiteral("version"), QStringLiteral(DEKKO_VERSION)); |
| 154 | // Dekkod may already be running as we previously didn't version it. |
| 155 | // So we still need to stop it if running. |
| 156 | return serviceRunning(); |
| 157 | } |
| 158 | |
| 159 | // We also want to support downgrades so just check the version doesn't match DEKKO_VERSION |
| 160 | const bool result = settings.value(QStringLiteral("version")).toString() != QStringLiteral(DEKKO_VERSION); |
| 161 | if (result) { |
| 162 | settings.setValue(QStringLiteral("version"), QStringLiteral(DEKKO_VERSION)); |
| 163 | } |
| 164 | settings.sync(); |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | QVariantMap DekkodService::documentation() const |
| 169 | { |
| 170 | return QVariantMap(); |
| 171 | } |
| 172 | |
| 173 | QString DekkodServicePlugin::name() const |
| 174 | { |
| 175 | return QStringLiteral("dekkod-service"); |
| 176 | } |
| 177 | |
| 178 | QString DekkodServicePlugin::description() const |
| 179 | { |
| 180 | return QStringLiteral("Dekko's messaging server"); |
| 181 | } |
| 182 | |
| 183 | PluginInfo *DekkodServicePlugin::create(QObject *parent) const |
| 184 | { |
| 185 | return new DekkodService(parent); |
| 186 | } |