1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "versioncontrolobserver.h"
22 #include "dolphin_versioncontrolsettings.h"
23 #include "dolphindebug.h"
24 #include "views/dolphinview.h"
25 #include "kitemviews/kfileitemmodel.h"
26 #include "updateitemstatesthread.h"
28 #include <KLocalizedString>
30 #include <KServiceTypeTrader>
34 VersionControlObserver::VersionControlObserver(QObject
* parent
) :
36 m_pendingItemStatesUpdate(false),
37 m_versionedDirectory(false),
38 m_silentUpdate(false),
41 m_dirVerificationTimer(nullptr),
42 m_pluginsInitialized(false),
44 m_updateItemStatesThread(nullptr)
46 // The verification timer specifies the timeout until the shown directory
47 // is checked whether it is versioned. Per default it is assumed that users
48 // don't iterate through versioned directories and a high timeout is used
49 // The timeout will be decreased as soon as a versioned directory has been
50 // found (see verifyDirectory()).
51 m_dirVerificationTimer
= new QTimer(this);
52 m_dirVerificationTimer
->setSingleShot(true);
53 m_dirVerificationTimer
->setInterval(500);
54 connect(m_dirVerificationTimer
, &QTimer::timeout
,
55 this, &VersionControlObserver::verifyDirectory
);
58 VersionControlObserver::~VersionControlObserver()
61 m_plugin
->disconnect(this);
66 void VersionControlObserver::setModel(KFileItemModel
* model
)
69 disconnect(m_model
, &KFileItemModel::itemsInserted
,
70 this, &VersionControlObserver::delayedDirectoryVerification
);
71 disconnect(m_model
, &KFileItemModel::itemsChanged
,
72 this, &VersionControlObserver::slotItemsChanged
);
78 connect(m_model
, &KFileItemModel::itemsInserted
,
79 this, &VersionControlObserver::delayedDirectoryVerification
);
80 connect(m_model
, &KFileItemModel::itemsChanged
,
81 this, &VersionControlObserver::slotItemsChanged
);
85 KFileItemModel
* VersionControlObserver::model() const
90 void VersionControlObserver::setView(DolphinView
* view
)
93 disconnect(m_view
, &DolphinView::activated
,
94 this, &VersionControlObserver::delayedDirectoryVerification
);
100 connect(m_view
, &DolphinView::activated
,
101 this, &VersionControlObserver::delayedDirectoryVerification
);
105 DolphinView
* VersionControlObserver::view() const
110 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
112 bool hasNullItems
= false;
113 foreach (const KFileItem
& item
, items
) {
115 qCWarning(DolphinDebug
) << "Requesting version-control-actions for empty items";
121 if (!m_model
|| hasNullItems
) {
125 if (isVersionControlled()) {
126 return m_plugin
->versionControlActions(items
);
128 QList
<QAction
*> actions
;
129 for (const auto &plugin
: qAsConst(m_plugins
)) {
130 actions
<< plugin
.first
->outOfVersionControlActions(items
);
136 void VersionControlObserver::delayedDirectoryVerification()
138 m_silentUpdate
= false;
139 m_dirVerificationTimer
->start();
142 void VersionControlObserver::silentDirectoryVerification()
144 m_silentUpdate
= true;
145 m_dirVerificationTimer
->start();
148 void VersionControlObserver::slotItemsChanged(const KItemRangeList
& itemRanges
, const QSet
<QByteArray
>& roles
)
152 // Because "version" role is emitted by VCS plugin (ourselfs) we don't need to
153 // analyze it and update directory item states information. So lets check if
154 // there is only "version".
155 if ( !(roles
.count() == 1 && roles
.contains("version")) ) {
156 delayedDirectoryVerification();
160 void VersionControlObserver::verifyDirectory()
166 const KFileItem rootItem
= m_model
->rootItem();
167 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
171 m_plugin
= searchPlugin(rootItem
.url());
173 if (!m_versionedDirectory
) {
174 m_versionedDirectory
= true;
176 // The directory is versioned. Assume that the user will further browse through
177 // versioned directories and decrease the verification timer.
178 m_dirVerificationTimer
->setInterval(100);
181 } else if (m_versionedDirectory
) {
182 m_versionedDirectory
= false;
184 // The directory is not versioned. Reset the verification timer to a higher
185 // value, so that browsing through non-versioned directories is not slown down
186 // by an immediate verification.
187 m_dirVerificationTimer
->setInterval(500);
191 void VersionControlObserver::slotThreadFinished()
193 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
194 m_updateItemStatesThread
= nullptr; // The thread deletes itself automatically (see updateItemStates())
196 if (!m_plugin
|| !thread
) {
200 const QMap
<QString
, QVector
<ItemState
> >& itemStates
= thread
->itemStates();
201 QMap
<QString
, QVector
<ItemState
> >::const_iterator it
= itemStates
.constBegin();
202 for (; it
!= itemStates
.constEnd(); ++it
) {
203 const QVector
<ItemState
>& items
= it
.value();
205 foreach (const ItemState
& item
, items
) {
206 const KFileItem
& fileItem
= item
.first
;
207 const KVersionControlPlugin::ItemVersion version
= item
.second
;
208 QHash
<QByteArray
, QVariant
> values
;
209 values
.insert("version", QVariant(version
));
210 m_model
->setData(m_model
->index(fileItem
), values
);
214 if (!m_silentUpdate
) {
215 // Using an empty message results in clearing the previously shown information message and showing
216 // the default status bar information. This is useful as the user already gets feedback that the
217 // operation has been completed because of the icon emblems.
218 emit
operationCompletedMessage(QString());
221 if (m_pendingItemStatesUpdate
) {
222 m_pendingItemStatesUpdate
= false;
227 void VersionControlObserver::updateItemStates()
230 if (m_updateItemStatesThread
) {
231 // An update is currently ongoing. Wait until the thread has finished
232 // the update (see slotThreadFinished()).
233 m_pendingItemStatesUpdate
= true;
237 QMap
<QString
, QVector
<ItemState
> > itemStates
;
238 createItemStatesList(itemStates
);
240 if (!itemStates
.isEmpty()) {
241 if (!m_silentUpdate
) {
242 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
244 m_updateItemStatesThread
= new UpdateItemStatesThread(m_plugin
, itemStates
);
245 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
246 this, &VersionControlObserver::slotThreadFinished
);
247 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
248 m_updateItemStatesThread
, &UpdateItemStatesThread::deleteLater
);
250 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
254 int VersionControlObserver::createItemStatesList(QMap
<QString
, QVector
<ItemState
> >& itemStates
,
255 const int firstIndex
)
257 const int itemCount
= m_model
->count();
258 const int currentExpansionLevel
= m_model
->expandedParentsCount(firstIndex
);
260 QVector
<ItemState
> items
;
261 items
.reserve(itemCount
- firstIndex
);
264 for (index
= firstIndex
; index
< itemCount
; ++index
) {
265 const int expansionLevel
= m_model
->expandedParentsCount(index
);
267 if (expansionLevel
== currentExpansionLevel
) {
269 itemState
.first
= m_model
->fileItem(index
);
270 itemState
.second
= KVersionControlPlugin::UnversionedVersion
;
272 items
.append(itemState
);
273 } else if (expansionLevel
> currentExpansionLevel
) {
275 index
+= createItemStatesList(itemStates
, index
) - 1;
281 if (!items
.isEmpty()) {
282 const QUrl
& url
= items
.first().first
.url();
283 itemStates
.insert(url
.adjusted(QUrl::RemoveFilename
).path(), items
);
286 return index
- firstIndex
; // number of processed items
289 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const QUrl
& directory
)
291 if (!m_pluginsInitialized
) {
292 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
293 // all fileview version control plugins and remember them in 'plugins'.
294 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
296 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
297 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
298 if (enabledPlugins
.contains((*it
)->name())) {
299 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>(this);
301 connect(plugin
, &KVersionControlPlugin::itemVersionsChanged
,
302 this, &VersionControlObserver::silentDirectoryVerification
);
303 connect(plugin
, &KVersionControlPlugin::infoMessage
,
304 this, &VersionControlObserver::infoMessage
);
305 connect(plugin
, &KVersionControlPlugin::errorMessage
,
306 this, &VersionControlObserver::errorMessage
);
307 connect(plugin
, &KVersionControlPlugin::operationCompletedMessage
,
308 this, &VersionControlObserver::operationCompletedMessage
);
310 m_plugins
.append( qMakePair(plugin
, plugin
->fileName()) );
314 m_pluginsInitialized
= true;
317 if (m_plugins
.empty()) {
318 // A searching for plugins has already been done, but no
319 // plugins are installed
323 // We use the number of upUrl() calls to find the best matching plugin
324 // for the given directory. The smaller value, the better it is (0 is best).
325 KVersionControlPlugin
* bestPlugin
= nullptr;
326 int bestScore
= INT_MAX
;
328 // Verify whether the current directory contains revision information
329 // like .svn, .git, ...
330 for (const auto &it
: qAsConst(m_plugins
)) {
331 const QString fileName
= directory
.path() + '/' + it
.second
;
332 if (QFile::exists(fileName
)) {
333 // The score of this plugin is 0 (best), so we can just return this plugin,
334 // instead of going through the plugin scoring procedure, we can't find a better one ;)
338 // Version control systems like Git provide the version information
339 // file only in the root directory. Check whether the version information file can
340 // be found in one of the parent directories. For performance reasons this
341 // step is only done, if the previous directory was marked as versioned by
342 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
343 // must be shown at least once.
344 if (m_versionedDirectory
) {
345 QUrl
dirUrl(directory
);
346 QUrl upUrl
= KIO::upUrl(dirUrl
);
347 int upUrlCounter
= 1;
348 while ((upUrlCounter
< bestScore
) && (upUrl
!= dirUrl
)) {
349 const QString fileName
= dirUrl
.path() + '/' + it
.second
;
350 if (QFile::exists(fileName
)) {
351 if (upUrlCounter
< bestScore
) {
352 bestPlugin
= it
.first
;
353 bestScore
= upUrlCounter
;
358 upUrl
= KIO::upUrl(dirUrl
);
367 bool VersionControlObserver::isVersionControlled() const
369 return m_versionedDirectory
&& m_plugin
;