]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
Provide KVersionControlPlugin version 2
[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 <KDirLister>
25 #include <KLocale>
26 #include <KService>
27 #include <KServiceTypeTrader>
28 #include <kitemviews/kfileitemmodel.h>
29 #include <kversioncontrolplugin2.h>
30
31 #include "updateitemstatesthread.h"
32
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 if (!m_model) {
94 return actions;
95 }
96
97 KVersionControlPlugin2* pluginV2 = qobject_cast<KVersionControlPlugin2*>(m_plugin);
98 if (pluginV2) {
99 // Use version 2 of the KVersionControlPlugin which allows providing actions
100 // also for non-versioned directories.
101 if (m_updateItemStatesThread && m_updateItemStatesThread->lockPlugin()) {
102 actions = pluginV2->actions(items);
103 m_updateItemStatesThread->unlockPlugin();
104 } else {
105 actions = pluginV2->actions(items);
106 }
107 } else if (isVersioned()) {
108 // Support deprecated interfaces from KVersionControlPlugin version 1.
109 // Context menu actions where only available for versioned directories.
110 QString directory;
111 if (items.count() == 1) {
112 const KFileItem rootItem = m_model->rootItem();
113 if (!rootItem.isNull() && items.first().url() == rootItem.url()) {
114 directory = rootItem.url().path(KUrl::AddTrailingSlash);
115 }
116 }
117
118 if (m_updateItemStatesThread && m_updateItemStatesThread->lockPlugin()) {
119 actions = directory.isEmpty() ? m_plugin->contextMenuActions(items)
120 : m_plugin->contextMenuActions(directory);
121 m_updateItemStatesThread->unlockPlugin();
122 } else {
123 actions = directory.isEmpty() ? m_plugin->contextMenuActions(items)
124 : m_plugin->contextMenuActions(directory);
125 }
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 KUrl versionControlUrl = m_model->rootItem().url();
150 if (!versionControlUrl.isLocalFile()) {
151 return;
152 }
153
154 if (m_plugin) {
155 m_plugin->disconnect(this);
156 }
157
158 m_plugin = searchPlugin(versionControlUrl);
159 if (m_plugin) {
160 KVersionControlPlugin2* pluginV2 = qobject_cast<KVersionControlPlugin2*>(m_plugin);
161 if (pluginV2) {
162 connect(pluginV2, SIGNAL(itemVersionsChanged()),
163 this, SLOT(silentDirectoryVerification()));
164 } else {
165 connect(m_plugin, SIGNAL(versionStatesChanged()),
166 this, SLOT(silentDirectoryVerification()));
167 }
168 connect(m_plugin, SIGNAL(infoMessage(QString)),
169 this, SIGNAL(infoMessage(QString)));
170 connect(m_plugin, SIGNAL(errorMessage(QString)),
171 this, SIGNAL(errorMessage(QString)));
172 connect(m_plugin, SIGNAL(operationCompletedMessage(QString)),
173 this, SIGNAL(operationCompletedMessage(QString)));
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) {
199 return;
200 }
201
202 if (!thread->retrievedItems()) {
203 // Ignore m_silentUpdate for an error message
204 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
205 return;
206 }
207
208 const QList<ItemState> itemStates = thread->itemStates();
209 foreach (const ItemState& itemState, itemStates) {
210 QHash<QByteArray, QVariant> values;
211 values.insert("version", QVariant(itemState.version));
212 m_model->setData(itemState.index, values);
213 }
214
215 if (!m_silentUpdate) {
216 // Using an empty message results in clearing the previously shown information message and showing
217 // the default status bar information. This is useful as the user already gets feedback that the
218 // operation has been completed because of the icon emblems.
219 emit operationCompletedMessage(QString());
220 }
221
222 if (m_pendingItemStatesUpdate) {
223 m_pendingItemStatesUpdate = false;
224 updateItemStates();
225 }
226 }
227
228 void VersionControlObserver::updateItemStates()
229 {
230 Q_ASSERT(m_plugin);
231 if (!m_updateItemStatesThread) {
232 m_updateItemStatesThread = new UpdateItemStatesThread();
233 connect(m_updateItemStatesThread, SIGNAL(finished()),
234 this, SLOT(slotThreadFinished()));
235 connect(m_updateItemStatesThread, SIGNAL(finished()),
236 m_updateItemStatesThread, SLOT(deleteLater()));
237 }
238 if (m_updateItemStatesThread->isRunning()) {
239 // An update is currently ongoing. Wait until the thread has finished
240 // the update (see slotThreadFinished()).
241 m_pendingItemStatesUpdate = true;
242 return;
243 }
244
245 QList<ItemState> itemStates;
246 const int itemCount = m_model->count();
247 itemStates.reserve(itemCount);
248
249 for (int i = 0; i < itemCount; ++i) {
250 ItemState itemState;
251 itemState.index = i;
252 itemState.item = m_model->fileItem(i);
253 itemState.version = KVersionControlPlugin2::UnversionedVersion;
254
255 itemStates.append(itemState);
256 }
257
258 if (!itemStates.isEmpty()) {
259 if (!m_silentUpdate) {
260 emit infoMessage(i18nc("@info:status", "Updating version information..."));
261 }
262 m_updateItemStatesThread->setData(m_plugin, itemStates);
263 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
264 }
265 }
266
267 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
268 {
269 static bool pluginsAvailable = true;
270 static QList<KVersionControlPlugin*> plugins;
271
272 if (!pluginsAvailable) {
273 // A searching for plugins has already been done, but no
274 // plugins are installed
275 return 0;
276 }
277
278 if (plugins.isEmpty()) {
279 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
280 // all fileview version control plugins and remember them in 'plugins'.
281 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
282
283 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
284 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
285 if (enabledPlugins.contains((*it)->name())) {
286 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
287 if (plugin) {
288 plugins.append(plugin);
289 }
290 }
291 }
292 if (plugins.isEmpty()) {
293 pluginsAvailable = false;
294 return 0;
295 }
296 }
297
298 // Verify whether the current directory contains revision information
299 // like .svn, .git, ...
300 foreach (KVersionControlPlugin* plugin, plugins) {
301 // Use the KDirLister cache to check for .svn, .git, ... files
302 const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
303 if (QFile::exists(fileName)) {
304 return plugin;
305 }
306
307 // Version control systems like Git provide the version information
308 // file only in the root directory. Check whether the version information file can
309 // be found in one of the parent directories. For performance reasons this
310 // step is only done, if the previous directory was marked as versioned by
311 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
312 // must be shown at least once.
313 if (m_versionedDirectory) {
314 KUrl dirUrl(directory);
315 KUrl upUrl = dirUrl.upUrl();
316 while (upUrl != dirUrl) {
317 const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
318 if (QFile::exists(fileName)) {
319 return plugin;
320 }
321 dirUrl = upUrl;
322 upUrl = dirUrl.upUrl();
323 }
324 }
325 }
326
327 return 0;
328 }
329
330 bool VersionControlObserver::isVersioned() const
331 {
332 return m_versionedDirectory && m_plugin;
333 }
334
335 #include "versioncontrolobserver.moc"