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