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