]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/updateitemstatesthread.cpp
Sourcecode hierarchy cleanup: Move folders "tooltips" and "versioncontrol" into ...
[dolphin.git] / src / views / versioncontrol / updateitemstatesthread.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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 "updateitemstatesthread.h"
21
22 #include <QMutexLocker>
23
24 UpdateItemStatesThread::UpdateItemStatesThread() :
25 QThread(),
26 m_globalPluginMutex(0),
27 m_plugin(0),
28 m_itemMutex(),
29 m_retrievedItems(false),
30 m_itemStates()
31 {
32 // Several threads may share one instance of a plugin. A global
33 // mutex is required to serialize the retrieval of version control
34 // states inside run().
35 static QMutex globalMutex;
36 m_globalPluginMutex = &globalMutex;
37 }
38
39 UpdateItemStatesThread::~UpdateItemStatesThread()
40 {
41 }
42
43 void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin,
44 const QList<VersionControlObserver::ItemState>& itemStates)
45 {
46 QMutexLocker itemLocker(&m_itemMutex);
47 m_itemStates = itemStates;
48
49 QMutexLocker pluginLocker(m_globalPluginMutex);
50 m_plugin = plugin;
51 }
52
53 void UpdateItemStatesThread::run()
54 {
55 Q_ASSERT(!m_itemStates.isEmpty());
56 Q_ASSERT(m_plugin != 0);
57
58 // The items from m_itemStates may be located in different directory levels. The version
59 // plugin requires the root directory for KVersionControlPlugin::beginRetrieval(). Instead
60 // of doing an expensive search, we utilize the knowledge of the implementation of
61 // VersionControlObserver::addDirectory() to be sure that the last item contains the root.
62 QMutexLocker itemLocker(&m_itemMutex);
63 const QString directory = m_itemStates.last().item.url().directory(KUrl::AppendTrailingSlash);
64 itemLocker.unlock();
65
66 QMutexLocker pluginLocker(m_globalPluginMutex);
67 m_retrievedItems = false;
68 if (m_plugin->beginRetrieval(directory)) {
69 itemLocker.relock();
70 const int count = m_itemStates.count();
71 for (int i = 0; i < count; ++i) {
72 m_itemStates[i].version = m_plugin->versionState(m_itemStates[i].item);
73 }
74 m_plugin->endRetrieval();
75 m_retrievedItems = true;
76 }
77 }
78
79 bool UpdateItemStatesThread::lockPlugin()
80 {
81 return m_globalPluginMutex->tryLock(300);
82 }
83
84 void UpdateItemStatesThread::unlockPlugin()
85 {
86 m_globalPluginMutex->unlock();
87 }
88
89 QList<VersionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
90 {
91 QMutexLocker locker(&m_itemMutex);
92 return m_itemStates;
93 }
94
95 bool UpdateItemStatesThread::retrievedItems() const
96 {
97 QMutexLocker locker(&m_itemMutex);
98 return m_retrievedItems;
99 }
100
101 #include "updateitemstatesthread.moc"