]> cloud.milkyroute.net Git - dolphin.git/blob - src/versioncontrolobserver.cpp
Restore "show hidden files" functionality in a generic way which works also for the...
[dolphin.git] / src / 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 "kversioncontrolplugin.h"
24
25 #include <kdirlister.h>
26 #include <klocale.h>
27
28 #include <QAbstractProxyModel>
29 #include <QAbstractItemView>
30 #include <QMutexLocker>
31 #include <QTimer>
32
33 /**
34 * The performance of updating the version state of items depends
35 * on the used plugin. To prevent that Dolphin gets blocked by a
36 * slow plugin, the updating is delegated to a thread.
37 */
38 class UpdateItemStatesThread : public QThread
39 {
40 public:
41 UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex);
42 void setData(KVersionControlPlugin* plugin,
43 const QList<VersionControlObserver::ItemState>& itemStates);
44 QList<VersionControlObserver::ItemState> itemStates() const;
45 bool retrievedItems() const;
46
47 protected:
48 virtual void run();
49
50 private:
51 bool m_retrievedItems;
52 KVersionControlPlugin* m_plugin;
53 QMutex* m_pluginMutex;
54 QList<VersionControlObserver::ItemState> m_itemStates;
55 };
56
57 UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex) :
58 QThread(parent),
59 m_retrievedItems(false),
60 m_pluginMutex(pluginMutex),
61 m_itemStates()
62 {
63 }
64
65 void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin,
66 const QList<VersionControlObserver::ItemState>& itemStates)
67 {
68 m_plugin = plugin;
69 m_itemStates = itemStates;
70 }
71
72 void UpdateItemStatesThread::run()
73 {
74 Q_ASSERT(!m_itemStates.isEmpty());
75 Q_ASSERT(m_plugin != 0);
76
77 // The items from m_itemStates may be located in different directory levels. The version
78 // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
79 // of doing an expensive search, we utilize the knowledge of the implementation of
80 // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
81 const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash);
82
83 QMutexLocker locker(m_pluginMutex);
84 m_retrievedItems = false;
85 if (m_plugin->beginRetrieval(directory)) {
86 const int count = m_itemStates.count();
87 for (int i = 0; i < count; ++i) {
88 m_itemStates[i].version = m_plugin->versionState(m_itemStates[i].item);
89 }
90 m_plugin->endRetrieval();
91 m_retrievedItems = true;
92 }
93 }
94
95 QList<VersionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
96 {
97 return m_itemStates;
98 }
99
100 bool UpdateItemStatesThread::retrievedItems() const
101 {
102 return m_retrievedItems;
103 }
104
105 // ------------------------------------------------------------------------------------------------
106
107 VersionControlObserver::VersionControlObserver(QAbstractItemView* view) :
108 QObject(view),
109 m_pendingItemStatesUpdate(false),
110 m_versionedDirectory(false),
111 m_silentUpdate(false),
112 m_view(view),
113 m_dirLister(0),
114 m_dolphinModel(0),
115 m_dirVerificationTimer(0),
116 m_pluginMutex(QMutex::Recursive),
117 m_plugin(0),
118 m_updateItemStatesThread(0)
119 {
120 Q_ASSERT(view != 0);
121
122 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
123 m_dolphinModel = (proxyModel == 0) ?
124 qobject_cast<DolphinModel*>(view->model()) :
125 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
126 if (m_dolphinModel != 0) {
127 m_dirLister = m_dolphinModel->dirLister();
128 connect(m_dirLister, SIGNAL(completed()),
129 this, SLOT(delayedDirectoryVerification()));
130
131 // The verification timer specifies the timeout until the shown directory
132 // is checked whether it is versioned. Per default it is assumed that users
133 // don't iterate through versioned directories and a high timeout is used
134 // The timeout will be decreased as soon as a versioned directory has been
135 // found (see verifyDirectory()).
136 m_dirVerificationTimer = new QTimer(this);
137 m_dirVerificationTimer->setSingleShot(true);
138 m_dirVerificationTimer->setInterval(500);
139 connect(m_dirVerificationTimer, SIGNAL(timeout()),
140 this, SLOT(verifyDirectory()));
141 }
142 }
143
144 VersionControlObserver::~VersionControlObserver()
145 {
146 if (m_updateItemStatesThread != 0) {
147 m_updateItemStatesThread->terminate();
148 m_updateItemStatesThread->wait();
149 }
150 delete m_plugin;
151 m_plugin = 0;
152 }
153
154 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
155 {
156 if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) {
157 QMutexLocker locker(&m_pluginMutex);
158 return m_plugin->contextMenuActions(items);
159 }
160 return QList<QAction*>();
161 }
162
163 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
164 {
165 if (m_dolphinModel->hasVersionData() && (m_plugin != 0)) {
166 QMutexLocker locker(&m_pluginMutex);
167 return m_plugin->contextMenuActions(directory);
168 }
169
170 return QList<QAction*>();
171 }
172
173 void VersionControlObserver::delayedDirectoryVerification()
174 {
175 m_silentUpdate = false;
176 m_dirVerificationTimer->start();
177 }
178
179 void VersionControlObserver::silentDirectoryVerification()
180 {
181 m_silentUpdate = true;
182 m_dirVerificationTimer->start();
183 }
184
185 void VersionControlObserver::verifyDirectory()
186 {
187 KUrl versionControlUrl = m_dirLister->url();
188 if (!versionControlUrl.isLocalFile()) {
189 return;
190 }
191
192 if (m_plugin == 0) {
193 // TODO: just for testing purposes. A plugin approach will be used later.
194 m_plugin = new SubversionPlugin();
195 connect(m_plugin, SIGNAL(infoMessage(const QString&)),
196 this, SIGNAL(infoMessage(const QString&)));
197 connect(m_plugin, SIGNAL(errorMessage(const QString&)),
198 this, SIGNAL(errorMessage(const QString&)));
199 connect(m_plugin, SIGNAL(operationCompletedMessage(const QString&)),
200 this, SIGNAL(operationCompletedMessage(const QString&)));
201 }
202
203 versionControlUrl.addPath(m_plugin->fileName());
204 const KFileItem item = m_dirLister->findByUrl(versionControlUrl);
205
206 bool foundVersionInfo = !item.isNull();
207 if (!foundVersionInfo && m_versionedDirectory) {
208 // Version control systems like Git provide the version information
209 // file only in the root directory. Check whether the version information file can
210 // be found in one of the parent directories.
211
212 // TODO...
213 }
214
215 if (foundVersionInfo) {
216 if (!m_versionedDirectory) {
217 m_versionedDirectory = true;
218
219 // The directory is versioned. Assume that the user will further browse through
220 // versioned directories and decrease the verification timer.
221 m_dirVerificationTimer->setInterval(100);
222 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
223 this, SLOT(delayedDirectoryVerification()));
224 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
225 this, SLOT(delayedDirectoryVerification()));
226 connect(m_plugin, SIGNAL(versionStatesChanged()),
227 this, SLOT(silentDirectoryVerification()));
228 }
229 updateItemStates();
230 } else if (m_versionedDirectory) {
231 m_versionedDirectory = false;
232
233 // The directory is not versioned. Reset the verification timer to a higher
234 // value, so that browsing through non-versioned directories is not slown down
235 // by an immediate verification.
236 m_dirVerificationTimer->setInterval(500);
237 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
238 this, SLOT(delayedDirectoryVerification()));
239 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
240 this, SLOT(delayedDirectoryVerification()));
241 disconnect(m_plugin, SIGNAL(versionStatesChanged()),
242 this, SLOT(silentDirectoryVerification()));
243 }
244 }
245
246 void VersionControlObserver::applyUpdatedItemStates()
247 {
248 if (!m_updateItemStatesThread->retrievedItems()) {
249 // ignore m_silentUpdate for an error message
250 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
251 return;
252 }
253
254 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
255 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
256 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
257 // This works as the update of the data does not require a relayout of the views used in Dolphin.
258 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
259 m_dolphinModel->blockSignals(true);
260
261 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
262 foreach (const ItemState& itemState, itemStates) {
263 m_dolphinModel->setData(itemState.index,
264 QVariant(static_cast<int>(itemState.version)),
265 Qt::DecorationRole);
266 }
267
268 m_dolphinModel->blockSignals(signalsBlocked);
269 m_view->viewport()->repaint();
270
271 if (!m_silentUpdate) {
272 // Using an empty message results in clearing the previously shown information message and showing
273 // the default status bar information. This is useful as the user already gets feedback that the
274 // operation has been completed because of the icon emblems.
275 emit operationCompletedMessage(QString());
276 }
277
278 if (m_pendingItemStatesUpdate) {
279 m_pendingItemStatesUpdate = false;
280 updateItemStates();
281 }
282 }
283
284 void VersionControlObserver::updateItemStates()
285 {
286 Q_ASSERT(m_plugin != 0);
287 if (m_updateItemStatesThread == 0) {
288 m_updateItemStatesThread = new UpdateItemStatesThread(this, &m_pluginMutex);
289 connect(m_updateItemStatesThread, SIGNAL(finished()),
290 this, SLOT(applyUpdatedItemStates()));
291 }
292 if (m_updateItemStatesThread->isRunning()) {
293 // An update is currently ongoing. Wait until the thread has finished
294 // the update (see applyUpdatedItemStates()).
295 m_pendingItemStatesUpdate = true;
296 return;
297 }
298
299 QList<ItemState> itemStates;
300 addDirectory(QModelIndex(), itemStates);
301 if (!itemStates.isEmpty()) {
302 if (!m_silentUpdate) {
303 emit infoMessage(i18nc("@info:status", "Updating version information..."));
304 }
305 m_updateItemStatesThread->setData(m_plugin, itemStates);
306 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
307 }
308 }
309
310 void VersionControlObserver::addDirectory(const QModelIndex& parentIndex, QList<ItemState>& itemStates)
311 {
312 const int rowCount = m_dolphinModel->rowCount(parentIndex);
313 for (int row = 0; row < rowCount; ++row) {
314 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Version, parentIndex);
315 addDirectory(index, itemStates);
316
317 ItemState itemState;
318 itemState.index = index;
319 itemState.item = m_dolphinModel->itemForIndex(index);
320 itemState.version = KVersionControlPlugin::UnversionedVersion;
321
322 itemStates.append(itemState);
323 }
324 }
325
326 #include "versioncontrolobserver.moc"