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;
107 m_plugin
->disconnect();
111 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
113 QList
<QAction
*> actions
;
114 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
115 actions
= m_plugin
->contextMenuActions(items
);
116 m_updateItemStatesThread
->endReadItemStates();
121 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
123 QList
<QAction
*> actions
;
124 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
125 actions
= m_plugin
->contextMenuActions(directory
);
126 m_updateItemStatesThread
->endReadItemStates();
132 void VersionControlObserver::delayedDirectoryVerification()
134 m_silentUpdate
= false;
135 m_dirVerificationTimer
->start();
138 void VersionControlObserver::silentDirectoryVerification()
140 m_silentUpdate
= true;
141 m_dirVerificationTimer
->start();
144 void VersionControlObserver::verifyDirectory()
146 if (!s_pendingThreads
->list
.isEmpty()) {
147 // Try to cleanup pending threads (see explanation in destructor)
148 QList
<UpdateItemStatesThread
*>::iterator it
= s_pendingThreads
->list
.begin();
149 while (it
!= s_pendingThreads
->list
.end()) {
150 if ((*it
)->isFinished()) {
151 (*it
)->deleteLater();
152 it
= s_pendingThreads
->list
.erase(it
);
159 KUrl versionControlUrl
= m_dirLister
->url();
160 if (!versionControlUrl
.isLocalFile()) {
165 m_plugin
->disconnect();
168 m_plugin
= searchPlugin(versionControlUrl
);
169 const bool foundVersionInfo
= (m_plugin
!= 0);
170 if (!foundVersionInfo
&& m_versionedDirectory
) {
171 // Version control systems like Git provide the version information
172 // file only in the root directory. Check whether the version information file can
173 // be found in one of the parent directories.
178 if (foundVersionInfo
) {
179 if (!m_versionedDirectory
) {
180 m_versionedDirectory
= true;
182 // The directory is versioned. Assume that the user will further browse through
183 // versioned directories and decrease the verification timer.
184 m_dirVerificationTimer
->setInterval(100);
185 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
186 this, SLOT(delayedDirectoryVerification()));
187 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
188 this, SLOT(delayedDirectoryVerification()));
189 connect(m_plugin
, SIGNAL(versionStatesChanged()),
190 this, SLOT(silentDirectoryVerification()));
191 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
192 this, SIGNAL(infoMessage(const QString
&)));
193 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
194 this, SIGNAL(errorMessage(const QString
&)));
195 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
196 this, SIGNAL(operationCompletedMessage(const QString
&)));
199 } else if (m_versionedDirectory
) {
200 m_versionedDirectory
= false;
202 // The directory is not versioned. Reset the verification timer to a higher
203 // value, so that browsing through non-versioned directories is not slown down
204 // by an immediate verification.
205 m_dirVerificationTimer
->setInterval(500);
206 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
207 this, SLOT(delayedDirectoryVerification()));
208 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
209 this, SLOT(delayedDirectoryVerification()));
213 void VersionControlObserver::slotThreadFinished()
219 if (!m_updateItemStatesThread
->retrievedItems()) {
220 // ignore m_silentUpdate for an error message
221 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
225 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
226 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
227 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
228 // This works as the update of the data does not require a relayout of the views used in Dolphin.
229 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
230 m_dolphinModel
->blockSignals(true);
232 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
233 foreach (const ItemState
& itemState
, itemStates
) {
234 m_dolphinModel
->setData(itemState
.index
,
235 QVariant(static_cast<int>(itemState
.version
)),
239 m_dolphinModel
->blockSignals(signalsBlocked
);
240 m_view
->viewport()->repaint();
242 if (!m_silentUpdate
) {
243 // Using an empty message results in clearing the previously shown information message and showing
244 // the default status bar information. This is useful as the user already gets feedback that the
245 // operation has been completed because of the icon emblems.
246 emit
operationCompletedMessage(QString());
249 if (m_pendingItemStatesUpdate
) {
250 m_pendingItemStatesUpdate
= false;
255 void VersionControlObserver::updateItemStates()
257 Q_ASSERT(m_plugin
!= 0);
258 if (m_updateItemStatesThread
== 0) {
259 m_updateItemStatesThread
= new UpdateItemStatesThread();
260 connect(m_updateItemStatesThread
, SIGNAL(finished()),
261 this, SLOT(slotThreadFinished()));
263 if (m_updateItemStatesThread
->isRunning()) {
264 // An update is currently ongoing. Wait until the thread has finished
265 // the update (see slotThreadFinished()).
266 m_pendingItemStatesUpdate
= true;
270 QList
<ItemState
> itemStates
;
271 addDirectory(QModelIndex(), itemStates
);
272 if (!itemStates
.isEmpty()) {
273 if (!m_silentUpdate
) {
274 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
276 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
277 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
281 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
283 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
284 for (int row
= 0; row
< rowCount
; ++row
) {
285 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
286 addDirectory(index
, itemStates
);
289 itemState
.index
= index
;
290 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
291 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
293 itemStates
.append(itemState
);
297 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
299 static bool pluginsAvailable
= true;
300 static QList
<KVersionControlPlugin
*> plugins
;
302 if (!pluginsAvailable
) {
303 // a searching for plugins has already been done, but no
304 // plugins are installed
308 if (plugins
.isEmpty()) {
309 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
310 // all fileview version control plugins and remember them in 'plugins'.
311 const QString disabledPlugins
= VersionControlSettings::disabledPlugins();
312 const QStringList disabledPluginsList
= disabledPlugins
.split(',');
314 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
315 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
316 if (!disabledPluginsList
.contains((*it
)->name())) {
317 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
318 Q_ASSERT(plugin
!= 0);
319 plugins
.append(plugin
);
322 if (plugins
.isEmpty()) {
323 pluginsAvailable
= false;
328 // verify whether the current directory contains revision information
329 // like .svn, .git, ...
330 foreach (KVersionControlPlugin
* plugin
, plugins
) {
331 KUrl fileUrl
= directory
;
332 fileUrl
.addPath(plugin
->fileName());
333 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
334 if (!item
.isNull()) {
342 bool VersionControlObserver::isVersioned() const
344 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
347 #include "versioncontrolobserver.moc"