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