]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrolobserver.cpp
Nicer formatting of the values - only a temp solution just like tuneLabel. To many...
[dolphin.git] / src / versioncontrolobserver.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 "versioncontrolobserver.h"
21
22 #include "dolphinmodel.h"
23
24 #include <kdirlister.h>
25 #include <klocale.h>
26 #include <kservice.h>
27 #include <kservicetypetrader.h>
28 #include <kversioncontrolplugin.h>
29
30 #include <QAbstractProxyModel>
31 #include <QAbstractItemView>
32 #include <QMutexLocker>
33 #include <QTimer>
34
35 /**
36 * The performance of updating the version state of items depends
37 * on the used plugin. To prevent that Dolphin gets blocked by a
38 * slow plugin, the updating is delegated to a thread.
39 */
40 class UpdateItemStatesThread : public QThread
41 {
42 public:
43 UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex);
44 void setData(KVersionControlPlugin* plugin,
45 const QList<VersionControlObserver::ItemState>& itemStates);
46 QList<VersionControlObserver::ItemState> itemStates() const;
47 bool retrievedItems() const;
48
49 protected:
50 virtual void run();
51
52 private:
53 bool m_retrievedItems;
54 KVersionControlPlugin* m_plugin;
55 QMutex* m_pluginMutex;
56 QList<VersionControlObserver::ItemState> m_itemStates;
57 };
58
59 UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex) :
60 QThread(parent),
61 m_retrievedItems(false),
62 m_pluginMutex(pluginMutex),
63 m_itemStates()
64 {
65 }
66
67 void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin,
68 const QList<VersionControlObserver::ItemState>& itemStates)
69 {
70 m_plugin = plugin;
71 m_itemStates = itemStates;
72 }
73
74 void UpdateItemStatesThread::run()
75 {
76 Q_ASSERT(!m_itemStates.isEmpty());
77 Q_ASSERT(m_plugin != 0);
78
79 // The items from m_itemStates may be located in different directory levels. The version
80 // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
81 // of doing an expensive search, we utilize the knowledge of the implementation of
82 // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
83 const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash);
84
85 QMutexLocker locker(m_pluginMutex);
86 m_retrievedItems = false;
87 if (m_plugin->beginRetrieval(directory)) {
88 const int count = m_itemStates.count();
89 for (int i = 0; i < count; ++i) {
90 m_itemStates[i].version = m_plugin->versionState(m_itemStates[i].item);
91 }
92 m_plugin->endRetrieval();
93 m_retrievedItems = true;
94 }
95 }
96
97 QList<VersionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
98 {
99 return m_itemStates;
100 }
101
102 bool UpdateItemStatesThread::retrievedItems() const
103 {
104 return m_retrievedItems;
105 }
106
107 // ------------------------------------------------------------------------------------------------
108
109 VersionControlObserver::VersionControlObserver(QAbstractItemView* view) :
110 QObject(view),
111 m_pendingItemStatesUpdate(false),
112 m_versionedDirectory(false),
113 m_silentUpdate(false),
114 m_view(view),
115 m_dirLister(0),
116 m_dolphinModel(0),
117 m_dirVerificationTimer(0),
118 m_pluginMutex(QMutex::Recursive),
119 m_plugin(0),
120 m_updateItemStatesThread(0)
121 {
122 Q_ASSERT(view != 0);
123
124 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
125 m_dolphinModel = (proxyModel == 0) ?
126 qobject_cast<DolphinModel*>(view->model()) :
127 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
128 if (m_dolphinModel != 0) {
129 m_dirLister = m_dolphinModel->dirLister();
130 connect(m_dirLister, SIGNAL(completed()),
131 this, SLOT(delayedDirectoryVerification()));
132
133 // The verification timer specifies the timeout until the shown directory
134 // is checked whether it is versioned. Per default it is assumed that users
135 // don't iterate through versioned directories and a high timeout is used
136 // The timeout will be decreased as soon as a versioned directory has been
137 // found (see verifyDirectory()).
138 m_dirVerificationTimer = new QTimer(this);
139 m_dirVerificationTimer->setSingleShot(true);
140 m_dirVerificationTimer->setInterval(500);
141 connect(m_dirVerificationTimer, SIGNAL(timeout()),
142 this, SLOT(verifyDirectory()));
143 }
144 }
145
146 VersionControlObserver::~VersionControlObserver()
147 {
148 if (m_updateItemStatesThread != 0) {
149 m_updateItemStatesThread->terminate();
150 m_updateItemStatesThread->wait();
151 }
152 delete m_plugin;
153 m_plugin = 0;
154 }
155
156 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
157 {
158 if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) {
159 QMutexLocker locker(&m_pluginMutex);
160 return m_plugin->contextMenuActions(items);
161 }
162 return QList<QAction*>();
163 }
164
165 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
166 {
167 if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) {
168 QMutexLocker locker(&m_pluginMutex);
169 return m_plugin->contextMenuActions(directory);
170 }
171
172 return QList<QAction*>();
173 }
174
175 void VersionControlObserver::delayedDirectoryVerification()
176 {
177 m_silentUpdate = false;
178 m_dirVerificationTimer->start();
179 }
180
181 void VersionControlObserver::silentDirectoryVerification()
182 {
183 m_silentUpdate = true;
184 m_dirVerificationTimer->start();
185 }
186
187 #include <kdebug.h>
188 void VersionControlObserver::verifyDirectory()
189 {
190 KUrl versionControlUrl = m_dirLister->url();
191 if (!versionControlUrl.isLocalFile()) {
192 return;
193 }
194
195 if (m_plugin == 0) {
196 return; // TODO: does not work yet, m_plugin will always be 0
197
198 kDebug() << "Searching FileViewVersionControlPlugins...";
199 const KService::List plugins = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
200 for (KService::List::ConstIterator it = plugins.constBegin(); it != plugins.constEnd(); ++it) {
201 kDebug() << "found plugin" << (*it)->desktopEntryName();
202 m_plugin = (*it)->createInstance<KVersionControlPlugin>();
203 }
204
205 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
206 this, SIGNAL(infoMessage(const QString&)));
207 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
208 this, SIGNAL(errorMessage(const QString&)));
209 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
210 this, SIGNAL(operationCompletedMessage(const QString&)));
211 }
212
213 versionControlUrl.addPath(m_plugin->fileName());
214 const KFileItem item = m_dirLister->findByUrl(versionControlUrl);
215
216 bool foundVersionInfo = !item.isNull();
217 if (!foundVersionInfo && m_versionedDirectory) {
218 // Version control systems like Git provide the version information
219 // file only in the root directory. Check whether the version information file can
220 // be found in one of the parent directories.
221
222 // TODO...
223 }
224
225 if (foundVersionInfo) {
226 if (!m_versionedDirectory) {
227 m_versionedDirectory = true;
228
229 // The directory is versioned. Assume that the user will further browse through
230 // versioned directories and decrease the verification timer.
231 m_dirVerificationTimer->setInterval(100);
232 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
233 this, SLOT(delayedDirectoryVerification()));
234 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
235 this, SLOT(delayedDirectoryVerification()));
236 connect(m_plugin, SIGNAL(versionStatesChanged()),
237 this, SLOT(silentDirectoryVerification()));
238 }
239 updateItemStates();
240 } else if (m_versionedDirectory) {
241 m_versionedDirectory = false;
242
243 // The directory is not versioned. Reset the verification timer to a higher
244 // value, so that browsing through non-versioned directories is not slown down
245 // by an immediate verification.
246 m_dirVerificationTimer->setInterval(500);
247 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
248 this, SLOT(delayedDirectoryVerification()));
249 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
250 this, SLOT(delayedDirectoryVerification()));
251 disconnect(m_plugin, SIGNAL(versionStatesChanged()),
252 this, SLOT(silentDirectoryVerification()));
253 }
254 }
255
256 void VersionControlObserver::applyUpdatedItemStates()
257 {
258 if (!m_updateItemStatesThread->retrievedItems()) {
259 // ignore m_silentUpdate for an error message
260 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
261 return;
262 }
263
264 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
265 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
266 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
267 // This works as the update of the data does not require a relayout of the views used in Dolphin.
268 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
269 m_dolphinModel->blockSignals(true);
270
271 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
272 foreach (const ItemState& itemState, itemStates) {
273 m_dolphinModel->setData(itemState.index,
274 QVariant(static_cast<int>(itemState.version)),
275 Qt::DecorationRole);
276 }
277
278 m_dolphinModel->blockSignals(signalsBlocked);
279 m_view->viewport()->repaint();
280
281 if (!m_silentUpdate) {
282 // Using an empty message results in clearing the previously shown information message and showing
283 // the default status bar information. This is useful as the user already gets feedback that the
284 // operation has been completed because of the icon emblems.
285 emit operationCompletedMessage(QString());
286 }
287
288 if (m_pendingItemStatesUpdate) {
289 m_pendingItemStatesUpdate = false;
290 updateItemStates();
291 }
292 }
293
294 void VersionControlObserver::updateItemStates()
295 {
296 Q_ASSERT(m_plugin != 0);
297 if (m_updateItemStatesThread == 0) {
298 m_updateItemStatesThread = new UpdateItemStatesThread(this, &m_pluginMutex);
299 connect(m_updateItemStatesThread, SIGNAL(finished()),
300 this, SLOT(applyUpdatedItemStates()));
301 }
302 if (m_updateItemStatesThread->isRunning()) {
303 // An update is currently ongoing. Wait until the thread has finished
304 // the update (see applyUpdatedItemStates()).
305 m_pendingItemStatesUpdate = true;
306 return;
307 }
308
309 QList<ItemState> itemStates;
310 addDirectory(QModelIndex(), itemStates);
311 if (!itemStates.isEmpty()) {
312 if (!m_silentUpdate) {
313 emit infoMessage(i18nc("@info:status", "Updating version information..."));
314 }
315 m_updateItemStatesThread->setData(m_plugin, itemStates);
316 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
317 }
318 }
319
320 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
321 {
322 const int rowCount = m_dolphinModel->rowCount(parentIndex);
323 for (int row = 0; row < rowCount; ++row) {
324 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
325 addDirectory(index, itemStates);
326
327 ItemState itemState;
328 itemState.index = index;
329 itemState.item = m_dolphinModel->itemForIndex(index);
330 itemState.version = KVersionControlPlugin::UnversionedVersion;
331
332 itemStates.append(itemState);
333 }
334 }
335
336 #include "versioncontrolobserver.moc"