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