File indexing completed on 2024-05-12 05:47:52

0001 /*
0002  * SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz19@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef VERSIONCONTROLOBSERVER_H
0008 #define VERSIONCONTROLOBSERVER_H
0009 
0010 #include "dolphin_export.h"
0011 
0012 #include "kversioncontrolplugin.h"
0013 
0014 #include <KFileItem>
0015 
0016 #include <QList>
0017 #include <QObject>
0018 #include <QString>
0019 #include <QUrl>
0020 
0021 class KFileItemList;
0022 class KFileItemModel;
0023 class KItemRangeList;
0024 class QAction;
0025 class QTimer;
0026 class UpdateItemStatesThread;
0027 
0028 class DolphinView;
0029 
0030 /**
0031  * @brief Observes all version control plugins.
0032  *
0033  * The items of the directory-model get updated automatically if the currently
0034  * shown directory is under version control.
0035  *
0036  * @see VersionControlPlugin
0037  */
0038 class DOLPHIN_EXPORT VersionControlObserver : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     explicit VersionControlObserver(QObject *parent = nullptr);
0044     ~VersionControlObserver() override;
0045 
0046     void setModel(KFileItemModel *model);
0047     KFileItemModel *model() const;
0048     void setView(DolphinView *view);
0049     DolphinView *view() const;
0050 
0051     QList<QAction *> actions(const KFileItemList &items) const;
0052 
0053 Q_SIGNALS:
0054     /**
0055      * Is emitted if an information message with the content \a msg
0056      * should be shown.
0057      */
0058     void infoMessage(const QString &msg);
0059 
0060     /**
0061      * Is emitted if an error message with the content \a msg
0062      * should be shown.
0063      */
0064     void errorMessage(const QString &msg);
0065 
0066     /**
0067      * Is emitted if an "operation completed" message with the content \a msg
0068      * should be shown.
0069      */
0070     void operationCompletedMessage(const QString &msg);
0071 
0072 private Q_SLOTS:
0073     /**
0074      * Invokes verifyDirectory() with a small delay. If delayedDirectoryVerification()
0075      * is invoked before the delay has been exceeded, the delay will be reset. This
0076      * assures that a lot of short requests for directory verification only result
0077      * in one (expensive) call.
0078      */
0079     void delayedDirectoryVerification();
0080 
0081     /**
0082      * Invokes verifyDirectory() with a small delay. In opposite to
0083      * delayedDirectoryVerification() it and assures that the verification of
0084      * the directory is done silently without information messages.
0085      */
0086     void silentDirectoryVerification();
0087 
0088     /**
0089      * Invokes delayedDirectoryVerification() only if the itemsChanged() signal has not
0090      * been triggered by the VCS plugin itself.
0091      */
0092     void slotItemsChanged(const KItemRangeList &itemRanges, const QSet<QByteArray> &roles);
0093 
0094     void verifyDirectory();
0095 
0096     /**
0097      * Is invoked if the thread m_updateItemStatesThread has been finished
0098      * and applies the item states.
0099      */
0100     void slotThreadFinished();
0101 
0102 private:
0103     typedef QPair<KFileItem, KVersionControlPlugin::ItemVersion> ItemState;
0104 
0105     void updateItemStates();
0106 
0107     /**
0108      * It creates a item state list for every expanded directory and stores
0109      * this list together with the directory url in the \a itemStates map.
0110      *
0111      * @itemStates      A map of item state lists for every expanded directory
0112      *                  and its items, where the "key" is the directory url and
0113      *                  the "value" is a list of ItemStates for every item
0114      *                  within this directory.
0115      * @firstIndex      The index to start the processing from, this is needed
0116      *                  because this function is recursively called.
0117      *
0118      * @return          The number of (recursive) processed items.
0119      */
0120     int createItemStatesList(QMap<QString, QVector<ItemState>> &itemStates, const int firstIndex = 0);
0121 
0122     /**
0123      * Returns a matching plugin for the given directory.
0124      * 0 is returned, if no matching plugin has been found.
0125      */
0126     KVersionControlPlugin *searchPlugin(const QUrl &directory);
0127 
0128     /**
0129      * Returns true, if the directory contains a version control information.
0130      */
0131     bool isVersionControlled() const;
0132 
0133 private:
0134     void initPlugins();
0135 
0136     bool m_pendingItemStatesUpdate;
0137     bool m_silentUpdate; // if true, no messages will be send during the update
0138                          // of version states
0139     QString m_localRepoRoot;
0140 
0141     DolphinView *m_view;
0142     KFileItemModel *m_model;
0143 
0144     QTimer *m_dirVerificationTimer;
0145 
0146     bool m_pluginsInitialized;
0147     KVersionControlPlugin *m_plugin;
0148     QList<QPointer<KVersionControlPlugin>> m_plugins;
0149     UpdateItemStatesThread *m_updateItemStatesThread;
0150 
0151     friend class UpdateItemStatesThread;
0152 };
0153 
0154 #endif // REVISIONCONTROLOBSERVER_H