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>
38 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
40 m_pendingItemStatesUpdate(false),
41 m_versionedDirectory(false),
42 m_silentUpdate(false),
46 m_dirVerificationTimer(0),
48 m_updateItemStatesThread(0)
52 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
53 m_dolphinModel
= (proxyModel
== 0) ?
54 qobject_cast
<DolphinModel
*>(view
->model()) :
55 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
56 if (m_dolphinModel
!= 0) {
57 m_dirLister
= m_dolphinModel
->dirLister();
58 connect(m_dirLister
, SIGNAL(completed()),
59 this, SLOT(delayedDirectoryVerification()));
61 // The verification timer specifies the timeout until the shown directory
62 // is checked whether it is versioned. Per default it is assumed that users
63 // don't iterate through versioned directories and a high timeout is used
64 // The timeout will be decreased as soon as a versioned directory has been
65 // found (see verifyDirectory()).
66 m_dirVerificationTimer
= new QTimer(this);
67 m_dirVerificationTimer
->setSingleShot(true);
68 m_dirVerificationTimer
->setInterval(500);
69 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
70 this, SLOT(verifyDirectory()));
74 VersionControlObserver::~VersionControlObserver()
76 if (m_updateItemStatesThread
!= 0) {
77 disconnect(m_updateItemStatesThread
, SIGNAL(finished()),
78 this, SLOT(applyUpdatedItemStates()));
79 if (m_updateItemStatesThread
->isFinished()) {
80 delete m_updateItemStatesThread
;
82 m_updateItemStatesThread
->deleteWhenFinished();
84 m_updateItemStatesThread
= 0;
87 m_plugin
->disconnect();
91 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
93 QList
<QAction
*> actions
;
94 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
95 actions
= m_plugin
->contextMenuActions(items
);
96 m_updateItemStatesThread
->endReadItemStates();
101 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
103 QList
<QAction
*> actions
;
104 if (isVersioned() && m_updateItemStatesThread
->beginReadItemStates()) {
105 actions
= m_plugin
->contextMenuActions(directory
);
106 m_updateItemStatesThread
->endReadItemStates();
112 void VersionControlObserver::delayedDirectoryVerification()
114 m_silentUpdate
= false;
115 m_dirVerificationTimer
->start();
118 void VersionControlObserver::silentDirectoryVerification()
120 m_silentUpdate
= true;
121 m_dirVerificationTimer
->start();
124 void VersionControlObserver::verifyDirectory()
126 KUrl versionControlUrl
= m_dirLister
->url();
127 if (!versionControlUrl
.isLocalFile()) {
132 m_plugin
->disconnect();
135 m_plugin
= searchPlugin(versionControlUrl
);
136 const bool foundVersionInfo
= (m_plugin
!= 0);
137 if (!foundVersionInfo
&& m_versionedDirectory
) {
138 // Version control systems like Git provide the version information
139 // file only in the root directory. Check whether the version information file can
140 // be found in one of the parent directories.
145 if (foundVersionInfo
) {
146 if (!m_versionedDirectory
) {
147 m_versionedDirectory
= true;
149 // The directory is versioned. Assume that the user will further browse through
150 // versioned directories and decrease the verification timer.
151 m_dirVerificationTimer
->setInterval(100);
152 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
153 this, SLOT(delayedDirectoryVerification()));
154 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
155 this, SLOT(delayedDirectoryVerification()));
156 connect(m_plugin
, SIGNAL(versionStatesChanged()),
157 this, SLOT(silentDirectoryVerification()));
158 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
159 this, SIGNAL(infoMessage(const QString
&)));
160 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
161 this, SIGNAL(errorMessage(const QString
&)));
162 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
163 this, SIGNAL(operationCompletedMessage(const QString
&)));
166 } else if (m_versionedDirectory
) {
167 m_versionedDirectory
= false;
169 // The directory is not versioned. Reset the verification timer to a higher
170 // value, so that browsing through non-versioned directories is not slown down
171 // by an immediate verification.
172 m_dirVerificationTimer
->setInterval(500);
173 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
174 this, SLOT(delayedDirectoryVerification()));
175 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
176 this, SLOT(delayedDirectoryVerification()));
180 void VersionControlObserver::applyUpdatedItemStates()
183 // The signal finished() has been emitted, but the thread has been marked
184 // as invalid in the meantime. Just ignore the signal in this case.
188 if (!m_updateItemStatesThread
->retrievedItems()) {
189 // ignore m_silentUpdate for an error message
190 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
194 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
195 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
196 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
197 // This works as the update of the data does not require a relayout of the views used in Dolphin.
198 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
199 m_dolphinModel
->blockSignals(true);
201 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
202 foreach (const ItemState
& itemState
, itemStates
) {
203 m_dolphinModel
->setData(itemState
.index
,
204 QVariant(static_cast<int>(itemState
.version
)),
208 m_dolphinModel
->blockSignals(signalsBlocked
);
209 m_view
->viewport()->repaint();
211 if (!m_silentUpdate
) {
212 // Using an empty message results in clearing the previously shown information message and showing
213 // the default status bar information. This is useful as the user already gets feedback that the
214 // operation has been completed because of the icon emblems.
215 emit
operationCompletedMessage(QString());
218 if (m_pendingItemStatesUpdate
) {
219 m_pendingItemStatesUpdate
= false;
224 void VersionControlObserver::updateItemStates()
226 Q_ASSERT(m_plugin
!= 0);
227 if (m_updateItemStatesThread
== 0) {
228 m_updateItemStatesThread
= new UpdateItemStatesThread();
229 connect(m_updateItemStatesThread
, SIGNAL(finished()),
230 this, SLOT(applyUpdatedItemStates()));
232 if (m_updateItemStatesThread
->isRunning()) {
233 // An update is currently ongoing. Wait until the thread has finished
234 // the update (see applyUpdatedItemStates()).
235 m_pendingItemStatesUpdate
= true;
239 QList
<ItemState
> itemStates
;
240 addDirectory(QModelIndex(), itemStates
);
241 if (!itemStates
.isEmpty()) {
242 if (!m_silentUpdate
) {
243 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
245 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
246 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
250 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
252 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
253 for (int row
= 0; row
< rowCount
; ++row
) {
254 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
255 addDirectory(index
, itemStates
);
258 itemState
.index
= index
;
259 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
260 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
262 itemStates
.append(itemState
);
266 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
268 static bool pluginsAvailable
= true;
269 static QList
<KVersionControlPlugin
*> plugins
;
271 if (!pluginsAvailable
) {
272 // a searching for plugins has already been done, but no
273 // plugins are installed
277 if (plugins
.isEmpty()) {
278 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
279 // all fileview version control plugins and remember them in 'plugins'.
280 const QString disabledPlugins
= VersionControlSettings::disabledPlugins();
281 const QStringList disabledPluginsList
= disabledPlugins
.split(',');
283 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
284 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
285 if (!disabledPluginsList
.contains((*it
)->name())) {
286 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
287 Q_ASSERT(plugin
!= 0);
288 plugins
.append(plugin
);
291 if (plugins
.isEmpty()) {
292 pluginsAvailable
= false;
297 // verify whether the current directory contains revision information
298 // like .svn, .git, ...
299 foreach (KVersionControlPlugin
* plugin
, plugins
) {
300 KUrl fileUrl
= directory
;
301 fileUrl
.addPath(plugin
->fileName());
302 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
303 if (!item
.isNull()) {
311 bool VersionControlObserver::isVersioned() const
313 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
316 #include "versioncontrolobserver.moc"