]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrolobserver.cpp
use correct name for the tags-entry, otherwise disabling of tags won't work...
[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 void VersionControlObserver::verifyDirectory()
188 {
189 KUrl versionControlUrl = m_dirLister->url();
190 if (!versionControlUrl.isLocalFile()) {
191 return;
192 }
193
194 if (m_plugin == 0) {
195 // TODO: does not work yet
196 const KService::List plugins = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
197 for (KService::List::ConstIterator it = plugins.begin(); it != plugins.end(); ++it) {
198 // kDebug() << "plugin: " << (*it)->desktopEntryName();
199 }
200 return;
201
202 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
203 this, SIGNAL(infoMessage(const QString&)));
204 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
205 this, SIGNAL(errorMessage(const QString&)));
206 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
207 this, SIGNAL(operationCompletedMessage(const QString&)));
208 }
209
210 versionControlUrl.addPath(m_plugin->fileName());
211 const KFileItem item = m_dirLister->findByUrl(versionControlUrl);
212
213 bool foundVersionInfo = !item.isNull();
214 if (!foundVersionInfo && m_versionedDirectory) {
215 // Version control systems like Git provide the version information
216 // file only in the root directory. Check whether the version information file can
217 // be found in one of the parent directories.
218
219 // TODO...
220 }
221
222 if (foundVersionInfo) {
223 if (!m_versionedDirectory) {
224 m_versionedDirectory = true;
225
226 // The directory is versioned. Assume that the user will further browse through
227 // versioned directories and decrease the verification timer.
228 m_dirVerificationTimer->setInterval(100);
229 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
230 this, SLOT(delayedDirectoryVerification()));
231 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
232 this, SLOT(delayedDirectoryVerification()));
233 connect(m_plugin, SIGNAL(versionStatesChanged()),
234 this, SLOT(silentDirectoryVerification()));
235 }
236 updateItemStates();
237 } else if (m_versionedDirectory) {
238 m_versionedDirectory = false;
239
240 // The directory is not versioned. Reset the verification timer to a higher
241 // value, so that browsing through non-versioned directories is not slown down
242 // by an immediate verification.
243 m_dirVerificationTimer->setInterval(500);
244 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
245 this, SLOT(delayedDirectoryVerification()));
246 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
247 this, SLOT(delayedDirectoryVerification()));
248 disconnect(m_plugin, SIGNAL(versionStatesChanged()),
249 this, SLOT(silentDirectoryVerification()));
250 }
251 }
252
253 void VersionControlObserver::applyUpdatedItemStates()
254 {
255 if (!m_updateItemStatesThread->retrievedItems()) {
256 // ignore m_silentUpdate for an error message
257 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
258 return;
259 }
260
261 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
262 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
263 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
264 // This works as the update of the data does not require a relayout of the views used in Dolphin.
265 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
266 m_dolphinModel->blockSignals(true);
267
268 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
269 foreach (const ItemState& itemState, itemStates) {
270 m_dolphinModel->setData(itemState.index,
271 QVariant(static_cast<int>(itemState.version)),
272 Qt::DecorationRole);
273 }
274
275 m_dolphinModel->blockSignals(signalsBlocked);
276 m_view->viewport()->repaint();
277
278 if (!m_silentUpdate) {
279 // Using an empty message results in clearing the previously shown information message and showing
280 // the default status bar information. This is useful as the user already gets feedback that the
281 // operation has been completed because of the icon emblems.
282 emit operationCompletedMessage(QString());
283 }
284
285 if (m_pendingItemStatesUpdate) {
286 m_pendingItemStatesUpdate = false;
287 updateItemStates();
288 }
289 }
290
291 void VersionControlObserver::updateItemStates()
292 {
293 Q_ASSERT(m_plugin != 0);
294 if (m_updateItemStatesThread == 0) {
295 m_updateItemStatesThread = new UpdateItemStatesThread(this, &m_pluginMutex);
296 connect(m_updateItemStatesThread, SIGNAL(finished()),
297 this, SLOT(applyUpdatedItemStates()));
298 }
299 if (m_updateItemStatesThread->isRunning()) {
300 // An update is currently ongoing. Wait until the thread has finished
301 // the update (see applyUpdatedItemStates()).
302 m_pendingItemStatesUpdate = true;
303 return;
304 }
305
306 QList<ItemState> itemStates;
307 addDirectory(QModelIndex(), itemStates);
308 if (!itemStates.isEmpty()) {
309 if (!m_silentUpdate) {
310 emit infoMessage(i18nc("@info:status", "Updating version information..."));
311 }
312 m_updateItemStatesThread->setData(m_plugin, itemStates);
313 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
314 }
315 }
316
317 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
318 {
319 const int rowCount = m_dolphinModel->rowCount(parentIndex);
320 for (int row = 0; row < rowCount; ++row) {
321 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
322 addDirectory(index, itemStates);
323
324 ItemState itemState;
325 itemState.index = index;
326 itemState.item = m_dolphinModel->itemForIndex(index);
327 itemState.version = KVersionControlPlugin::UnversionedVersion;
328
329 itemStates.append(itemState);
330 }
331 }
332
333 #include "versioncontrolobserver.moc"