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 KUrl versionControlUrl
= m_dirLister
->url();
135 if (!versionControlUrl
.isLocalFile()) {
140 m_plugin
->disconnect();
143 m_plugin
= searchPlugin(versionControlUrl
);
145 if (!m_versionedDirectory
) {
146 m_versionedDirectory
= true;
148 // The directory is versioned. Assume that the user will further browse through
149 // versioned directories and decrease the verification timer.
150 m_dirVerificationTimer
->setInterval(100);
151 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
152 this, SLOT(delayedDirectoryVerification()));
153 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
154 this, SLOT(delayedDirectoryVerification()));
155 connect(m_plugin
, SIGNAL(versionStatesChanged()),
156 this, SLOT(silentDirectoryVerification()));
157 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
158 this, SIGNAL(infoMessage(const QString
&)));
159 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
160 this, SIGNAL(errorMessage(const QString
&)));
161 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
162 this, SIGNAL(operationCompletedMessage(const QString
&)));
165 } else if (m_versionedDirectory
) {
166 m_versionedDirectory
= false;
168 // The directory is not versioned. Reset the verification timer to a higher
169 // value, so that browsing through non-versioned directories is not slown down
170 // by an immediate verification.
171 m_dirVerificationTimer
->setInterval(500);
172 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
173 this, SLOT(delayedDirectoryVerification()));
174 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
175 this, SLOT(delayedDirectoryVerification()));
179 void VersionControlObserver::slotThreadFinished()
185 if (!m_updateItemStatesThread
->retrievedItems()) {
186 // ignore m_silentUpdate for an error message
187 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
191 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
192 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
193 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
194 // This works as the update of the data does not require a relayout of the views used in Dolphin.
195 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
196 m_dolphinModel
->blockSignals(true);
198 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
199 foreach (const ItemState
& itemState
, itemStates
) {
200 m_dolphinModel
->setData(itemState
.index
,
201 QVariant(static_cast<int>(itemState
.version
)),
205 m_dolphinModel
->blockSignals(signalsBlocked
);
206 m_view
->viewport()->repaint();
208 if (!m_silentUpdate
) {
209 // Using an empty message results in clearing the previously shown information message and showing
210 // the default status bar information. This is useful as the user already gets feedback that the
211 // operation has been completed because of the icon emblems.
212 emit
operationCompletedMessage(QString());
215 if (m_pendingItemStatesUpdate
) {
216 m_pendingItemStatesUpdate
= false;
221 void VersionControlObserver::updateItemStates()
223 Q_ASSERT(m_plugin
!= 0);
224 if (m_updateItemStatesThread
== 0) {
225 m_updateItemStatesThread
= new UpdateItemStatesThread();
226 connect(m_updateItemStatesThread
, SIGNAL(finished()),
227 this, SLOT(slotThreadFinished()));
229 if (m_updateItemStatesThread
->isRunning()) {
230 // An update is currently ongoing. Wait until the thread has finished
231 // the update (see slotThreadFinished()).
232 m_pendingItemStatesUpdate
= true;
236 QList
<ItemState
> itemStates
;
237 addDirectory(QModelIndex(), itemStates
);
238 if (!itemStates
.isEmpty()) {
239 if (!m_silentUpdate
) {
240 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
242 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
243 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
247 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
249 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
250 for (int row
= 0; row
< rowCount
; ++row
) {
251 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
252 addDirectory(index
, itemStates
);
255 itemState
.index
= index
;
256 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
257 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
259 itemStates
.append(itemState
);
263 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
265 static bool pluginsAvailable
= true;
266 static QList
<KVersionControlPlugin
*> plugins
;
268 if (!pluginsAvailable
) {
269 // A searching for plugins has already been done, but no
270 // plugins are installed
274 if (plugins
.isEmpty()) {
275 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
276 // all fileview version control plugins and remember them in 'plugins'.
277 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
279 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
280 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
281 if (enabledPlugins
.contains((*it
)->name())) {
282 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
283 Q_ASSERT(plugin
!= 0);
284 plugins
.append(plugin
);
287 if (plugins
.isEmpty()) {
288 pluginsAvailable
= false;
293 // Verify whether the current directory contains revision information
294 // like .svn, .git, ...
295 foreach (KVersionControlPlugin
* plugin
, plugins
) {
296 // Use the KDirLister cache to check for .svn, .git, ... files
297 KUrl
dirUrl(directory
);
298 KUrl fileUrl
= dirUrl
;
299 fileUrl
.addPath(plugin
->fileName());
300 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
301 if (!item
.isNull()) {
305 // Version control systems like Git provide the version information
306 // file only in the root directory. Check whether the version information file can
307 // be found in one of the parent directories. For performance reasons this
308 // step is only done, if the previous directory was marked as versioned by
309 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
310 // must be shown at least once.
311 if (m_versionedDirectory
) {
312 KUrl upUrl
= dirUrl
.upUrl();
313 while (upUrl
!= dirUrl
) {
314 const QString filePath
= dirUrl
.pathOrUrl(KUrl::AddTrailingSlash
) + plugin
->fileName();
315 QFileInfo
file(filePath
);
320 upUrl
= dirUrl
.upUrl();
328 bool VersionControlObserver::isVersioned() const
330 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
333 #include "versioncontrolobserver.moc"