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