]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/versioncontrolobserver.cpp
Use KFileMetaDataWidget from kdelibs. Still open: Provide dialog which wraps KFileMet...
[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 m_plugin->disconnect();
108 m_plugin = 0;
109 }
110
111 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
112 {
113 QList<QAction*> actions;
114 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
115 actions = m_plugin->contextMenuActions(items);
116 m_updateItemStatesThread->endReadItemStates();
117 }
118 return actions;
119 }
120
121 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
122 {
123 QList<QAction*> actions;
124 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
125 actions = m_plugin->contextMenuActions(directory);
126 m_updateItemStatesThread->endReadItemStates();
127 }
128
129 return actions;
130 }
131
132 void VersionControlObserver::delayedDirectoryVerification()
133 {
134 m_silentUpdate = false;
135 m_dirVerificationTimer->start();
136 }
137
138 void VersionControlObserver::silentDirectoryVerification()
139 {
140 m_silentUpdate = true;
141 m_dirVerificationTimer->start();
142 }
143
144 void VersionControlObserver::verifyDirectory()
145 {
146 if (!s_pendingThreads->list.isEmpty()) {
147 // Try to cleanup pending threads (see explanation in destructor)
148 QList<UpdateItemStatesThread*>::iterator it = s_pendingThreads->list.begin();
149 while (it != s_pendingThreads->list.end()) {
150 if ((*it)->isFinished()) {
151 (*it)->deleteLater();
152 it = s_pendingThreads->list.erase(it);
153 } else {
154 ++it;
155 }
156 }
157 }
158
159 KUrl versionControlUrl = m_dirLister->url();
160 if (!versionControlUrl.isLocalFile()) {
161 return;
162 }
163
164 if (m_plugin != 0) {
165 m_plugin->disconnect();
166 }
167
168 m_plugin = searchPlugin(versionControlUrl);
169 const bool foundVersionInfo = (m_plugin != 0);
170 if (!foundVersionInfo && m_versionedDirectory) {
171 // Version control systems like Git provide the version information
172 // file only in the root directory. Check whether the version information file can
173 // be found in one of the parent directories.
174
175 // TODO...
176 }
177
178 if (foundVersionInfo) {
179 if (!m_versionedDirectory) {
180 m_versionedDirectory = true;
181
182 // The directory is versioned. Assume that the user will further browse through
183 // versioned directories and decrease the verification timer.
184 m_dirVerificationTimer->setInterval(100);
185 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
186 this, SLOT(delayedDirectoryVerification()));
187 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
188 this, SLOT(delayedDirectoryVerification()));
189 connect(m_plugin, SIGNAL(versionStatesChanged()),
190 this, SLOT(silentDirectoryVerification()));
191 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
192 this, SIGNAL(infoMessage(const QString&)));
193 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
194 this, SIGNAL(errorMessage(const QString&)));
195 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
196 this, SIGNAL(operationCompletedMessage(const QString&)));
197 }
198 updateItemStates();
199 } else if (m_versionedDirectory) {
200 m_versionedDirectory = false;
201
202 // The directory is not versioned. Reset the verification timer to a higher
203 // value, so that browsing through non-versioned directories is not slown down
204 // by an immediate verification.
205 m_dirVerificationTimer->setInterval(500);
206 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
207 this, SLOT(delayedDirectoryVerification()));
208 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
209 this, SLOT(delayedDirectoryVerification()));
210 }
211 }
212
213 void VersionControlObserver::slotThreadFinished()
214 {
215 if (m_plugin == 0) {
216 return;
217 }
218
219 if (!m_updateItemStatesThread->retrievedItems()) {
220 // ignore m_silentUpdate for an error message
221 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
222 return;
223 }
224
225 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
226 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
227 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
228 // This works as the update of the data does not require a relayout of the views used in Dolphin.
229 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
230 m_dolphinModel->blockSignals(true);
231
232 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
233 foreach (const ItemState& itemState, itemStates) {
234 m_dolphinModel->setData(itemState.index,
235 QVariant(static_cast<int>(itemState.version)),
236 Qt::DecorationRole);
237 }
238
239 m_dolphinModel->blockSignals(signalsBlocked);
240 m_view->viewport()->repaint();
241
242 if (!m_silentUpdate) {
243 // Using an empty message results in clearing the previously shown information message and showing
244 // the default status bar information. This is useful as the user already gets feedback that the
245 // operation has been completed because of the icon emblems.
246 emit operationCompletedMessage(QString());
247 }
248
249 if (m_pendingItemStatesUpdate) {
250 m_pendingItemStatesUpdate = false;
251 updateItemStates();
252 }
253 }
254
255 void VersionControlObserver::updateItemStates()
256 {
257 Q_ASSERT(m_plugin != 0);
258 if (m_updateItemStatesThread == 0) {
259 m_updateItemStatesThread = new UpdateItemStatesThread();
260 connect(m_updateItemStatesThread, SIGNAL(finished()),
261 this, SLOT(slotThreadFinished()));
262 }
263 if (m_updateItemStatesThread->isRunning()) {
264 // An update is currently ongoing. Wait until the thread has finished
265 // the update (see slotThreadFinished()).
266 m_pendingItemStatesUpdate = true;
267 return;
268 }
269
270 QList<ItemState> itemStates;
271 addDirectory(QModelIndex(), itemStates);
272 if (!itemStates.isEmpty()) {
273 if (!m_silentUpdate) {
274 emit infoMessage(i18nc("@info:status", "Updating version information..."));
275 }
276 m_updateItemStatesThread->setData(m_plugin, itemStates);
277 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
278 }
279 }
280
281 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
282 {
283 const int rowCount = m_dolphinModel->rowCount(parentIndex);
284 for (int row = 0; row < rowCount; ++row) {
285 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
286 addDirectory(index, itemStates);
287
288 ItemState itemState;
289 itemState.index = index;
290 itemState.item = m_dolphinModel->itemForIndex(index);
291 itemState.version = KVersionControlPlugin::UnversionedVersion;
292
293 itemStates.append(itemState);
294 }
295 }
296
297 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
298 {
299 static bool pluginsAvailable = true;
300 static QList<KVersionControlPlugin*> plugins;
301
302 if (!pluginsAvailable) {
303 // a searching for plugins has already been done, but no
304 // plugins are installed
305 return 0;
306 }
307
308 if (plugins.isEmpty()) {
309 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
310 // all fileview version control plugins and remember them in 'plugins'.
311 const QString disabledPlugins = VersionControlSettings::disabledPlugins();
312 const QStringList disabledPluginsList = disabledPlugins.split(',');
313
314 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
315 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
316 if (!disabledPluginsList.contains((*it)->name())) {
317 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
318 Q_ASSERT(plugin != 0);
319 plugins.append(plugin);
320 }
321 }
322 if (plugins.isEmpty()) {
323 pluginsAvailable = false;
324 return 0;
325 }
326 }
327
328 // verify whether the current directory contains revision information
329 // like .svn, .git, ...
330 foreach (KVersionControlPlugin* plugin, plugins) {
331 KUrl fileUrl = directory;
332 fileUrl.addPath(plugin->fileName());
333 const KFileItem item = m_dirLister->findByUrl(fileUrl);
334 if (!item.isNull()) {
335 return plugin;
336 }
337 }
338
339 return 0;
340 }
341
342 bool VersionControlObserver::isVersioned() const
343 {
344 return m_dolphinModel->hasVersionData() && (m_plugin != 0);
345 }
346
347 #include "versioncontrolobserver.moc"