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