]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/views/versioncontrol/versioncontrolobserver.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / views / versioncontrol / versioncontrolobserver.cpp
index 44dffac45d8784a43f930538b848a45690eb95ce..7cf78c8aaa6914bf24fcb54b9a673199d9ffb32f 100644 (file)
@@ -1,35 +1,21 @@
-/***************************************************************************
- *   Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com>             *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz19@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #include "versioncontrolobserver.h"
 
 #include "dolphin_versioncontrolsettings.h"
+#include "dolphindebug.h"
+#include "views/dolphinview.h"
+#include "kitemviews/kfileitemmodel.h"
+#include "updateitemstatesthread.h"
 
 #include <KLocalizedString>
 #include <KService>
-#include "dolphindebug.h"
 #include <KServiceTypeTrader>
-#include <kitemviews/kfileitemmodel.h>
 
-#include "updateitemstatesthread.h"
-
-#include <QFile>
 #include <QTimer>
 
 VersionControlObserver::VersionControlObserver(QObject* parent) :
@@ -37,8 +23,10 @@ VersionControlObserver::VersionControlObserver(QObject* parent) :
     m_pendingItemStatesUpdate(false),
     m_versionedDirectory(false),
     m_silentUpdate(false),
+    m_view(nullptr),
     m_model(nullptr),
     m_dirVerificationTimer(nullptr),
+    m_pluginsInitialized(false),
     m_plugin(nullptr),
     m_updateItemStatesThread(nullptr)
 {
@@ -68,7 +56,7 @@ void VersionControlObserver::setModel(KFileItemModel* model)
         disconnect(m_model, &KFileItemModel::itemsInserted,
                    this, &VersionControlObserver::delayedDirectoryVerification);
         disconnect(m_model, &KFileItemModel::itemsChanged,
-                   this, &VersionControlObserver::delayedDirectoryVerification);
+                   this, &VersionControlObserver::slotItemsChanged);
     }
 
     m_model = model;
@@ -77,7 +65,7 @@ void VersionControlObserver::setModel(KFileItemModel* model)
         connect(m_model, &KFileItemModel::itemsInserted,
                 this, &VersionControlObserver::delayedDirectoryVerification);
         connect(m_model, &KFileItemModel::itemsChanged,
-                this, &VersionControlObserver::delayedDirectoryVerification);
+                this, &VersionControlObserver::slotItemsChanged);
     }
 }
 
@@ -86,6 +74,26 @@ KFileItemModel* VersionControlObserver::model() const
     return m_model;
 }
 
+void VersionControlObserver::setView(DolphinView* view)
+{
+    if (m_view) {
+        disconnect(m_view, &DolphinView::activated,
+                   this, &VersionControlObserver::delayedDirectoryVerification);
+    }
+
+    m_view = view;
+
+    if (m_view) {
+        connect(m_view, &DolphinView::activated,
+                this, &VersionControlObserver::delayedDirectoryVerification);
+    }
+}
+
+DolphinView* VersionControlObserver::view() const
+{
+    return m_view;
+}
+
 QList<QAction*> VersionControlObserver::actions(const KFileItemList& items) const
 {
     bool hasNullItems = false;
@@ -97,11 +105,19 @@ QList<QAction*> VersionControlObserver::actions(const KFileItemList& items) cons
         }
     }
 
-    if (!m_model || hasNullItems || !isVersioned()) {
+    if (!m_model || hasNullItems) {
         return {};
     }
 
-    return m_plugin->actions(items);
+    if (isVersionControlled()) {
+        return m_plugin->versionControlActions(items);
+    } else {
+        QList<QAction*> actions;
+        for (const auto &plugin : qAsConst(m_plugins)) {
+            actions << plugin.first->outOfVersionControlActions(items);
+        }
+        return actions;
+    }
 }
 
 void VersionControlObserver::delayedDirectoryVerification()
@@ -116,6 +132,18 @@ void VersionControlObserver::silentDirectoryVerification()
     m_dirVerificationTimer->start();
 }
 
+void VersionControlObserver::slotItemsChanged(const KItemRangeList& itemRanges, const QSet<QByteArray>& roles)
+{
+    Q_UNUSED(itemRanges)
+
+    // Because "version" role is emitted by VCS plugin (ourselfs) we don't need to
+    // analyze it and update directory item states information. So lets check if
+    // there is only "version".
+    if ( !(roles.count() == 1 && roles.contains("version")) ) {
+        delayedDirectoryVerification();
+    }
+}
+
 void VersionControlObserver::verifyDirectory()
 {
     if (!m_model) {
@@ -127,21 +155,8 @@ void VersionControlObserver::verifyDirectory()
         return;
     }
 
-    if (m_plugin) {
-        m_plugin->disconnect(this);
-    }
-
     m_plugin = searchPlugin(rootItem.url());
     if (m_plugin) {
-        connect(m_plugin, &KVersionControlPlugin::itemVersionsChanged,
-                this, &VersionControlObserver::silentDirectoryVerification);
-        connect(m_plugin, &KVersionControlPlugin::infoMessage,
-                this, &VersionControlObserver::infoMessage);
-        connect(m_plugin, &KVersionControlPlugin::errorMessage,
-                this, &VersionControlObserver::errorMessage);
-        connect(m_plugin, &KVersionControlPlugin::operationCompletedMessage,
-                this, &VersionControlObserver::operationCompletedMessage);
-
         if (!m_versionedDirectory) {
             m_versionedDirectory = true;
 
@@ -250,7 +265,7 @@ int VersionControlObserver::createItemStatesList(QMap<QString, QVector<ItemState
         }
     }
 
-    if (items.count() > 0) {
+    if (!items.isEmpty()) {
         const QUrl& url = items.first().first.url();
         itemStates.insert(url.adjusted(QUrl::RemoveFilename).path(), items);
     }
@@ -258,18 +273,9 @@ int VersionControlObserver::createItemStatesList(QMap<QString, QVector<ItemState
     return index - firstIndex; // number of processed items
 }
 
-KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& directory) const
+KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& directory)
 {
-    static bool pluginsAvailable = true;
-    static QList<KVersionControlPlugin*> plugins;
-
-    if (!pluginsAvailable) {
-        // A searching for plugins has already been done, but no
-        // plugins are installed
-        return nullptr;
-    }
-
-    if (plugins.isEmpty()) {
+    if (!m_pluginsInitialized) {
         // No searching for plugins has been done yet. Query the KServiceTypeTrader for
         // all fileview version control plugins and remember them in 'plugins'.
         const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
@@ -277,16 +283,28 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& director
         const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
         for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
             if (enabledPlugins.contains((*it)->name())) {
-                KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
+                KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>(this);
                 if (plugin) {
-                    plugins.append(plugin);
+                    connect(plugin, &KVersionControlPlugin::itemVersionsChanged,
+                            this, &VersionControlObserver::silentDirectoryVerification);
+                    connect(plugin, &KVersionControlPlugin::infoMessage,
+                            this, &VersionControlObserver::infoMessage);
+                    connect(plugin, &KVersionControlPlugin::errorMessage,
+                            this, &VersionControlObserver::errorMessage);
+                    connect(plugin, &KVersionControlPlugin::operationCompletedMessage,
+                            this, &VersionControlObserver::operationCompletedMessage);
+
+                    m_plugins.append( qMakePair(plugin, plugin->fileName()) );
                 }
             }
         }
-        if (plugins.isEmpty()) {
-            pluginsAvailable = false;
-            return nullptr;
-        }
+        m_pluginsInitialized = true;
+    }
+
+    if (m_plugins.empty()) {
+        // A searching for plugins has already been done, but no
+        // plugins are installed
+        return nullptr;
     }
 
     // We use the number of upUrl() calls to find the best matching plugin
@@ -296,12 +314,12 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& director
 
     // Verify whether the current directory contains revision information
     // like .svn, .git, ...
-    foreach (KVersionControlPlugin* plugin, plugins) {
-        const QString fileName = directory.path() + '/' + plugin->fileName();
+    for (const auto &it : qAsConst(m_plugins)) {
+        const QString fileName = directory.path() + '/' + it.second;
         if (QFile::exists(fileName)) {
             // The score of this plugin is 0 (best), so we can just return this plugin,
             // instead of going through the plugin scoring procedure, we can't find a better one ;)
-            return plugin;
+            return it.first;
         }
 
         // Version control systems like Git provide the version information
@@ -315,10 +333,10 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& director
             QUrl upUrl = KIO::upUrl(dirUrl);
             int upUrlCounter = 1;
             while ((upUrlCounter < bestScore) && (upUrl != dirUrl)) {
-                const QString fileName = dirUrl.path() + '/' + plugin->fileName();
+                const QString fileName = dirUrl.path() + '/' + it.second;
                 if (QFile::exists(fileName)) {
                     if (upUrlCounter < bestScore) {
-                        bestPlugin = plugin;
+                        bestPlugin = it.first;
                         bestScore = upUrlCounter;
                     }
                     break;
@@ -333,7 +351,7 @@ KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& director
     return bestPlugin;
 }
 
-bool VersionControlObserver::isVersioned() const
+bool VersionControlObserver::isVersionControlled() const
 {
     return m_versionedDirectory && m_plugin;
 }