]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/versioncontrol/updateitemstatesthread.cpp
Do not rename files unexpectedly when changing the URL
[dolphin.git] / src / views / versioncontrol / updateitemstatesthread.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 "updateitemstatesthread.h"
21
22 #include <kversioncontrolplugin2.h>
23
24 #include <QMutexLocker>
25
26 UpdateItemStatesThread::UpdateItemStatesThread() :
27 QThread(),
28 m_globalPluginMutex(0),
29 m_plugin(0),
30 m_itemMutex(),
31 m_retrievedItems(false),
32 m_itemStates()
33 {
34 // Several threads may share one instance of a plugin. A global
35 // mutex is required to serialize the retrieval of version control
36 // states inside run().
37 static QMutex globalMutex;
38 m_globalPluginMutex = &globalMutex;
39 }
40
41 UpdateItemStatesThread::~UpdateItemStatesThread()
42 {
43 }
44
45 void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin,
46 const QList<VersionControlObserver::ItemState>& itemStates)
47 {
48 // The locks are taken in the same order as in run()
49 // to avoid potential deadlock.
50 QMutexLocker pluginLocker(m_globalPluginMutex);
51 QMutexLocker itemLocker(&m_itemMutex);
52
53 m_itemStates = itemStates;
54 m_plugin = plugin;
55 }
56
57 void UpdateItemStatesThread::run()
58 {
59 Q_ASSERT(!m_itemStates.isEmpty());
60 Q_ASSERT(m_plugin);
61
62 QMutexLocker itemLocker(&m_itemMutex);
63
64 const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash);
65 m_retrievedItems = false;
66 itemLocker.unlock();
67
68 QMutexLocker pluginLocker(m_globalPluginMutex);
69 if (m_plugin->beginRetrieval(directory)) {
70 itemLocker.relock();
71 const int count = m_itemStates.count();
72
73 KVersionControlPlugin2* pluginV2 = qobject_cast<KVersionControlPlugin2*>(m_plugin);
74 if (pluginV2) {
75 for (int i = 0; i < count; ++i) {
76 m_itemStates[i].version = pluginV2->itemVersion(m_itemStates[i].item);
77 }
78 } else {
79 for (int i = 0; i < count; ++i) {
80 const KVersionControlPlugin::VersionState state = m_plugin->versionState(m_itemStates[i].item);
81 m_itemStates[i].version = static_cast<KVersionControlPlugin2::ItemVersion>(state);
82 }
83 }
84
85 m_plugin->endRetrieval();
86 m_retrievedItems = true;
87 }
88 }
89
90 bool UpdateItemStatesThread::lockPlugin()
91 {
92 return m_globalPluginMutex->tryLock(300);
93 }
94
95 void UpdateItemStatesThread::unlockPlugin()
96 {
97 m_globalPluginMutex->unlock();
98 }
99
100 QList<VersionControlObserver::ItemState> UpdateItemStatesThread::itemStates() const
101 {
102 QMutexLocker locker(&m_itemMutex);
103 return m_itemStates;
104 }
105
106 bool UpdateItemStatesThread::retrievedItems() const
107 {
108 QMutexLocker locker(&m_itemMutex);
109 return m_retrievedItems;
110 }
111
112 #include "updateitemstatesthread.moc"