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