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"
24 #include <KLocalizedString>
27 #include <KServiceTypeTrader>
28 #include <kitemviews/kfileitemmodel.h>
29 #include <kversioncontrolplugin2.h>
31 #include "updateitemstatesthread.h"
36 VersionControlObserver::VersionControlObserver(QObject
* parent
) :
38 m_pendingItemStatesUpdate(false),
39 m_versionedDirectory(false),
40 m_silentUpdate(false),
42 m_dirVerificationTimer(0),
44 m_updateItemStatesThread(0)
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::delayedDirectoryVerification
);
78 connect(m_model
, &KFileItemModel::itemsInserted
,
79 this, &VersionControlObserver::delayedDirectoryVerification
);
80 connect(m_model
, &KFileItemModel::itemsChanged
,
81 this, &VersionControlObserver::delayedDirectoryVerification
);
85 KFileItemModel
* VersionControlObserver::model() const
90 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
92 QList
<QAction
*> actions
;
94 bool hasNullItems
= false;
95 foreach (const KFileItem
& item
, items
) {
97 kWarning() << "Requesting version-control-actions for empty items";
103 if (!m_model
|| hasNullItems
) {
107 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
109 // Use version 2 of the KVersionControlPlugin which allows providing actions
110 // also for non-versioned directories.
111 actions
= pluginV2
->actions(items
);
112 } else if (isVersioned()) {
113 // Support deprecated interfaces from KVersionControlPlugin version 1.
114 // Context menu actions where only available for versioned directories.
116 if (items
.count() == 1) {
117 const KFileItem rootItem
= m_model
->rootItem();
118 if (!rootItem
.isNull() && items
.first().url() == rootItem
.url()) {
119 directory
= rootItem
.url().path();
123 actions
= directory
.isEmpty() ? m_plugin
->contextMenuActions(items
)
124 : m_plugin
->contextMenuActions(directory
);
130 void VersionControlObserver::delayedDirectoryVerification()
132 m_silentUpdate
= false;
133 m_dirVerificationTimer
->start();
136 void VersionControlObserver::silentDirectoryVerification()
138 m_silentUpdate
= true;
139 m_dirVerificationTimer
->start();
142 void VersionControlObserver::verifyDirectory()
148 const KFileItem rootItem
= m_model
->rootItem();
149 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
154 m_plugin
->disconnect(this);
157 m_plugin
= searchPlugin(rootItem
.url());
159 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
161 connect(pluginV2
, &KVersionControlPlugin2::itemVersionsChanged
,
162 this, &VersionControlObserver::silentDirectoryVerification
);
164 connect(m_plugin
, &KVersionControlPlugin::versionStatesChanged
,
165 this, &VersionControlObserver::silentDirectoryVerification
);
167 connect(m_plugin
, &KVersionControlPlugin::infoMessage
,
168 this, &VersionControlObserver::infoMessage
);
169 connect(m_plugin
, &KVersionControlPlugin::errorMessage
,
170 this, &VersionControlObserver::errorMessage
);
171 connect(m_plugin
, &KVersionControlPlugin::operationCompletedMessage
,
172 this, &VersionControlObserver::operationCompletedMessage
);
174 if (!m_versionedDirectory
) {
175 m_versionedDirectory
= true;
177 // The directory is versioned. Assume that the user will further browse through
178 // versioned directories and decrease the verification timer.
179 m_dirVerificationTimer
->setInterval(100);
182 } else if (m_versionedDirectory
) {
183 m_versionedDirectory
= false;
185 // The directory is not versioned. Reset the verification timer to a higher
186 // value, so that browsing through non-versioned directories is not slown down
187 // by an immediate verification.
188 m_dirVerificationTimer
->setInterval(500);
192 void VersionControlObserver::slotThreadFinished()
194 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
195 m_updateItemStatesThread
= 0; // The thread deletes itself automatically (see updateItemStates())
197 if (!m_plugin
|| !thread
) {
201 const QMap
<QString
, QVector
<ItemState
> >& itemStates
= thread
->itemStates();
202 QMap
<QString
, QVector
<ItemState
> >::const_iterator it
= itemStates
.constBegin();
203 for (; it
!= itemStates
.constEnd(); ++it
) {
204 const QVector
<ItemState
>& items
= it
.value();
206 foreach (const ItemState
& item
, items
) {
207 const KFileItem
& fileItem
= item
.first
;
208 const KVersionControlPlugin2::ItemVersion version
= item
.second
;
209 QHash
<QByteArray
, QVariant
> values
;
210 values
.insert("version", QVariant(version
));
211 m_model
->setData(m_model
->index(fileItem
), values
);
215 if (!m_silentUpdate
) {
216 // Using an empty message results in clearing the previously shown information message and showing
217 // the default status bar information. This is useful as the user already gets feedback that the
218 // operation has been completed because of the icon emblems.
219 emit
operationCompletedMessage(QString());
222 if (m_pendingItemStatesUpdate
) {
223 m_pendingItemStatesUpdate
= false;
228 void VersionControlObserver::updateItemStates()
231 if (m_updateItemStatesThread
) {
232 // An update is currently ongoing. Wait until the thread has finished
233 // the update (see slotThreadFinished()).
234 m_pendingItemStatesUpdate
= true;
238 QMap
<QString
, QVector
<ItemState
> > itemStates
;
239 createItemStatesList(itemStates
);
241 if (!itemStates
.isEmpty()) {
242 if (!m_silentUpdate
) {
243 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
245 m_updateItemStatesThread
= new UpdateItemStatesThread(m_plugin
, itemStates
);
246 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
247 this, &VersionControlObserver::slotThreadFinished
);
248 connect(m_updateItemStatesThread
, &UpdateItemStatesThread::finished
,
249 m_updateItemStatesThread
, &UpdateItemStatesThread::deleteLater
);
251 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
255 int VersionControlObserver::createItemStatesList(QMap
<QString
, QVector
<ItemState
> >& itemStates
,
256 const int firstIndex
)
258 const int itemCount
= m_model
->count();
259 const int currentExpansionLevel
= m_model
->expandedParentsCount(firstIndex
);
261 QVector
<ItemState
> items
;
262 items
.reserve(itemCount
- firstIndex
);
265 for (index
= firstIndex
; index
< itemCount
; ++index
) {
266 const int expansionLevel
= m_model
->expandedParentsCount(index
);
268 if (expansionLevel
== currentExpansionLevel
) {
270 itemState
.first
= m_model
->fileItem(index
);
271 itemState
.second
= KVersionControlPlugin2::UnversionedVersion
;
273 items
.append(itemState
);
274 } else if (expansionLevel
> currentExpansionLevel
) {
276 index
+= createItemStatesList(itemStates
, index
) - 1;
282 if (items
.count() > 0) {
283 const QUrl
& url
= items
.first().first
.url();
284 itemStates
.insert(url
.adjusted(QUrl::RemoveFilename
).path(), items
);
287 return index
- firstIndex
; // number of processed items
290 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const QUrl
& directory
) const
292 static bool pluginsAvailable
= true;
293 static QList
<KVersionControlPlugin
*> plugins
;
295 if (!pluginsAvailable
) {
296 // A searching for plugins has already been done, but no
297 // plugins are installed
301 if (plugins
.isEmpty()) {
302 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
303 // all fileview version control plugins and remember them in 'plugins'.
304 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
306 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
307 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
308 if (enabledPlugins
.contains((*it
)->name())) {
309 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
311 plugins
.append(plugin
);
315 if (plugins
.isEmpty()) {
316 pluginsAvailable
= false;
321 // We use the number of upUrl() calls to find the best matching plugin
322 // for the given directory. The smaller value, the better it is (0 is best).
323 KVersionControlPlugin
* bestPlugin
= 0;
324 int bestScore
= INT_MAX
;
326 // Verify whether the current directory contains revision information
327 // like .svn, .git, ...
328 foreach (KVersionControlPlugin
* plugin
, plugins
) {
329 const QString fileName
= directory
.path() + plugin
->fileName();
330 if (QFile::exists(fileName
)) {
331 // The score of this plugin is 0 (best), so we can just return this plugin,
332 // instead of going through the plugin scoring procedure, we can't find a better one ;)
336 // Version control systems like Git provide the version information
337 // file only in the root directory. Check whether the version information file can
338 // be found in one of the parent directories. For performance reasons this
339 // step is only done, if the previous directory was marked as versioned by
340 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
341 // must be shown at least once.
342 if (m_versionedDirectory
) {
343 QUrl
dirUrl(directory
);
344 QUrl upUrl
= KIO::upUrl(dirUrl
);
345 int upUrlCounter
= 1;
346 while ((upUrlCounter
< bestScore
) && (upUrl
!= dirUrl
)) {
347 const QString fileName
= dirUrl
.path() + plugin
->fileName();
348 if (QFile::exists(fileName
)) {
349 if (upUrlCounter
< bestScore
) {
351 bestScore
= upUrlCounter
;
356 upUrl
= KIO::upUrl(dirUrl
);
365 bool VersionControlObserver::isVersioned() const
367 return m_versionedDirectory
&& m_plugin
;