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