File indexing completed on 2024-06-02 05:20:37

0001 /*
0002  * SPDX-FileCopyrightText: 2013 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  *
0006  */
0007 
0008 #pragma once
0009 
0010 #include "migratorbase.h"
0011 #include <QAbstractItemModel>
0012 class QObject;
0013 #include <QPointer>
0014 #include <QSharedPointer>
0015 #include <QStandardItemModel>
0016 
0017 class MigrationExecutor;
0018 class KJobTrackerInterface;
0019 class MigratorModel;
0020 
0021 class LogModel : public QStandardItemModel
0022 {
0023     Q_OBJECT
0024 public Q_SLOTS:
0025     void message(MigratorBase::MessageType type, const QString &msg);
0026 };
0027 
0028 class Row : public QObject
0029 {
0030     Q_OBJECT
0031 public:
0032     QSharedPointer<MigratorBase> mMigrator;
0033     MigratorModel &mModel;
0034 
0035     explicit Row(const QSharedPointer<MigratorBase> &migrator, MigratorModel &model);
0036 
0037     bool operator==(const Row &other) const;
0038 
0039 private:
0040     void stateChanged(MigratorBase::MigrationState);
0041     void progress(int);
0042 };
0043 
0044 /**
0045  * The model serves as container for the migrators and exposes the status of each migrator.
0046  *
0047  * It can be plugged into a Listview to inform about the migration progress.
0048  */
0049 class MigratorModel : public QAbstractItemModel
0050 {
0051     Q_OBJECT
0052 public:
0053     enum Roles {
0054         IdentifierRole = Qt::UserRole + 1,
0055         LogfileRole,
0056     };
0057     bool addMigrator(const QSharedPointer<MigratorBase> &migrator);
0058 
0059     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0060     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0061     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0062     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0063     QModelIndex parent(const QModelIndex &child) const override;
0064     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0065 
0066     QSharedPointer<MigratorBase> migrator(const QString &identifier) const;
0067     QList<QSharedPointer<MigratorBase>> migrators() const;
0068 
0069 private:
0070     enum Columns {
0071         Name = 0,
0072         Progress = 1,
0073         State = 2,
0074         ColumnCount,
0075     };
0076     friend class Row;
0077     int positionOf(const Row &);
0078     void columnChanged(const Row &, int column);
0079     QList<QSharedPointer<Row>> mMigrators;
0080 };
0081 
0082 /**
0083  * Scheduler for migration jobs.
0084  *
0085  * Status information is exposed via getModel, which returns a list model containing all migrators with basic information.
0086  * Additionally a logmodel is available via getLogModel for each migrator. The logmodel is continuously filled with information, and can be requested and
0087  * displayed at any time.
0088  *
0089  * Migrators which return true on shouldAutostart() automatically enter a queue to be processed one after the other.
0090  * When manually triggered it is possible though to run multiple jobs in parallel.
0091  */
0092 class MigrationScheduler : public QObject
0093 {
0094     Q_OBJECT
0095 public:
0096     explicit MigrationScheduler(KJobTrackerInterface *jobTracker = nullptr, QObject *parent = nullptr);
0097     ~MigrationScheduler() override;
0098 
0099     void addMigrator(const QSharedPointer<MigratorBase> &migrator);
0100 
0101     // A model for the view
0102     QAbstractItemModel &model();
0103     QStandardItemModel &logModel(const QString &identifier);
0104 
0105     // Control
0106     void start(const QString &identifier);
0107     void pause(const QString &identifier);
0108     void abort(const QString &identifier);
0109 
0110 private:
0111     void checkForAutostart(const QSharedPointer<MigratorBase> &migrator);
0112 
0113     QScopedPointer<MigratorModel> mModel;
0114     QHash<QString, QSharedPointer<LogModel>> mLogModel;
0115     QPointer<MigrationExecutor> mAutostartExecutor;
0116     KJobTrackerInterface *mJobTracker = nullptr;
0117 };