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>
32 * The performance of updating the revision state of items depends
33 * on the used plugin. To prevent that Dolphin gets blocked by a
34 * slow plugin, the updating is delegated to a thread.
36 class UpdateItemStatesThread
: public QThread
39 UpdateItemStatesThread(QObject
* parent
);
40 void setData(RevisionControlPlugin
* plugin
,
41 const QList
<RevisionControlObserver::ItemState
>& itemStates
);
42 QList
<RevisionControlObserver::ItemState
> itemStates() const;
48 RevisionControlPlugin
* m_plugin
;
49 QList
<RevisionControlObserver::ItemState
> m_itemStates
;
52 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
) :
57 void UpdateItemStatesThread::setData(RevisionControlPlugin
* plugin
,
58 const QList
<RevisionControlObserver::ItemState
>& itemStates
)
61 m_itemStates
= itemStates
;
64 void UpdateItemStatesThread::run()
66 Q_ASSERT(m_itemStates
.count() > 0);
67 Q_ASSERT(m_plugin
!= 0);
69 // it is assumed that all items have the same parent directory
70 const QString directory
= m_itemStates
.first().item
.url().directory(KUrl::AppendTrailingSlash
);
72 if (m_plugin
->beginRetrieval(directory
)) {
73 const int count
= m_itemStates
.count();
74 for (int i
= 0; i
< count
; ++i
) {
75 m_itemStates
[i
].revision
= m_plugin
->revisionState(m_itemStates
[i
].item
);
77 m_plugin
->endRetrieval();
81 QList
<RevisionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
88 RevisionControlObserver::RevisionControlObserver(QAbstractItemView
* view
) :
90 m_pendingItemStatesUpdate(false),
91 m_revisionedDirectory(false),
95 m_dirVerificationTimer(0),
97 m_updateItemStatesThread(0)
101 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
102 m_dolphinModel
= (proxyModel
== 0) ?
103 qobject_cast
<DolphinModel
*>(view
->model()) :
104 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
105 if (m_dolphinModel
!= 0) {
106 m_dirLister
= m_dolphinModel
->dirLister();
107 connect(m_dirLister
, SIGNAL(completed()),
108 this, SLOT(delayedDirectoryVerification()));
110 // The verification timer specifies the timeout until the shown directory
111 // is checked whether it is versioned. Per default it is assumed that users
112 // don't iterate through versioned directories and a high timeout is used
113 // The timeout will be decreased as soon as a versioned directory has been
114 // found (see verifyDirectory()).
115 m_dirVerificationTimer
= new QTimer(this);
116 m_dirVerificationTimer
->setSingleShot(true);
117 m_dirVerificationTimer
->setInterval(500);
118 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
119 this, SLOT(verifyDirectory()));
123 RevisionControlObserver::~RevisionControlObserver()
129 QList
<QAction
*> RevisionControlObserver::contextMenuActions(const KFileItemList
& items
) const
131 if (m_dolphinModel
->hasRevisionData() && (m_plugin
!= 0)) {
132 return m_plugin
->contextMenuActions(items
);
135 return QList
<QAction
*>();
138 void RevisionControlObserver::delayedDirectoryVerification()
140 m_dirVerificationTimer
->start();
143 void RevisionControlObserver::verifyDirectory()
145 KUrl revisionControlUrl
= m_dirLister
->url();
146 if (!revisionControlUrl
.isLocalFile()) {
151 // TODO: just for testing purposes. A plugin approach will be used later.
152 m_plugin
= new SubversionPlugin();
155 revisionControlUrl
.addPath(m_plugin
->fileName());
156 const KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
157 if (item
.isNull() && m_revisionedDirectory
) {
158 // The directory is not versioned. Reset the verification timer to a higher
159 // value, so that browsing through non-versioned directories is not slown down
160 // by an immediate verification.
161 m_dirVerificationTimer
->setInterval(500);
162 m_revisionedDirectory
= false;
163 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
164 this, SLOT(delayedDirectoryVerification()));
165 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
166 this, SLOT(delayedDirectoryVerification()));
167 } else if (!item
.isNull()) {
168 if (!m_revisionedDirectory
) {
169 // The directory is versioned. Assume that the user will further browse through
170 // versioned directories and decrease the verification timer.
171 m_dirVerificationTimer
->setInterval(100);
172 m_revisionedDirectory
= true;
173 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
174 this, SLOT(delayedDirectoryVerification()));
175 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
176 this, SLOT(delayedDirectoryVerification()));
182 void RevisionControlObserver::applyUpdatedItemStates()
184 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
185 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
186 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
187 // This works as the update of the data does not require a relayout of the views used in Dolphin.
188 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
189 m_dolphinModel
->blockSignals(true);
191 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
192 foreach (const ItemState
& itemState
, itemStates
) {
193 m_dolphinModel
->setData(itemState
.index
,
194 QVariant(static_cast<int>(itemState
.revision
)),
198 m_dolphinModel
->blockSignals(signalsBlocked
);
199 m_view
->viewport()->repaint();
201 if (m_pendingItemStatesUpdate
) {
202 m_pendingItemStatesUpdate
= false;
207 void RevisionControlObserver::updateItemStates()
209 Q_ASSERT(m_plugin
!= 0);
210 if (m_updateItemStatesThread
== 0) {
211 m_updateItemStatesThread
= new UpdateItemStatesThread(this);
212 connect(m_updateItemStatesThread
, SIGNAL(finished()),
213 this, SLOT(applyUpdatedItemStates()));
215 if (m_updateItemStatesThread
->isRunning()) {
216 // An update is currently ongoing. Wait until the thread has finished
217 // the update (see applyUpdatedItemStates()).
218 m_pendingItemStatesUpdate
= true;
222 const int rowCount
= m_dolphinModel
->rowCount();
224 // Build a list of all items in the current directory and delegate
225 // this list to the thread, which adjusts the revision states.
226 QList
<ItemState
> itemStates
;
227 for (int row
= 0; row
< rowCount
; ++row
) {
228 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
231 itemState
.index
= index
;
232 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
233 itemState
.revision
= RevisionControlPlugin::LocalRevision
;
235 itemStates
.append(itemState
);
238 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
239 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
243 #include "revisioncontrolobserver.moc"