]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
Improved Subversion test plugin to allow committing, updating, diffing, adding and...
[dolphin.git] / src / revisioncontrolobserver.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 "revisioncontrolobserver.h"
21
22 #include "dolphinmodel.h"
23 #include "revisioncontrolplugin.h"
24
25 #include <kdirlister.h>
26
27 #include <QAbstractProxyModel>
28 #include <QAbstractItemView>
29 #include <QMutexLocker>
30 #include <QTimer>
31
32 /**
33 * The performance of updating the revision state of items depends
34 * on the used plugin. To prevent that Dolphin gets blocked by a
35 * slow plugin, the updating is delegated to a thread.
36 */
37 class UpdateItemStatesThread : public QThread
38 {
39 public:
40 UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex);
41 void setData(RevisionControlPlugin* plugin,
42 const QList<RevisionControlObserver::ItemState>& itemStates);
43 QList<RevisionControlObserver::ItemState> itemStates() const;
44
45 protected:
46 virtual void run();
47
48 private:
49 RevisionControlPlugin* m_plugin;
50 QMutex* m_pluginMutex;
51 QList<RevisionControlObserver::ItemState> m_itemStates;
52 };
53
54 UpdateItemStatesThread::UpdateItemStatesThread(QObject* parent, QMutex* pluginMutex) :
55 QThread(parent),
56 m_pluginMutex(pluginMutex)
57 {
58 }
59
60 void UpdateItemStatesThread::setData(RevisionControlPlugin* plugin,
61 const QList<RevisionControlObserver::ItemState>& itemStates)
62 {
63 m_plugin = plugin;
64 m_itemStates = itemStates;
65 }
66
67 void UpdateItemStatesThread::run()
68 {
69 Q_ASSERT(!m_itemStates.isEmpty());
70 Q_ASSERT(m_plugin != 0);
71
72 // it is assumed that all items have the same parent directory
73 const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash);
74
75 QMutexLocker locker(m_pluginMutex);
76 if (m_plugin->beginRetrieval(directory)) {
77 const int count = m_itemStates.count();
78 for (int i = 0; i < count; ++i) {
79 m_itemStates[i].revision = m_plugin->revisionState(m_itemStates[i].item);
80 }
81 m_plugin->endRetrieval();
82 }
83 }
84
85 QList<RevisionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
86 {
87 return m_itemStates;
88 }
89
90 // ------------------------------------------------------------------------------------------------
91
92 RevisionControlObserver::RevisionControlObserver(QAbstractItemView* view) :
93 QObject(view),
94 m_pendingItemStatesUpdate(false),
95 m_revisionedDirectory(false),
96 m_view(view),
97 m_dirLister(0),
98 m_dolphinModel(0),
99 m_dirVerificationTimer(0),
100 m_pluginMutex(QMutex::Recursive),
101 m_plugin(0),
102 m_updateItemStatesThread(0)
103 {
104 Q_ASSERT(view != 0);
105
106 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
107 m_dolphinModel = (proxyModel == 0) ?
108 qobject_cast<DolphinModel*>(view->model()) :
109 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
110 if (m_dolphinModel != 0) {
111 m_dirLister = m_dolphinModel->dirLister();
112 connect(m_dirLister, SIGNAL(completed()),
113 this, SLOT(delayedDirectoryVerification()));
114
115 // The verification timer specifies the timeout until the shown directory
116 // is checked whether it is versioned. Per default it is assumed that users
117 // don't iterate through versioned directories and a high timeout is used
118 // The timeout will be decreased as soon as a versioned directory has been
119 // found (see verifyDirectory()).
120 m_dirVerificationTimer = new QTimer(this);
121 m_dirVerificationTimer->setSingleShot(true);
122 m_dirVerificationTimer->setInterval(500);
123 connect(m_dirVerificationTimer, SIGNAL(timeout()),
124 this, SLOT(verifyDirectory()));
125 }
126 }
127
128 RevisionControlObserver::~RevisionControlObserver()
129 {
130 delete m_plugin;
131 m_plugin = 0;
132 }
133
134 QList<QAction*> RevisionControlObserver::contextMenuActions(const KFileItemList& items) const
135 {
136 if (m_dolphinModel->hasRevisionData() && (m_plugin != 0)) {
137 QMutexLocker locker(&m_pluginMutex);
138 return m_plugin->contextMenuActions(items);
139 }
140 return QList<QAction*>();
141 }
142
143 QList<QAction*> RevisionControlObserver::contextMenuActions(const QString& directory) const
144 {
145 if (m_dolphinModel->hasRevisionData() && (m_plugin != 0)) {
146 QMutexLocker locker(&m_pluginMutex);
147 return m_plugin->contextMenuActions(directory);
148 }
149
150 return QList<QAction*>();
151 }
152
153 void RevisionControlObserver::delayedDirectoryVerification()
154 {
155 m_dirVerificationTimer->start();
156 }
157
158 void RevisionControlObserver::verifyDirectory()
159 {
160 KUrl revisionControlUrl = m_dirLister->url();
161 if (!revisionControlUrl.isLocalFile()) {
162 return;
163 }
164
165 if (m_plugin == 0) {
166 // TODO: just for testing purposes. A plugin approach will be used later.
167 m_plugin = new SubversionPlugin();
168 }
169
170 revisionControlUrl.addPath(m_plugin->fileName());
171 const KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
172
173 bool foundRevisionInfo = !item.isNull();
174 if (!foundRevisionInfo && m_revisionedDirectory) {
175 // Revision control systems like Git provide the revision information
176 // file only in the root directory. Check whether the revision information file can
177 // be found in one of the parent directories.
178
179 // TODO...
180 }
181
182 if (foundRevisionInfo) {
183 if (!m_revisionedDirectory) {
184 m_revisionedDirectory = true;
185
186 // The directory is versioned. Assume that the user will further browse through
187 // versioned directories and decrease the verification timer.
188 m_dirVerificationTimer->setInterval(100);
189 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
190 this, SLOT(delayedDirectoryVerification()));
191 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
192 this, SLOT(delayedDirectoryVerification()));
193 connect(m_plugin, SIGNAL(revisionStatesChanged(const QString&)),
194 this, SLOT(delayedDirectoryVerification()));
195 }
196 updateItemStates();
197 } else if (m_revisionedDirectory) {
198 m_revisionedDirectory = false;
199
200 // The directory is not versioned. Reset the verification timer to a higher
201 // value, so that browsing through non-versioned directories is not slown down
202 // by an immediate verification.
203 m_dirVerificationTimer->setInterval(500);
204 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
205 this, SLOT(delayedDirectoryVerification()));
206 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
207 this, SLOT(delayedDirectoryVerification()));
208 disconnect(m_plugin, SIGNAL(revisionStatesChanged(const QString&)),
209 this, SLOT(delayedDirectoryVerification()));
210 }
211 }
212
213 void RevisionControlObserver::applyUpdatedItemStates()
214 {
215 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
216 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
217 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
218 // This works as the update of the data does not require a relayout of the views used in Dolphin.
219 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
220 m_dolphinModel->blockSignals(true);
221
222 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
223 foreach (const ItemState& itemState, itemStates) {
224 m_dolphinModel->setData(itemState.index,
225 QVariant(static_cast<int>(itemState.revision)),
226 Qt::DecorationRole);
227 }
228
229 m_dolphinModel->blockSignals(signalsBlocked);
230 m_view->viewport()->repaint();
231
232 if (m_pendingItemStatesUpdate) {
233 m_pendingItemStatesUpdate = false;
234 updateItemStates();
235 }
236 }
237
238 void RevisionControlObserver::updateItemStates()
239 {
240 Q_ASSERT(m_plugin != 0);
241 if (m_updateItemStatesThread == 0) {
242 m_updateItemStatesThread = new UpdateItemStatesThread(this, &m_pluginMutex);
243 connect(m_updateItemStatesThread, SIGNAL(finished()),
244 this, SLOT(applyUpdatedItemStates()));
245 }
246 if (m_updateItemStatesThread->isRunning()) {
247 // An update is currently ongoing. Wait until the thread has finished
248 // the update (see applyUpdatedItemStates()).
249 m_pendingItemStatesUpdate = true;
250 return;
251 }
252
253 const int rowCount = m_dolphinModel->rowCount();
254 if (rowCount > 0) {
255 // Build a list of all items in the current directory and delegate
256 // this list to the thread, which adjusts the revision states.
257 QList<ItemState> itemStates;
258 for (int row = 0; row < rowCount; ++row) {
259 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Revision);
260
261 ItemState itemState;
262 itemState.index = index;
263 itemState.item = m_dolphinModel->itemForIndex(index);
264 itemState.revision = RevisionControlPlugin::UnversionedRevision;
265
266 itemStates.append(itemState);
267 }
268
269 m_updateItemStatesThread->setData(m_plugin, itemStates);
270 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
271 }
272 }
273
274 #include "revisioncontrolobserver.moc"