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()
142 if (m_updateItemStatesThread
!= 0) {
143 m_updateItemStatesThread
->terminate();
144 m_updateItemStatesThread
->wait();
150 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const KFileItemList
& items
) const
152 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
153 QMutexLocker
locker(&m_pluginMutex
);
154 return m_plugin
->contextMenuActions(items
);
156 return QList
<QAction
*>();
159 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const QString
& directory
) const
161 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
162 QMutexLocker
locker(&m_pluginMutex
);
163 return m_plugin
->contextMenuActions(directory
);
166 return QList
<QAction
*>();
169 void RevisionControlObserver::delayedDirectoryVerification()
171 m_dirVerificationTimer
->start();
174 void RevisionControlObserver::verifyDirectory()
176 KUrl revisionControlUrl
= m_dirLister
->url();
177 if (!revisionControlUrl
.isLocalFile()) {
182 // TODO: just for testing purposes. A plugin approach will be used later.
183 m_plugin
= new SubversionPlugin();
184 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
185 this, SIGNAL(infoMessage(const QString
&)));
186 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
187 this, SIGNAL(errorMessage(const QString
&)));
188 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
189 this, SIGNAL(operationCompletedMessage(const QString
&)));
192 revisionControlUrl
.addPath(m_plugin
->fileName());
193 const KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
195 bool foundRevisionInfo
= !item
.isNull();
196 if (!foundRevisionInfo
&& m_revisionedDirectory
) {
197 // Revision control systems like Git provide the revision information
198 // file only in the root directory. Check whether the revision information file can
199 // be found in one of the parent directories.
204 if (foundRevisionInfo
) {
205 if (!m_revisionedDirectory
) {
206 m_revisionedDirectory
= true;
208 // The directory is versioned. Assume that the user will further browse through
209 // versioned directories and decrease the verification timer.
210 m_dirVerificationTimer
->setInterval(100);
211 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
212 this, SLOT(delayedDirectoryVerification()));
213 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
214 this, SLOT(delayedDirectoryVerification()));
215 connect(m_plugin
, SIGNAL(revisionStatesChanged()),
216 this, SLOT(delayedDirectoryVerification()));
219 } else if (m_revisionedDirectory
) {
220 m_revisionedDirectory
= false;
222 // The directory is not versioned. Reset the verification timer to a higher
223 // value, so that browsing through non-versioned directories is not slown down
224 // by an immediate verification.
225 m_dirVerificationTimer
->setInterval(500);
226 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
227 this, SLOT(delayedDirectoryVerification()));
228 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
229 this, SLOT(delayedDirectoryVerification()));
230 disconnect(m_plugin
, SIGNAL(revisionStatesChanged()),
231 this, SLOT(delayedDirectoryVerification()));
235 void RevisionControlObserver::applyUpdatedItemStates()
237 if (!m_updateItemStatesThread
->retrievedItems()) {
238 emit
errorMessage(i18nc("@info:status", "Update of revision information failed."));
242 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
243 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
244 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
245 // This works as the update of the data does not require a relayout of the views used in Dolphin.
246 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
247 m_dolphinModel
->blockSignals(true);
249 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
250 foreach (const ItemState
& itemState
, itemStates
) {
251 m_dolphinModel
->setData(itemState
.index
,
252 QVariant(static_cast<int>(itemState
.revision
)),
256 m_dolphinModel
->blockSignals(signalsBlocked
);
257 m_view
->viewport()->repaint();
259 // Using an empty message results in clearing the previously shown information message and showing
260 // the default status bar information. This is useful as the user already gets feedback that the
261 // operation has been completed because of the icon emblems.
262 emit
operationCompletedMessage(QString());
264 if (m_pendingItemStatesUpdate
) {
265 m_pendingItemStatesUpdate
= false;
270 void RevisionControlObserver::updateItemStates()
272 Q_ASSERT(m_plugin
!= 0);
273 if (m_updateItemStatesThread
== 0) {
274 m_updateItemStatesThread
= new UpdateItemStatesThread(this, &m_pluginMutex
);
275 connect(m_updateItemStatesThread
, SIGNAL(finished()),
276 this, SLOT(applyUpdatedItemStates()));
278 if (m_updateItemStatesThread
->isRunning()) {
279 // An update is currently ongoing. Wait until the thread has finished
280 // the update (see applyUpdatedItemStates()).
281 m_pendingItemStatesUpdate
= true;
285 const int rowCount
= m_dolphinModel
->rowCount();
287 // Build a list of all items in the current directory and delegate
288 // this list to the thread, which adjusts the revision states.
289 QList
<ItemState
> itemStates
;
290 for (int row
= 0; row
< rowCount
; ++row
) {
291 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
294 itemState
.index
= index
;
295 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
296 itemState
.revision
= RevisionControlPlugin::UnversionedRevision
;
298 itemStates
.append(itemState
);
301 emit
infoMessage(i18nc("@info:status", "Updating revision information..."));
302 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
303 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
307 #include "revisioncontrolobserver.moc"