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