]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
Replaced the ItemState structure with QPair<KFileItem, KVersionControlPlugin2::ItemVe...
[dolphin.git] / src / views / versioncontrol / versioncontrolobserver.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphin_versioncontrolsettings.h"
23
24 #include <KLocale>
25 #include <KService>
26 #include <KDebug>
27 #include <KServiceTypeTrader>
28 #include <kitemviews/kfileitemmodel.h>
29 #include <kversioncontrolplugin2.h>
30
31 #include "updateitemstatesthread.h"
32
33 #include <QFile>
34 #include <QMutexLocker>
35 #include <QTimer>
36
37 VersionControlObserver::VersionControlObserver(QObject* parent) :
38 QObject(parent),
39 m_pendingItemStatesUpdate(false),
40 m_versionedDirectory(false),
41 m_silentUpdate(false),
42 m_model(0),
43 m_dirVerificationTimer(0),
44 m_plugin(0),
45 m_updateItemStatesThread(0)
46 {
47 // The verification timer specifies the timeout until the shown directory
48 // is checked whether it is versioned. Per default it is assumed that users
49 // don't iterate through versioned directories and a high timeout is used
50 // The timeout will be decreased as soon as a versioned directory has been
51 // found (see verifyDirectory()).
52 m_dirVerificationTimer = new QTimer(this);
53 m_dirVerificationTimer->setSingleShot(true);
54 m_dirVerificationTimer->setInterval(500);
55 connect(m_dirVerificationTimer, &QTimer::timeout,
56 this, &VersionControlObserver::verifyDirectory);
57 }
58
59 VersionControlObserver::~VersionControlObserver()
60 {
61 if (m_plugin) {
62 m_plugin->disconnect(this);
63 m_plugin = 0;
64 }
65 }
66
67 void VersionControlObserver::setModel(KFileItemModel* model)
68 {
69 if (m_model) {
70 disconnect(m_model, &KFileItemModel::itemsInserted,
71 this, &VersionControlObserver::delayedDirectoryVerification);
72 disconnect(m_model, &KFileItemModel::itemsChanged,
73 this, &VersionControlObserver::delayedDirectoryVerification);
74 }
75
76 m_model = model;
77
78 if (model) {
79 connect(m_model, &KFileItemModel::itemsInserted,
80 this, &VersionControlObserver::delayedDirectoryVerification);
81 connect(m_model, &KFileItemModel::itemsChanged,
82 this, &VersionControlObserver::delayedDirectoryVerification);
83 }
84 }
85
86 KFileItemModel* VersionControlObserver::model() const
87 {
88 return m_model;
89 }
90
91 QList<QAction*> VersionControlObserver::actions(const KFileItemList& items) const
92 {
93 QList<QAction*> actions;
94
95 bool hasNullItems = false;
96 foreach (const KFileItem& item, items) {
97 if (item.isNull()) {
98 kWarning() << "Requesting version-control-actions for empty items";
99 hasNullItems = true;
100 break;
101 }
102 }
103
104 if (!m_model || hasNullItems) {
105 return actions;
106 }
107
108 KVersionControlPlugin2* pluginV2 = qobject_cast<KVersionControlPlugin2*>(m_plugin);
109 if (pluginV2) {
110 // Use version 2 of the KVersionControlPlugin which allows providing actions
111 // also for non-versioned directories.
112 actions = pluginV2->actions(items);
113 } else if (isVersioned()) {
114 // Support deprecated interfaces from KVersionControlPlugin version 1.
115 // Context menu actions where only available for versioned directories.
116 QString directory;
117 if (items.count() == 1) {
118 const KFileItem rootItem = m_model->rootItem();
119 if (!rootItem.isNull() && items.first().url() == rootItem.url()) {
120 directory = rootItem.url().path();
121 }
122 }
123
124 actions = directory.isEmpty() ? m_plugin->contextMenuActions(items)
125 : m_plugin->contextMenuActions(directory);
126 }
127
128 return actions;
129 }
130
131 void VersionControlObserver::delayedDirectoryVerification()
132 {
133 m_silentUpdate = false;
134 m_dirVerificationTimer->start();
135 }
136
137 void VersionControlObserver::silentDirectoryVerification()
138 {
139 m_silentUpdate = true;
140 m_dirVerificationTimer->start();
141 }
142
143 void VersionControlObserver::verifyDirectory()
144 {
145 if (!m_model) {
146 return;
147 }
148
149 const KFileItem rootItem = m_model->rootItem();
150 if (rootItem.isNull() || !rootItem.url().isLocalFile()) {
151 return;
152 }
153
154 if (m_plugin) {
155 m_plugin->disconnect(this);
156 }
157
158 m_plugin = searchPlugin(rootItem.url());
159 if (m_plugin) {
160 KVersionControlPlugin2* pluginV2 = qobject_cast<KVersionControlPlugin2*>(m_plugin);
161 if (pluginV2) {
162 connect(pluginV2, &KVersionControlPlugin2::itemVersionsChanged,
163 this, &VersionControlObserver::silentDirectoryVerification);
164 } else {
165 connect(m_plugin, &KVersionControlPlugin::versionStatesChanged,
166 this, &VersionControlObserver::silentDirectoryVerification);
167 }
168 connect(m_plugin, &KVersionControlPlugin::infoMessage,
169 this, &VersionControlObserver::infoMessage);
170 connect(m_plugin, &KVersionControlPlugin::errorMessage,
171 this, &VersionControlObserver::errorMessage);
172 connect(m_plugin, &KVersionControlPlugin::operationCompletedMessage,
173 this, &VersionControlObserver::operationCompletedMessage);
174
175 if (!m_versionedDirectory) {
176 m_versionedDirectory = true;
177
178 // The directory is versioned. Assume that the user will further browse through
179 // versioned directories and decrease the verification timer.
180 m_dirVerificationTimer->setInterval(100);
181 }
182 updateItemStates();
183 } else if (m_versionedDirectory) {
184 m_versionedDirectory = false;
185
186 // The directory is not versioned. Reset the verification timer to a higher
187 // value, so that browsing through non-versioned directories is not slown down
188 // by an immediate verification.
189 m_dirVerificationTimer->setInterval(500);
190 }
191 }
192
193 void VersionControlObserver::slotThreadFinished()
194 {
195 UpdateItemStatesThread* thread = m_updateItemStatesThread;
196 m_updateItemStatesThread = 0; // The thread deletes itself automatically (see updateItemStates())
197
198 if (!m_plugin || !thread) {
199 return;
200 }
201
202 const QMap<QString, QVector<ItemState> >& itemStates = thread->itemStates();
203 QMap<QString, QVector<ItemState> >::const_iterator it = itemStates.constBegin();
204 for (; it != itemStates.constEnd(); ++it) {
205 const QVector<ItemState>& items = it.value();
206
207 foreach (const ItemState& item, items) {
208 const KFileItem& fileItem = item.first;
209 const KVersionControlPlugin2::ItemVersion version = item.second;
210 QHash<QByteArray, QVariant> values;
211 values.insert("version", QVariant(version));
212 m_model->setData(m_model->index(fileItem), values);
213 }
214 }
215
216 if (!m_silentUpdate) {
217 // Using an empty message results in clearing the previously shown information message and showing
218 // the default status bar information. This is useful as the user already gets feedback that the
219 // operation has been completed because of the icon emblems.
220 emit operationCompletedMessage(QString());
221 }
222
223 if (m_pendingItemStatesUpdate) {
224 m_pendingItemStatesUpdate = false;
225 updateItemStates();
226 }
227 }
228
229 void VersionControlObserver::updateItemStates()
230 {
231 Q_ASSERT(m_plugin);
232 if (m_updateItemStatesThread) {
233 // An update is currently ongoing. Wait until the thread has finished
234 // the update (see slotThreadFinished()).
235 m_pendingItemStatesUpdate = true;
236 return;
237 }
238
239 QMap<QString, QVector<ItemState> > itemStates;
240 createItemStatesList(itemStates);
241
242 if (!itemStates.isEmpty()) {
243 if (!m_silentUpdate) {
244 emit infoMessage(i18nc("@info:status", "Updating version information..."));
245 }
246 m_updateItemStatesThread = new UpdateItemStatesThread(m_plugin, itemStates);
247 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
248 this, &VersionControlObserver::slotThreadFinished);
249 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
250 m_updateItemStatesThread, &UpdateItemStatesThread::deleteLater);
251
252 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
253 }
254 }
255
256 int VersionControlObserver::createItemStatesList(QMap<QString, QVector<ItemState> >& itemStates,
257 const int firstIndex)
258 {
259 const int itemCount = m_model->count();
260 const int currentExpansionLevel = m_model->expandedParentsCount(firstIndex);
261
262 QVector<ItemState> items;
263 items.reserve(itemCount - firstIndex);
264
265 int index;
266 for (index = firstIndex; index < itemCount; ++index) {
267 const int expansionLevel = m_model->expandedParentsCount(index);
268
269 if (expansionLevel == currentExpansionLevel) {
270 ItemState itemState;
271 itemState.first = m_model->fileItem(index);
272 itemState.second = KVersionControlPlugin2::UnversionedVersion;
273
274 items.append(itemState);
275 } else if (expansionLevel > currentExpansionLevel) {
276 // Sub folder
277 index += createItemStatesList(itemStates, index) - 1;
278 } else {
279 break;
280 }
281 }
282
283 if (items.count() > 0) {
284 const KUrl& url = items.first().first.url();
285 itemStates.insert(url.directory(KUrl::AppendTrailingSlash), items);
286 }
287
288 return index - firstIndex; // number of processed items
289 }
290
291 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
292 {
293 static bool pluginsAvailable = true;
294 static QList<KVersionControlPlugin*> plugins;
295
296 if (!pluginsAvailable) {
297 // A searching for plugins has already been done, but no
298 // plugins are installed
299 return 0;
300 }
301
302 if (plugins.isEmpty()) {
303 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
304 // all fileview version control plugins and remember them in 'plugins'.
305 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
306
307 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
308 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
309 if (enabledPlugins.contains((*it)->name())) {
310 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
311 if (plugin) {
312 plugins.append(plugin);
313 }
314 }
315 }
316 if (plugins.isEmpty()) {
317 pluginsAvailable = false;
318 return 0;
319 }
320 }
321
322 // We use the number of upUrl() calls to find the best matching plugin
323 // for the given directory. The smaller value, the better it is (0 is best).
324 KVersionControlPlugin* bestPlugin = 0;
325 int bestScore = INT_MAX;
326
327 // Verify whether the current directory contains revision information
328 // like .svn, .git, ...
329 foreach (KVersionControlPlugin* plugin, plugins) {
330 const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
331 if (QFile::exists(fileName)) {
332 // The score of this plugin is 0 (best), so we can just return this plugin,
333 // instead of going through the plugin scoring procedure, we can't find a better one ;)
334 return plugin;
335 }
336
337 // Version control systems like Git provide the version information
338 // file only in the root directory. Check whether the version information file can
339 // be found in one of the parent directories. For performance reasons this
340 // step is only done, if the previous directory was marked as versioned by
341 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
342 // must be shown at least once.
343 if (m_versionedDirectory) {
344 KUrl dirUrl(directory);
345 KUrl upUrl = dirUrl.upUrl();
346 int upUrlCounter = 1;
347 while ((upUrlCounter < bestScore) && (upUrl != dirUrl)) {
348 const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
349 if (QFile::exists(fileName)) {
350 if (upUrlCounter < bestScore) {
351 bestPlugin = plugin;
352 bestScore = upUrlCounter;
353 }
354 break;
355 }
356 dirUrl = upUrl;
357 upUrl = dirUrl.upUrl();
358 ++upUrlCounter;
359 }
360 }
361 }
362
363 return bestPlugin;
364 }
365
366 bool VersionControlObserver::isVersioned() const
367 {
368 return m_versionedDirectory && m_plugin;
369 }
370