File indexing completed on 2023-09-24 06:02:01

0001 /*
0002     SPDX-License-Identifier: LGPL-2.1-or-later OR MIT
0003     SPDX-FileCopyrightText: 2021 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0004 */
0005 
0006 #ifndef JOURNALDUNIQUEQUERYMODEL_H
0007 #define JOURNALDUNIQUEQUERYMODEL_H
0008 
0009 #include "journaldhelper.h"
0010 #include "kjournald_export.h"
0011 #include <QAbstractItemModel>
0012 #include <memory>
0013 
0014 class JournaldUniqueQueryModelPrivate;
0015 
0016 /**
0017  * @brief The JournaldUniqueQueryModel class provides an item model abstraction for journald queryunique API
0018  *
0019  * This class is useful when creating a list model for contents like:
0020  * - boot ids in specific journal database
0021  * - systemd units in specific journal database
0022  * - priorities in specific journal database
0023  *
0024  * The model can be create from an arbitrary local journald database by defining a path or from the
0025  * system's default journal. Values can either be set by @a setFieldString for arbitrary values or in a
0026  * typesafe manner via @a setField for most common fields.
0027  */
0028 class KJOURNALD_EXPORT JournaldUniqueQueryModel : public QAbstractItemModel
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(QString journalPath WRITE setJournaldPath RESET setSystemJournal)
0032     Q_PROPERTY(QString field WRITE setFieldString)
0033 
0034 public:
0035     enum Roles {
0036         FIELD = Qt::UserRole + 1,
0037         SELECTED, //!< supports UI integration by storing checked
0038     };
0039     Q_ENUM(Roles)
0040 
0041     explicit JournaldUniqueQueryModel(QObject *parent = nullptr);
0042 
0043     /**
0044      * @brief Create model from a specific journal database
0045      *
0046      * This constructor works similar to "journalctl -D" and allows to use a custom path to the
0047      * journald database.
0048      *
0049      * @param journalPath path to the journald database
0050      * @param parent the QObject parent
0051      */
0052     JournaldUniqueQueryModel(const QString &journalPath, QObject *parent = nullptr);
0053 
0054     /**
0055      * @brief Destroys the JournaldUniqueQueryModel object
0056      */
0057     ~JournaldUniqueQueryModel() override;
0058 
0059     /**
0060      * Reset model by reading from a new journald database
0061      *
0062      * @param path The path to directory that obtains the journald DB, usually ending with "journal".
0063      * @return true if path could be found and opened, otherwise false
0064      */
0065     bool setJournaldPath(const QString &path);
0066 
0067     /**
0068      * Switch to local system's default journald database
0069      *
0070      * For details regarding preference, see journald documentation.
0071      */
0072     void setSystemJournal();
0073 
0074     /**
0075      * Set field for which unique query shall be run
0076      *
0077      * This method allows defining an arbitrary string as query string. The most common fields are available
0078      * by type-safe @a setField setter. Examples for the argument are "_SYSTEMD_UNIT", "PRIORITY", "_BOOT_ID".
0079      *
0080      * @param fieldString the string that names the field
0081      */
0082     void setFieldString(const QString &fieldString);
0083 
0084     /**
0085      * Set field for which unique query shall be run
0086      *
0087      * @param field enum for field for which query shall be run
0088      */
0089     void setField(JournaldHelper::Field field);
0090 
0091     /**
0092      * @return the currently set field string
0093      */
0094     QString fieldString() const;
0095 
0096     /**
0097      * @copydoc QAbstractItemModel::rolesNames()
0098      */
0099     QHash<int, QByteArray> roleNames() const override;
0100 
0101     /**
0102      * @copydoc QAbstractItemModel::index()
0103      */
0104     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0105 
0106     /**
0107      * @copydoc QAbstractItemModel::parent()
0108      */
0109     QModelIndex parent(const QModelIndex &index) const override;
0110 
0111     /**
0112      * @copydoc QAbstractItemModel::rowCount()
0113      */
0114     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0115 
0116     /**
0117      * @copydoc QAbstractItemModel::columnCount()
0118      */
0119     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0120 
0121     /**
0122      * @copydoc QAbstractItemModel::data()
0123      */
0124     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0125 
0126     /**
0127      * @copydoc QAbstractItemModel::setData()
0128      */
0129     Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0130 
0131 private:
0132     std::unique_ptr<JournaldUniqueQueryModelPrivate> d;
0133 };
0134 
0135 #endif // JOURNALDUNIQUEQUERYMODEL_H