]> cloud.milkyroute.net Git - dolphin.git/blob - src/revisioncontrolobserver.cpp
SVN_SILENT made messages (.desktop file)
[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_view(view),
92 m_dirLister(0),
93 m_dolphinModel(0),
94 m_dirVerificationTimer(0),
95 m_plugin(0),
96 m_updateItemStatesThread(0)
97 {
98 Q_ASSERT(view != 0);
99
100 QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>(view->model());
101 m_dolphinModel = (proxyModel == 0) ?
102 qobject_cast<DolphinModel*>(view->model()) :
103 qobject_cast<DolphinModel*>(proxyModel->sourceModel());
104 if (m_dolphinModel != 0) {
105 m_dirLister = m_dolphinModel->dirLister();
106 connect(m_dirLister, SIGNAL(completed()),
107 this, SLOT(delayedDirectoryVerification()));
108 // TODO:
109 // connect(m_dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
110 // this, SLOT(refreshItems()));
111
112 // The verification timer specifies the timeout until the shown directory
113 // is checked whether it is versioned. Per default it is assumed that users
114 // don't iterate through versioned directories and a high timeout is used
115 // The timeout will be decreased as soon as a versioned directory has been
116 // found (see verifyDirectory()).
117 m_dirVerificationTimer = new QTimer(this);
118 m_dirVerificationTimer->setSingleShot(true);
119 m_dirVerificationTimer->setInterval(500);
120 connect(m_dirVerificationTimer, SIGNAL(timeout()),
121 this, SLOT(verifyDirectory()));
122 }
123 }
124
125 RevisionControlObserver::~RevisionControlObserver()
126 {
127 delete m_plugin;
128 m_plugin = 0;
129 }
130
131 void RevisionControlObserver::delayedDirectoryVerification()
132 {
133 m_dirVerificationTimer->start();
134 }
135
136 void RevisionControlObserver::verifyDirectory()
137 {
138 KUrl revisionControlUrl = m_dirLister->url();
139 if (!revisionControlUrl.isLocalFile()) {
140 return;
141 }
142
143 if (m_plugin == 0) {
144 // TODO: just for testing purposes. A plugin approach will be used later.
145 m_plugin = new SubversionPlugin();
146 }
147
148 revisionControlUrl.addPath(m_plugin->fileName());
149 KFileItem item = m_dirLister->findByUrl(revisionControlUrl);
150 if (item.isNull()) {
151 // The directory is not versioned. Reset the verification timer to a higher
152 // value, so that browsing through non-versioned directories is not slown down
153 // by an immediate verification.
154 m_dirVerificationTimer->setInterval(500);
155 } else {
156 // The directory is versioned. Assume that the user will further browse through
157 // versioned directories and decrease the verification timer.
158 m_dirVerificationTimer->setInterval(100);
159 updateItemStates();
160 }
161 }
162
163 void RevisionControlObserver::applyUpdatedItemStates()
164 {
165 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
166 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
167 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
168 // This works as the update of the data does not require a relayout of the views used in Dolphin.
169 const bool signalsBlocked = m_dolphinModel->signalsBlocked();
170 m_dolphinModel->blockSignals(true);
171
172 const QList<ItemState> itemStates = m_updateItemStatesThread->itemStates();
173 foreach (const ItemState& itemState, itemStates) {
174 m_dolphinModel->setData(itemState.index,
175 QVariant(static_cast<int>(itemState.revision)),
176 Qt::DecorationRole);
177 }
178
179 m_dolphinModel->blockSignals(signalsBlocked);
180 m_view->viewport()->repaint();
181
182 if (m_pendingItemStatesUpdate) {
183 m_pendingItemStatesUpdate = false;
184 updateItemStates();
185 }
186 }
187
188 void RevisionControlObserver::updateItemStates()
189 {
190 Q_ASSERT(m_plugin != 0);
191 if (m_updateItemStatesThread == 0) {
192 m_updateItemStatesThread = new UpdateItemStatesThread(this);
193 connect(m_updateItemStatesThread, SIGNAL(finished()),
194 this, SLOT(applyUpdatedItemStates()));
195 }
196 if (m_updateItemStatesThread->isRunning()) {
197 // An update is currently ongoing. Wait until the thread has finished
198 // the update (see applyUpdatedItemStates()).
199 m_pendingItemStatesUpdate = true;
200 return;
201 }
202
203 const int rowCount = m_dolphinModel->rowCount();
204 if (rowCount > 0) {
205 // Build a list of all items in the current directory and delegate
206 // this list to the thread, which adjusts the revision states.
207 QList<ItemState> itemStates;
208 for (int row = 0; row < rowCount; ++row) {
209 const QModelIndex index = m_dolphinModel->index(row, DolphinModel::Revision);
210
211 ItemState itemState;
212 itemState.index = index;
213 itemState.item = m_dolphinModel->itemForIndex(index);
214 itemState.revision = RevisionControlPlugin::LocalRevision;
215
216 itemStates.append(itemState);
217 }
218
219 m_updateItemStatesThread->setData(m_plugin, itemStates);
220 m_updateItemStatesThread->start(); // applyUpdatedItemStates() is called when finished
221 }
222 }
223
224 #include "revisioncontrolobserver.moc"