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 "pendingthreadsmaintainer.h"
32 #include "updateitemstatesthread.h"
34 #include <QAbstractProxyModel>
35 #include <QAbstractItemView>
36 #include <QMutexLocker>
39 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
41 m_pendingItemStatesUpdate(false),
42 m_versionedDirectory(false),
43 m_silentUpdate(false),
47 m_dirVerificationTimer(0),
49 m_updateItemStatesThread(0)
53 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
54 m_dolphinModel
= (proxyModel
== 0) ?
55 qobject_cast
<DolphinModel
*>(view
->model()) :
56 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
57 if (m_dolphinModel
!= 0) {
58 m_dirLister
= m_dolphinModel
->dirLister();
59 connect(m_dirLister
, SIGNAL(completed()),
60 this, SLOT(delayedDirectoryVerification()));
62 // The verification timer specifies the timeout until the shown directory
63 // is checked whether it is versioned. Per default it is assumed that users
64 // don't iterate through versioned directories and a high timeout is used
65 // The timeout will be decreased as soon as a versioned directory has been
66 // found (see verifyDirectory()).
67 m_dirVerificationTimer
= new QTimer(this);
68 m_dirVerificationTimer
->setSingleShot(true);
69 m_dirVerificationTimer
->setInterval(500);
70 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
71 this, SLOT(verifyDirectory()));
75 VersionControlObserver::~VersionControlObserver()
77 if (m_updateItemStatesThread
!= 0) {
78 if (m_updateItemStatesThread
->isFinished()) {
79 delete m_updateItemStatesThread
;
80 m_updateItemStatesThread
= 0;
82 // The version controller gets deleted, while a thread still
83 // is working to get the version information. To avoid a blocking
84 // user interface, the thread will be forwarded to the
85 // PendingThreadsMaintainer, which will delete the thread later.
86 disconnect(m_updateItemStatesThread
, SIGNAL(finished()),
87 this, SLOT(slotThreadFinished()));
88 PendingThreadsMaintainer::instance().append(m_updateItemStatesThread
);
89 m_updateItemStatesThread
= 0;
94 m_plugin
->disconnect();
99 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
101 QList
<QAction
*> actions
;
102 if (isVersioned() && m_updateItemStatesThread
->lockPlugin()) {
103 actions
= m_plugin
->contextMenuActions(items
);
104 m_updateItemStatesThread
->unlockPlugin();
109 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
111 QList
<QAction
*> actions
;
112 if (isVersioned() && m_updateItemStatesThread
->lockPlugin()) {
113 actions
= m_plugin
->contextMenuActions(directory
);
114 m_updateItemStatesThread
->unlockPlugin();
120 void VersionControlObserver::delayedDirectoryVerification()
122 m_silentUpdate
= false;
123 m_dirVerificationTimer
->start();
126 void VersionControlObserver::silentDirectoryVerification()
128 m_silentUpdate
= true;
129 m_dirVerificationTimer
->start();
132 void VersionControlObserver::verifyDirectory()
134 const KUrl versionControlUrl
= m_dirLister
->url();
135 if (!versionControlUrl
.isLocalFile()) {
140 m_plugin
->disconnect();
143 m_plugin
= searchPlugin(versionControlUrl
);
145 connect(m_plugin
, SIGNAL(versionStatesChanged()),
146 this, SLOT(silentDirectoryVerification()));
147 connect(m_plugin
, SIGNAL(infoMessage(QString
)),
148 this, SIGNAL(infoMessage(QString
)));
149 connect(m_plugin
, SIGNAL(errorMessage(QString
)),
150 this, SIGNAL(errorMessage(QString
)));
151 connect(m_plugin
, SIGNAL(operationCompletedMessage(QString
)),
152 this, SIGNAL(operationCompletedMessage(QString
)));
154 if (!m_versionedDirectory
) {
155 m_versionedDirectory
= true;
157 // The directory is versioned. Assume that the user will further browse through
158 // versioned directories and decrease the verification timer.
159 m_dirVerificationTimer
->setInterval(100);
160 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
161 this, SLOT(delayedDirectoryVerification()));
162 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
163 this, SLOT(delayedDirectoryVerification()));
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::slotThreadFinished()
186 if (!m_updateItemStatesThread
->retrievedItems()) {
187 // ignore m_silentUpdate for an error message
188 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
192 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
193 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
194 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
195 // This works as the update of the data does not require a relayout of the views used in Dolphin.
196 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
197 m_dolphinModel
->blockSignals(true);
199 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
200 foreach (const ItemState
& itemState
, itemStates
) {
201 m_dolphinModel
->setData(itemState
.index
,
202 QVariant(static_cast<int>(itemState
.version
)),
206 m_dolphinModel
->blockSignals(signalsBlocked
);
207 m_view
->viewport()->repaint();
209 if (!m_silentUpdate
) {
210 // Using an empty message results in clearing the previously shown information message and showing
211 // the default status bar information. This is useful as the user already gets feedback that the
212 // operation has been completed because of the icon emblems.
213 emit
operationCompletedMessage(QString());
216 if (m_pendingItemStatesUpdate
) {
217 m_pendingItemStatesUpdate
= false;
222 void VersionControlObserver::updateItemStates()
224 Q_ASSERT(m_plugin
!= 0);
225 if (m_updateItemStatesThread
== 0) {
226 m_updateItemStatesThread
= new UpdateItemStatesThread();
227 connect(m_updateItemStatesThread
, SIGNAL(finished()),
228 this, SLOT(slotThreadFinished()));
230 if (m_updateItemStatesThread
->isRunning()) {
231 // An update is currently ongoing. Wait until the thread has finished
232 // the update (see slotThreadFinished()).
233 m_pendingItemStatesUpdate
= true;
237 QList
<ItemState
> itemStates
;
238 addDirectory(QModelIndex(), itemStates
);
239 if (!itemStates
.isEmpty()) {
240 if (!m_silentUpdate
) {
241 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
243 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
244 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
248 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
250 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
251 for (int row
= 0; row
< rowCount
; ++row
) {
252 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
253 addDirectory(index
, itemStates
);
256 itemState
.index
= index
;
257 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
258 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
260 itemStates
.append(itemState
);
264 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
266 static bool pluginsAvailable
= true;
267 static QList
<KVersionControlPlugin
*> plugins
;
269 if (!pluginsAvailable
) {
270 // A searching for plugins has already been done, but no
271 // plugins are installed
275 if (plugins
.isEmpty()) {
276 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
277 // all fileview version control plugins and remember them in 'plugins'.
278 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
280 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
281 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
282 if (enabledPlugins
.contains((*it
)->name())) {
283 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
284 Q_ASSERT(plugin
!= 0);
285 plugins
.append(plugin
);
288 if (plugins
.isEmpty()) {
289 pluginsAvailable
= false;
294 // Verify whether the current directory contains revision information
295 // like .svn, .git, ...
296 foreach (KVersionControlPlugin
* plugin
, plugins
) {
297 // Use the KDirLister cache to check for .svn, .git, ... files
298 KUrl
dirUrl(directory
);
299 KUrl fileUrl
= dirUrl
;
300 fileUrl
.addPath(plugin
->fileName());
301 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
302 if (!item
.isNull()) {
306 // Version control systems like Git provide the version information
307 // file only in the root directory. Check whether the version information file can
308 // be found in one of the parent directories. For performance reasons this
309 // step is only done, if the previous directory was marked as versioned by
310 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
311 // must be shown at least once.
312 if (m_versionedDirectory
) {
313 KUrl upUrl
= dirUrl
.upUrl();
314 while (upUrl
!= dirUrl
) {
315 const QString filePath
= dirUrl
.pathOrUrl(KUrl::AddTrailingSlash
) + plugin
->fileName();
316 QFileInfo
file(filePath
);
321 upUrl
= dirUrl
.upUrl();
329 bool VersionControlObserver::isVersioned() const
331 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
334 #include "versioncontrolobserver.moc"