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"
27 #include <KServiceTypeTrader>
28 #include <kitemviews/kfileitemmodel.h>
29 #include <kversioncontrolplugin2.h>
31 #include "updateitemstatesthread.h"
34 #include <QMutexLocker>
37 VersionControlObserver::VersionControlObserver(QObject
* parent
) :
39 m_pendingItemStatesUpdate(false),
40 m_versionedDirectory(false),
41 m_silentUpdate(false),
43 m_dirVerificationTimer(0),
45 m_updateItemStatesThread(0)
47 // The verification timer specifies the timeout until the shown directory
48 // is checked whether it is versioned. Per default it is assumed that users
49 // don't iterate through versioned directories and a high timeout is used
50 // The timeout will be decreased as soon as a versioned directory has been
51 // found (see verifyDirectory()).
52 m_dirVerificationTimer
= new QTimer(this);
53 m_dirVerificationTimer
->setSingleShot(true);
54 m_dirVerificationTimer
->setInterval(500);
55 connect(m_dirVerificationTimer
, &QTimer::timeout
,
56 this, &VersionControlObserver::verifyDirectory
);
59 VersionControlObserver::~VersionControlObserver()
62 m_plugin
->disconnect(this);
67 void VersionControlObserver::setModel(KFileItemModel
* model
)
70 disconnect(m_model
, &KFileItemModel::itemsInserted
,
71 this, &VersionControlObserver::delayedDirectoryVerification
);
72 disconnect(m_model
, &KFileItemModel::itemsChanged
,
73 this, &VersionControlObserver::delayedDirectoryVerification
);
79 connect(m_model
, &KFileItemModel::itemsInserted
,
80 this, &VersionControlObserver::delayedDirectoryVerification
);
81 connect(m_model
, &KFileItemModel::itemsChanged
,
82 this, &VersionControlObserver::delayedDirectoryVerification
);
86 KFileItemModel
* VersionControlObserver::model() const
91 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
93 QList
<QAction
*> actions
;
95 bool hasNullItems
= false;
96 foreach (const KFileItem
& item
, items
) {
98 kWarning() << "Requesting version-control-actions for empty items";
104 if (!m_model
|| hasNullItems
) {
108 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
110 // Use version 2 of the KVersionControlPlugin which allows providing actions
111 // also for non-versioned directories.
112 actions
= pluginV2
->actions(items
);
113 } else if (isVersioned()) {
114 // Support deprecated interfaces from KVersionControlPlugin version 1.
115 // Context menu actions where only available for versioned directories.
117 if (items
.count() == 1) {
118 const KFileItem rootItem
= m_model
->rootItem();
119 if (!rootItem
.isNull() && items
.first().url() == rootItem
.url()) {
120 directory
= rootItem
.url().path();
124 actions
= directory
.isEmpty() ? m_plugin
->contextMenuActions(items
)
125 : m_plugin
->contextMenuActions(directory
);
131 void VersionControlObserver::delayedDirectoryVerification()
133 m_silentUpdate
= false;
134 m_dirVerificationTimer
->start();
137 void VersionControlObserver::silentDirectoryVerification()
139 m_silentUpdate
= true;
140 m_dirVerificationTimer
->start();
143 void VersionControlObserver::verifyDirectory()
149 const KFileItem rootItem
= m_model
->rootItem();
150 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
155 m_plugin
->disconnect(this);
158 m_plugin
= searchPlugin(rootItem
.url());
160 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
162 connect(pluginV2
, &KVersionControlPlugin2::itemVersionsChanged
,
163 this, &VersionControlObserver::silentDirectoryVerification
);
165 connect(m_plugin
, &KVersionControlPlugin::versionStatesChanged
,
166 this, &VersionControlObserver::silentDirectoryVerification
);
168 connect(m_plugin
, &KVersionControlPlugin::infoMessage
,
169 this, &VersionControlObserver::infoMessage
);
170 connect(m_plugin
, &KVersionControlPlugin::errorMessage
,
171 this, &VersionControlObserver::errorMessage
);
172 connect(m_plugin
, &KVersionControlPlugin::operationCompletedMessage
,
173 this, &VersionControlObserver::operationCompletedMessage
);
175 if (!m_versionedDirectory
) {
176 m_versionedDirectory
= true;
178 // The directory is versioned. Assume that the user will further browse through
179 // versioned directories and decrease the verification timer.
180 m_dirVerificationTimer
->setInterval(100);
183 } else if (m_versionedDirectory
) {
184 m_versionedDirectory
= false;
186 // The directory is not versioned. Reset the verification timer to a higher
187 // value, so that browsing through non-versioned directories is not slown down
188 // by an immediate verification.
189 m_dirVerificationTimer
->setInterval(500);
193 void VersionControlObserver::slotThreadFinished()
195 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
196 m_updateItemStatesThread
= 0; // The thread deletes itself automatically (see updateItemStates())
198 if (!m_plugin
|| !thread
) {
202 const QMap
<QString
, QVector
<ItemState
> >& itemStates
= thread
->itemStates();
203 QMap
<QString
, QVector
<ItemState
> >::const_iterator it
= itemStates
.constBegin();
204 for (; it
!= itemStates
.constEnd(); ++it
) {
205 const QVector
<ItemState
>& items
= it
.value();
207 foreach (const ItemState
& item
, items
) {
208 QHash
<QByteArray
, QVariant
> values
;
209 values
.insert("version", QVariant(item
.version
));
210 m_model
->setData(m_model
->index(item
.item
), 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
.item
= m_model
->fileItem(index
);
270 itemState
.version
= KVersionControlPlugin2::UnversionedVersion
;
272 items
.append(itemState
);
273 } else if (expansionLevel
> currentExpansionLevel
) {
275 index
+= createItemStatesList(itemStates
, index
) - 1;
281 if (items
.count() > 0) {
282 const KUrl
& url
= items
.first().item
.url();
283 itemStates
.insert(url
.directory(KUrl::AppendTrailingSlash
), items
);
286 return index
- firstIndex
; // number of processed items
289 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
291 static bool pluginsAvailable
= true;
292 static QList
<KVersionControlPlugin
*> plugins
;
294 if (!pluginsAvailable
) {
295 // A searching for plugins has already been done, but no
296 // plugins are installed
300 if (plugins
.isEmpty()) {
301 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
302 // all fileview version control plugins and remember them in 'plugins'.
303 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
305 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
306 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
307 if (enabledPlugins
.contains((*it
)->name())) {
308 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
310 plugins
.append(plugin
);
314 if (plugins
.isEmpty()) {
315 pluginsAvailable
= false;
320 // We use the number of upUrl() calls to find the best matching plugin
321 // for the given directory. The smaller value, the better it is (0 is best).
322 KVersionControlPlugin
* bestPlugin
= 0;
323 int bestScore
= INT_MAX
;
325 // Verify whether the current directory contains revision information
326 // like .svn, .git, ...
327 foreach (KVersionControlPlugin
* plugin
, plugins
) {
328 const QString fileName
= directory
.path(KUrl::AddTrailingSlash
) + plugin
->fileName();
329 if (QFile::exists(fileName
)) {
330 // The score of this plugin is 0 (best), so we can just return this plugin,
331 // instead of going through the plugin scoring procedure, we can't find a better one ;)
335 // Version control systems like Git provide the version information
336 // file only in the root directory. Check whether the version information file can
337 // be found in one of the parent directories. For performance reasons this
338 // step is only done, if the previous directory was marked as versioned by
339 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
340 // must be shown at least once.
341 if (m_versionedDirectory
) {
342 KUrl
dirUrl(directory
);
343 KUrl upUrl
= dirUrl
.upUrl();
344 int upUrlCounter
= 1;
345 while ((upUrlCounter
< bestScore
) && (upUrl
!= dirUrl
)) {
346 const QString fileName
= dirUrl
.path(KUrl::AddTrailingSlash
) + plugin
->fileName();
347 if (QFile::exists(fileName
)) {
348 if (upUrlCounter
< bestScore
) {
350 bestScore
= upUrlCounter
;
355 upUrl
= dirUrl
.upUrl();
364 bool VersionControlObserver::isVersioned() const
366 return m_versionedDirectory
&& m_plugin
;
369 #include "versioncontrolobserver.moc"