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
);
144 const bool foundVersionInfo
= (m_plugin
!= 0);
145 if (!foundVersionInfo
&& m_versionedDirectory
) {
146 // Version control systems like Git provide the version information
147 // file only in the root directory. Check whether the version information file can
148 // be found in one of the parent directories.
153 if (foundVersionInfo
) {
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()));
164 connect(m_plugin
, SIGNAL(versionStatesChanged()),
165 this, SLOT(silentDirectoryVerification()));
166 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
167 this, SIGNAL(infoMessage(const QString
&)));
168 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
169 this, SIGNAL(errorMessage(const QString
&)));
170 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
171 this, SIGNAL(operationCompletedMessage(const QString
&)));
174 } else if (m_versionedDirectory
) {
175 m_versionedDirectory
= false;
177 // The directory is not versioned. Reset the verification timer to a higher
178 // value, so that browsing through non-versioned directories is not slown down
179 // by an immediate verification.
180 m_dirVerificationTimer
->setInterval(500);
181 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
182 this, SLOT(delayedDirectoryVerification()));
183 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
184 this, SLOT(delayedDirectoryVerification()));
188 void VersionControlObserver::slotThreadFinished()
194 if (!m_updateItemStatesThread
->retrievedItems()) {
195 // ignore m_silentUpdate for an error message
196 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
200 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
201 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
202 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
203 // This works as the update of the data does not require a relayout of the views used in Dolphin.
204 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
205 m_dolphinModel
->blockSignals(true);
207 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
208 foreach (const ItemState
& itemState
, itemStates
) {
209 m_dolphinModel
->setData(itemState
.index
,
210 QVariant(static_cast<int>(itemState
.version
)),
214 m_dolphinModel
->blockSignals(signalsBlocked
);
215 m_view
->viewport()->repaint();
217 if (!m_silentUpdate
) {
218 // Using an empty message results in clearing the previously shown information message and showing
219 // the default status bar information. This is useful as the user already gets feedback that the
220 // operation has been completed because of the icon emblems.
221 emit
operationCompletedMessage(QString());
224 if (m_pendingItemStatesUpdate
) {
225 m_pendingItemStatesUpdate
= false;
230 void VersionControlObserver::updateItemStates()
232 Q_ASSERT(m_plugin
!= 0);
233 if (m_updateItemStatesThread
== 0) {
234 m_updateItemStatesThread
= new UpdateItemStatesThread();
235 connect(m_updateItemStatesThread
, SIGNAL(finished()),
236 this, SLOT(slotThreadFinished()));
238 if (m_updateItemStatesThread
->isRunning()) {
239 // An update is currently ongoing. Wait until the thread has finished
240 // the update (see slotThreadFinished()).
241 m_pendingItemStatesUpdate
= true;
245 QList
<ItemState
> itemStates
;
246 addDirectory(QModelIndex(), itemStates
);
247 if (!itemStates
.isEmpty()) {
248 if (!m_silentUpdate
) {
249 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
251 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
252 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
256 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
258 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
259 for (int row
= 0; row
< rowCount
; ++row
) {
260 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
261 addDirectory(index
, itemStates
);
264 itemState
.index
= index
;
265 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
266 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
268 itemStates
.append(itemState
);
272 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
274 static bool pluginsAvailable
= true;
275 static QList
<KVersionControlPlugin
*> plugins
;
277 if (!pluginsAvailable
) {
278 // a searching for plugins has already been done, but no
279 // plugins are installed
283 if (plugins
.isEmpty()) {
284 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
285 // all fileview version control plugins and remember them in 'plugins'.
286 const QString disabledPlugins
= VersionControlSettings::disabledPlugins();
287 const QStringList disabledPluginsList
= disabledPlugins
.split(',');
289 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
290 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
291 if (!disabledPluginsList
.contains((*it
)->name())) {
292 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
293 Q_ASSERT(plugin
!= 0);
294 plugins
.append(plugin
);
297 if (plugins
.isEmpty()) {
298 pluginsAvailable
= false;
303 // verify whether the current directory contains revision information
304 // like .svn, .git, ...
305 foreach (KVersionControlPlugin
* plugin
, plugins
) {
306 KUrl fileUrl
= directory
;
307 fileUrl
.addPath(plugin
->fileName());
308 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
309 if (!item
.isNull()) {
317 bool VersionControlObserver::isVersioned() const
319 return m_dolphinModel
->hasVersionData() && (m_plugin
!= 0);
322 #include "versioncontrolobserver.moc"