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 "kitemviews/kfileitemmodel.h"
25 #include "updateitemstatesthread.h"
27 #include <KLocalizedString>
29 #include <KServiceTypeTrader>
33 VersionControlObserver::VersionControlObserver(QObject
* parent
) :
35 m_pendingItemStatesUpdate(false),
36 m_versionedDirectory(false),
37 m_silentUpdate(false),
39 m_dirVerificationTimer(nullptr),
41 m_updateItemStatesThread(nullptr)
43 // The verification timer specifies the timeout until the shown directory
44 // is checked whether it is versioned. Per default it is assumed that users
45 // don't iterate through versioned directories and a high timeout is used
46 // The timeout will be decreased as soon as a versioned directory has been
47 // found (see verifyDirectory()).
48 m_dirVerificationTimer
= new QTimer(this);
49 m_dirVerificationTimer
->setSingleShot(true);
50 m_dirVerificationTimer
->setInterval(500);
51 connect(m_dirVerificationTimer
, &QTimer::timeout
,
52 this, &VersionControlObserver::verifyDirectory
);
55 VersionControlObserver::~VersionControlObserver()
58 m_plugin
->disconnect(this);
63 void VersionControlObserver::setModel(KFileItemModel
* model
)
66 disconnect(m_model
, &KFileItemModel::itemsInserted
,
67 this, &VersionControlObserver::delayedDirectoryVerification
);
68 disconnect(m_model
, &KFileItemModel::itemsChanged
,
69 this, &VersionControlObserver::delayedDirectoryVerification
);
75 connect(m_model
, &KFileItemModel::itemsInserted
,
76 this, &VersionControlObserver::delayedDirectoryVerification
);
77 connect(m_model
, &KFileItemModel::itemsChanged
,
78 this, &VersionControlObserver::delayedDirectoryVerification
);
82 KFileItemModel
* VersionControlObserver::model() const
87 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
89 bool hasNullItems
= false;
90 foreach (const KFileItem
& item
, items
) {
92 qCWarning(DolphinDebug
) << "Requesting version-control-actions for empty items";
98 if (!m_model
|| hasNullItems
|| !isVersioned()) {
102 return m_plugin
->actions(items
);
105 void VersionControlObserver::delayedDirectoryVerification()
107 m_silentUpdate
= false;
108 m_dirVerificationTimer
->start();
111 void VersionControlObserver::silentDirectoryVerification()
113 m_silentUpdate
= true;
114 m_dirVerificationTimer
->start();
117 void VersionControlObserver::verifyDirectory()
123 const KFileItem rootItem
= m_model
->rootItem();
124 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
129 m_plugin
->disconnect(this);
132 m_plugin
= searchPlugin(rootItem
.url());
134 connect(m_plugin
, &KVersionControlPlugin::itemVersionsChanged
,
135 this, &VersionControlObserver::silentDirectoryVerification
);
136 connect(m_plugin
, &KVersionControlPlugin::infoMessage
,
137 this, &VersionControlObserver::infoMessage
);
138 connect(m_plugin
, &KVersionControlPlugin::errorMessage
,
139 this, &VersionControlObserver::errorMessage
);
140 connect(m_plugin
, &KVersionControlPlugin::operationCompletedMessage
,
141 this, &VersionControlObserver::operationCompletedMessage
);
143 if (!m_versionedDirectory
) {
144 m_versionedDirectory
= true;
146 // The directory is versioned. Assume that the user will further browse through
147 // versioned directories and decrease the verification timer.
148 m_dirVerificationTimer
->setInterval(100);
151 } else if (m_versionedDirectory
) {
152 m_versionedDirectory
= false;
154 // The directory is not versioned. Reset the verification timer to a higher
155 // value, so that browsing through non-versioned directories is not slown down
156 // by an immediate verification.
157 m_dirVerificationTimer
->setInterval(500);
161 void VersionControlObserver::slotThreadFinished()
163 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
164 m_updateItemStatesThread
= nullptr; // The thread deletes itself automatically (see updateItemStates())
166 if (!m_plugin
|| !thread
) {
170 const QMap
<QString
, QVector
<ItemState
> >& itemStates
= thread
->itemStates();
171 QMap
<QString
, QVector
<ItemState
> >::const_iterator it
= itemStates
.constBegin();
172 for (; it
!= itemStates
.constEnd(); ++it
) {
173 const QVector
<ItemState
>& items
= it
.value();
175 foreach (const ItemState
& item
, items
) {
176 const KFileItem
& fileItem
= item
.first
;
177 const KVersionControlPlugin::ItemVersion version
= item
.second
;
178 QHash
<QByteArray
, QVariant
> values
;
179 values
.insert("version", QVariant(version
));
180 m_model
->setData(m_model
->index(fileItem
), values
);
184 if (!m_silentUpdate
) {
185 // Using an empty message results in clearing the previously shown information message and showing
186 // the default status bar information. This is useful as the user already gets feedback that the
187 // operation has been completed because of the icon emblems.
188 emit
operationCompletedMessage(QString());
191 if (m_pendingItemStatesUpdate
) {
192 m_pendingItemStatesUpdate
= false;
197 void VersionControlObserver::updateItemStates()
200 if (m_updateItemStatesThread
) {
201 // An update is currently ongoing. Wait until the thread has finished
202 // the update (see slotThreadFinished()).
203 m_pendingItemStatesUpdate
= true;
207 QMap
<QString
, QVector
<ItemState
> > itemStates
;
208 createItemStatesList(itemStates
);
210 if (!itemStates
.isEmpty()) {
211 if (!m_silentUpdate
) {
212 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
214 m_updateItemStatesThread
= new UpdateItemStatesThread(m_plugin
, itemStates
);
215 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
216 this, &VersionControlObserver::slotThreadFinished
);
217 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
218 m_updateItemStatesThread
, &UpdateItemStatesThread::deleteLater
);
220 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
224 int VersionControlObserver::createItemStatesList(QMap
<QString
, QVector
<ItemState
> >& itemStates
,
225 const int firstIndex
)
227 const int itemCount
= m_model
->count();
228 const int currentExpansionLevel
= m_model
->expandedParentsCount(firstIndex
);
230 QVector
<ItemState
> items
;
231 items
.reserve(itemCount
- firstIndex
);
234 for (index
= firstIndex
; index
< itemCount
; ++index
) {
235 const int expansionLevel
= m_model
->expandedParentsCount(index
);
237 if (expansionLevel
== currentExpansionLevel
) {
239 itemState
.first
= m_model
->fileItem(index
);
240 itemState
.second
= KVersionControlPlugin::UnversionedVersion
;
242 items
.append(itemState
);
243 } else if (expansionLevel
> currentExpansionLevel
) {
245 index
+= createItemStatesList(itemStates
, index
) - 1;
251 if (items
.count() > 0) {
252 const QUrl
& url
= items
.first().first
.url();
253 itemStates
.insert(url
.adjusted(QUrl::RemoveFilename
).path(), items
);
256 return index
- firstIndex
; // number of processed items
259 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const QUrl
& directory
) const
261 static bool pluginsAvailable
= true;
262 static QList
<KVersionControlPlugin
*> plugins
;
264 if (!pluginsAvailable
) {
265 // A searching for plugins has already been done, but no
266 // plugins are installed
270 if (plugins
.isEmpty()) {
271 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
272 // all fileview version control plugins and remember them in 'plugins'.
273 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
275 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
276 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
277 if (enabledPlugins
.contains((*it
)->name())) {
278 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
280 plugins
.append(plugin
);
284 if (plugins
.isEmpty()) {
285 pluginsAvailable
= false;
290 // We use the number of upUrl() calls to find the best matching plugin
291 // for the given directory. The smaller value, the better it is (0 is best).
292 KVersionControlPlugin
* bestPlugin
= nullptr;
293 int bestScore
= INT_MAX
;
295 // Verify whether the current directory contains revision information
296 // like .svn, .git, ...
297 foreach (KVersionControlPlugin
* plugin
, plugins
) {
298 const QString fileName
= directory
.path() + '/' + plugin
->fileName();
299 if (QFile::exists(fileName
)) {
300 // The score of this plugin is 0 (best), so we can just return this plugin,
301 // instead of going through the plugin scoring procedure, we can't find a better one ;)
305 // Version control systems like Git provide the version information
306 // file only in the root directory. Check whether the version information file can
307 // be found in one of the parent directories. For performance reasons this
308 // step is only done, if the previous directory was marked as versioned by
309 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
310 // must be shown at least once.
311 if (m_versionedDirectory
) {
312 QUrl
dirUrl(directory
);
313 QUrl upUrl
= KIO::upUrl(dirUrl
);
314 int upUrlCounter
= 1;
315 while ((upUrlCounter
< bestScore
) && (upUrl
!= dirUrl
)) {
316 const QString fileName
= dirUrl
.path() + '/' + plugin
->fileName();
317 if (QFile::exists(fileName
)) {
318 if (upUrlCounter
< bestScore
) {
320 bestScore
= upUrlCounter
;
325 upUrl
= KIO::upUrl(dirUrl
);
334 bool VersionControlObserver::isVersioned() const
336 return m_versionedDirectory
&& m_plugin
;