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