]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[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::delayedDirectoryVerification);
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::delayedDirectoryVerification);
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 || !isVersioned()) {
122 return {};
123 }
124
125 return m_plugin->actions(items);
126 }
127
128 void VersionControlObserver::delayedDirectoryVerification()
129 {
130 m_silentUpdate = false;
131 m_dirVerificationTimer->start();
132 }
133
134 void VersionControlObserver::silentDirectoryVerification()
135 {
136 m_silentUpdate = true;
137 m_dirVerificationTimer->start();
138 }
139
140 void VersionControlObserver::verifyDirectory()
141 {
142 if (!m_model) {
143 return;
144 }
145
146 const KFileItem rootItem = m_model->rootItem();
147 if (rootItem.isNull() || !rootItem.url().isLocalFile()) {
148 return;
149 }
150
151 if (m_plugin) {
152 m_plugin->disconnect(this);
153 }
154
155 m_plugin = searchPlugin(rootItem.url());
156 if (m_plugin) {
157 connect(m_plugin, &KVersionControlPlugin::itemVersionsChanged,
158 this, &VersionControlObserver::silentDirectoryVerification);
159 connect(m_plugin, &KVersionControlPlugin::infoMessage,
160 this, &VersionControlObserver::infoMessage);
161 connect(m_plugin, &KVersionControlPlugin::errorMessage,
162 this, &VersionControlObserver::errorMessage);
163 connect(m_plugin, &KVersionControlPlugin::operationCompletedMessage,
164 this, &VersionControlObserver::operationCompletedMessage);
165
166 if (!m_versionedDirectory) {
167 m_versionedDirectory = true;
168
169 // The directory is versioned. Assume that the user will further browse through
170 // versioned directories and decrease the verification timer.
171 m_dirVerificationTimer->setInterval(100);
172 }
173 updateItemStates();
174 } else if (m_versionedDirectory) {
175 m_versionedDirectory = false;
176
177 // The directory is not versioned. Reset the verification timer to a higher
178 // value, so that browsing through non-versioned directories is not slown down
179 // by an immediate verification.
180 m_dirVerificationTimer->setInterval(500);
181 }
182 }
183
184 void VersionControlObserver::slotThreadFinished()
185 {
186 UpdateItemStatesThread* thread = m_updateItemStatesThread;
187 m_updateItemStatesThread = nullptr; // The thread deletes itself automatically (see updateItemStates())
188
189 if (!m_plugin || !thread) {
190 return;
191 }
192
193 const QMap<QString, QVector<ItemState> >& itemStates = thread->itemStates();
194 QMap<QString, QVector<ItemState> >::const_iterator it = itemStates.constBegin();
195 for (; it != itemStates.constEnd(); ++it) {
196 const QVector<ItemState>& items = it.value();
197
198 foreach (const ItemState& item, items) {
199 const KFileItem& fileItem = item.first;
200 const KVersionControlPlugin::ItemVersion version = item.second;
201 QHash<QByteArray, QVariant> values;
202 values.insert("version", QVariant(version));
203 m_model->setData(m_model->index(fileItem), values);
204 }
205 }
206
207 if (!m_silentUpdate) {
208 // Using an empty message results in clearing the previously shown information message and showing
209 // the default status bar information. This is useful as the user already gets feedback that the
210 // operation has been completed because of the icon emblems.
211 emit operationCompletedMessage(QString());
212 }
213
214 if (m_pendingItemStatesUpdate) {
215 m_pendingItemStatesUpdate = false;
216 updateItemStates();
217 }
218 }
219
220 void VersionControlObserver::updateItemStates()
221 {
222 Q_ASSERT(m_plugin);
223 if (m_updateItemStatesThread) {
224 // An update is currently ongoing. Wait until the thread has finished
225 // the update (see slotThreadFinished()).
226 m_pendingItemStatesUpdate = true;
227 return;
228 }
229
230 QMap<QString, QVector<ItemState> > itemStates;
231 createItemStatesList(itemStates);
232
233 if (!itemStates.isEmpty()) {
234 if (!m_silentUpdate) {
235 emit infoMessage(i18nc("@info:status", "Updating version information..."));
236 }
237 m_updateItemStatesThread = new UpdateItemStatesThread(m_plugin, itemStates);
238 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
239 this, &VersionControlObserver::slotThreadFinished);
240 connect(m_updateItemStatesThread, &UpdateItemStatesThread::finished,
241 m_updateItemStatesThread, &UpdateItemStatesThread::deleteLater);
242
243 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
244 }
245 }
246
247 int VersionControlObserver::createItemStatesList(QMap<QString, QVector<ItemState> >& itemStates,
248 const int firstIndex)
249 {
250 const int itemCount = m_model->count();
251 const int currentExpansionLevel = m_model->expandedParentsCount(firstIndex);
252
253 QVector<ItemState> items;
254 items.reserve(itemCount - firstIndex);
255
256 int index;
257 for (index = firstIndex; index < itemCount; ++index) {
258 const int expansionLevel = m_model->expandedParentsCount(index);
259
260 if (expansionLevel == currentExpansionLevel) {
261 ItemState itemState;
262 itemState.first = m_model->fileItem(index);
263 itemState.second = KVersionControlPlugin::UnversionedVersion;
264
265 items.append(itemState);
266 } else if (expansionLevel > currentExpansionLevel) {
267 // Sub folder
268 index += createItemStatesList(itemStates, index) - 1;
269 } else {
270 break;
271 }
272 }
273
274 if (!items.isEmpty()) {
275 const QUrl& url = items.first().first.url();
276 itemStates.insert(url.adjusted(QUrl::RemoveFilename).path(), items);
277 }
278
279 return index - firstIndex; // number of processed items
280 }
281
282 KVersionControlPlugin* VersionControlObserver::searchPlugin(const QUrl& directory)
283 {
284 if (!m_pluginsInitialized) {
285 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
286 // all fileview version control plugins and remember them in 'plugins'.
287 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
288
289 const KService::List pluginServices = KServiceTypeTrader::self()->query(QStringLiteral("FileViewVersionControlPlugin"));
290 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
291 if (enabledPlugins.contains((*it)->name())) {
292 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>(this);
293 if (plugin) {
294 m_plugins.append(plugin);
295 }
296 }
297 }
298 m_pluginsInitialized = true;
299 }
300
301 if (m_plugins.empty()) {
302 // A searching for plugins has already been done, but no
303 // plugins are installed
304 return nullptr;
305 }
306
307 // We use the number of upUrl() calls to find the best matching plugin
308 // for the given directory. The smaller value, the better it is (0 is best).
309 KVersionControlPlugin* bestPlugin = nullptr;
310 int bestScore = INT_MAX;
311
312 // Verify whether the current directory contains revision information
313 // like .svn, .git, ...
314 foreach (KVersionControlPlugin* plugin, m_plugins) {
315 const QString fileName = directory.path() + '/' + plugin->fileName();
316 if (QFile::exists(fileName)) {
317 // The score of this plugin is 0 (best), so we can just return this plugin,
318 // instead of going through the plugin scoring procedure, we can't find a better one ;)
319 return plugin;
320 }
321
322 // Version control systems like Git provide the version information
323 // file only in the root directory. Check whether the version information file can
324 // be found in one of the parent directories. For performance reasons this
325 // step is only done, if the previous directory was marked as versioned by
326 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
327 // must be shown at least once.
328 if (m_versionedDirectory) {
329 QUrl dirUrl(directory);
330 QUrl upUrl = KIO::upUrl(dirUrl);
331 int upUrlCounter = 1;
332 while ((upUrlCounter < bestScore) && (upUrl != dirUrl)) {
333 const QString fileName = dirUrl.path() + '/' + plugin->fileName();
334 if (QFile::exists(fileName)) {
335 if (upUrlCounter < bestScore) {
336 bestPlugin = plugin;
337 bestScore = upUrlCounter;
338 }
339 break;
340 }
341 dirUrl = upUrl;
342 upUrl = KIO::upUrl(dirUrl);
343 ++upUrlCounter;
344 }
345 }
346 }
347
348 return bestPlugin;
349 }
350
351 bool VersionControlObserver::isVersioned() const
352 {
353 return m_versionedDirectory && m_plugin;
354 }
355