]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/versioncontrolobserver.cpp
SVN_SILENT made messages (.desktop file)
[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 VersionControlObserver::VersionControlObserver(QAbstractItemView* view) :
39 QObject(view),
40 m_pendingItemStatesUpdate(false),
41 m_versionedDirectory(false),
42 m_silentUpdate(false),
43 m_view(view),
44 m_dirLister(0),
45 m_dolphinModel(0),
46 m_dirVerificationTimer(0),
47 m_plugin(0),
48 m_updateItemStatesThread(0)
49 {
50 Q_ASSERT(view != 0);
51
52 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
53 m_dolphinModel = (proxyModel == 0) ?
54 qobject_cast<DolphinModel*>(view->model()) :
55 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
56 if (m_dolphinModel != 0) {
57 m_dirLister = m_dolphinModel->dirLister();
58 connect(m_dirLister, SIGNAL(completed()),
59 this, SLOT(delayedDirectoryVerification()));
60
61 // The verification timer specifies the timeout until the shown directory
62 // is checked whether it is versioned. Per default it is assumed that users
63 // don't iterate through versioned directories and a high timeout is used
64 // The timeout will be decreased as soon as a versioned directory has been
65 // found (see verifyDirectory()).
66 m_dirVerificationTimer = new QTimer(this);
67 m_dirVerificationTimer->setSingleShot(true);
68 m_dirVerificationTimer->setInterval(500);
69 connect(m_dirVerificationTimer, SIGNAL(timeout()),
70 this, SLOT(verifyDirectory()));
71 }
72 }
73
74 VersionControlObserver::~VersionControlObserver()
75 {
76 if (m_updateItemStatesThread != 0) {
77 disconnect(m_updateItemStatesThread, SIGNAL(finished()),
78 this, SLOT(applyUpdatedItemStates()));
79 if (m_updateItemStatesThread->isFinished()) {
80 delete m_updateItemStatesThread;
81 } else {
82 m_updateItemStatesThread->deleteWhenFinished();
83 }
84 m_updateItemStatesThread = 0;
85 }
86
87 m_plugin->disconnect();
88 m_plugin = 0;
89 }
90
91 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
92 {
93 QList<QAction*> actions;
94 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
95 actions = m_plugin->contextMenuActions(items);
96 m_updateItemStatesThread->endReadItemStates();
97 }
98 return actions;
99 }
100
101 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
102 {
103 QList<QAction*> actions;
104 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
105 actions = m_plugin->contextMenuActions(directory);
106 m_updateItemStatesThread->endReadItemStates();
107 }
108
109 return actions;
110 }
111
112 void VersionControlObserver::delayedDirectoryVerification()
113 {
114 m_silentUpdate = false;
115 m_dirVerificationTimer->start();
116 }
117
118 void VersionControlObserver::silentDirectoryVerification()
119 {
120 m_silentUpdate = true;
121 m_dirVerificationTimer->start();
122 }
123
124 void VersionControlObserver::verifyDirectory()
125 {
126 KUrl versionControlUrl = m_dirLister->url();
127 if (!versionControlUrl.isLocalFile()) {
128 return;
129 }
130
131 if (m_plugin != 0) {
132 m_plugin->disconnect();
133 }
134
135 m_plugin = searchPlugin(versionControlUrl);
136 const bool foundVersionInfo = (m_plugin != 0);
137 if (!foundVersionInfo && m_versionedDirectory) {
138 // Version control systems like Git provide the version information
139 // file only in the root directory. Check whether the version information file can
140 // be found in one of the parent directories.
141
142 // TODO...
143 }
144
145 if (foundVersionInfo) {
146 if (!m_versionedDirectory) {
147 m_versionedDirectory = true;
148
149 // The directory is versioned. Assume that the user will further browse through
150 // versioned directories and decrease the verification timer.
151 m_dirVerificationTimer->setInterval(100);
152 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
153 this, SLOT(delayedDirectoryVerification()));
154 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
155 this, SLOT(delayedDirectoryVerification()));
156 connect(m_plugin, SIGNAL(versionStatesChanged()),
157 this, SLOT(silentDirectoryVerification()));
158 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
159 this, SIGNAL(infoMessage(const QString&)));
160 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
161 this, SIGNAL(errorMessage(const QString&)));
162 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
163 this, SIGNAL(operationCompletedMessage(const QString&)));
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 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
174 this, SLOT(delayedDirectoryVerification()));
175 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
176 this, SLOT(delayedDirectoryVerification()));
177 }
178 }
179
180 void VersionControlObserver::applyUpdatedItemStates()
181 {
182 if (m_plugin == 0) {
183 // The signal finished() has been emitted, but the thread has been marked
184 // as invalid in the meantime. Just ignore the signal in this case.
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 != 0);
227 if (m_updateItemStatesThread == 0) {
228 m_updateItemStatesThread = new UpdateItemStatesThread();
229 connect(m_updateItemStatesThread, SIGNAL(finished()),
230 this, SLOT(applyUpdatedItemStates()));
231 }
232 if (m_updateItemStatesThread->isRunning()) {
233 // An update is currently ongoing. Wait until the thread has finished
234 // the update (see applyUpdatedItemStates()).
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(); // applyUpdatedItemStates() 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 QString disabledPlugins = VersionControlSettings::disabledPlugins();
281 const QStringList disabledPluginsList = disabledPlugins.split(',');
282
283 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
284 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
285 if (!disabledPluginsList.contains((*it)->name())) {
286 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
287 Q_ASSERT(plugin != 0);
288 plugins.append(plugin);
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 KUrl fileUrl = directory;
301 fileUrl.addPath(plugin->fileName());
302 const KFileItem item = m_dirLister->findByUrl(fileUrl);
303 if (!item.isNull()) {
304 return plugin;
305 }
306 }
307
308 return 0;
309 }
310
311 bool VersionControlObserver::isVersioned() const
312 {
313 return m_dolphinModel->hasVersionData() && (m_plugin != 0);
314 }
315
316 #include "versioncontrolobserver.moc"