]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/versioncontrolobserver.cpp
Version control: Get rid of PendingThreadsMaintainer
[dolphin.git] / src / views / versioncontrol / versioncontrolobserver.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphin_versioncontrolsettings.h"
23
24 #include <KDirLister>
25 #include <KLocale>
26 #include <KService>
27 #include <KServiceTypeTrader>
28 #include <kitemviews/kfileitemmodel.h>
29 #include <kversioncontrolplugin.h>
30
31 #include "updateitemstatesthread.h"
32
33 #include <QMutexLocker>
34 #include <QTimer>
35
36 VersionControlObserver::VersionControlObserver(QObject* parent) :
37 QObject(parent),
38 m_pendingItemStatesUpdate(false),
39 m_versionedDirectory(false),
40 m_silentUpdate(false),
41 m_model(0),
42 m_dirVerificationTimer(0),
43 m_plugin(0),
44 m_updateItemStatesThread(0)
45 {
46 // The verification timer specifies the timeout until the shown directory
47 // is checked whether it is versioned. Per default it is assumed that users
48 // don't iterate through versioned directories and a high timeout is used
49 // The timeout will be decreased as soon as a versioned directory has been
50 // found (see verifyDirectory()).
51 m_dirVerificationTimer = new QTimer(this);
52 m_dirVerificationTimer->setSingleShot(true);
53 m_dirVerificationTimer->setInterval(500);
54 connect(m_dirVerificationTimer, SIGNAL(timeout()),
55 this, SLOT(verifyDirectory()));
56 }
57
58 VersionControlObserver::~VersionControlObserver()
59 {
60 if (m_plugin) {
61 m_plugin->disconnect(this);
62 m_plugin = 0;
63 }
64 }
65
66 void VersionControlObserver::setModel(KFileItemModel* model)
67 {
68 if (m_model) {
69 disconnect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
70 this, SLOT(delayedDirectoryVerification()));
71 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
72 this, SLOT(delayedDirectoryVerification()));
73 }
74
75 m_model = model;
76
77 if (model) {
78 connect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
79 this, SLOT(delayedDirectoryVerification()));
80 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
81 this, SLOT(delayedDirectoryVerification()));
82 }
83 }
84
85 KFileItemModel* VersionControlObserver::model() const
86 {
87 return m_model;
88 }
89
90 QList<QAction*> VersionControlObserver::contextMenuActions(const KFileItemList& items) const
91 {
92 QList<QAction*> actions;
93 if (isVersioned() && m_updateItemStatesThread->lockPlugin()) {
94 actions = m_plugin->contextMenuActions(items);
95 m_updateItemStatesThread->unlockPlugin();
96 }
97 return actions;
98 }
99
100 QList<QAction*> VersionControlObserver::contextMenuActions(const QString& directory) const
101 {
102 QList<QAction*> actions;
103 if (isVersioned() && m_updateItemStatesThread->lockPlugin()) {
104 actions = m_plugin->contextMenuActions(directory);
105 m_updateItemStatesThread->unlockPlugin();
106 }
107
108 return actions;
109 }
110
111 void VersionControlObserver::delayedDirectoryVerification()
112 {
113 m_silentUpdate = false;
114 m_dirVerificationTimer->start();
115 }
116
117 void VersionControlObserver::silentDirectoryVerification()
118 {
119 m_silentUpdate = true;
120 m_dirVerificationTimer->start();
121 }
122
123 void VersionControlObserver::verifyDirectory()
124 {
125 if (!m_model) {
126 return;
127 }
128
129 const KUrl versionControlUrl = m_model->rootDirectory();
130 if (!versionControlUrl.isLocalFile()) {
131 return;
132 }
133
134 if (m_plugin) {
135 m_plugin->disconnect(this);
136 }
137
138 m_plugin = searchPlugin(versionControlUrl);
139 if (m_plugin) {
140 connect(m_plugin, SIGNAL(versionStatesChanged()),
141 this, SLOT(silentDirectoryVerification()));
142 connect(m_plugin, SIGNAL(infoMessage(QString)),
143 this, SIGNAL(infoMessage(QString)));
144 connect(m_plugin, SIGNAL(errorMessage(QString)),
145 this, SIGNAL(errorMessage(QString)));
146 connect(m_plugin, SIGNAL(operationCompletedMessage(QString)),
147 this, SIGNAL(operationCompletedMessage(QString)));
148
149 if (!m_versionedDirectory) {
150 m_versionedDirectory = true;
151
152 // The directory is versioned. Assume that the user will further browse through
153 // versioned directories and decrease the verification timer.
154 m_dirVerificationTimer->setInterval(100);
155 }
156 updateItemStates();
157 } else if (m_versionedDirectory) {
158 m_versionedDirectory = false;
159
160 // The directory is not versioned. Reset the verification timer to a higher
161 // value, so that browsing through non-versioned directories is not slown down
162 // by an immediate verification.
163 m_dirVerificationTimer->setInterval(500);
164 }
165 }
166
167 void VersionControlObserver::slotThreadFinished()
168 {
169 UpdateItemStatesThread* thread = m_updateItemStatesThread;
170 m_updateItemStatesThread = 0; // The thread deletes itself automatically (see updateItemStates())
171
172 if (!m_plugin) {
173 return;
174 }
175
176 if (!thread->retrievedItems()) {
177 // Ignore m_silentUpdate for an error message
178 emit errorMessage(i18nc("@info:status", "Update of version information failed."));
179 return;
180 }
181
182 const QList<ItemState> itemStates = thread->itemStates();
183 foreach (const ItemState& itemState, itemStates) {
184 QHash<QByteArray, QVariant> values;
185 values.insert("version", QVariant(itemState.version));
186 m_model->setData(itemState.index, values);
187 }
188
189 if (!m_silentUpdate) {
190 // Using an empty message results in clearing the previously shown information message and showing
191 // the default status bar information. This is useful as the user already gets feedback that the
192 // operation has been completed because of the icon emblems.
193 emit operationCompletedMessage(QString());
194 }
195
196 if (m_pendingItemStatesUpdate) {
197 m_pendingItemStatesUpdate = false;
198 updateItemStates();
199 }
200 }
201
202 void VersionControlObserver::updateItemStates()
203 {
204 Q_ASSERT(m_plugin);
205 if (!m_updateItemStatesThread) {
206 m_updateItemStatesThread = new UpdateItemStatesThread();
207 connect(m_updateItemStatesThread, SIGNAL(finished()),
208 this, SLOT(slotThreadFinished()));
209 connect(m_updateItemStatesThread, SIGNAL(finished()),
210 m_updateItemStatesThread, SLOT(deleteLater()));
211 }
212 if (m_updateItemStatesThread->isRunning()) {
213 // An update is currently ongoing. Wait until the thread has finished
214 // the update (see slotThreadFinished()).
215 m_pendingItemStatesUpdate = true;
216 return;
217 }
218
219 QList<ItemState> itemStates;
220 const int itemCount = m_model->count();
221 itemStates.reserve(itemCount);
222
223 for (int i = 0; i < itemCount; ++i) {
224 ItemState itemState;
225 itemState.index = i;
226 itemState.item = m_model->fileItem(i);
227 itemState.version = KVersionControlPlugin::UnversionedVersion;
228
229 itemStates.append(itemState);
230 }
231
232 if (!itemStates.isEmpty()) {
233 if (!m_silentUpdate) {
234 emit infoMessage(i18nc("@info:status", "Updating version information..."));
235 }
236 m_updateItemStatesThread->setData(m_plugin, itemStates);
237 m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished
238 }
239 }
240
241 KVersionControlPlugin* VersionControlObserver::searchPlugin(const KUrl& directory) const
242 {
243 static bool pluginsAvailable = true;
244 static QList<KVersionControlPlugin*> plugins;
245
246 if (!pluginsAvailable) {
247 // A searching for plugins has already been done, but no
248 // plugins are installed
249 return 0;
250 }
251
252 if (plugins.isEmpty()) {
253 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
254 // all fileview version control plugins and remember them in 'plugins'.
255 const QStringList enabledPlugins = VersionControlSettings::enabledPlugins();
256
257 const KService::List pluginServices = KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
258 for (KService::List::ConstIterator it = pluginServices.constBegin(); it != pluginServices.constEnd(); ++it) {
259 if (enabledPlugins.contains((*it)->name())) {
260 KVersionControlPlugin* plugin = (*it)->createInstance<KVersionControlPlugin>();
261 if (plugin) {
262 plugins.append(plugin);
263 }
264 }
265 }
266 if (plugins.isEmpty()) {
267 pluginsAvailable = false;
268 return 0;
269 }
270 }
271
272 // Verify whether the current directory contains revision information
273 // like .svn, .git, ...
274 foreach (KVersionControlPlugin* plugin, plugins) {
275 // Use the KDirLister cache to check for .svn, .git, ... files
276 const QString fileName = directory.path(KUrl::AddTrailingSlash) + plugin->fileName();
277 if (QFile::exists(fileName)) {
278 return plugin;
279 }
280
281 // Version control systems like Git provide the version information
282 // file only in the root directory. Check whether the version information file can
283 // be found in one of the parent directories. For performance reasons this
284 // step is only done, if the previous directory was marked as versioned by
285 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
286 // must be shown at least once.
287 if (m_versionedDirectory) {
288 KUrl dirUrl(directory);
289 KUrl upUrl = dirUrl.upUrl();
290 while (upUrl != dirUrl) {
291 const QString fileName = dirUrl.path(KUrl::AddTrailingSlash) + plugin->fileName();
292 if (QFile::exists(fileName)) {
293 return plugin;
294 }
295 dirUrl = upUrl;
296 upUrl = dirUrl.upUrl();
297 }
298 }
299 }
300
301 return 0;
302 }
303
304 bool VersionControlObserver::isVersioned() const
305 {
306 return false; //m_dolphinModel->hasVersionData() && m_plugin;
307 }
308
309 #include "versioncontrolobserver.moc"