1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "versioncontrolobserver.h"
22 #include "dolphin_versioncontrolsettings.h"
26 #include <KServiceTypeTrader>
27 #include <kitemviews/kfileitemmodel.h>
28 #include <kversioncontrolplugin2.h>
30 #include "updateitemstatesthread.h"
33 #include <QMutexLocker>
36 VersionControlObserver::VersionControlObserver(QObject
* parent
) :
38 m_pendingItemStatesUpdate(false),
39 m_versionedDirectory(false),
40 m_silentUpdate(false),
42 m_dirVerificationTimer(0),
44 m_updateItemStatesThread(0)
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()));
58 VersionControlObserver::~VersionControlObserver()
61 m_plugin
->disconnect(this);
66 void VersionControlObserver::setModel(KFileItemModel
* 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()));
78 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
79 this, SLOT(delayedDirectoryVerification()));
80 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
81 this, SLOT(delayedDirectoryVerification()));
85 KFileItemModel
* VersionControlObserver::model() const
90 QList
<QAction
*> VersionControlObserver::actions(const KFileItemList
& items
) const
92 QList
<QAction
*> actions
;
94 bool hasNullItems
= false;
95 foreach (const KFileItem
& item
, items
) {
97 kWarning() << "Requesting version-control-actions for empty items";
103 if (!m_model
|| hasNullItems
) {
107 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
109 // Use version 2 of the KVersionControlPlugin which allows providing actions
110 // also for non-versioned directories.
111 if (m_updateItemStatesThread
&& m_updateItemStatesThread
->lockPlugin()) {
112 actions
= pluginV2
->actions(items
);
113 m_updateItemStatesThread
->unlockPlugin();
115 actions
= pluginV2
->actions(items
);
117 } else if (isVersioned()) {
118 // Support deprecated interfaces from KVersionControlPlugin version 1.
119 // Context menu actions where only available for versioned directories.
121 if (items
.count() == 1) {
122 const KFileItem rootItem
= m_model
->rootItem();
123 if (!rootItem
.isNull() && items
.first().url() == rootItem
.url()) {
124 directory
= rootItem
.url().path(KUrl::AddTrailingSlash
);
128 if (m_updateItemStatesThread
&& m_updateItemStatesThread
->lockPlugin()) {
129 actions
= directory
.isEmpty() ? m_plugin
->contextMenuActions(items
)
130 : m_plugin
->contextMenuActions(directory
);
131 m_updateItemStatesThread
->unlockPlugin();
133 actions
= directory
.isEmpty() ? m_plugin
->contextMenuActions(items
)
134 : m_plugin
->contextMenuActions(directory
);
141 void VersionControlObserver::delayedDirectoryVerification()
143 m_silentUpdate
= false;
144 m_dirVerificationTimer
->start();
147 void VersionControlObserver::silentDirectoryVerification()
149 m_silentUpdate
= true;
150 m_dirVerificationTimer
->start();
153 void VersionControlObserver::verifyDirectory()
159 const KFileItem rootItem
= m_model
->rootItem();
160 if (rootItem
.isNull() || !rootItem
.url().isLocalFile()) {
165 m_plugin
->disconnect(this);
168 m_plugin
= searchPlugin(rootItem
.url());
170 KVersionControlPlugin2
* pluginV2
= qobject_cast
<KVersionControlPlugin2
*>(m_plugin
);
172 connect(pluginV2
, SIGNAL(itemVersionsChanged()),
173 this, SLOT(silentDirectoryVerification()));
175 connect(m_plugin
, SIGNAL(versionStatesChanged()),
176 this, SLOT(silentDirectoryVerification()));
178 connect(m_plugin
, SIGNAL(infoMessage(QString
)),
179 this, SIGNAL(infoMessage(QString
)));
180 connect(m_plugin
, SIGNAL(errorMessage(QString
)),
181 this, SIGNAL(errorMessage(QString
)));
182 connect(m_plugin
, SIGNAL(operationCompletedMessage(QString
)),
183 this, SIGNAL(operationCompletedMessage(QString
)));
185 if (!m_versionedDirectory
) {
186 m_versionedDirectory
= true;
188 // The directory is versioned. Assume that the user will further browse through
189 // versioned directories and decrease the verification timer.
190 m_dirVerificationTimer
->setInterval(100);
193 } else if (m_versionedDirectory
) {
194 m_versionedDirectory
= false;
196 // The directory is not versioned. Reset the verification timer to a higher
197 // value, so that browsing through non-versioned directories is not slown down
198 // by an immediate verification.
199 m_dirVerificationTimer
->setInterval(500);
203 void VersionControlObserver::slotThreadFinished()
205 UpdateItemStatesThread
* thread
= m_updateItemStatesThread
;
206 m_updateItemStatesThread
= 0; // The thread deletes itself automatically (see updateItemStates())
208 if (!m_plugin
|| !thread
) {
212 if (!thread
->retrievedItems()) {
213 // Ignore m_silentUpdate for an error message
214 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
218 const QList
<ItemState
> itemStates
= thread
->itemStates();
219 foreach (const ItemState
& itemState
, itemStates
) {
220 QHash
<QByteArray
, QVariant
> values
;
221 values
.insert("version", QVariant(itemState
.version
));
222 m_model
->setData(itemState
.index
, values
);
225 if (!m_silentUpdate
) {
226 // Using an empty message results in clearing the previously shown information message and showing
227 // the default status bar information. This is useful as the user already gets feedback that the
228 // operation has been completed because of the icon emblems.
229 emit
operationCompletedMessage(QString());
232 if (m_pendingItemStatesUpdate
) {
233 m_pendingItemStatesUpdate
= false;
238 void VersionControlObserver::updateItemStates()
241 if (!m_updateItemStatesThread
) {
242 m_updateItemStatesThread
= new UpdateItemStatesThread();
243 connect(m_updateItemStatesThread
, SIGNAL(finished()),
244 this, SLOT(slotThreadFinished()));
245 connect(m_updateItemStatesThread
, SIGNAL(finished()),
246 m_updateItemStatesThread
, SLOT(deleteLater()));
249 // An update is currently ongoing. Wait until the thread has finished
250 // the update (see slotThreadFinished()).
251 m_pendingItemStatesUpdate
= true;
255 QList
<ItemState
> itemStates
;
256 const int itemCount
= m_model
->count();
257 itemStates
.reserve(itemCount
);
259 for (int i
= 0; i
< itemCount
; ++i
) {
262 itemState
.item
= m_model
->fileItem(i
);
263 itemState
.version
= KVersionControlPlugin2::UnversionedVersion
;
265 itemStates
.append(itemState
);
268 if (!itemStates
.isEmpty()) {
269 if (!m_silentUpdate
) {
270 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
272 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
273 m_updateItemStatesThread
->start(); // slotThreadFinished() is called when finished
277 KVersionControlPlugin
* VersionControlObserver::searchPlugin(const KUrl
& directory
) const
279 static bool pluginsAvailable
= true;
280 static QList
<KVersionControlPlugin
*> plugins
;
282 if (!pluginsAvailable
) {
283 // A searching for plugins has already been done, but no
284 // plugins are installed
288 if (plugins
.isEmpty()) {
289 // No searching for plugins has been done yet. Query the KServiceTypeTrader for
290 // all fileview version control plugins and remember them in 'plugins'.
291 const QStringList enabledPlugins
= VersionControlSettings::enabledPlugins();
293 const KService::List pluginServices
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
294 for (KService::List::ConstIterator it
= pluginServices
.constBegin(); it
!= pluginServices
.constEnd(); ++it
) {
295 if (enabledPlugins
.contains((*it
)->name())) {
296 KVersionControlPlugin
* plugin
= (*it
)->createInstance
<KVersionControlPlugin
>();
298 plugins
.append(plugin
);
302 if (plugins
.isEmpty()) {
303 pluginsAvailable
= false;
308 // Verify whether the current directory contains revision information
309 // like .svn, .git, ...
310 foreach (KVersionControlPlugin
* plugin
, plugins
) {
311 const QString fileName
= directory
.path(KUrl::AddTrailingSlash
) + plugin
->fileName();
312 if (QFile::exists(fileName
)) {
316 // Version control systems like Git provide the version information
317 // file only in the root directory. Check whether the version information file can
318 // be found in one of the parent directories. For performance reasons this
319 // step is only done, if the previous directory was marked as versioned by
320 // m_versionedDirectory. Drawback: Until e. g. Git is recognized, the root directory
321 // must be shown at least once.
322 if (m_versionedDirectory
) {
323 KUrl
dirUrl(directory
);
324 KUrl upUrl
= dirUrl
.upUrl();
325 while (upUrl
!= dirUrl
) {
326 const QString fileName
= dirUrl
.path(KUrl::AddTrailingSlash
) + plugin
->fileName();
327 if (QFile::exists(fileName
)) {
331 upUrl
= dirUrl
.upUrl();
339 bool VersionControlObserver::isVersioned() const
341 return m_versionedDirectory
&& m_plugin
;
344 #include "versioncontrolobserver.moc"