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),
94 m_dirVerificationTimer(0),
96 m_updateItemStatesThread(0)
100 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
101 m_dolphinModel
= (proxyModel
== 0) ?
102 qobject_cast
<DolphinModel
*>(view
->model()) :
103 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
104 if (m_dolphinModel
!= 0) {
105 m_dirLister
= m_dolphinModel
->dirLister();
106 connect(m_dirLister
, SIGNAL(completed()),
107 this, SLOT(delayedDirectoryVerification()));
109 // connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
110 // this, SLOT(refreshItems()));
112 // The verification timer specifies the timeout until the shown directory
113 // is checked whether it is versioned. Per default it is assumed that users
114 // don't iterate through versioned directories and a high timeout is used
115 // The timeout will be decreased as soon as a versioned directory has been
116 // found (see verifyDirectory()).
117 m_dirVerificationTimer
= new QTimer(this);
118 m_dirVerificationTimer
->setSingleShot(true);
119 m_dirVerificationTimer
->setInterval(500);
120 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
121 this, SLOT(verifyDirectory()));
125 RevisionControlObserver::~RevisionControlObserver()
131 void RevisionControlObserver::delayedDirectoryVerification()
133 m_dirVerificationTimer
->start();
136 void RevisionControlObserver::verifyDirectory()
138 KUrl revisionControlUrl
= m_dirLister
->url();
139 if (!revisionControlUrl
.isLocalFile()) {
144 // TODO: just for testing purposes. A plugin approach will be used later.
145 m_plugin
= new SubversionPlugin();
148 revisionControlUrl
.addPath(m_plugin
->fileName());
149 KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
151 // The directory is not versioned. Reset the verification timer to a higher
152 // value, so that browsing through non-versioned directories is not slown down
153 // by an immediate verification.
154 m_dirVerificationTimer
->setInterval(500);
156 // The directory is versioned. Assume that the user will further browse through
157 // versioned directories and decrease the verification timer.
158 m_dirVerificationTimer
->setInterval(100);
163 void RevisionControlObserver::applyUpdatedItemStates()
165 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
166 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
167 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
168 // This works as the update of the data does not require a relayout of the views used in Dolphin.
169 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
170 m_dolphinModel
->blockSignals(true);
172 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
173 foreach (const ItemState
& itemState
, itemStates
) {
174 m_dolphinModel
->setData(itemState
.index
,
175 QVariant(static_cast<int>(itemState
.revision
)),
179 m_dolphinModel
->blockSignals(signalsBlocked
);
180 m_view
->viewport()->repaint();
182 if (m_pendingItemStatesUpdate
) {
183 m_pendingItemStatesUpdate
= false;
188 void RevisionControlObserver::updateItemStates()
190 Q_ASSERT(m_plugin
!= 0);
191 if (m_updateItemStatesThread
== 0) {
192 m_updateItemStatesThread
= new UpdateItemStatesThread(this);
193 connect(m_updateItemStatesThread
, SIGNAL(finished()),
194 this, SLOT(applyUpdatedItemStates()));
196 if (m_updateItemStatesThread
->isRunning()) {
197 // An update is currently ongoing. Wait until the thread has finished
198 // the update (see applyUpdatedItemStates()).
199 m_pendingItemStatesUpdate
= true;
203 const int rowCount
= m_dolphinModel
->rowCount();
205 // Build a list of all items in the current directory and delegate
206 // this list to the thread, which adjusts the revision states.
207 QList
<ItemState
> itemStates
;
208 for (int row
= 0; row
< rowCount
; ++row
) {
209 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
212 itemState
.index
= index
;
213 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
214 itemState
.revision
= RevisionControlPlugin::LocalRevision
;
216 itemStates
.append(itemState
);
219 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
220 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
224 #include "revisioncontrolobserver.moc"