]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
c8d9d6874fb5c3d3b1945741c2b5a91774e3df54
[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 <kversioncontrolplugin.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::contextMenuActions(const KFileItemList& items) const
91 {
92 QList<QAction*> actions;
93 if (isVersioned()) {
94 if (m_updateItemStatesThread && m_updateItemStatesThread->lockPlugin()) {
95 actions = m_plugin->contextMenuActions(items);
96 m_updateItemStatesThread->unlockPlugin();
97 } else {
98 actions = m_plugin->contextMenuActions(items);
99 }
100 }
101
102 return actions;
103 }
104
105 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
106 {
107 QList<QAction*> actions;
108 if (isVersioned()) {
109 if (m_updateItemStatesThread && m_updateItemStatesThread->lockPlugin()) {
110 actions = m_plugin->contextMenuActions(directory);
111 m_updateItemStatesThread->unlockPlugin();
112 } else {
113 actions = m_plugin->contextMenuActions(directory);
114 }
115 }
116
117 return actions;
118 }
119
120 void VersionControlObserver::delayedDirectoryVerification()
121 {
122 m_silentUpdate = false;
123 m_dirVerificationTimer->start();
124 }
125
126 void VersionControlObserver::silentDirectoryVerification()
127 {
128 m_silentUpdate = true;
129 m_dirVerificationTimer->start();
130 }
131
132 void VersionControlObserver::verifyDirectory()
133 {
134 if (!m_model) {
135 return;
136 }
137
138 const KUrl versionControlUrl = m_model->rootDirectory();
139 if (!versionControlUrl.isLocalFile()) {
140 return;
141 }
142
143 if (m_plugin) {
144 m_plugin->disconnect(this);
145 }
146
147 m_plugin = searchPlugin(versionControlUrl);
148 if (m_plugin) {
149 connect(m_plugin, SIGNAL(versionStatesChanged()),
150 this, SLOT(silentDirectoryVerification()));
151 connect(m_plugin, SIGNAL(infoMessage(QString)),
152 this, SIGNAL(infoMessage(QString)));
153 connect(m_plugin, SIGNAL(errorMessage(QString)),
154 this, SIGNAL(errorMessage(QString)));
155 connect(m_plugin, SIGNAL(operationCompletedMessage(QString)),
156 this, SIGNAL(operationCompletedMessage(QString)));
157
158 if (!m_versionedDirectory) {
159 m_versionedDirectory = true;
160
161 // The directory is versioned. Assume that the user will further browse through
162 // versioned directories and decrease the verification timer.
163 m_dirVerificationTimer->setInterval(100);
164 }
165 updateItemStates();
166 } else if (m_versionedDirectory) {
167 m_versionedDirectory = false;
168
169 // The directory is not versioned. Reset the verification timer to a higher
170 // value, so that browsing through non-versioned directories is not slown down
171 // by an immediate verification.
172 m_dirVerificationTimer->setInterval(500);
173 }
174 }
175
176 void VersionControlObserver::slotThreadFinished()
177 {
178 UpdateItemStatesThread* thread = m_updateItemStatesThread;
179 m_updateItemStatesThread = 0; // The thread deletes itself automatically (see updateItemStates())
180
181 if (!m_plugin) {
182 return;
183 }
184
185 if (!thread->retrievedItems()) {
186 // Ignore m_silentUpdate for an error message
187 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
188 return;
189 }
190
191 const QList<ItemState> itemStates = thread->itemStates();
192 foreach (const ItemState& itemState, itemStates) {
193 QHash<QByteArray, QVariant> values;
194 values.insert("version", QVariant(itemState.version));
195 m_model->setData(itemState.index, values);
196 }
197
198 if (!m_silentUpdate) {
199 // Using an empty message results in clearing the previously shown information message and showing
200 // the default status bar information. This is useful as the user already gets feedback that the
201 // operation has been completed because of the icon emblems.
202 emit operationCompletedMessage(QString());
203 }
204
205 if (m_pendingItemStatesUpdate) {
206 m_pendingItemStatesUpdate = false;
207 updateItemStates();
208 }
209 }
210
211 void VersionControlObserver::updateItemStates()
212 {
213 Q_ASSERT(m_plugin);
214 if (!m_updateItemStatesThread) {
215 m_updateItemStatesThread = new UpdateItemStatesThread();
216 connect(m_updateItemStatesThread, SIGNAL(finished()),
217 this, SLOT(slotThreadFinished()));
218 connect(m_updateItemStatesThread, SIGNAL(finished()),
219 m_updateItemStatesThread, SLOT(deleteLater()));
220 }
221 if (m_updateItemStatesThread->isRunning()) {
222 // An update is currently ongoing. Wait until the thread has finished
223 // the update (see slotThreadFinished()).
224 m_pendingItemStatesUpdate = true;
225 return;
226 }
227
228 QList<ItemState> itemStates;
229 const int itemCount = m_model->count();
230 itemStates.reserve(itemCount);
231
232 for (int i = 0; i < itemCount; ++i) {
233 ItemState itemState;
234 itemState.index = i;
235 itemState.item = m_model->fileItem(i);
236 itemState.version = KVersionControlPlugin::UnversionedVersion;
237
238 itemStates.append(itemState);
239 }
240
241 if (!itemStates.isEmpty()) {
242 if (!m_silentUpdate) {
243 emit infoMessage(i18nc("@info:status", "Updating version information..."));
244 }
245 m_updateItemStatesThread->setData(m_plugin, itemStates);
246 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
247 }
248 }
249
250 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
251 {
252 static bool pluginsAvailable = true;
253 static QList<KVersionControlPlugin*> plugins;
254
255 if (!pluginsAvailable) {
256 // A searching for plugins has already been done, but no
257 // plugins are installed
258 return 0;
259 }
260
261 if (plugins.isEmpty()) {
262 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
263 // all fileview version control plugins and remember them in 'plugins'.
264 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
265
266 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
267 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
268 if (enabledPlugins.contains((*it)->name())) {
269 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
270 if (plugin) {
271 plugins.append(plugin);
272 }
273 }
274 }
275 if (plugins.isEmpty()) {
276 pluginsAvailable = false;
277 return 0;
278 }
279 }
280
281 // Verify whether the current directory contains revision information
282 // like .svn, .git, ...
283 foreach (KVersionControlPlugin* plugin, plugins) {
284 // Use the KDirLister cache to check for .svn, .git, ... files
285 const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
286 if (QFile::exists(fileName)) {
287 return plugin;
288 }
289
290 // Version control systems like Git provide the version information
291 // file only in the root directory. Check whether the version information file can
292 // be found in one of the parent directories. For performance reasons this
293 // step is only done, if the previous directory was marked as versioned by
294 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
295 // must be shown at least once.
296 if (m_versionedDirectory) {
297 KUrl dirUrl(directory);
298 KUrl upUrl = dirUrl.upUrl();
299 while (upUrl != dirUrl) {
300 const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
301 if (QFile::exists(fileName)) {
302 return plugin;
303 }
304 dirUrl = upUrl;
305 upUrl = dirUrl.upUrl();
306 }
307 }
308 }
309
310 return 0;
311 }
312
313 bool VersionControlObserver::isVersioned() const
314 {
315 return m_versionedDirectory && m_plugin;
316 }
317
318 #include "versioncontrolobserver.moc"