]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
Merged very early alpha-version of Dolphin 2.0
[dolphin.git] / src / views / versioncontrol / versioncontrolobserver.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphin_versioncontrolsettings.h"
23
24 #include <KDirLister>
25 #include <KLocale>
26 #include <KService>
27 #include <KServiceTypeTrader>
28 #include <kversioncontrolplugin.h>
29
30 #include "pendingthreadsmaintainer.h"
31 #include "updateitemstatesthread.h"
32
33 #include <QAbstractProxyModel>
34 #include <QAbstractItemView>
35 #include <QMutexLocker>
36 #include <QTimer>
37
38 VersionControlObserver::VersionControlObserver(QWidget* view) :
39 QObject(view),
40 m_pendingItemStatesUpdate(false),
41 m_versionedDirectory(false),
42 m_silentUpdate(false),
43 m_view(view),
44 //m_dirLister(0),
45 //m_dolphinModel(0),
46 m_dirVerificationTimer(0),
47 m_plugin(0),
48 m_updateItemStatesThread(0)
49 {
50 Q_ASSERT(view);
51
52 /*QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
53 m_dolphinModel = proxyModel ?
54 qobject_cast<DolphinModel*>(proxyModel->sourceModel()) :
55 qobject_cast<DolphinModel*>(view->model());
56
57 if (m_dolphinModel) {
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) {
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) {
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) {
140 m_plugin->disconnect();
141 }
142
143 m_plugin = searchPlugin(versionControlUrl);
144 /*if (m_plugin) {
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) {
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);
225 if (!m_updateItemStatesThread) {
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 Q_UNUSED(parentIndex);
251 Q_UNUSED(itemStates);
252 /*const int rowCount = m_dolphinModel->rowCount(parentIndex);
253 for (int row = 0; row < rowCount; ++row) {
254 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
255 addDirectory(index, itemStates);
256
257 ItemState itemState;
258 itemState.index = index;
259 itemState.item = m_dolphinModel->itemForIndex(index);
260 itemState.version = KVersionControlPlugin::UnversionedVersion;
261
262 itemStates.append(itemState);
263 }*/
264 }
265
266 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
267 {
268 static bool pluginsAvailable = true;
269 static QList<KVersionControlPlugin*> plugins;
270
271 if (!pluginsAvailable) {
272 // A searching for plugins has already been done, but no
273 // plugins are installed
274 return 0;
275 }
276
277 if (plugins.isEmpty()) {
278 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
279 // all fileview version control plugins and remember them in 'plugins'.
280 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
281
282 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
283 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
284 if (enabledPlugins.contains((*it)->name())) {
285 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
286 if (plugin) {
287 plugins.append(plugin);
288 }
289 }
290 }
291 if (plugins.isEmpty()) {
292 pluginsAvailable = false;
293 return 0;
294 }
295 }
296
297 // Verify whether the current directory contains revision information
298 // like .svn, .git, ...
299 Q_UNUSED(directory);
300 /*foreach (KVersionControlPlugin* plugin, plugins) {
301 // Use the KDirLister cache to check for .svn, .git, ... files
302 KUrl dirUrl(directory);
303 KUrl fileUrl = dirUrl;
304 fileUrl.addPath(plugin->fileName());
305 const KFileItem item = m_dirLister->findByUrl(fileUrl);
306 if (!item.isNull()) {
307 return plugin;
308 }
309
310 // Version control systems like Git provide the version information
311 // file only in the root directory. Check whether the version information file can
312 // be found in one of the parent directories. For performance reasons this
313 // step is only done, if the previous directory was marked as versioned by
314 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
315 // must be shown at least once.
316 if (m_versionedDirectory) {
317 KUrl upUrl = dirUrl.upUrl();
318 while (upUrl != dirUrl) {
319 const QString filePath = dirUrl.pathOrUrl(KUrl::AddTrailingSlash) + plugin->fileName();
320 QFileInfo file(filePath);
321 if (file.exists()) {
322 return plugin;
323 }
324 dirUrl = upUrl;
325 upUrl = dirUrl.upUrl();
326 }
327 }
328 }*/
329
330 return 0;
331 }
332
333 bool VersionControlObserver::isVersioned() const
334 {
335 return false; //m_dolphinModel->hasVersionData() && m_plugin;
336 }
337
338 #include "versioncontrolobserver.moc"