1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinmodel.h"
24 #include <kdirlister.h>
27 #include <kservicetypetrader.h>
28 #include <kversioncontrolplugin.h>
30 #include <QAbstractProxyModel>
31 #include <QAbstractItemView>
32 #include <QMutexLocker>
36 * The performance of updating the version state of items depends
37 * on the used plugin. To prevent that Dolphin gets blocked by a
38 * slow plugin, the updating is delegated to a thread.
40 class UpdateItemStatesThread
: public QThread
43 UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
);
44 void setData(KVersionControlPlugin
* plugin
,
45 const QList
<VersionControlObserver::ItemState
>& itemStates
);
46 QList
<VersionControlObserver::ItemState
> itemStates() const;
47 bool retrievedItems() const;
53 bool m_retrievedItems
;
54 KVersionControlPlugin
* m_plugin
;
55 QMutex
* m_pluginMutex
;
56 QList
<VersionControlObserver::ItemState
> m_itemStates
;
59 UpdateItemStatesThread::UpdateItemStatesThread(QObject
* parent
, QMutex
* pluginMutex
) :
61 m_retrievedItems(false),
62 m_pluginMutex(pluginMutex
),
67 void UpdateItemStatesThread::setData(KVersionControlPlugin
* plugin
,
68 const QList
<VersionControlObserver::ItemState
>& itemStates
)
71 m_itemStates
= itemStates
;
74 void UpdateItemStatesThread::run()
76 Q_ASSERT(!m_itemStates
.isEmpty());
77 Q_ASSERT(m_plugin
!= 0);
79 // The items from m_itemStates may be located in different directory levels. The version
80 // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
81 // of doing an expensive search, we utilize the knowledge of the implementation of
82 // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
83 const QString directory
= m_itemStates
.last().item
.url().directory(KUrl::AppendTrailingSlash
);
85 QMutexLocker
locker(m_pluginMutex
);
86 m_retrievedItems
= false;
87 if (m_plugin
->beginRetrieval(directory
)) {
88 const int count
= m_itemStates
.count();
89 for (int i
= 0; i
< count
; ++i
) {
90 m_itemStates
[i
].version
= m_plugin
->versionState(m_itemStates
[i
].item
);
92 m_plugin
->endRetrieval();
93 m_retrievedItems
= true;
97 QList
<VersionControlObserver::ItemState
> UpdateItemStatesThread::itemStates() const
102 bool UpdateItemStatesThread::retrievedItems() const
104 return m_retrievedItems
;
107 // ------------------------------------------------------------------------------------------------
109 VersionControlObserver::VersionControlObserver(QAbstractItemView
* view
) :
111 m_pendingItemStatesUpdate(false),
112 m_versionedDirectory(false),
113 m_silentUpdate(false),
117 m_dirVerificationTimer(0),
118 m_pluginMutex(QMutex::Recursive
),
120 m_updateItemStatesThread(0)
124 QAbstractProxyModel
* proxyModel
= qobject_cast
<QAbstractProxyModel
*>(view
->model());
125 m_dolphinModel
= (proxyModel
== 0) ?
126 qobject_cast
<DolphinModel
*>(view
->model()) :
127 qobject_cast
<DolphinModel
*>(proxyModel
->sourceModel());
128 if (m_dolphinModel
!= 0) {
129 m_dirLister
= m_dolphinModel
->dirLister();
130 connect(m_dirLister
, SIGNAL(completed()),
131 this, SLOT(delayedDirectoryVerification()));
133 // The verification timer specifies the timeout until the shown directory
134 // is checked whether it is versioned. Per default it is assumed that users
135 // don't iterate through versioned directories and a high timeout is used
136 // The timeout will be decreased as soon as a versioned directory has been
137 // found (see verifyDirectory()).
138 m_dirVerificationTimer
= new QTimer(this);
139 m_dirVerificationTimer
->setSingleShot(true);
140 m_dirVerificationTimer
->setInterval(500);
141 connect(m_dirVerificationTimer
, SIGNAL(timeout()),
142 this, SLOT(verifyDirectory()));
146 VersionControlObserver::~VersionControlObserver()
148 if (m_updateItemStatesThread
!= 0) {
149 m_updateItemStatesThread
->terminate();
150 m_updateItemStatesThread
->wait();
156 QList
<QAction
*> VersionControlObserver::contextMenuActions(const KFileItemList
& items
) const
158 if (m_dolphinModel
->hasVersionData() && (m_plugin
!= 0)) {
159 QMutexLocker
locker(&m_pluginMutex
);
160 return m_plugin
->contextMenuActions(items
);
162 return QList
<QAction
*>();
165 QList
<QAction
*> VersionControlObserver::contextMenuActions(const QString
& directory
) const
167 if (m_dolphinModel
->hasVersionData() && (m_plugin
!= 0)) {
168 QMutexLocker
locker(&m_pluginMutex
);
169 return m_plugin
->contextMenuActions(directory
);
172 return QList
<QAction
*>();
175 void VersionControlObserver::delayedDirectoryVerification()
177 m_silentUpdate
= false;
178 m_dirVerificationTimer
->start();
181 void VersionControlObserver::silentDirectoryVerification()
183 m_silentUpdate
= true;
184 m_dirVerificationTimer
->start();
187 void VersionControlObserver::verifyDirectory()
189 KUrl versionControlUrl
= m_dirLister
->url();
190 if (!versionControlUrl
.isLocalFile()) {
195 // TODO: does not work yet
196 const KService::List plugins
= KServiceTypeTrader::self()->query("FileViewVersionControlPlugin");
197 for (KService::List::ConstIterator it
= plugins
.begin(); it
!= plugins
.end(); ++it
) {
198 // kDebug() << "plugin: " << (*it)->desktopEntryName();
202 connect(m_plugin
, SIGNAL(infoMessage(const QString
&)),
203 this, SIGNAL(infoMessage(const QString
&)));
204 connect(m_plugin
, SIGNAL(errorMessage(const QString
&)),
205 this, SIGNAL(errorMessage(const QString
&)));
206 connect(m_plugin
, SIGNAL(operationCompletedMessage(const QString
&)),
207 this, SIGNAL(operationCompletedMessage(const QString
&)));
210 versionControlUrl
.addPath(m_plugin
->fileName());
211 const KFileItem item
= m_dirLister
->findByUrl(versionControlUrl
);
213 bool foundVersionInfo
= !item
.isNull();
214 if (!foundVersionInfo
&& m_versionedDirectory
) {
215 // Version control systems like Git provide the version information
216 // file only in the root directory. Check whether the version information file can
217 // be found in one of the parent directories.
222 if (foundVersionInfo
) {
223 if (!m_versionedDirectory
) {
224 m_versionedDirectory
= true;
226 // The directory is versioned. Assume that the user will further browse through
227 // versioned directories and decrease the verification timer.
228 m_dirVerificationTimer
->setInterval(100);
229 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
230 this, SLOT(delayedDirectoryVerification()));
231 connect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
232 this, SLOT(delayedDirectoryVerification()));
233 connect(m_plugin
, SIGNAL(versionStatesChanged()),
234 this, SLOT(silentDirectoryVerification()));
237 } else if (m_versionedDirectory
) {
238 m_versionedDirectory
= false;
240 // The directory is not versioned. Reset the verification timer to a higher
241 // value, so that browsing through non-versioned directories is not slown down
242 // by an immediate verification.
243 m_dirVerificationTimer
->setInterval(500);
244 disconnect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
245 this, SLOT(delayedDirectoryVerification()));
246 disconnect(m_dirLister
, SIGNAL(newItems(const KFileItemList
&)),
247 this, SLOT(delayedDirectoryVerification()));
248 disconnect(m_plugin
, SIGNAL(versionStatesChanged()),
249 this, SLOT(silentDirectoryVerification()));
253 void VersionControlObserver::applyUpdatedItemStates()
255 if (!m_updateItemStatesThread
->retrievedItems()) {
256 // ignore m_silentUpdate for an error message
257 emit
errorMessage(i18nc("@info:status", "Update of version information failed."));
261 // QAbstractItemModel::setData() triggers a bottleneck in combination with QListView
262 // (a detailed description of the root cause is given in the class KFilePreviewGenerator
263 // from kdelibs). To bypass this bottleneck, the signals of the model are temporary blocked.
264 // This works as the update of the data does not require a relayout of the views used in Dolphin.
265 const bool signalsBlocked
= m_dolphinModel
->signalsBlocked();
266 m_dolphinModel
->blockSignals(true);
268 const QList
<ItemState
> itemStates
= m_updateItemStatesThread
->itemStates();
269 foreach (const ItemState
& itemState
, itemStates
) {
270 m_dolphinModel
->setData(itemState
.index
,
271 QVariant(static_cast<int>(itemState
.version
)),
275 m_dolphinModel
->blockSignals(signalsBlocked
);
276 m_view
->viewport()->repaint();
278 if (!m_silentUpdate
) {
279 // Using an empty message results in clearing the previously shown information message and showing
280 // the default status bar information. This is useful as the user already gets feedback that the
281 // operation has been completed because of the icon emblems.
282 emit
operationCompletedMessage(QString());
285 if (m_pendingItemStatesUpdate
) {
286 m_pendingItemStatesUpdate
= false;
291 void VersionControlObserver::updateItemStates()
293 Q_ASSERT(m_plugin
!= 0);
294 if (m_updateItemStatesThread
== 0) {
295 m_updateItemStatesThread
= new UpdateItemStatesThread(this, &m_pluginMutex
);
296 connect(m_updateItemStatesThread
, SIGNAL(finished()),
297 this, SLOT(applyUpdatedItemStates()));
299 if (m_updateItemStatesThread
->isRunning()) {
300 // An update is currently ongoing. Wait until the thread has finished
301 // the update (see applyUpdatedItemStates()).
302 m_pendingItemStatesUpdate
= true;
306 QList
<ItemState
> itemStates
;
307 addDirectory(QModelIndex(), itemStates
);
308 if (!itemStates
.isEmpty()) {
309 if (!m_silentUpdate
) {
310 emit
infoMessage(i18nc("@info:status", "Updating version information..."));
312 m_updateItemStatesThread
->setData(m_plugin
, itemStates
);
313 m_updateItemStatesThread
->start(); // applyUpdatedItemStates() is called when finished
317 void VersionControlObserver::addDirectory(const QModelIndex
& parentIndex
, QList
<ItemState
>& itemStates
)
319 const int rowCount
= m_dolphinModel
->rowCount(parentIndex
);
320 for (int row
= 0; row
< rowCount
; ++row
) {
321 const QModelIndex index
= m_dolphinModel
->index(row
, DolphinModel::Version
, parentIndex
);
322 addDirectory(index
, itemStates
);
325 itemState
.index
= index
;
326 itemState
.item
= m_dolphinModel
->itemForIndex(index
);
327 itemState
.version
= KVersionControlPlugin::UnversionedVersion
;
329 itemStates
.append(itemState
);
333 #include "versioncontrolobserver.moc"