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