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