]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
remove revision-information entries when items got deleted in the model
[dolphin.git] / src / revisioncontrolobserver.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "revisioncontrolobserver.h"
21
22 #include "dolphinmodel.h"
23 #include "revisioncontrolplugin.h"
24
25 #include <kdirlister.h>
26
27 #include <QAbstractProxyModel>
28 #include <QAbstractItemView>
29 #include <QListView>
30 #include <QTimer>
31
32 /**
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.
36 */
37 class LIBDOLPHINPRIVATE_EXPORT UpdateItemStatesThread : public QThread
38 {
39 public:
40 UpdateItemStatesThread(QObject* parent);
41 void setData(RevisionControlPlugin* plugin,
42 const QList<RevisionControlObserver::ItemState>& itemStates);
43 QList<RevisionControlObserver::ItemState> itemStates() const;
44
45 protected:
46 virtual void run();
47
48 private:
49 RevisionControlPlugin* m_plugin;
50 QList<RevisionControlObserver::ItemState> m_itemStates;
51 };
52
53 UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent) :
54 QThread(parent)
55 {
56 }
57
58 void UpdateItemStatesThread::setData(RevisionControlPlugin* plugin,
59 const QList<RevisionControlObserver::ItemState>& itemStates)
60 {
61 m_plugin = plugin;
62 m_itemStates = itemStates;
63 }
64
65 void UpdateItemStatesThread::run()
66 {
67 Q_ASSERT(m_itemStates.count() > 0);
68 Q_ASSERT(m_plugin != 0);
69
70 // it is assumed that all items have the same parent directory
71 const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash);
72
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);
77 }
78 m_plugin->endRetrieval();
79 }
80 }
81
82 QList<RevisionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
83 {
84 return m_itemStates;
85 }
86
87 // ---
88
89 RevisionControlObserver::RevisionControlObserver(QAbstractItemView* view) :
90 QObject(view),
91 m_pendingItemStatesUpdate(false),
92 m_view(view),
93 m_dirLister(0),
94 m_dolphinModel(0),
95 m_dirVerificationTimer(0),
96 m_plugin(0),
97 m_updateItemStatesThread(0)
98 {
99 Q_ASSERT(view != 0);
100
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()));
109 // TODO:
110 // connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
111 // this, SLOT(refreshItems()));
112
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()));
123 }
124 }
125
126 RevisionControlObserver::~RevisionControlObserver()
127 {
128 delete m_plugin;
129 m_plugin = 0;
130 }
131
132 void RevisionControlObserver::delayedDirectoryVerification()
133 {
134 m_dirVerificationTimer->start();
135 }
136
137 void RevisionControlObserver::verifyDirectory()
138 {
139 KUrl revisionControlUrl = m_dirLister->url();
140 if (!revisionControlUrl.isLocalFile()) {
141 return;
142 }
143
144 if (m_plugin == 0) {
145 // TODO: just for testing purposes. A plugin approach will be used later.
146 m_plugin = new SubversionPlugin();
147 }
148
149 revisionControlUrl.addPath(m_plugin->fileName());
150 KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
151 if (item.isNull()) {
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);
156 } else {
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);
160 updateItemStates();
161 }
162 }
163
164 void RevisionControlObserver::applyUpdatedItemStates()
165 {
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;
170 if (listView != 0) {
171 uniformSizes = listView->uniformItemSizes();
172 //listView->setUniformItemSizes(true); TODO: does not work as well as in KFilePreviewGenerator
173 }
174
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)),
179 Qt::DecorationRole);
180 }
181
182 if (listView != 0) {
183 listView->setUniformItemSizes(uniformSizes);
184 }
185
186 m_view->viewport()->repaint(); // TODO: this should not be necessary, as DolphinModel::setData() calls dataChanged()
187
188 if (m_pendingItemStatesUpdate) {
189 m_pendingItemStatesUpdate = false;
190 updateItemStates();
191 }
192 }
193
194 void RevisionControlObserver::updateItemStates()
195 {
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()));
201 }
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;
206 return;
207 }
208
209 const int rowCount = m_dolphinModel->rowCount();
210 if (rowCount > 0) {
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);
216
217 ItemState itemState;
218 itemState.index = index;
219 itemState.item = m_dolphinModel->itemForIndex(index);
220 itemState.revision = RevisionControlPlugin::LocalRevision;
221
222 itemStates.append(itemState);
223 }
224
225 m_updateItemStatesThread->setData(m_plugin, itemStates);
226 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
227 }
228 }
229
230 #include "revisioncontrolobserver.moc"