]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
Coding style update for pointer comparison
[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 <kversioncontrolplugin.h>
29
30 #include "pendingthreadsmaintainer.h"
31 #include "updateitemstatesthread.h"
32
33 #include <QAbstractProxyModel>
34 #include <QAbstractItemView>
35 #include <QMutexLocker>
36 #include <QTimer>
37
38 #include <views/dolphinmodel.h>
39
40 VersionControlObserver::VersionControlObserver(QAbstractItemView* view) :
41 QObject(view),
42 m_pendingItemStatesUpdate(false),
43 m_versionedDirectory(false),
44 m_silentUpdate(false),
45 m_view(view),
46 m_dirLister(0),
47 m_dolphinModel(0),
48 m_dirVerificationTimer(0),
49 m_plugin(0),
50 m_updateItemStatesThread(0)
51 {
52 Q_ASSERT(view);
53
54 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
55 m_dolphinModel = proxyModel ?
56 qobject_cast<DolphinModel*>(proxyModel->sourceModel()) :
57 qobject_cast<DolphinModel*>(view->model());
58
59 if (m_dolphinModel) {
60 m_dirLister = m_dolphinModel->dirLister();
61 connect(m_dirLister, SIGNAL(completed()),
62 this, SLOT(delayedDirectoryVerification()));
63
64 // The verification timer specifies the timeout until the shown directory
65 // is checked whether it is versioned. Per default it is assumed that users
66 // don't iterate through versioned directories and a high timeout is used
67 // The timeout will be decreased as soon as a versioned directory has been
68 // found (see verifyDirectory()).
69 m_dirVerificationTimer = new QTimer(this);
70 m_dirVerificationTimer->setSingleShot(true);
71 m_dirVerificationTimer->setInterval(500);
72 connect(m_dirVerificationTimer, SIGNAL(timeout()),
73 this, SLOT(verifyDirectory()));
74 }
75 }
76
77 VersionControlObserver::~VersionControlObserver()
78 {
79 if (m_updateItemStatesThread) {
80 if (m_updateItemStatesThread->isFinished()) {
81 delete m_updateItemStatesThread;
82 m_updateItemStatesThread = 0;
83 } else {
84 // The version controller gets deleted, while a thread still
85 // is working to get the version information. To avoid a blocking
86 // user interface, the thread will be forwarded to the
87 // PendingThreadsMaintainer, which will delete the thread later.
88 disconnect(m_updateItemStatesThread, SIGNAL(finished()),
89 this, SLOT(slotThreadFinished()));
90 PendingThreadsMaintainer::instance().append(m_updateItemStatesThread);
91 m_updateItemStatesThread = 0;
92 }
93 }
94
95 if (m_plugin) {
96 m_plugin->disconnect();
97 m_plugin = 0;
98 }
99 }
100
101 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
102 {
103 QList<QAction*> actions;
104 if (isVersioned() && m_updateItemStatesThread->lockPlugin()) {
105 actions = m_plugin->contextMenuActions(items);
106 m_updateItemStatesThread->unlockPlugin();
107 }
108 return actions;
109 }
110
111 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
112 {
113 QList<QAction*> actions;
114 if (isVersioned() && m_updateItemStatesThread->lockPlugin()) {
115 actions = m_plugin->contextMenuActions(directory);
116 m_updateItemStatesThread->unlockPlugin();
117 }
118
119 return actions;
120 }
121
122 void VersionControlObserver::delayedDirectoryVerification()
123 {
124 m_silentUpdate = false;
125 m_dirVerificationTimer->start();
126 }
127
128 void VersionControlObserver::silentDirectoryVerification()
129 {
130 m_silentUpdate = true;
131 m_dirVerificationTimer->start();
132 }
133
134 void VersionControlObserver::verifyDirectory()
135 {
136 const KUrl versionControlUrl = m_dirLister->url();
137 if (!versionControlUrl.isLocalFile()) {
138 return;
139 }
140
141 if (m_plugin) {
142 m_plugin->disconnect();
143 }
144
145 m_plugin = searchPlugin(versionControlUrl);
146 if (m_plugin) {
147 connect(m_plugin, SIGNAL(versionStatesChanged()),
148 this, SLOT(silentDirectoryVerification()));
149 connect(m_plugin, SIGNAL(infoMessage(QString)),
150 this, SIGNAL(infoMessage(QString)));
151 connect(m_plugin, SIGNAL(errorMessage(QString)),
152 this, SIGNAL(errorMessage(QString)));
153 connect(m_plugin, SIGNAL(operationCompletedMessage(QString)),
154 this, SIGNAL(operationCompletedMessage(QString)));
155
156 if (!m_versionedDirectory) {
157 m_versionedDirectory = true;
158
159 // The directory is versioned. Assume that the user will further browse through
160 // versioned directories and decrease the verification timer.
161 m_dirVerificationTimer->setInterval(100);
162 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
163 this, SLOT(delayedDirectoryVerification()));
164 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
165 this, SLOT(delayedDirectoryVerification()));
166 }
167 updateItemStates();
168 } else if (m_versionedDirectory) {
169 m_versionedDirectory = false;
170
171 // The directory is not versioned. Reset the verification timer to a higher
172 // value, so that browsing through non-versioned directories is not slown down
173 // by an immediate verification.
174 m_dirVerificationTimer->setInterval(500);
175 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
176 this, SLOT(delayedDirectoryVerification()));
177 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
178 this, SLOT(delayedDirectoryVerification()));
179 }
180 }
181
182 void VersionControlObserver::slotThreadFinished()
183 {
184 if (!m_plugin) {
185 return;
186 }
187
188 if (!m_updateItemStatesThread->retrievedItems()) {
189 // ignore m_silentUpdate for an error message
190 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
191 return;
192 }
193
194 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
195 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
196 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
197 // This works as the update of the data does not require a relayout of the views used in Dolphin.
198 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
199 m_dolphinModel->blockSignals(true);
200
201 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
202 foreach (const ItemState& itemState, itemStates) {
203 m_dolphinModel->setData(itemState.index,
204 QVariant(static_cast<int>(itemState.version)),
205 Qt::DecorationRole);
206 }
207
208 m_dolphinModel->blockSignals(signalsBlocked);
209 m_view->viewport()->repaint();
210
211 if (!m_silentUpdate) {
212 // Using an empty message results in clearing the previously shown information message and showing
213 // the default status bar information. This is useful as the user already gets feedback that the
214 // operation has been completed because of the icon emblems.
215 emit operationCompletedMessage(QString());
216 }
217
218 if (m_pendingItemStatesUpdate) {
219 m_pendingItemStatesUpdate = false;
220 updateItemStates();
221 }
222 }
223
224 void VersionControlObserver::updateItemStates()
225 {
226 Q_ASSERT(m_plugin);
227 if (!m_updateItemStatesThread) {
228 m_updateItemStatesThread = new UpdateItemStatesThread();
229 connect(m_updateItemStatesThread, SIGNAL(finished()),
230 this, SLOT(slotThreadFinished()));
231 }
232 if (m_updateItemStatesThread->isRunning()) {
233 // An update is currently ongoing. Wait until the thread has finished
234 // the update (see slotThreadFinished()).
235 m_pendingItemStatesUpdate = true;
236 return;
237 }
238
239 QList<ItemState> itemStates;
240 addDirectory(QModelIndex(), itemStates);
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 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
251 {
252 const int rowCount = m_dolphinModel->rowCount(parentIndex);
253 for (int row = 0; row < rowCount; ++row) {
254 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
255 addDirectory(index, itemStates);
256
257 ItemState itemState;
258 itemState.index = index;
259 itemState.item = m_dolphinModel->itemForIndex(index);
260 itemState.version = KVersionControlPlugin::UnversionedVersion;
261
262 itemStates.append(itemState);
263 }
264 }
265
266 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
267 {
268 static bool pluginsAvailable = true;
269 static QList<KVersionControlPlugin*> plugins;
270
271 if (!pluginsAvailable) {
272 // A searching for plugins has already been done, but no
273 // plugins are installed
274 return 0;
275 }
276
277 if (plugins.isEmpty()) {
278 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
279 // all fileview version control plugins and remember them in 'plugins'.
280 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
281
282 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
283 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
284 if (enabledPlugins.contains((*it)->name())) {
285 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
286 if (plugin) {
287 plugins.append(plugin);
288 }
289 }
290 }
291 if (plugins.isEmpty()) {
292 pluginsAvailable = false;
293 return 0;
294 }
295 }
296
297 // Verify whether the current directory contains revision information
298 // like .svn, .git, ...
299 foreach (KVersionControlPlugin* plugin, plugins) {
300 // Use the KDirLister cache to check for .svn, .git, ... files
301 KUrl dirUrl(directory);
302 KUrl fileUrl = dirUrl;
303 fileUrl.addPath(plugin->fileName());
304 const KFileItem item = m_dirLister->findByUrl(fileUrl);
305 if (!item.isNull()) {
306 return plugin;
307 }
308
309 // Version control systems like Git provide the version information
310 // file only in the root directory. Check whether the version information file can
311 // be found in one of the parent directories. For performance reasons this
312 // step is only done, if the previous directory was marked as versioned by
313 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
314 // must be shown at least once.
315 if (m_versionedDirectory) {
316 KUrl upUrl = dirUrl.upUrl();
317 while (upUrl != dirUrl) {
318 const QString filePath = dirUrl.pathOrUrl(KUrl::AddTrailingSlash) + plugin->fileName();
319 QFileInfo file(filePath);
320 if (file.exists()) {
321 return plugin;
322 }
323 dirUrl = upUrl;
324 upUrl = dirUrl.upUrl();
325 }
326 }
327 }
328
329 return 0;
330 }
331
332 bool VersionControlObserver::isVersioned() const
333 {
334 return m_dolphinModel->hasVersionData() && m_plugin;
335 }
336
337 #include "versioncontrolobserver.moc"