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 "revisioncontrolobserver.h"
22 #include "dolphinmodel.h"
23 #include "revisioncontrolplugin.h"
25 #include <kdirlister.h>
27 #include <QAbstractProxyModel>
28 #include <QAbstractItemView>
29 #include <QMutexLocker>
33 * The performance of updating the revision state of items depends
34 * on the used plugin. To prevent that Dolphin gets blocked by a
35 * slow plugin, the updating is delegated to a thread.
37 class UpdateItemStatesThread
: public QThread
40 UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
);
41 void setData(RevisionControlPlugin
* plugin
,
42 const QList
<RevisionControlObserver::ItemState
>& itemStates
);
43 QList
<RevisionControlObserver::ItemState
> itemStates() const;
49 RevisionControlPlugin
* m_plugin
;
50 QMutex
* m_pluginMutex
;
51 QList
<RevisionControlObserver::ItemState
> m_itemStates
;
54 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
) :
56 m_pluginMutex(pluginMutex
)
60 void UpdateItemStatesThread::setData(RevisionControlPlugin
* plugin
,
61 const QList
<RevisionControlObserver::ItemState
>& itemStates
)
64 m_itemStates
= itemStates
;
67 void UpdateItemStatesThread::run()
69 Q_ASSERT(!m_itemStates
.isEmpty());
70 Q_ASSERT(m_plugin
!= 0);
72 // it is assumed that all items have the same parent directory
73 const QString directory
= m_itemStates
.first().item
.url().directory(KUrl::AppendTrailingSlash
);
75 QMutexLocker
locker(m_pluginMutex
);
76 if (m_plugin
->beginRetrieval(directory
)) {
77 const int count
= m_itemStates
.count();
78 for (int i
= 0; i
< count
; ++i
) {
79 m_itemStates
[i
].revision
= m_plugin
->revisionState(m_itemStates
[i
].item
);
81 m_plugin
->endRetrieval();
85 QList
<RevisionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
90 // ------------------------------------------------------------------------------------------------
92 RevisionControlObserver::RevisionControlObserver(QAbstractItemView
* view
) :
94 m_pendingItemStatesUpdate(false),
95 m_revisionedDirectory(false),
99 m_dirVerificationTimer(0),
100 m_pluginMutex(QMutex::Recursive
),
102 m_updateItemStatesThread(0)
106 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
107 m_dolphinModel
= (proxyModel
== 0) ?
108 qobject_cast
<DolphinModel
*>(view
->model()) :
109 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
110 if (m_dolphinModel
!= 0) {
111 m_dirLister
= m_dolphinModel
->dirLister();
112 connect(m_dirLister
, SIGNAL(completed()),
113 this, SLOT(delayedDirectoryVerification()));
115 // The verification timer specifies the timeout until the shown directory
116 // is checked whether it is versioned. Per default it is assumed that users
117 // don't iterate through versioned directories and a high timeout is used
118 // The timeout will be decreased as soon as a versioned directory has been
119 // found (see verifyDirectory()).
120 m_dirVerificationTimer
= new QTimer(this);
121 m_dirVerificationTimer
->setSingleShot(true);
122 m_dirVerificationTimer
->setInterval(500);
123 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
124 this, SLOT(verifyDirectory()));
128 RevisionControlObserver::~RevisionControlObserver()
134 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const KFileItemList
& items
) const
136 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
137 QMutexLocker
locker(&m_pluginMutex
);
138 return m_plugin
->contextMenuActions(items
);
140 return QList
<QAction
*>();
143 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const QString
& directory
) const
145 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
146 QMutexLocker
locker(&m_pluginMutex
);
147 return m_plugin
->contextMenuActions(directory
);
150 return QList
<QAction
*>();
153 void RevisionControlObserver::delayedDirectoryVerification()
155 m_dirVerificationTimer
->start();
158 void RevisionControlObserver::verifyDirectory()
160 KUrl revisionControlUrl
= m_dirLister
->url();
161 if (!revisionControlUrl
.isLocalFile()) {
166 // TODO: just for testing purposes. A plugin approach will be used later.
167 m_plugin
= new SubversionPlugin();
170 revisionControlUrl
.addPath(m_plugin
->fileName());
171 const KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
173 bool foundRevisionInfo
= !item
.isNull();
174 if (!foundRevisionInfo
&& m_revisionedDirectory
) {
175 // Revision control systems like Git provide the revision information
176 // file only in the root directory. Check whether the revision information file can
177 // be found in one of the parent directories.
182 if (foundRevisionInfo
) {
183 if (!m_revisionedDirectory
) {
184 m_revisionedDirectory
= true;
186 // The directory is versioned. Assume that the user will further browse through
187 // versioned directories and decrease the verification timer.
188 m_dirVerificationTimer
->setInterval(100);
189 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
190 this, SLOT(delayedDirectoryVerification()));
191 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
192 this, SLOT(delayedDirectoryVerification()));
193 connect(m_plugin
, SIGNAL(revisionStatesChanged(const QString
&)),
194 this, SLOT(delayedDirectoryVerification()));
197 } else if (m_revisionedDirectory
) {
198 m_revisionedDirectory
= false;
200 // The directory is not versioned. Reset the verification timer to a higher
201 // value, so that browsing through non-versioned directories is not slown down
202 // by an immediate verification.
203 m_dirVerificationTimer
->setInterval(500);
204 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
205 this, SLOT(delayedDirectoryVerification()));
206 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
207 this, SLOT(delayedDirectoryVerification()));
208 disconnect(m_plugin
, SIGNAL(revisionStatesChanged(const QString
&)),
209 this, SLOT(delayedDirectoryVerification()));
213 void RevisionControlObserver::applyUpdatedItemStates()
215 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
216 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
217 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
218 // This works as the update of the data does not require a relayout of the views used in Dolphin.
219 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
220 m_dolphinModel
->blockSignals(true);
222 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
223 foreach (const ItemState
& itemState
, itemStates
) {
224 m_dolphinModel
->setData(itemState
.index
,
225 QVariant(static_cast<int>(itemState
.revision
)),
229 m_dolphinModel
->blockSignals(signalsBlocked
);
230 m_view
->viewport()->repaint();
232 if (m_pendingItemStatesUpdate
) {
233 m_pendingItemStatesUpdate
= false;
238 void RevisionControlObserver::updateItemStates()
240 Q_ASSERT(m_plugin
!= 0);
241 if (m_updateItemStatesThread
== 0) {
242 m_updateItemStatesThread
= new UpdateItemStatesThread(this, &m_pluginMutex
);
243 connect(m_updateItemStatesThread
, SIGNAL(finished()),
244 this, SLOT(applyUpdatedItemStates()));
246 if (m_updateItemStatesThread
->isRunning()) {
247 // An update is currently ongoing. Wait until the thread has finished
248 // the update (see applyUpdatedItemStates()).
249 m_pendingItemStatesUpdate
= true;
253 const int rowCount
= m_dolphinModel
->rowCount();
255 // Build a list of all items in the current directory and delegate
256 // this list to the thread, which adjusts the revision states.
257 QList
<ItemState
> itemStates
;
258 for (int row
= 0; row
< rowCount
; ++row
) {
259 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
262 itemState
.index
= index
;
263 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
264 itemState
.revision
= RevisionControlPlugin::UnversionedRevision
;
266 itemStates
.append(itemState
);
269 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
270 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
274 #include "revisioncontrolobserver.moc"