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>
28 #include <QAbstractProxyModel>
29 #include <QAbstractItemView>
30 #include <QMutexLocker>
34 * The performance of updating the revision 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(RevisionControlPlugin
* plugin
,
43 const QList
<RevisionControlObserver::ItemState
>& itemStates
);
44 QList
<RevisionControlObserver::ItemState
> itemStates() const;
45 bool retrievedItems() const;
51 bool m_retrievedItems
;
52 RevisionControlPlugin
* m_plugin
;
53 QMutex
* m_pluginMutex
;
54 QList
<RevisionControlObserver::ItemState
> m_itemStates
;
57 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
) :
59 m_retrievedItems(false),
60 m_pluginMutex(pluginMutex
),
65 void UpdateItemStatesThread::setData(RevisionControlPlugin
* plugin
,
66 const QList
<RevisionControlObserver::ItemState
>& itemStates
)
69 m_itemStates
= itemStates
;
72 void UpdateItemStatesThread::run()
74 Q_ASSERT(!m_itemStates
.isEmpty());
75 Q_ASSERT(m_plugin
!= 0);
77 // it is assumed that all items have the same parent directory
78 const QString directory
= m_itemStates
.first().item
.url().directory(KUrl::AppendTrailingSlash
);
80 QMutexLocker
locker(m_pluginMutex
);
81 m_retrievedItems
= false;
82 if (m_plugin
->beginRetrieval(directory
)) {
83 const int count
= m_itemStates
.count();
84 for (int i
= 0; i
< count
; ++i
) {
85 m_itemStates
[i
].revision
= m_plugin
->revisionState(m_itemStates
[i
].item
);
87 m_plugin
->endRetrieval();
88 m_retrievedItems
= true;
92 QList
<RevisionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
97 bool UpdateItemStatesThread::retrievedItems() const
99 return m_retrievedItems
;
102 // ------------------------------------------------------------------------------------------------
104 RevisionControlObserver::RevisionControlObserver(QAbstractItemView
* view
) :
106 m_pendingItemStatesUpdate(false),
107 m_revisionedDirectory(false),
111 m_dirVerificationTimer(0),
112 m_pluginMutex(QMutex::Recursive
),
114 m_updateItemStatesThread(0)
118 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
119 m_dolphinModel
= (proxyModel
== 0) ?
120 qobject_cast
<DolphinModel
*>(view
->model()) :
121 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
122 if (m_dolphinModel
!= 0) {
123 m_dirLister
= m_dolphinModel
->dirLister();
124 connect(m_dirLister
, SIGNAL(completed()),
125 this, SLOT(delayedDirectoryVerification()));
127 // The verification timer specifies the timeout until the shown directory
128 // is checked whether it is versioned. Per default it is assumed that users
129 // don't iterate through versioned directories and a high timeout is used
130 // The timeout will be decreased as soon as a versioned directory has been
131 // found (see verifyDirectory()).
132 m_dirVerificationTimer
= new QTimer(this);
133 m_dirVerificationTimer
->setSingleShot(true);
134 m_dirVerificationTimer
->setInterval(500);
135 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
136 this, SLOT(verifyDirectory()));
140 RevisionControlObserver::~RevisionControlObserver()
146 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const KFileItemList
& items
) const
148 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
149 QMutexLocker
locker(&m_pluginMutex
);
150 return m_plugin
->contextMenuActions(items
);
152 return QList
<QAction
*>();
155 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const QString
& directory
) const
157 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
158 QMutexLocker
locker(&m_pluginMutex
);
159 return m_plugin
->contextMenuActions(directory
);
162 return QList
<QAction
*>();
165 void RevisionControlObserver::delayedDirectoryVerification()
167 m_dirVerificationTimer
->start();
170 void RevisionControlObserver::verifyDirectory()
172 KUrl revisionControlUrl
= m_dirLister
->url();
173 if (!revisionControlUrl
.isLocalFile()) {
178 // TODO: just for testing purposes. A plugin approach will be used later.
179 m_plugin
= new SubversionPlugin();
180 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
181 this, SIGNAL(infoMessage(const QString
&)));
182 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
183 this, SIGNAL(errorMessage(const QString
&)));
184 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
185 this, SIGNAL(operationCompletedMessage(const QString
&)));
188 revisionControlUrl
.addPath(m_plugin
->fileName());
189 const KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
191 bool foundRevisionInfo
= !item
.isNull();
192 if (!foundRevisionInfo
&& m_revisionedDirectory
) {
193 // Revision control systems like Git provide the revision information
194 // file only in the root directory. Check whether the revision information file can
195 // be found in one of the parent directories.
200 if (foundRevisionInfo
) {
201 if (!m_revisionedDirectory
) {
202 m_revisionedDirectory
= true;
204 // The directory is versioned. Assume that the user will further browse through
205 // versioned directories and decrease the verification timer.
206 m_dirVerificationTimer
->setInterval(100);
207 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
208 this, SLOT(delayedDirectoryVerification()));
209 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
210 this, SLOT(delayedDirectoryVerification()));
211 connect(m_plugin
, SIGNAL(revisionStatesChanged()),
212 this, SLOT(delayedDirectoryVerification()));
215 } else if (m_revisionedDirectory
) {
216 m_revisionedDirectory
= false;
218 // The directory is not versioned. Reset the verification timer to a higher
219 // value, so that browsing through non-versioned directories is not slown down
220 // by an immediate verification.
221 m_dirVerificationTimer
->setInterval(500);
222 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
223 this, SLOT(delayedDirectoryVerification()));
224 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
225 this, SLOT(delayedDirectoryVerification()));
226 disconnect(m_plugin
, SIGNAL(revisionStatesChanged()),
227 this, SLOT(delayedDirectoryVerification()));
231 void RevisionControlObserver::applyUpdatedItemStates()
233 if (!m_updateItemStatesThread
->retrievedItems()) {
234 emit
errorMessage(i18nc("@info:status", "Update of revision information failed."));
238 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
239 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
240 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
241 // This works as the update of the data does not require a relayout of the views used in Dolphin.
242 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
243 m_dolphinModel
->blockSignals(true);
245 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
246 foreach (const ItemState
& itemState
, itemStates
) {
247 m_dolphinModel
->setData(itemState
.index
,
248 QVariant(static_cast<int>(itemState
.revision
)),
252 m_dolphinModel
->blockSignals(signalsBlocked
);
253 m_view
->viewport()->repaint();
255 // Using an empty message results in clearing the previously shown information message and showing
256 // the default status bar information. This is useful as the user already gets feedback that the
257 // operation has been completed because of the icon emblems.
258 emit
operationCompletedMessage(QString());
260 if (m_pendingItemStatesUpdate
) {
261 m_pendingItemStatesUpdate
= false;
266 void RevisionControlObserver::updateItemStates()
268 Q_ASSERT(m_plugin
!= 0);
269 if (m_updateItemStatesThread
== 0) {
270 m_updateItemStatesThread
= new UpdateItemStatesThread(this, &m_pluginMutex
);
271 connect(m_updateItemStatesThread
, SIGNAL(finished()),
272 this, SLOT(applyUpdatedItemStates()));
274 if (m_updateItemStatesThread
->isRunning()) {
275 // An update is currently ongoing. Wait until the thread has finished
276 // the update (see applyUpdatedItemStates()).
277 m_pendingItemStatesUpdate
= true;
281 const int rowCount
= m_dolphinModel
->rowCount();
283 // Build a list of all items in the current directory and delegate
284 // this list to the thread, which adjusts the revision states.
285 QList
<ItemState
> itemStates
;
286 for (int row
= 0; row
< rowCount
; ++row
) {
287 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
290 itemState
.index
= index
;
291 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
292 itemState
.revision
= RevisionControlPlugin::UnversionedRevision
;
294 itemStates
.append(itemState
);
297 emit
infoMessage(i18nc("@info:status", "Updating revision information..."));
298 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
299 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
303 #include "revisioncontrolobserver.moc"