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