]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
9033e19dababb69d148b348a2cf4319b36468d17
[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 QHash<QByteArray, QVariant> values;
209 values.insert("version", QVariant(item.version));
210 m_model->setData(m_model->index(item.item), values);
211 }
212 }
213
214 if (!m_silentUpdate) {
215 // Using an empty message results in clearing the previously shown information message and showing
216 // the default status bar information. This is useful as the user already gets feedback that the
217 // operation has been completed because of the icon emblems.
218 emit operationCompletedMessage(QString());
219 }
220
221 if (m_pendingItemStatesUpdate) {
222 m_pendingItemStatesUpdate = false;
223 updateItemStates();
224 }
225 }
226
227 void VersionControlObserver::updateItemStates()
228 {
229 Q_ASSERT(m_plugin);
230 if (m_updateItemStatesThread) {
231 // An update is currently ongoing. Wait until the thread has finished
232 // the update (see slotThreadFinished()).
233 m_pendingItemStatesUpdate = true;
234 return;
235 }
236
237 QMap<QString, QVector<ItemState> > itemStates;
238 createItemStatesList(itemStates);
239
240 if (!itemStates.isEmpty()) {
241 if (!m_silentUpdate) {
242 emit infoMessage(i18nc("@info:status", "Updating version information..."));
243 }
244 m_updateItemStatesThread = new UpdateItemStatesThread(m_plugin, itemStates);
245 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
246 this, &VersionControlObserver::slotThreadFinished);
247 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
248 m_updateItemStatesThread, &UpdateItemStatesThread::deleteLater);
249
250 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
251 }
252 }
253
254 int VersionControlObserver::createItemStatesList(QMap<QString, QVector<ItemState> >& itemStates,
255 const int firstIndex)
256 {
257 const int itemCount = m_model->count();
258 const int currentExpansionLevel = m_model->expandedParentsCount(firstIndex);
259
260 QVector<ItemState> items;
261 items.reserve(itemCount - firstIndex);
262
263 int index;
264 for (index = firstIndex; index < itemCount; ++index) {
265 const int expansionLevel = m_model->expandedParentsCount(index);
266
267 if (expansionLevel == currentExpansionLevel) {
268 ItemState itemState;
269 itemState.item = m_model->fileItem(index);
270 itemState.version = KVersionControlPlugin2::UnversionedVersion;
271
272 items.append(itemState);
273 } else if (expansionLevel > currentExpansionLevel) {
274 // Sub folder
275 index += createItemStatesList(itemStates, index) - 1;
276 } else {
277 break;
278 }
279 }
280
281 if (items.count() > 0) {
282 const KUrl& url = items.first().item.url();
283 itemStates.insert(url.directory(KUrl::AppendTrailingSlash), items);
284 }
285
286 return index - firstIndex; // number of processed items
287 }
288
289 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
290 {
291 static bool pluginsAvailable = true;
292 static QList<KVersionControlPlugin*> plugins;
293
294 if (!pluginsAvailable) {
295 // A searching for plugins has already been done, but no
296 // plugins are installed
297 return 0;
298 }
299
300 if (plugins.isEmpty()) {
301 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
302 // all fileview version control plugins and remember them in 'plugins'.
303 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
304
305 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
306 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
307 if (enabledPlugins.contains((*it)->name())) {
308 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
309 if (plugin) {
310 plugins.append(plugin);
311 }
312 }
313 }
314 if (plugins.isEmpty()) {
315 pluginsAvailable = false;
316 return 0;
317 }
318 }
319
320 // We use the number of upUrl() calls to find the best matching plugin
321 // for the given directory. The smaller value, the better it is (0 is best).
322 KVersionControlPlugin* bestPlugin = 0;
323 int bestScore = INT_MAX;
324
325 // Verify whether the current directory contains revision information
326 // like .svn, .git, ...
327 foreach (KVersionControlPlugin* plugin, plugins) {
328 const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
329 if (QFile::exists(fileName)) {
330 // The score of this plugin is 0 (best), so we can just return this plugin,
331 // instead of going through the plugin scoring procedure, we can't find a better one ;)
332 return plugin;
333 }
334
335 // Version control systems like Git provide the version information
336 // file only in the root directory. Check whether the version information file can
337 // be found in one of the parent directories. For performance reasons this
338 // step is only done, if the previous directory was marked as versioned by
339 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
340 // must be shown at least once.
341 if (m_versionedDirectory) {
342 KUrl dirUrl(directory);
343 KUrl upUrl = dirUrl.upUrl();
344 int upUrlCounter = 1;
345 while ((upUrlCounter < bestScore) && (upUrl != dirUrl)) {
346 const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
347 if (QFile::exists(fileName)) {
348 if (upUrlCounter < bestScore) {
349 bestPlugin = plugin;
350 bestScore = upUrlCounter;
351 }
352 break;
353 }
354 dirUrl = upUrl;
355 upUrl = dirUrl.upUrl();
356 ++upUrlCounter;
357 }
358 }
359 }
360
361 return bestPlugin;
362 }
363
364 bool VersionControlObserver::isVersioned() const
365 {
366 return m_versionedDirectory && m_plugin;
367 }
368
369 #include "versioncontrolobserver.moc"