1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphin_versioncontrolsettings.h"
27 #include <KServiceTypeTrader>
28 #include <kversioncontrolplugin.h>
30 #include "pendingthreadsmaintainer.h"
31 #include "updateitemstatesthread.h"
33 #include <QAbstractProxyModel>
34 #include <QAbstractItemView>
35 #include <QMutexLocker>
38 #include <views/dolphinmodel.h>
40 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
42 m_pendingItemStatesUpdate(false),
43 m_versionedDirectory(false),
44 m_silentUpdate(false),
48 m_dirVerificationTimer(0),
50 m_updateItemStatesThread(0)
54 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
55 m_dolphinModel
= proxyModel
?
56 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel()) :
57 qobject_cast
<DolphinModel
*>(view
->model());
60 m_dirLister
= m_dolphinModel
->dirLister();
61 connect(m_dirLister
, SIGNAL(completed()),
62 this, SLOT(delayedDirectoryVerification()));
64 // The verification timer specifies the timeout until the shown directory
65 // is checked whether it is versioned. Per default it is assumed that users
66 // don't iterate through versioned directories and a high timeout is used
67 // The timeout will be decreased as soon as a versioned directory has been
68 // found (see verifyDirectory()).
69 m_dirVerificationTimer
= new QTimer(this);
70 m_dirVerificationTimer
->setSingleShot(true);
71 m_dirVerificationTimer
->setInterval(500);
72 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
73 this, SLOT(verifyDirectory()));
77 VersionControlObserver::~VersionControlObserver()
79 if (m_updateItemStatesThread
) {
80 if (m_updateItemStatesThread
->isFinished()) {
81 delete m_updateItemStatesThread
;
82 m_updateItemStatesThread
= 0;
84 // The version controller gets deleted, while a thread still
85 // is working to get the version information. To avoid a blocking
86 // user interface, the thread will be forwarded to the
87 // PendingThreadsMaintainer, which will delete the thread later.
88 disconnect(m_updateItemStatesThread
, SIGNAL(finished()),
89 this, SLOT(slotThreadFinished()));
90 PendingThreadsMaintainer::instance().append(m_updateItemStatesThread
);
91 m_updateItemStatesThread
= 0;
96 m_plugin
->disconnect();
101 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
103 QList
<QAction
*> actions
;
104 if (isVersioned() && m_updateItemStatesThread
->lockPlugin()) {
105 actions
= m_plugin
->contextMenuActions(items
);
106 m_updateItemStatesThread
->unlockPlugin();
111 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
113 QList
<QAction
*> actions
;
114 if (isVersioned() && m_updateItemStatesThread
->lockPlugin()) {
115 actions
= m_plugin
->contextMenuActions(directory
);
116 m_updateItemStatesThread
->unlockPlugin();
122 void VersionControlObserver::delayedDirectoryVerification()
124 m_silentUpdate
= false;
125 m_dirVerificationTimer
->start();
128 void VersionControlObserver::silentDirectoryVerification()
130 m_silentUpdate
= true;
131 m_dirVerificationTimer
->start();
134 void VersionControlObserver::verifyDirectory()
136 const KUrl versionControlUrl
= m_dirLister
->url();
137 if (!versionControlUrl
.isLocalFile()) {
142 m_plugin
->disconnect();
145 m_plugin
= searchPlugin(versionControlUrl
);
147 connect(m_plugin
, SIGNAL(versionStatesChanged()),
148 this, SLOT(silentDirectoryVerification()));
149 connect(m_plugin
, SIGNAL(infoMessage(QString
)),
150 this, SIGNAL(infoMessage(QString
)));
151 connect(m_plugin
, SIGNAL(errorMessage(QString
)),
152 this, SIGNAL(errorMessage(QString
)));
153 connect(m_plugin
, SIGNAL(operationCompletedMessage(QString
)),
154 this, SIGNAL(operationCompletedMessage(QString
)));
156 if (!m_versionedDirectory
) {
157 m_versionedDirectory
= true;
159 // The directory is versioned. Assume that the user will further browse through
160 // versioned directories and decrease the verification timer.
161 m_dirVerificationTimer
->setInterval(100);
162 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
163 this, SLOT(delayedDirectoryVerification()));
164 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
165 this, SLOT(delayedDirectoryVerification()));
168 } else if (m_versionedDirectory
) {
169 m_versionedDirectory
= false;
171 // The directory is not versioned. Reset the verification timer to a higher
172 // value, so that browsing through non-versioned directories is not slown down
173 // by an immediate verification.
174 m_dirVerificationTimer
->setInterval(500);
175 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
176 this, SLOT(delayedDirectoryVerification()));
177 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
178 this, SLOT(delayedDirectoryVerification()));
182 void VersionControlObserver::slotThreadFinished()
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()
227 if (!m_updateItemStatesThread
) {
228 m_updateItemStatesThread
= new UpdateItemStatesThread();
229 connect(m_updateItemStatesThread
, SIGNAL(finished()),
230 this, SLOT(slotThreadFinished()));
232 if (m_updateItemStatesThread
->isRunning()) {
233 // An update is currently ongoing. Wait until the thread has finished
234 // the update (see slotThreadFinished()).
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(); // slotThreadFinished() 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 QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
282 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
283 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
284 if (enabledPlugins
.contains((*it
)->name())) {
285 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
287 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 // Use the KDirLister cache to check for .svn, .git, ... files
301 KUrl
dirUrl(directory
);
302 KUrl fileUrl
= dirUrl
;
303 fileUrl
.addPath(plugin
->fileName());
304 const KFileItem item
= m_dirLister
->findByUrl(fileUrl
);
305 if (!item
.isNull()) {
309 // Version control systems like Git provide the version information
310 // file only in the root directory. Check whether the version information file can
311 // be found in one of the parent directories. For performance reasons this
312 // step is only done, if the previous directory was marked as versioned by
313 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
314 // must be shown at least once.
315 if (m_versionedDirectory
) {
316 KUrl upUrl
= dirUrl
.upUrl();
317 while (upUrl
!= dirUrl
) {
318 const QString filePath
= dirUrl
.pathOrUrl(KUrl::AddTrailingSlash
) + plugin
->fileName();
319 QFileInfo
file(filePath
);
324 upUrl
= dirUrl
.upUrl();
332 bool VersionControlObserver::isVersioned() const
334 return m_dolphinModel
->hasVersionData() && m_plugin
;
337 #include "versioncontrolobserver.moc"