2 * SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "versioncontrolobserver.h"
9 #include "dolphin_versioncontrolsettings.h"
10 #include "dolphindebug.h"
11 #include "kitemviews/kfileitemmodel.h"
12 #include "updateitemstatesthread.h"
13 #include "views/dolphinview.h"
15 #include <KLocalizedString>
16 #include <KPluginFactory>
17 #include <KPluginMetaData>
21 VersionControlObserver::VersionControlObserver(QObject
*parent
)
23 , m_pendingItemStatesUpdate(false)
24 , m_silentUpdate(false)
27 , m_dirVerificationTimer(nullptr)
28 , m_pluginsInitialized(false)
29 , m_currentPlugin(nullptr)
30 , m_updateItemStatesThread(nullptr)
32 // The verification timer specifies the timeout until the shown directory
33 // is checked whether it is versioned. Per default it is assumed that users
34 // don't iterate through versioned directories and a high timeout is used
35 // The timeout will be decreased as soon as a versioned directory has been
36 // found (see verifyDirectory()).
37 m_dirVerificationTimer
= new QTimer(this);
38 m_dirVerificationTimer
->setSingleShot(true);
39 m_dirVerificationTimer
->setInterval(500);
40 connect(m_dirVerificationTimer
, &QTimer::timeout
, this, &VersionControlObserver::verifyDirectory
);
43 VersionControlObserver::~VersionControlObserver()
45 if (m_currentPlugin
) {
46 m_currentPlugin
->disconnect(this);
48 if (m_updateItemStatesThread
) {
49 m_updateItemStatesThread
->requestInterruption();
50 m_updateItemStatesThread
->wait();
51 m_updateItemStatesThread
->deleteLater();
54 if (m_currentPlugin
) {
55 delete m_currentPlugin
;
56 m_currentPlugin
= nullptr;
61 void VersionControlObserver::setModel(KFileItemModel
*model
)
64 disconnect(m_model
, &KFileItemModel::itemsInserted
, this, &VersionControlObserver::delayedDirectoryVerification
);
65 disconnect(m_model
, &KFileItemModel::itemsChanged
, this, &VersionControlObserver::slotItemsChanged
);
71 connect(m_model
, &KFileItemModel::itemsInserted
, this, &VersionControlObserver::delayedDirectoryVerification
);
72 connect(m_model
, &KFileItemModel::itemsChanged
, this, &VersionControlObserver::slotItemsChanged
);
76 KFileItemModel
*VersionControlObserver::model() const
81 void VersionControlObserver::setView(DolphinView
*view
)
84 disconnect(m_view
, &DolphinView::activated
, this, &VersionControlObserver::delayedDirectoryVerification
);
90 connect(m_view
, &DolphinView::activated
, this, &VersionControlObserver::delayedDirectoryVerification
);
94 DolphinView
*VersionControlObserver::view() const
99 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
&items
) const
101 bool hasNullItems
= false;
102 for (const KFileItem
&item
: items
) {
104 qCWarning(DolphinDebug
) << "Requesting version-control-actions for empty items";
110 if (!m_model
|| hasNullItems
) {
114 if (isVersionControlled()) {
115 return m_currentPlugin
->versionControlActions(items
);
117 QList
<QAction
*> actions
;
118 for (const KVersionControlPlugin
*plugin
: std::as_const(m_plugins
)) {
119 actions
<< plugin
->outOfVersionControlActions(items
);
125 void VersionControlObserver::delayedDirectoryVerification()
127 m_silentUpdate
= false;
128 m_dirVerificationTimer
->start();
131 void VersionControlObserver::silentDirectoryVerification()
133 m_silentUpdate
= true;
134 m_dirVerificationTimer
->start();
137 void VersionControlObserver::slotItemsChanged(const KItemRangeList
&itemRanges
, const QSet
<QByteArray
> &roles
)
141 // Because "version" role is emitted by VCS plugin (ourselves) we don't need to
142 // analyze it and update directory item states information. So lets check if
143 // there is only "version".
144 if (!(roles
.count() == 1 && roles
.contains("version"))) {
145 delayedDirectoryVerification();
149 void VersionControlObserver::verifyDirectory()
155 const KFileItem rootItem
= m_model
->rootItem();
156 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
160 if (m_currentPlugin
&& rootItem
.url().path().startsWith(m_localRepoRoot
) && QFile::exists(m_localRepoRoot
+ '/' + m_currentPlugin
->fileName())) {
161 // current directory is still versionned
166 if ((m_currentPlugin
= searchPlugin(rootItem
.url()))) {
167 // The directory is versioned. Assume that the user will further browse through
168 // versioned directories and decrease the verification timer.
169 m_dirVerificationTimer
->setInterval(100);
174 // The directory is not versioned. Reset the verification timer to a higher
175 // value, so that browsing through non-versioned directories is not slown down
176 // by an immediate verification.
177 m_dirVerificationTimer
->setInterval(500);
180 void VersionControlObserver::slotThreadFinished()
182 UpdateItemStatesThread
*thread
= m_updateItemStatesThread
;
183 m_updateItemStatesThread
= nullptr; // The thread deletes itself automatically (see updateItemStates())
185 if (!m_currentPlugin
|| !thread
) {
189 const QMap
<QString
, QVector
<ItemState
>> &itemStates
= thread
->itemStates();
190 QMap
<QString
, QVector
<ItemState
>>::const_iterator it
= itemStates
.constBegin();
191 for (; it
!= itemStates
.constEnd(); ++it
) {
192 const QVector
<ItemState
> &items
= it
.value();
194 for (const ItemState
&item
: items
) {
195 const KFileItem
&fileItem
= item
.first
;
196 const KVersionControlPlugin::ItemVersion version
= item
.second
;
197 QHash
<QByteArray
, QVariant
> values
;
198 values
.insert("version", QVariant(version
));
199 m_model
->setData(m_model
->index(fileItem
), values
);
203 if (!m_silentUpdate
) {
204 // Using an empty message results in clearing the previously shown information message and showing
205 // the default status bar information. This is useful as the user already gets feedback that the
206 // operation has been completed because of the icon emblems.
207 Q_EMIT
operationCompletedMessage(QString());
210 if (m_pendingItemStatesUpdate
) {
211 m_pendingItemStatesUpdate
= false;
216 void VersionControlObserver::updateItemStates()
218 Q_ASSERT(m_currentPlugin
);
219 if (m_updateItemStatesThread
) {
220 // An update is currently ongoing. Wait until the thread has finished
221 // the update (see slotThreadFinished()).
222 m_pendingItemStatesUpdate
= true;
226 QMap
<QString
, QVector
<ItemState
>> itemStates
;
227 createItemStatesList(itemStates
);
229 if (!itemStates
.isEmpty()) {
230 if (!m_silentUpdate
) {
231 Q_EMIT
infoMessage(i18nc("@info:status", "Updating version information…"));
233 m_updateItemStatesThread
= new UpdateItemStatesThread(m_currentPlugin
, itemStates
);
234 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
, this, &VersionControlObserver::slotThreadFinished
);
235 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
, m_updateItemStatesThread
, &UpdateItemStatesThread::deleteLater
);
237 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
241 int VersionControlObserver::createItemStatesList(QMap
<QString
, QVector
<ItemState
>> &itemStates
, const int firstIndex
)
243 const int itemCount
= m_model
->count();
244 const int currentExpansionLevel
= m_model
->expandedParentsCount(firstIndex
);
246 QVector
<ItemState
> items
;
247 items
.reserve(itemCount
- firstIndex
);
250 for (index
= firstIndex
; index
< itemCount
; ++index
) {
251 const int expansionLevel
= m_model
->expandedParentsCount(index
);
253 if (expansionLevel
== currentExpansionLevel
) {
255 itemState
.first
= m_model
->fileItem(index
);
256 itemState
.second
= KVersionControlPlugin::UnversionedVersion
;
258 items
.append(itemState
);
259 } else if (expansionLevel
> currentExpansionLevel
) {
261 index
+= createItemStatesList(itemStates
, index
) - 1;
267 if (!items
.isEmpty()) {
268 const QUrl
&url
= items
.first().first
.url();
269 itemStates
.insert(url
.adjusted(QUrl::RemoveFilename
).path(), items
);
272 return index
- firstIndex
; // number of processed items
275 void VersionControlObserver::initPlugins()
277 if (!m_pluginsInitialized
) {
278 // No searching for plugins has been done yet. Query all fileview version control
279 // plugins and remember them in 'plugins'.
280 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
282 const QVector
<KPluginMetaData
> plugins
= KPluginMetaData::findPlugins(QStringLiteral("dolphin/vcs"));
284 for (const auto &p
: plugins
) {
285 if (enabledPlugins
.contains(p
.name())) {
286 auto plugin
= KPluginFactory::instantiatePlugin
<KVersionControlPlugin
>(p
, parent()).plugin
;
288 m_plugins
.append(plugin
);
293 for (const auto *plugin
: std::as_const(m_plugins
)) {
294 connect(plugin
, &KVersionControlPlugin::itemVersionsChanged
, this, &VersionControlObserver::silentDirectoryVerification
);
295 connect(plugin
, &KVersionControlPlugin::infoMessage
, this, &VersionControlObserver::infoMessage
);
296 connect(plugin
, &KVersionControlPlugin::errorMessage
, this, &VersionControlObserver::errorMessage
);
297 connect(plugin
, &KVersionControlPlugin::operationCompletedMessage
, this, &VersionControlObserver::operationCompletedMessage
);
300 m_pluginsInitialized
= true;
304 KVersionControlPlugin
*VersionControlObserver::searchPlugin(const QUrl
&directory
)
308 // Verify whether the current directory is under a version system
309 for (KVersionControlPlugin
*plugin
: std::as_const(m_plugins
)) {
310 // first naively check if we are at working copy root
311 const QString fileName
= directory
.path() + '/' + plugin
->fileName();
312 if (QFile::exists(fileName
)) {
313 m_localRepoRoot
= directory
.path();
317 const QString root
= plugin
->localRepositoryRoot(directory
.path());
318 if (!root
.isEmpty()) {
319 m_localRepoRoot
= root
;
326 bool VersionControlObserver::isVersionControlled() const
328 return m_currentPlugin
!= nullptr;
331 #include "moc_versioncontrolobserver.cpp"