]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
The revision control plugin must be aware on which directory the context-menu-actions...
[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.isEmpty());
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 return QList<QAction*>();
135 }
136
137 QList<QAction*> RevisionControlObserver::contextMenuActions(const QString& directory) const
138 {
139 if (m_dolphinModel->hasRevisionData() && (m_plugin != 0)) {
140 return m_plugin->contextMenuActions(directory);
141 }
142
143 return QList<QAction*>();
144 }
145
146 void RevisionControlObserver::delayedDirectoryVerification()
147 {
148 m_dirVerificationTimer->start();
149 }
150
151 void RevisionControlObserver::verifyDirectory()
152 {
153 KUrl revisionControlUrl = m_dirLister->url();
154 if (!revisionControlUrl.isLocalFile()) {
155 return;
156 }
157
158 if (m_plugin == 0) {
159 // TODO: just for testing purposes. A plugin approach will be used later.
160 m_plugin = new SubversionPlugin();
161 }
162
163 revisionControlUrl.addPath(m_plugin->fileName());
164 const KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
165 if (item.isNull() && m_revisionedDirectory) {
166 // The directory is not versioned. Reset the verification timer to a higher
167 // value, so that browsing through non-versioned directories is not slown down
168 // by an immediate verification.
169 m_dirVerificationTimer->setInterval(500);
170 m_revisionedDirectory = false;
171 disconnect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
172 this, SLOT(delayedDirectoryVerification()));
173 disconnect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
174 this, SLOT(delayedDirectoryVerification()));
175 } else if (!item.isNull()) {
176 if (!m_revisionedDirectory) {
177 // The directory is versioned. Assume that the user will further browse through
178 // versioned directories and decrease the verification timer.
179 m_dirVerificationTimer->setInterval(100);
180 m_revisionedDirectory = true;
181 connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
182 this, SLOT(delayedDirectoryVerification()));
183 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
184 this, SLOT(delayedDirectoryVerification()));
185 }
186 updateItemStates();
187 }
188 }
189
190 void RevisionControlObserver::applyUpdatedItemStates()
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.revision)),
203 Qt::DecorationRole);
204 }
205
206 m_dolphinModel->blockSignals(signalsBlocked);
207 m_view->viewport()->repaint();
208
209 if (m_pendingItemStatesUpdate) {
210 m_pendingItemStatesUpdate = false;
211 updateItemStates();
212 }
213 }
214
215 void RevisionControlObserver::updateItemStates()
216 {
217 Q_ASSERT(m_plugin != 0);
218 if (m_updateItemStatesThread == 0) {
219 m_updateItemStatesThread = new UpdateItemStatesThread(this);
220 connect(m_updateItemStatesThread, SIGNAL(finished()),
221 this, SLOT(applyUpdatedItemStates()));
222 }
223 if (m_updateItemStatesThread->isRunning()) {
224 // An update is currently ongoing. Wait until the thread has finished
225 // the update (see applyUpdatedItemStates()).
226 m_pendingItemStatesUpdate = true;
227 return;
228 }
229
230 const int rowCount = m_dolphinModel->rowCount();
231 if (rowCount > 0) {
232 // Build a list of all items in the current directory and delegate
233 // this list to the thread, which adjusts the revision states.
234 QList<ItemState> itemStates;
235 for (int row = 0; row < rowCount; ++row) {
236 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Revision);
237
238 ItemState itemState;
239 itemState.index = index;
240 itemState.item = m_dolphinModel->itemForIndex(index);
241 itemState.revision = RevisionControlPlugin::LocalRevision;
242
243 itemStates.append(itemState);
244 }
245
246 m_updateItemStatesThread->setData(m_plugin, itemStates);
247 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
248 }
249 }
250
251 #include "revisioncontrolobserver.moc"