1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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 <dolphinmodel.h>
23 #include "dolphin_versioncontrolsettings.h"
25 #include <kdirlister.h>
28 #include <kservicetypetrader.h>
29 #include <kversioncontrolplugin.h>
31 #include "updateitemstatesthread.h"
33 #include <QAbstractProxyModel>
34 #include <QAbstractItemView>
35 #include <QMutexLocker>
39 * Maintains a list of pending threads, that get regulary checked
40 * whether they are finished and hence can get deleted. QThread::wait()
41 * is never used to prevent any blocking of the user interface.
43 struct PendingThreadsSingleton
45 QList
<UpdateItemStatesThread
*> list
;
47 K_GLOBAL_STATIC(PendingThreadsSingleton
, s_pendingThreads
)
50 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
52 m_pendingItemStatesUpdate(false),
53 m_versionedDirectory(false),
54 m_silentUpdate(false),
58 m_dirVerificationTimer(0),
60 m_updateItemStatesThread(0)
64 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
65 m_dolphinModel
= (proxyModel
== 0) ?
66 qobject_cast
<DolphinModel
*>(view
->model()) :
67 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
68 if (m_dolphinModel
!= 0) {
69 m_dirLister
= m_dolphinModel
->dirLister();
70 connect(m_dirLister
, SIGNAL(completed()),
71 this, SLOT(delayedDirectoryVerification()));
73 // The verification timer specifies the timeout until the shown directory
74 // is checked whether it is versioned. Per default it is assumed that users
75 // don't iterate through versioned directories and a high timeout is used
76 // The timeout will be decreased as soon as a versioned directory has been
77 // found (see verifyDirectory()).
78 m_dirVerificationTimer
= new QTimer(this);
79 m_dirVerificationTimer
->setSingleShot(true);
80 m_dirVerificationTimer
->setInterval(500);
81 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
82 this, SLOT(verifyDirectory()));
86 VersionControlObserver::~VersionControlObserver()
88 if (m_updateItemStatesThread
!= 0) {
89 if (m_updateItemStatesThread
->isFinished()) {
90 delete m_updateItemStatesThread
;
91 m_updateItemStatesThread
= 0;
93 // The version controller gets deleted, while a thread still
94 // is working to get the version information. To avoid a blocking
95 // user interface, no waiting for the finished() signal of the thread is
96 // done. Instead the thread will be remembered inside the global
97 // list s_pendingThreads, which will checked regulary. The thread does
98 // not work on shared data that is part of the VersionController instance,
99 // so skipping the waiting is save.
100 disconnect(m_updateItemStatesThread
, SIGNAL(finished()),
101 this, SLOT(slotThreadFinished()));
102 s_pendingThreads
->list
.append(m_updateItemStatesThread
);
103 m_updateItemStatesThread
= 0;
108 m_plugin
->disconnect();
113 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
115 QList
<QAction
*> actions
;
116 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
117 actions
= m_plugin
->contextMenuActions(items
);
118 m_updateItemStatesThread
->endReadItemStates();
123 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
125 QList
<QAction
*> actions
;
126 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
127 actions
= m_plugin
->contextMenuActions(directory
);
128 m_updateItemStatesThread
->endReadItemStates();
134 void VersionControlObserver::delayedDirectoryVerification()
136 m_silentUpdate
= false;
137 m_dirVerificationTimer
->start();
140 void VersionControlObserver::silentDirectoryVerification()
142 m_silentUpdate
= true;
143 m_dirVerificationTimer
->start();
146 void VersionControlObserver::verifyDirectory()
148 if (!s_pendingThreads
->list
.isEmpty()) {
149 // Try to cleanup pending threads (see explanation in destructor)
150 QList
<UpdateItemStatesThread
*>::iterator it
= s_pendingThreads
->list
.begin();
151 while (it
!= s_pendingThreads
->list
.end()) {
152 if ((*it
)->isFinished()) {
153 (*it
)->deleteLater();
154 it
= s_pendingThreads
->list
.erase(it
);
161 KUrl versionControlUrl
= m_dirLister
->url();
162 if (!versionControlUrl
.isLocalFile()) {
167 m_plugin
->disconnect();
170 m_plugin
= searchPlugin(versionControlUrl
);
171 const bool foundVersionInfo
= (m_plugin
!= 0);
172 if (!foundVersionInfo
&& m_versionedDirectory
) {
173 // Version control systems like Git provide the version information
174 // file only in the root directory. Check whether the version information file can
175 // be found in one of the parent directories.
180 if (foundVersionInfo
) {
181 if (!m_versionedDirectory
) {
182 m_versionedDirectory
= true;
184 // The directory is versioned. Assume that the user will further browse through
185 // versioned directories and decrease the verification timer.
186 m_dirVerificationTimer
->setInterval(100);
187 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
188 this, SLOT(delayedDirectoryVerification()));
189 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
190 this, SLOT(delayedDirectoryVerification()));
191 connect(m_plugin
, SIGNAL(versionStatesChanged()),
192 this, SLOT(silentDirectoryVerification()));
193 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
194 this, SIGNAL(infoMessage(const QString
&)));
195 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
196 this, SIGNAL(errorMessage(const QString
&)));
197 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
198 this, SIGNAL(operationCompletedMessage(const QString
&)));
201 } else if (m_versionedDirectory
) {
202 m_versionedDirectory
= false;
204 // The directory is not versioned. Reset the verification timer to a higher
205 // value, so that browsing through non-versioned directories is not slown down
206 // by an immediate verification.
207 m_dirVerificationTimer
->setInterval(500);
208 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
209 this, SLOT(delayedDirectoryVerification()));
210 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
211 this, SLOT(delayedDirectoryVerification()));
215 void VersionControlObserver::slotThreadFinished()
221 if (!m_updateItemStatesThread
->retrievedItems()) {
222 // ignore m_silentUpdate for an error message
223 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
227 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
228 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
229 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
230 // This works as the update of the data does not require a relayout of the views used in Dolphin.
231 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
232 m_dolphinModel
->blockSignals(true);
234 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
235 foreach (const ItemState
& itemState
, itemStates
) {
236 m_dolphinModel
->setData(itemState
.index
,
237 QVariant(static_cast<int>(itemState
.version
)),
241 m_dolphinModel
->blockSignals(signalsBlocked
);
242 m_view
->viewport()->repaint();
244 if (!m_silentUpdate
) {
245 // Using an empty message results in clearing the previously shown information message and showing
246 // the default status bar information. This is useful as the user already gets feedback that the
247 // operation has been completed because of the icon emblems.
248 emit
operationCompletedMessage(QString());
251 if (m_pendingItemStatesUpdate
) {
252 m_pendingItemStatesUpdate
= false;
257 void VersionControlObserver::updateItemStates()
259 Q_ASSERT(m_plugin
!= 0);
260 if (m_updateItemStatesThread
== 0) {
261 m_updateItemStatesThread
= new UpdateItemStatesThread();
262 connect(m_updateItemStatesThread
, SIGNAL(finished()),
263 this, SLOT(slotThreadFinished()));
265 if (m_updateItemStatesThread
->isRunning()) {
266 // An update is currently ongoing. Wait until the thread has finished
267 // the update (see slotThreadFinished()).
268 m_pendingItemStatesUpdate
= true;
272 QList
<ItemState
> itemStates
;
273 addDirectory(QModelIndex(), itemStates
);
274 if (!itemStates
.isEmpty()) {
275 if (!m_silentUpdate
) {
276 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
278 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
279 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
283 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
285 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
286 for (int row
= 0; row
< rowCount
; ++row
) {
287 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
288 addDirectory(index
, itemStates
);
291 itemState
.index
= index
;
292 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
293 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
295 itemStates
.append(itemState
);
299 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
301 static bool pluginsAvailable
= true;
302 static QList
<KVersionControlPlugin
*> plugins
;
304 if (!pluginsAvailable
) {
305 // a searching for plugins has already been done, but no
306 // plugins are installed
310 if (plugins
.isEmpty()) {
311 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
312 // all fileview version control plugins and remember them in 'plugins'.
313 const QString disabledPlugins
= VersionControlSettings::disabledPlugins();
314 const QStringList disabledPluginsList
= disabledPlugins
.split(',');
316 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
317 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
318 if (!disabledPluginsList
.contains((*it
)->name())) {
319 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
320 Q_ASSERT(plugin
!= 0);
321 plugins
.append(plugin
);
324 if (plugins
.isEmpty()) {
325 pluginsAvailable
= false;
330 // verify whether the current directory contains revision information
331 // like .svn, .git, ...
332 foreach (KVersionControlPlugin
* plugin
, plugins
) {
333 KUrl fileUrl
= directory
;
334 fileUrl
.addPath(plugin
->fileName());
335 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
336 if (!item
.isNull()) {
344 bool VersionControlObserver::isVersioned() const
346 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
349 #include "versioncontrolobserver.moc"