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>
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 LIBDOLPHINPRIVATE_EXPORT UpdateItemStatesThread
: public QThread
40 UpdateItemStatesThread(QObject
* parent
);
41 void setData(RevisionControlPlugin
* plugin
,
42 const QList
<RevisionControlObserver::ItemState
>& itemStates
);
43 QList
<RevisionControlObserver::ItemState
> itemStates() const;
49 RevisionControlPlugin
* m_plugin
;
50 QList
<RevisionControlObserver::ItemState
> m_itemStates
;
53 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
) :
58 void UpdateItemStatesThread::setData(RevisionControlPlugin
* plugin
,
59 const QList
<RevisionControlObserver::ItemState
>& itemStates
)
62 m_itemStates
= itemStates
;
65 void UpdateItemStatesThread::run()
67 Q_ASSERT(m_itemStates
.count() > 0);
68 Q_ASSERT(m_plugin
!= 0);
70 // it is assumed that all items have the same parent directory
71 const QString directory
= m_itemStates
.first().item
.url().directory(KUrl::AppendTrailingSlash
);
73 if (m_plugin
->beginRetrieval(directory
)) {
74 const int count
= m_itemStates
.count();
75 for (int i
= 0; i
< count
; ++i
) {
76 m_itemStates
[i
].revision
= m_plugin
->revisionState(m_itemStates
[i
].item
);
78 m_plugin
->endRetrieval();
82 QList
<RevisionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
89 RevisionControlObserver::RevisionControlObserver(QAbstractItemView
* view
) :
91 m_pendingItemStatesUpdate(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 // connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
111 // this, SLOT(refreshItems()));
113 // The verification timer specifies the timeout until the shown directory
114 // is checked whether it is versioned. Per default it is assumed that users
115 // don't iterate through versioned directories and a high timeout is used
116 // The timeout will be decreased as soon as a versioned directory has been
117 // found (see verifyDirectory()).
118 m_dirVerificationTimer
= new QTimer(this);
119 m_dirVerificationTimer
->setSingleShot(true);
120 m_dirVerificationTimer
->setInterval(500);
121 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
122 this, SLOT(verifyDirectory()));
126 RevisionControlObserver::~RevisionControlObserver()
132 void RevisionControlObserver::delayedDirectoryVerification()
134 m_dirVerificationTimer
->start();
137 void RevisionControlObserver::verifyDirectory()
139 KUrl revisionControlUrl
= m_dirLister
->url();
140 if (!revisionControlUrl
.isLocalFile()) {
145 // TODO: just for testing purposes. A plugin approach will be used later.
146 m_plugin
= new SubversionPlugin();
149 revisionControlUrl
.addPath(m_plugin
->fileName());
150 KFileItem item
= m_dirLister
->findByUrl(revisionControlUrl
);
152 // The directory is not versioned. Reset the verification timer to a higher
153 // value, so that browsing through non-versioned directories is not slown down
154 // by an immediate verification.
155 m_dirVerificationTimer
->setInterval(500);
157 // The directory is versioned. Assume that the user will further browse through
158 // versioned directories and decrease the verification timer.
159 m_dirVerificationTimer
->setInterval(100);
164 void RevisionControlObserver::applyUpdatedItemStates()
166 // Updating items with non-uniform item sizes is a serious bottleneck
167 // in QListView. Temporary disable the non-uniform item sizes.
168 QListView
* listView
= qobject_cast
<QListView
*>(m_view
);
169 bool uniformSizes
= true;
171 uniformSizes
= listView
->uniformItemSizes();
172 //listView->setUniformItemSizes(true); TODO: does not work as well as in KFilePreviewGenerator
175 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
176 foreach (const ItemState
& itemState
, itemStates
) {
177 m_dolphinModel
->setData(itemState
.index
,
178 QVariant(static_cast<int>(itemState
.revision
)),
183 listView
->setUniformItemSizes(uniformSizes
);
186 m_view
->viewport()->repaint(); // TODO: this should not be necessary, as DolphinModel::setData() calls dataChanged()
188 if (m_pendingItemStatesUpdate
) {
189 m_pendingItemStatesUpdate
= false;
194 void RevisionControlObserver::updateItemStates()
196 Q_ASSERT(m_plugin
!= 0);
197 if (m_updateItemStatesThread
== 0) {
198 m_updateItemStatesThread
= new UpdateItemStatesThread(this);
199 connect(m_updateItemStatesThread
, SIGNAL(finished()),
200 this, SLOT(applyUpdatedItemStates()));
202 if (m_updateItemStatesThread
->isRunning()) {
203 // An update is currently ongoing. Wait until the thread has finished
204 // the update (see applyUpdatedItemStates()).
205 m_pendingItemStatesUpdate
= true;
209 const int rowCount
= m_dolphinModel
->rowCount();
211 // Build a list of all items in the current directory and delegate
212 // this list to the thread, which adjusts the revision states.
213 QList
<ItemState
> itemStates
;
214 for (int row
= 0; row
< rowCount
; ++row
) {
215 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Revision
);
218 itemState
.index
= index
;
219 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
220 itemState
.revision
= RevisionControlPlugin::LocalRevision
;
222 itemStates
.append(itemState
);
225 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
226 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
230 #include "revisioncontrolobserver.moc"