X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/9aee5d22513f0367febab54b38b3a7dc58d120bb..67ebd66f94356b4e66005b1072919cb7b5e858bb:/src/views/versioncontrol/versioncontrolobserver.cpp diff --git a/src/views/versioncontrol/versioncontrolobserver.cpp b/src/views/versioncontrol/versioncontrolobserver.cpp index 4b0d65029..7cf78c8aa 100644 --- a/src/views/versioncontrol/versioncontrolobserver.cpp +++ b/src/views/versioncontrol/versioncontrolobserver.cpp @@ -1,35 +1,21 @@ -/*************************************************************************** - * Copyright (C) 2009 by Peter Penz * - * * - * 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 + * + * 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 #include -#include "dolphindebug.h" #include -#include -#include "updateitemstatesthread.h" - -#include #include VersionControlObserver::VersionControlObserver(QObject* parent) : @@ -37,10 +23,12 @@ VersionControlObserver::VersionControlObserver(QObject* parent) : m_pendingItemStatesUpdate(false), m_versionedDirectory(false), m_silentUpdate(false), - m_model(0), - m_dirVerificationTimer(0), - m_plugin(0), - m_updateItemStatesThread(0) + m_view(nullptr), + m_model(nullptr), + m_dirVerificationTimer(nullptr), + m_pluginsInitialized(false), + m_plugin(nullptr), + m_updateItemStatesThread(nullptr) { // The verification timer specifies the timeout until the shown directory // is checked whether it is versioned. Per default it is assumed that users @@ -58,7 +46,7 @@ VersionControlObserver::~VersionControlObserver() { if (m_plugin) { m_plugin->disconnect(this); - m_plugin = 0; + m_plugin = 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 VersionControlObserver::actions(const KFileItemList& items) const { bool hasNullItems = false; @@ -97,11 +105,19 @@ QList 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 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& 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; @@ -163,7 +178,7 @@ void VersionControlObserver::verifyDirectory() void VersionControlObserver::slotThreadFinished() { UpdateItemStatesThread* thread = m_updateItemStatesThread; - m_updateItemStatesThread = 0; // The thread deletes itself automatically (see updateItemStates()) + m_updateItemStatesThread = nullptr; // The thread deletes itself automatically (see updateItemStates()) if (!m_plugin || !thread) { return; @@ -250,7 +265,7 @@ int VersionControlObserver::createItemStatesList(QMap 0) { + if (!items.isEmpty()) { const QUrl& url = items.first().first.url(); itemStates.insert(url.adjusted(QUrl::RemoveFilename).path(), items); } @@ -258,50 +273,53 @@ int VersionControlObserver::createItemStatesList(QMap plugins; - - if (!pluginsAvailable) { - // A searching for plugins has already been done, but no - // plugins are installed - return 0; - } - - 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(); - const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin"); + 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* plugin = (*it)->createInstance(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 0; - } + 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 // for the given directory. The smaller value, the better it is (0 is best). - KVersionControlPlugin* bestPlugin = 0; + KVersionControlPlugin* bestPlugin = nullptr; int bestScore = INT_MAX; // 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; }