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 "kversioncontrolplugin.h"
25 #include <kdirlister.h>
28 #include <QAbstractProxyModel>
29 #include <QAbstractItemView>
30 #include <QMutexLocker>
34 * The performance of updating the version state of items depends
35 * on the used plugin. To prevent that Dolphin gets blocked by a
36 * slow plugin, the updating is delegated to a thread.
38 class UpdateItemStatesThread
: public QThread
41 UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
);
42 void setData(KVersionControlPlugin
* plugin
,
43 const QList
<VersionControlObserver::ItemState
>& itemStates
);
44 QList
<VersionControlObserver::ItemState
> itemStates() const;
45 bool retrievedItems() const;
51 bool m_retrievedItems
;
52 KVersionControlPlugin
* m_plugin
;
53 QMutex
* m_pluginMutex
;
54 QList
<VersionControlObserver::ItemState
> m_itemStates
;
57 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
) :
59 m_retrievedItems(false),
60 m_pluginMutex(pluginMutex
),
65 void UpdateItemStatesThread::setData(KVersionControlPlugin
* plugin
,
66 const QList
<VersionControlObserver::ItemState
>& itemStates
)
69 m_itemStates
= itemStates
;
72 void UpdateItemStatesThread::run()
74 Q_ASSERT(!m_itemStates
.isEmpty());
75 Q_ASSERT(m_plugin
!= 0);
77 // The items from m_itemStates may be located in different directory levels. The version
78 // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
79 // of doing an expensive search, we utilize the knowledge of the implementation of
80 // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
81 const QString directory
= m_itemStates
.last().item
.url().directory(KUrl::AppendTrailingSlash
);
83 QMutexLocker
locker(m_pluginMutex
);
84 m_retrievedItems
= false;
85 if (m_plugin
->beginRetrieval(directory
)) {
86 const int count
= m_itemStates
.count();
87 for (int i
= 0; i
< count
; ++i
) {
88 m_itemStates
[i
].version
= m_plugin
->versionState(m_itemStates
[i
].item
);
90 m_plugin
->endRetrieval();
91 m_retrievedItems
= true;
95 QList
<VersionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
100 bool UpdateItemStatesThread::retrievedItems() const
102 return m_retrievedItems
;
105 // ------------------------------------------------------------------------------------------------
107 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
109 m_pendingItemStatesUpdate(false),
110 m_versionedDirectory(false),
111 m_silentUpdate(false),
115 m_dirVerificationTimer(0),
116 m_pluginMutex(QMutex::Recursive
),
118 m_updateItemStatesThread(0)
122 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
123 m_dolphinModel
= (proxyModel
== 0) ?
124 qobject_cast
<DolphinModel
*>(view
->model()) :
125 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
126 if (m_dolphinModel
!= 0) {
127 m_dirLister
= m_dolphinModel
->dirLister();
128 connect(m_dirLister
, SIGNAL(completed()),
129 this, SLOT(delayedDirectoryVerification()));
131 // The verification timer specifies the timeout until the shown directory
132 // is checked whether it is versioned. Per default it is assumed that users
133 // don't iterate through versioned directories and a high timeout is used
134 // The timeout will be decreased as soon as a versioned directory has been
135 // found (see verifyDirectory()).
136 m_dirVerificationTimer
= new QTimer(this);
137 m_dirVerificationTimer
->setSingleShot(true);
138 m_dirVerificationTimer
->setInterval(500);
139 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
140 this, SLOT(verifyDirectory()));
144 VersionControlObserver::~VersionControlObserver()
146 if (m_updateItemStatesThread
!= 0) {
147 m_updateItemStatesThread
->terminate();
148 m_updateItemStatesThread
->wait();
154 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
156 if (m_dolphinModel
->hasVersionData() && (m_plugin
!= 0)) {
157 QMutexLocker
locker(&m_pluginMutex
);
158 return m_plugin
->contextMenuActions(items
);
160 return QList
<QAction
*>();
163 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
165 if (m_dolphinModel
->hasVersionData() && (m_plugin
!= 0)) {
166 QMutexLocker
locker(&m_pluginMutex
);
167 return m_plugin
->contextMenuActions(directory
);
170 return QList
<QAction
*>();
173 void VersionControlObserver::delayedDirectoryVerification()
175 m_silentUpdate
= false;
176 m_dirVerificationTimer
->start();
179 void VersionControlObserver::silentDirectoryVerification()
181 m_silentUpdate
= true;
182 m_dirVerificationTimer
->start();
185 void VersionControlObserver::verifyDirectory()
187 KUrl versionControlUrl
= m_dirLister
->url();
188 if (!versionControlUrl
.isLocalFile()) {
193 // TODO: just for testing purposes. A plugin approach will be used later.
194 m_plugin
= new SubversionPlugin();
195 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
196 this, SIGNAL(infoMessage(const QString
&)));
197 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
198 this, SIGNAL(errorMessage(const QString
&)));
199 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
200 this, SIGNAL(operationCompletedMessage(const QString
&)));
203 versionControlUrl
.addPath(m_plugin
->fileName());
204 const KFileItem item
= m_dirLister
->findByUrl(versionControlUrl
);
206 bool foundVersionInfo
= !item
.isNull();
207 if (!foundVersionInfo
&& m_versionedDirectory
) {
208 // Version control systems like Git provide the version information
209 // file only in the root directory. Check whether the version information file can
210 // be found in one of the parent directories.
215 if (foundVersionInfo
) {
216 if (!m_versionedDirectory
) {
217 m_versionedDirectory
= true;
219 // The directory is versioned. Assume that the user will further browse through
220 // versioned directories and decrease the verification timer.
221 m_dirVerificationTimer
->setInterval(100);
222 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
223 this, SLOT(delayedDirectoryVerification()));
224 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
225 this, SLOT(delayedDirectoryVerification()));
226 connect(m_plugin
, SIGNAL(versionStatesChanged()),
227 this, SLOT(silentDirectoryVerification()));
230 } else if (m_versionedDirectory
) {
231 m_versionedDirectory
= false;
233 // The directory is not versioned. Reset the verification timer to a higher
234 // value, so that browsing through non-versioned directories is not slown down
235 // by an immediate verification.
236 m_dirVerificationTimer
->setInterval(500);
237 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
238 this, SLOT(delayedDirectoryVerification()));
239 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
240 this, SLOT(delayedDirectoryVerification()));
241 disconnect(m_plugin
, SIGNAL(versionStatesChanged()),
242 this, SLOT(silentDirectoryVerification()));
246 void VersionControlObserver::applyUpdatedItemStates()
248 if (!m_updateItemStatesThread
->retrievedItems()) {
249 // ignore m_silentUpdate for an error message
250 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
254 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
255 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
256 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
257 // This works as the update of the data does not require a relayout of the views used in Dolphin.
258 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
259 m_dolphinModel
->blockSignals(true);
261 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
262 foreach (const ItemState
& itemState
, itemStates
) {
263 m_dolphinModel
->setData(itemState
.index
,
264 QVariant(static_cast<int>(itemState
.version
)),
268 m_dolphinModel
->blockSignals(signalsBlocked
);
269 m_view
->viewport()->repaint();
271 if (!m_silentUpdate
) {
272 // Using an empty message results in clearing the previously shown information message and showing
273 // the default status bar information. This is useful as the user already gets feedback that the
274 // operation has been completed because of the icon emblems.
275 emit
operationCompletedMessage(QString());
278 if (m_pendingItemStatesUpdate
) {
279 m_pendingItemStatesUpdate
= false;
284 void VersionControlObserver::updateItemStates()
286 Q_ASSERT(m_plugin
!= 0);
287 if (m_updateItemStatesThread
== 0) {
288 m_updateItemStatesThread
= new UpdateItemStatesThread(this, &m_pluginMutex
);
289 connect(m_updateItemStatesThread
, SIGNAL(finished()),
290 this, SLOT(applyUpdatedItemStates()));
292 if (m_updateItemStatesThread
->isRunning()) {
293 // An update is currently ongoing. Wait until the thread has finished
294 // the update (see applyUpdatedItemStates()).
295 m_pendingItemStatesUpdate
= true;
299 QList
<ItemState
> itemStates
;
300 addDirectory(QModelIndex(), itemStates
);
301 if (!itemStates
.isEmpty()) {
302 if (!m_silentUpdate
) {
303 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
305 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
306 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
310 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
312 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
313 for (int row
= 0; row
< rowCount
; ++row
) {
314 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
315 addDirectory(index
, itemStates
);
318 itemState
.index
= index
;
319 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
320 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
322 itemStates
.append(itemState
);
326 #include "versioncontrolobserver.moc"