]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
terminate the thread which checks the revision state of items if the revision control...
[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 #include <klocale.h>
27
28 #include <QAbstractProxyModel>
29 #include <QAbstractItemView>
30 #include <QMutexLocker>
31 #include <QTimer>
32
33 /**
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.
37 */
38 class UpdateItemStatesThread : public QThread
39 {
40 public:
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;
46
47 protected:
48 virtual void run();
49
50 private:
51 bool m_retrievedItems;
52 RevisionControlPlugin* m_plugin;
53 QMutex* m_pluginMutex;
54 QList<RevisionControlObserver::ItemState> m_itemStates;
55 };
56
57 UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex) :
58 QThread(parent),
59 m_retrievedItems(false),
60 m_pluginMutex(pluginMutex),
61 m_itemStates()
62 {
63 }
64
65 void UpdateItemStatesThread::setData(RevisionControlPlugin* plugin,
66 const QList<RevisionControlObserver::ItemState>& itemStates)
67 {
68 m_plugin = plugin;
69 m_itemStates = itemStates;
70 }
71
72 void UpdateItemStatesThread::run()
73 {
74 Q_ASSERT(!m_itemStates.isEmpty());
75 Q_ASSERT(m_plugin != 0);
76
77 // it is assumed that all items have the same parent directory
78 const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash);
79
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);
86 }
87 m_plugin->endRetrieval();
88 m_retrievedItems = true;
89 }
90 }
91
92 QList<RevisionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
93 {
94 return m_itemStates;
95 }
96
97 bool UpdateItemStatesThread::retrievedItems() const
98 {
99 return m_retrievedItems;
100 }
101
102 // ------------------------------------------------------------------------------------------------
103
104 RevisionControlObserver::RevisionControlObserver(QAbstractItemView* view) :
105 QObject(view),
106 m_pendingItemStatesUpdate(false),
107 m_revisionedDirectory(false),
108 m_view(view),
109 m_dirLister(0),
110 m_dolphinModel(0),
111 m_dirVerificationTimer(0),
112 m_pluginMutex(QMutex::Recursive),
113 m_plugin(0),
114 m_updateItemStatesThread(0)
115 {
116 Q_ASSERT(view != 0);
117
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()));
126
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()));
137 }
138 }
139
140 RevisionControlObserver::~RevisionControlObserver()
141 {
142 if (m_updateItemStatesThread != 0) {
143 m_updateItemStatesThread->terminate();
144 m_updateItemStatesThread->wait();
145 }
146 delete m_plugin;
147 m_plugin = 0;
148 }
149
150 QList<QAction*> RevisionControlObserver::contextMenuActions(const KFileItemList& items) const
151 {
152 if (m_dolphinModel->hasRevisionData() && (m_plugin != 0)) {
153 QMutexLocker locker(&m_pluginMutex);
154 return m_plugin->contextMenuActions(items);
155 }
156 return QList<QAction*>();
157 }
158
159 QList<QAction*> RevisionControlObserver::contextMenuActions(const QString& directory) const
160 {
161 if (m_dolphinModel->hasRevisionData() && (m_plugin != 0)) {
162 QMutexLocker locker(&m_pluginMutex);
163 return m_plugin->contextMenuActions(directory);
164 }
165
166 return QList<QAction*>();
167 }
168
169 void RevisionControlObserver::delayedDirectoryVerification()
170 {
171 m_dirVerificationTimer->start();
172 }
173
174 void RevisionControlObserver::verifyDirectory()
175 {
176 KUrl revisionControlUrl = m_dirLister->url();
177 if (!revisionControlUrl.isLocalFile()) {
178 return;
179 }
180
181 if (m_plugin == 0) {
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&)));
190 }
191
192 revisionControlUrl.addPath(m_plugin->fileName());
193 const KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
194
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.
200
201 // TODO...
202 }
203
204 if (foundRevisionInfo) {
205 if (!m_revisionedDirectory) {
206 m_revisionedDirectory = true;
207
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()));
217 }
218 updateItemStates();
219 } else if (m_revisionedDirectory) {
220 m_revisionedDirectory = false;
221
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()));
232 }
233 }
234
235 void RevisionControlObserver::applyUpdatedItemStates()
236 {
237 if (!m_updateItemStatesThread->retrievedItems()) {
238 emit errorMessage(i18nc("@info:status", "Update of revision information failed."));
239 return;
240 }
241
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);
248
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)),
253 Qt::DecorationRole);
254 }
255
256 m_dolphinModel->blockSignals(signalsBlocked);
257 m_view->viewport()->repaint();
258
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());
263
264 if (m_pendingItemStatesUpdate) {
265 m_pendingItemStatesUpdate = false;
266 updateItemStates();
267 }
268 }
269
270 void RevisionControlObserver::updateItemStates()
271 {
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()));
277 }
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;
282 return;
283 }
284
285 const int rowCount = m_dolphinModel->rowCount();
286 if (rowCount > 0) {
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);
292
293 ItemState itemState;
294 itemState.index = index;
295 itemState.item = m_dolphinModel->itemForIndex(index);
296 itemState.revision = RevisionControlPlugin::UnversionedRevision;
297
298 itemStates.append(itemState);
299 }
300
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
304 }
305 }
306
307 #include "revisioncontrolobserver.moc"