File indexing completed on 2024-04-21 05:47:46

0001 /*******************************************************************************
0002  * Copyright (C) 2016 Ragnar Thomsen <rthomsen6@gmail.com>                     *
0003  *                                                                             *
0004  * This program is free software: you can redistribute it and/or modify it     *
0005  * under the terms of the GNU General Public License as published by the Free  *
0006  * Software Foundation, either version 2 of the License, or (at your option)   *
0007  * any later version.                                                          *
0008  *                                                                             *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT *
0010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
0011  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
0012  * more details.                                                               *
0013  *                                                                             *
0014  * You should have received a copy of the GNU General Public License along     *
0015  * with this program. If not, see <http://www.gnu.org/licenses/>.              *
0016  *******************************************************************************/
0017 
0018 #ifndef MAINWINDOW_H
0019 #define MAINWINDOW_H
0020 
0021 #include "ui_mainwindow.h"
0022 #include "systemdunit.h"
0023 #include "sortfilterunitmodel.h"
0024 #include "unitmodel.h"
0025 
0026 #include <KMessageWidget>
0027 #include <KXmlGuiWindow>
0028 
0029 #include <QDBusConnection>
0030 #include <QStandardItemModel>
0031 
0032 struct conffile
0033 {
0034     QString filePath;
0035     QString manPage;
0036     QString description;
0037 
0038     conffile(QString f, QString m, QString d)
0039     {
0040         filePath = f;
0041         manPage = m;
0042         description = d;
0043     }
0044 
0045     conffile(){}
0046 
0047     conffile(QString f)
0048     {
0049         filePath = f;
0050     }
0051 
0052     bool operator==(const QString& right) const
0053     {
0054         if (filePath == right)
0055             return true;
0056         else
0057             return false;
0058     }
0059 
0060     bool operator==(const conffile& right) const
0061     {
0062         if (filePath == right.filePath)
0063             return true;
0064         else
0065             return false;
0066     }
0067 };
0068 
0069 struct unitfile
0070 {
0071     QString name;
0072     QString status;
0073 
0074     bool operator==(const unitfile& right) const
0075     {
0076         if (name.section(QLatin1Char('/'), -1) == right.name)
0077             return true;
0078         else
0079             return false;
0080     }
0081 };
0082 
0083 enum dbusConn
0084 {
0085     systemd, logind
0086 };
0087 
0088 enum dbusIface
0089 {
0090     sysdMgr, sysdUnit, sysdTimer, logdMgr, logdSession
0091 };
0092 
0093 class MainWindow : public KXmlGuiWindow
0094 {
0095     Q_OBJECT
0096 public:
0097     explicit MainWindow(QWidget *parent = 0);
0098     ~MainWindow();
0099 
0100 private:
0101     Ui::MainWindow ui;
0102 
0103     void setupSignalSlots();
0104     void setupUnitslist();
0105     void setupSessionlist();
0106     void setupTimerlist();
0107     void setupConfFilelist();
0108     void authServiceAction(const QString &, const QString &, const QString &, const QString &, const QList<QVariant> &);
0109     bool eventFilter(QObject *, QEvent*);
0110     void updateUnitCount();
0111     void displayMsgWidget(KMessageWidget::MessageType type, QString msg);
0112     void setupActions();
0113     void openEditor(const QString &file);
0114     QVector<SystemdUnit> getUnitsFromDbus(dbusBus bus);
0115     QVariant getDbusProperty(QString prop, dbusIface ifaceName, QDBusObjectPath path = QDBusObjectPath("/org/freedesktop/systemd1"), dbusBus bus = sys);
0116     QDBusMessage callDbusMethod(QString method, dbusIface ifaceName, dbusBus bus = sys, const QList<QVariant> &args = QList<QVariant> ());
0117     QList<QStandardItem *> buildTimerListRow(const SystemdUnit &unit, const QVector<SystemdUnit> &list, dbusBus bus);
0118     void executeUnitAction(const QString &method);
0119     void executeSystemDaemonAction(const QString &method);
0120     void executeUserDaemonAction(const QString &method);
0121     void executeSessionAction(const QString &method);
0122 
0123 
0124     SortFilterUnitModel *m_systemUnitFilterModel;
0125     SortFilterUnitModel *m_userUnitFilterModel;
0126     QStandardItemModel *m_sessionModel;
0127     QStandardItemModel *m_timerModel;
0128     QStandardItemModel *m_confFileModel;
0129     UnitModel *m_systemUnitModel;
0130     UnitModel *m_userUnitModel;
0131     QVector<SystemdUnit> m_systemUnitsList;
0132     QVector<SystemdUnit> m_userUnitsList;
0133     QVector<SystemdSession> m_sessionList;
0134     QVector<conffile> m_confFileList;
0135     QString m_userBusPath;
0136     int systemdVersion;
0137     int lastSessionRowChecked = -1;
0138     int m_noActSystemUnits;
0139     int m_noActUserUnits;
0140     bool enableUserUnits = true;
0141     QTimer *timer;
0142     const QStringList unitTypeSufx = QStringList{QString(),
0143                                                  QStringLiteral(".service"),
0144                                                  QStringLiteral(".automount"),
0145                                                  QStringLiteral(".device"),
0146                                                  QStringLiteral(".mount"),
0147                                                  QStringLiteral(".path"),
0148                                                  QStringLiteral(".scope"),
0149                                                  QStringLiteral(".slice"),
0150                                                  QStringLiteral(".socket"),
0151                                                  QStringLiteral(".swap"),
0152                                                  QStringLiteral(".target"),
0153                                                  QStringLiteral(".timer")};
0154     const QString connSystemd = QStringLiteral("org.freedesktop.systemd1");
0155     const QString connLogind = QStringLiteral("org.freedesktop.login1");
0156     const QString pathSysdMgr = QStringLiteral("/org/freedesktop/systemd1");
0157     const QString pathLogdMgr = QStringLiteral("/org/freedesktop/login1");
0158     const QString ifaceMgr = QStringLiteral("org.freedesktop.systemd1.Manager");
0159     const QString ifaceLogdMgr = QStringLiteral("org.freedesktop.login1.Manager");
0160     const QString ifaceUnit = QStringLiteral("org.freedesktop.systemd1.Unit");
0161     const QString ifaceTimer = QStringLiteral("org.freedesktop.systemd1.Timer");
0162     const QString ifaceSession = QStringLiteral("org.freedesktop.login1.Session");
0163     const QString ifaceDbusProp = QStringLiteral("org.freedesktop.DBus.Properties");
0164     QDBusConnection m_systemBus = QDBusConnection::systemBus();
0165 
0166     QLabel *m_lblLog;
0167 
0168     QAction *m_refreshAction;
0169     QAction *m_reloadSystemDaemonAction;
0170     QAction *m_reexecSystemDaemonAction;
0171     QAction *m_reloadUserDaemonAction;
0172     QAction *m_reexecUserDaemonAction;
0173 
0174     QAction *m_startUnitAction;
0175     QAction *m_stopUnitAction;
0176     QAction *m_reloadUnitAction;
0177     QAction *m_restartUnitAction;
0178 
0179     QAction *m_enableUnitAction;
0180     QAction *m_disableUnitAction;
0181     QAction *m_maskUnitAction;
0182     QAction *m_unmaskUnitAction;
0183     QAction *m_editUnitFileAction;
0184 
0185     QAction *m_editConfFileAction;
0186     QAction *m_openManPageAction;
0187 
0188     QAction *m_activateSessionAction;
0189     QAction *m_terminateSessionAction;
0190     QAction *m_lockSessionAction;
0191 
0192 private slots:
0193     void quit();
0194 
0195     void updateActions();
0196     void slotChkShowUnits(int);
0197     void slotCmbUnitTypes(int);
0198     void slotUnitContextMenu(const QPoint &pos);
0199     void slotConfFileContextMenu(const QPoint &pos);
0200     void slotSessionContextMenu(const QPoint &);
0201     void slotRefreshUnitsList(bool inital, dbusBus bus);
0202     void slotRefreshSessionList();
0203     void slotRefreshTimerList();
0204     void slotRefreshConfFileList();
0205 
0206     void slotSystemSystemdReloading(bool);
0207     void slotSystemUnitFilesChanged();
0208     //void slotSystemUnitNew(const QString &id, const QDBusObjectPath &path);
0209     //void slotSystemUnitRemoved(const QString &id, const QDBusObjectPath &path);
0210     void slotSystemJobNew(uint id, const QDBusObjectPath &path, const QString &unit);
0211     void slotSystemJobRemoved(uint id, const QDBusObjectPath &path, const QString &unit, const QString &result);
0212     void slotSystemPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps);
0213 
0214     void slotUserSystemdReloading(bool);
0215     void slotUserUnitFilesChanged();
0216     //void slotUserUnitNew(const QString &id, const QDBusObjectPath &path);
0217     //void slotUserUnitRemoved(const QString &id, const QDBusObjectPath &path);
0218     void slotUserJobNew(uint id, const QDBusObjectPath &path, const QString &unit);
0219     void slotUserJobRemoved(uint id, const QDBusObjectPath &path, const QString &unit, const QString &result);
0220     void slotUserPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps);
0221 
0222     void slotLogindPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps);
0223     void slotLeSearchUnitChanged(QString);
0224     void slotUpdateTimers();
0225     void slotRefreshAll();
0226 
0227     void slotEditUnitFile();
0228     void slotEditConfFile();
0229     void slotOpenManPage();
0230 };
0231 
0232 #endif // MAINWINDOW_H