2
* This file is part of buteo-syncfw package
4
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public License
10
* version 2.1 as published by the Free Software Foundation.
12
* This library is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25
#include "StorageBooker.h"
26
#include <QMutexLocker>
27
#include "LogMacros.h"
29
using namespace Buteo;
31
StorageBooker::StorageBooker()
32
: iMutex(QMutex::Recursive)
37
StorageBooker::~StorageBooker()
42
bool StorageBooker::reserveStorage(const QString &aStorageName,
43
const QString &aClientId)
47
QMutexLocker locker(&iMutex);
51
if (iStorageMap.contains(aStorageName))
53
StorageMapItem item = iStorageMap[aStorageName];
54
if (aClientId.isEmpty() || aClientId != item.iClientId)
56
// Already reserved for different client.
61
// Already reserved for the same client. Increase ref count.
63
iStorageMap[aStorageName] = item;
69
// No reservations for the storage. Add a new entry to the storage
71
iStorageMap.insert(aStorageName, aClientId);
78
bool StorageBooker::reserveStorages(const QStringList &aStorageNames,
79
const QString &aClientId)
83
QMutexLocker locker(&iMutex);
86
if (storagesAvailable(aStorageNames, aClientId))
88
foreach (QString storage, aStorageNames)
90
reserveStorage(storage, aClientId);
102
unsigned StorageBooker::releaseStorage(const QString &aStorageName)
106
QMutexLocker locker(&iMutex);
108
unsigned remainingRefCount = 0;
110
if (iStorageMap.contains(aStorageName))
112
StorageMapItem item = iStorageMap[aStorageName];
114
remainingRefCount = item.iRefCount;
116
if (remainingRefCount == 0)
118
iStorageMap.remove(aStorageName);
122
iStorageMap[aStorageName] = item;
126
return remainingRefCount;
129
void StorageBooker::releaseStorages(const QStringList &aStorageNames)
133
QMutexLocker locker(&iMutex);
135
foreach (QString storage, aStorageNames)
137
releaseStorage(storage);
141
bool StorageBooker::isStorageAvailable(const QString &aStorageName,
142
const QString &aClientId) const
146
QMutexLocker locker(&iMutex);
148
return (!iStorageMap.contains(aStorageName) ||
149
(!aClientId.isEmpty() &&
150
(aClientId == iStorageMap[aStorageName].iClientId)));
154
bool StorageBooker::storagesAvailable(const QStringList &aStorageNames,
155
const QString &aClientId) const
159
QMutexLocker locker(&iMutex);
161
foreach (QString storage, aStorageNames)
163
if (!isStorageAvailable(storage, aClientId))