]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrol/versioncontrolobserver.cpp
9e52d1f5126c24f9be16be020d3c5fefd5d34086
[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
24 #include <kdirlister.h>
25 #include <klocale.h>
26 #include <kservice.h>
27 #include <kservicetypetrader.h>
28 #include <kversioncontrolplugin.h>
29
30 #include "updateitemstatesthread.h"
31
32 #include <QAbstractProxyModel>
33 #include <QAbstractItemView>
34 #include <QMutexLocker>
35 #include <QTimer>
36
37 VersionControlObserver::VersionControlObserver(QAbstractItemView* view) :
38 QObject(view),
39 m_pendingItemStatesUpdate(false),
40 m_versionedDirectory(false),
41 m_silentUpdate(false),
42 m_view(view),
43 m_dirLister(0),
44 m_dolphinModel(0),
45 m_dirVerificationTimer(0),
46 m_plugin(0),
47 m_updateItemStatesThread(0)
48 {
49 Q_ASSERT(view != 0);
50
51 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
52 m_dolphinModel = (proxyModel == 0) ?
53 qobject_cast<DolphinModel*>(view->model()) :
54 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
55 if (m_dolphinModel != 0) {
56 m_dirLister = m_dolphinModel->dirLister();
57 connect(m_dirLister, SIGNAL(completed()),
58 this, SLOT(delayedDirectoryVerification()));
59
60 // The verification timer specifies the timeout until the shown directory
61 // is checked whether it is versioned. Per default it is assumed that users
62 // don't iterate through versioned directories and a high timeout is used
63 // The timeout will be decreased as soon as a versioned directory has been
64 // found (see verifyDirectory()).
65 m_dirVerificationTimer = new QTimer(this);
66 m_dirVerificationTimer->setSingleShot(true);
67 m_dirVerificationTimer->setInterval(500);
68 connect(m_dirVerificationTimer, SIGNAL(timeout()),
69 this, SLOT(verifyDirectory()));
70 }
71 }
72
73 VersionControlObserver::~VersionControlObserver()
74 {
75 if (m_updateItemStatesThread != 0) {
76 disconnect(m_updateItemStatesThread, SIGNAL(finished()),
77 this, SLOT(applyUpdatedItemStates()));
78 if (m_updateItemStatesThread->isFinished()) {
79 delete m_updateItemStatesThread;
80 } else {
81 m_updateItemStatesThread->deleteWhenFinished();
82 }
83 m_updateItemStatesThread = 0;
84 }
85
86 m_plugin->disconnect();
87 m_plugin = 0;
88 }
89
90 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
91 {
92 QList<QAction*> actions;
93 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
94 actions = m_plugin->contextMenuActions(items);
95 m_updateItemStatesThread->endReadItemStates();
96 }
97 return actions;
98 }
99
100 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
101 {
102 QList<QAction*> actions;
103 if (isVersioned() && m_updateItemStatesThread->beginReadItemStates()) {
104 actions = m_plugin->contextMenuActions(directory);
105 m_updateItemStatesThread->endReadItemStates();
106 }
107
108 return actions;
109 }
110
111 void VersionControlObserver::delayedDirectoryVerification()
112 {
113 m_silentUpdate = false;
114 m_dirVerificationTimer->start();
115 }
116
117 void VersionControlObserver::silentDirectoryVerification()
118 {
119 m_silentUpdate = true;
120 m_dirVerificationTimer->start();
121 }
122
123 void VersionControlObserver::verifyDirectory()
124 {
125 KUrl versionControlUrl = m_dirLister->url();
126 if (!versionControlUrl.isLocalFile()) {
127 return;
128 }
129
130 if (m_plugin != 0) {
131 m_plugin->disconnect();
132 }
133
134 m_plugin = searchPlugin(versionControlUrl);
135 const bool foundVersionInfo = (m_plugin != 0);
136 if (!foundVersionInfo && m_versionedDirectory) {
137 // Version control systems like Git provide the version information
138 // file only in the root directory. Check whether the version information file can
139 // be found in one of the parent directories.
140
141 // TODO...
142 }
143
144 if (foundVersionInfo) {
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::applyUpdatedItemStates()
180 {
181 if (!m_updateItemStatesThread->retrievedItems()) {
182 // ignore m_silentUpdate for an error message
183 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
184 return;
185 }
186
187 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
188 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
189 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
190 // This works as the update of the data does not require a relayout of the views used in Dolphin.
191 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
192 m_dolphinModel->blockSignals(true);
193
194 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
195 foreach (const ItemState& itemState, itemStates) {
196 m_dolphinModel->setData(itemState.index,
197 QVariant(static_cast<int>(itemState.version)),
198 Qt::DecorationRole);
199 }
200
201 m_dolphinModel->blockSignals(signalsBlocked);
202 m_view->viewport()->repaint();
203
204 if (!m_silentUpdate) {
205 // Using an empty message results in clearing the previously shown information message and showing
206 // the default status bar information. This is useful as the user already gets feedback that the
207 // operation has been completed because of the icon emblems.
208 emit operationCompletedMessage(QString());
209 }
210
211 if (m_pendingItemStatesUpdate) {
212 m_pendingItemStatesUpdate = false;
213 updateItemStates();
214 }
215 }
216
217 void VersionControlObserver::updateItemStates()
218 {
219 Q_ASSERT(m_plugin != 0);
220 if (m_updateItemStatesThread == 0) {
221 m_updateItemStatesThread = new UpdateItemStatesThread();
222 connect(m_updateItemStatesThread, SIGNAL(finished()),
223 this, SLOT(applyUpdatedItemStates()));
224 }
225 if (m_updateItemStatesThread->isRunning()) {
226 // An update is currently ongoing. Wait until the thread has finished
227 // the update (see applyUpdatedItemStates()).
228 m_pendingItemStatesUpdate = true;
229 return;
230 }
231
232 QList<ItemState> itemStates;
233 addDirectory(QModelIndex(), itemStates);
234 if (!itemStates.isEmpty()) {
235 if (!m_silentUpdate) {
236 emit infoMessage(i18nc("@info:status", "Updating version information..."));
237 }
238 m_updateItemStatesThread->setData(m_plugin, itemStates);
239 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
240 }
241 }
242
243 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
244 {
245 const int rowCount = m_dolphinModel->rowCount(parentIndex);
246 for (int row = 0; row < rowCount; ++row) {
247 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
248 addDirectory(index, itemStates);
249
250 ItemState itemState;
251 itemState.index = index;
252 itemState.item = m_dolphinModel->itemForIndex(index);
253 itemState.version = KVersionControlPlugin::UnversionedVersion;
254
255 itemStates.append(itemState);
256 }
257 }
258
259 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
260 {
261 static bool pluginsAvailable = true;
262 static QList<KVersionControlPlugin*> plugins;
263
264 if (!pluginsAvailable) {
265 // a searching for plugins has already been done, but no
266 // plugins are installed
267 return 0;
268 }
269
270 if (plugins.isEmpty()) {
271 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
272 // all fileview version control plugins and remember them in 'plugins'.
273 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
274 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
275 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
276 Q_ASSERT(plugin != 0);
277 plugins.append(plugin);
278 }
279 if (plugins.isEmpty()) {
280 pluginsAvailable = false;
281 return 0;
282 }
283 }
284
285 // verify whether the current directory contains revision information
286 // like .svn, .git, ...
287 foreach (KVersionControlPlugin* plugin, plugins) {
288 KUrl fileUrl = directory;
289 fileUrl.addPath(plugin->fileName());
290 const KFileItem item = m_dirLister->findByUrl(fileUrl);
291 if (!item.isNull()) {
292 return plugin;
293 }
294 }
295
296 return 0;
297 }
298
299 bool VersionControlObserver::isVersioned() const
300 {
301 return m_dolphinModel->hasVersionData() && (m_plugin != 0);
302 }
303
304 #include "versioncontrolobserver.moc"