]> 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 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 if (!m_versionedDirectory) {
146 m_versionedDirectory = true;
147
148 // The directory is versioned. Assume that the user will further browse through
149 // versioned directories and decrease the verification timer.
150 m_dirVerificationTimer->setInterval(100);
151 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
152 this, SLOT(delayedDirectoryVerification()));
153 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
154 this, SLOT(delayedDirectoryVerification()));
155 connect(m_plugin, SIGNAL(versionStatesChanged()),
156 this, SLOT(silentDirectoryVerification()));
157 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
158 this, SIGNAL(infoMessage(const QString&)));
159 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
160 this, SIGNAL(errorMessage(const QString&)));
161 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
162 this, SIGNAL(operationCompletedMessage(const QString&)));
163 }
164 updateItemStates();
165 } else if (m_versionedDirectory) {
166 m_versionedDirectory = false;
167
168 // The directory is not versioned. Reset the verification timer to a higher
169 // value, so that browsing through non-versioned directories is not slown down
170 // by an immediate verification.
171 m_dirVerificationTimer->setInterval(500);
172 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
173 this, SLOT(delayedDirectoryVerification()));
174 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
175 this, SLOT(delayedDirectoryVerification()));
176 }
177 }
178
179 void VersionControlObserver::slotThreadFinished()
180 {
181 if (m_plugin == 0) {
182 return;
183 }
184
185 if (!m_updateItemStatesThread->retrievedItems()) {
186 // ignore m_silentUpdate for an error message
187 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
188 return;
189 }
190
191 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
192 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
193 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
194 // This works as the update of the data does not require a relayout of the views used in Dolphin.
195 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
196 m_dolphinModel->blockSignals(true);
197
198 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
199 foreach (const ItemState& itemState, itemStates) {
200 m_dolphinModel->setData(itemState.index,
201 QVariant(static_cast<int>(itemState.version)),
202 Qt::DecorationRole);
203 }
204
205 m_dolphinModel->blockSignals(signalsBlocked);
206 m_view->viewport()->repaint();
207
208 if (!m_silentUpdate) {
209 // Using an empty message results in clearing the previously shown information message and showing
210 // the default status bar information. This is useful as the user already gets feedback that the
211 // operation has been completed because of the icon emblems.
212 emit operationCompletedMessage(QString());
213 }
214
215 if (m_pendingItemStatesUpdate) {
216 m_pendingItemStatesUpdate = false;
217 updateItemStates();
218 }
219 }
220
221 void VersionControlObserver::updateItemStates()
222 {
223 Q_ASSERT(m_plugin != 0);
224 if (m_updateItemStatesThread == 0) {
225 m_updateItemStatesThread = new UpdateItemStatesThread();
226 connect(m_updateItemStatesThread, SIGNAL(finished()),
227 this, SLOT(slotThreadFinished()));
228 }
229 if (m_updateItemStatesThread->isRunning()) {
230 // An update is currently ongoing. Wait until the thread has finished
231 // the update (see slotThreadFinished()).
232 m_pendingItemStatesUpdate = true;
233 return;
234 }
235
236 QList<ItemState> itemStates;
237 addDirectory(QModelIndex(), itemStates);
238 if (!itemStates.isEmpty()) {
239 if (!m_silentUpdate) {
240 emit infoMessage(i18nc("@info:status", "Updating version information..."));
241 }
242 m_updateItemStatesThread->setData(m_plugin, itemStates);
243 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
244 }
245 }
246
247 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
248 {
249 const int rowCount = m_dolphinModel->rowCount(parentIndex);
250 for (int row = 0; row < rowCount; ++row) {
251 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
252 addDirectory(index, itemStates);
253
254 ItemState itemState;
255 itemState.index = index;
256 itemState.item = m_dolphinModel->itemForIndex(index);
257 itemState.version = KVersionControlPlugin::UnversionedVersion;
258
259 itemStates.append(itemState);
260 }
261 }
262
263 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
264 {
265 static bool pluginsAvailable = true;
266 static QList<KVersionControlPlugin*> plugins;
267
268 if (!pluginsAvailable) {
269 // A searching for plugins has already been done, but no
270 // plugins are installed
271 return 0;
272 }
273
274 if (plugins.isEmpty()) {
275 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
276 // all fileview version control plugins and remember them in 'plugins'.
277 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
278
279 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
280 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
281 if (enabledPlugins.contains((*it)->name())) {
282 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
283 Q_ASSERT(plugin != 0);
284 plugins.append(plugin);
285 }
286 }
287 if (plugins.isEmpty()) {
288 pluginsAvailable = false;
289 return 0;
290 }
291 }
292
293 // Verify whether the current directory contains revision information
294 // like .svn, .git, ...
295 foreach (KVersionControlPlugin* plugin, plugins) {
296 // Use the KDirLister cache to check for .svn, .git, ... files
297 KUrl dirUrl(directory);
298 KUrl fileUrl = dirUrl;
299 fileUrl.addPath(plugin->fileName());
300 const KFileItem item = m_dirLister->findByUrl(fileUrl);
301 if (!item.isNull()) {
302 return plugin;
303 }
304
305 // Version control systems like Git provide the version information
306 // file only in the root directory. Check whether the version information file can
307 // be found in one of the parent directories. For performance reasons this
308 // step is only done, if the previous directory was marked as versioned by
309 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
310 // must be shown at least once.
311 if (m_versionedDirectory) {
312 KUrl upUrl = dirUrl.upUrl();
313 while (upUrl != dirUrl) {
314 const QString filePath = dirUrl.pathOrUrl(KUrl::AddTrailingSlash) + plugin->fileName();
315 QFileInfo file(filePath);
316 if (file.exists()) {
317 return plugin;
318 }
319 dirUrl = upUrl;
320 upUrl = dirUrl.upUrl();
321 }
322 }
323 }
324
325 return 0;
326 }
327
328 bool VersionControlObserver::isVersioned() const
329 {
330 return m_dolphinModel->hasVersionData() && (m_plugin != 0);
331 }
332
333 #include "versioncontrolobserver.moc"