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),
40 m_pluginsInitialized(false),
42 m_updateItemStatesThread(nullptr)
44 // The verification timer specifies the timeout until the shown directory
45 // is checked whether it is versioned. Per default it is assumed that users
46 // don't iterate through versioned directories and a high timeout is used
47 // The timeout will be decreased as soon as a versioned directory has been
48 // found (see verifyDirectory()).
49 m_dirVerificationTimer
= new QTimer(this);
50 m_dirVerificationTimer
->setSingleShot(true);
51 m_dirVerificationTimer
->setInterval(500);
52 connect(m_dirVerificationTimer
, &QTimer::timeout
,
53 this, &VersionControlObserver::verifyDirectory
);
56 VersionControlObserver::~VersionControlObserver()
59 m_plugin
->disconnect(this);
64 void VersionControlObserver::setModel(KFileItemModel
* model
)
67 disconnect(m_model
, &KFileItemModel::itemsInserted
,
68 this, &VersionControlObserver::delayedDirectoryVerification
);
69 disconnect(m_model
, &KFileItemModel::itemsChanged
,
70 this, &VersionControlObserver::delayedDirectoryVerification
);
76 connect(m_model
, &KFileItemModel::itemsInserted
,
77 this, &VersionControlObserver::delayedDirectoryVerification
);
78 connect(m_model
, &KFileItemModel::itemsChanged
,
79 this, &VersionControlObserver::delayedDirectoryVerification
);
83 KFileItemModel
* VersionControlObserver::model() const
88 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
90 bool hasNullItems
= false;
91 foreach (const KFileItem
& item
, items
) {
93 qCWarning(DolphinDebug
) << "Requesting version-control-actions for empty items";
99 if (!m_model
|| hasNullItems
|| !isVersioned()) {
103 return m_plugin
->actions(items
);
106 void VersionControlObserver::delayedDirectoryVerification()
108 m_silentUpdate
= false;
109 m_dirVerificationTimer
->start();
112 void VersionControlObserver::silentDirectoryVerification()
114 m_silentUpdate
= true;
115 m_dirVerificationTimer
->start();
118 void VersionControlObserver::verifyDirectory()
124 const KFileItem rootItem
= m_model
->rootItem();
125 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
130 m_plugin
->disconnect(this);
133 m_plugin
= searchPlugin(rootItem
.url());
135 connect(m_plugin
, &KVersionControlPlugin::itemVersionsChanged
,
136 this, &VersionControlObserver::silentDirectoryVerification
);
137 connect(m_plugin
, &KVersionControlPlugin::infoMessage
,
138 this, &VersionControlObserver::infoMessage
);
139 connect(m_plugin
, &KVersionControlPlugin::errorMessage
,
140 this, &VersionControlObserver::errorMessage
);
141 connect(m_plugin
, &KVersionControlPlugin::operationCompletedMessage
,
142 this, &VersionControlObserver::operationCompletedMessage
);
144 if (!m_versionedDirectory
) {
145 m_versionedDirectory
= true;
147 // The directory is versioned. Assume that the user will further browse through
148 // versioned directories and decrease the verification timer.
149 m_dirVerificationTimer
->setInterval(100);
152 } else if (m_versionedDirectory
) {
153 m_versionedDirectory
= false;
155 // The directory is not versioned. Reset the verification timer to a higher
156 // value, so that browsing through non-versioned directories is not slown down
157 // by an immediate verification.
158 m_dirVerificationTimer
->setInterval(500);
162 void VersionControlObserver::slotThreadFinished()
164 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
165 m_updateItemStatesThread
= nullptr; // The thread deletes itself automatically (see updateItemStates())
167 if (!m_plugin
|| !thread
) {
171 const QMap
<QString
, QVector
<ItemState
> >& itemStates
= thread
->itemStates();
172 QMap
<QString
, QVector
<ItemState
> >::const_iterator it
= itemStates
.constBegin();
173 for (; it
!= itemStates
.constEnd(); ++it
) {
174 const QVector
<ItemState
>& items
= it
.value();
176 foreach (const ItemState
& item
, items
) {
177 const KFileItem
& fileItem
= item
.first
;
178 const KVersionControlPlugin::ItemVersion version
= item
.second
;
179 QHash
<QByteArray
, QVariant
> values
;
180 values
.insert("version", QVariant(version
));
181 m_model
->setData(m_model
->index(fileItem
), values
);
185 if (!m_silentUpdate
) {
186 // Using an empty message results in clearing the previously shown information message and showing
187 // the default status bar information. This is useful as the user already gets feedback that the
188 // operation has been completed because of the icon emblems.
189 emit
operationCompletedMessage(QString());
192 if (m_pendingItemStatesUpdate
) {
193 m_pendingItemStatesUpdate
= false;
198 void VersionControlObserver::updateItemStates()
201 if (m_updateItemStatesThread
) {
202 // An update is currently ongoing. Wait until the thread has finished
203 // the update (see slotThreadFinished()).
204 m_pendingItemStatesUpdate
= true;
208 QMap
<QString
, QVector
<ItemState
> > itemStates
;
209 createItemStatesList(itemStates
);
211 if (!itemStates
.isEmpty()) {
212 if (!m_silentUpdate
) {
213 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
215 m_updateItemStatesThread
= new UpdateItemStatesThread(m_plugin
, itemStates
);
216 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
217 this, &VersionControlObserver::slotThreadFinished
);
218 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
219 m_updateItemStatesThread
, &UpdateItemStatesThread::deleteLater
);
221 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
225 int VersionControlObserver::createItemStatesList(QMap
<QString
, QVector
<ItemState
> >& itemStates
,
226 const int firstIndex
)
228 const int itemCount
= m_model
->count();
229 const int currentExpansionLevel
= m_model
->expandedParentsCount(firstIndex
);
231 QVector
<ItemState
> items
;
232 items
.reserve(itemCount
- firstIndex
);
235 for (index
= firstIndex
; index
< itemCount
; ++index
) {
236 const int expansionLevel
= m_model
->expandedParentsCount(index
);
238 if (expansionLevel
== currentExpansionLevel
) {
240 itemState
.first
= m_model
->fileItem(index
);
241 itemState
.second
= KVersionControlPlugin::UnversionedVersion
;
243 items
.append(itemState
);
244 } else if (expansionLevel
> currentExpansionLevel
) {
246 index
+= createItemStatesList(itemStates
, index
) - 1;
252 if (items
.count() > 0) {
253 const QUrl
& url
= items
.first().first
.url();
254 itemStates
.insert(url
.adjusted(QUrl::RemoveFilename
).path(), items
);
257 return index
- firstIndex
; // number of processed items
260 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const QUrl
& directory
)
262 if (!m_pluginsInitialized
) {
263 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
264 // all fileview version control plugins and remember them in 'plugins'.
265 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
267 const KService::List pluginServices
= KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
268 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
269 if (enabledPlugins
.contains((*it
)->name())) {
270 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>(this);
272 m_plugins
.append(plugin
);
276 m_pluginsInitialized
= true;
279 if (m_plugins
.empty()) {
280 // A searching for plugins has already been done, but no
281 // plugins are installed
285 // We use the number of upUrl() calls to find the best matching plugin
286 // for the given directory. The smaller value, the better it is (0 is best).
287 KVersionControlPlugin
* bestPlugin
= nullptr;
288 int bestScore
= INT_MAX
;
290 // Verify whether the current directory contains revision information
291 // like .svn, .git, ...
292 foreach (KVersionControlPlugin
* plugin
, m_plugins
) {
293 const QString fileName
= directory
.path() + '/' + plugin
->fileName();
294 if (QFile::exists(fileName
)) {
295 // The score of this plugin is 0 (best), so we can just return this plugin,
296 // instead of going through the plugin scoring procedure, we can't find a better one ;)
300 // Version control systems like Git provide the version information
301 // file only in the root directory. Check whether the version information file can
302 // be found in one of the parent directories. For performance reasons this
303 // step is only done, if the previous directory was marked as versioned by
304 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
305 // must be shown at least once.
306 if (m_versionedDirectory
) {
307 QUrl
dirUrl(directory
);
308 QUrl upUrl
= KIO::upUrl(dirUrl
);
309 int upUrlCounter
= 1;
310 while ((upUrlCounter
< bestScore
) && (upUrl
!= dirUrl
)) {
311 const QString fileName
= dirUrl
.path() + '/' + plugin
->fileName();
312 if (QFile::exists(fileName
)) {
313 if (upUrlCounter
< bestScore
) {
315 bestScore
= upUrlCounter
;
320 upUrl
= KIO::upUrl(dirUrl
);
329 bool VersionControlObserver::isVersioned() const
331 return m_versionedDirectory
&& m_plugin
;