~renatofilho/buteo-syncfw/more-verbose

« back to all changes in this revision

Viewing changes to msyncd/StorageBooker.cpp

  • Committer: Sergey Gerasimenko
  • Date: 2010-06-29 12:51:21 UTC
  • Revision ID: git-v1:cd8dab07b102ac96752ece4f3cde5fc62697d717
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of buteo-syncfw package
 
3
 *
 
4
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
 *
 
6
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
20
 * 02110-1301 USA
 
21
 *
 
22
 */
 
23
 
 
24
 
 
25
#include "StorageBooker.h"
 
26
#include <QMutexLocker>
 
27
#include "LogMacros.h"
 
28
 
 
29
using namespace Buteo;
 
30
 
 
31
StorageBooker::StorageBooker()
 
32
:   iMutex(QMutex::Recursive)
 
33
{
 
34
    FUNCTION_CALL_TRACE;
 
35
}
 
36
 
 
37
StorageBooker::~StorageBooker()
 
38
{
 
39
    FUNCTION_CALL_TRACE;
 
40
}
 
41
 
 
42
bool StorageBooker::reserveStorage(const QString &aStorageName,
 
43
                                   const QString &aClientId)
 
44
{
 
45
    FUNCTION_CALL_TRACE;
 
46
 
 
47
    QMutexLocker locker(&iMutex);
 
48
 
 
49
    bool success = false;
 
50
 
 
51
    if (iStorageMap.contains(aStorageName))
 
52
    {
 
53
        StorageMapItem item = iStorageMap[aStorageName];
 
54
        if (aClientId.isEmpty() || aClientId != item.iClientId)
 
55
        {
 
56
            // Already reserved for different client.
 
57
            success = false;
 
58
        }
 
59
        else
 
60
        {
 
61
            // Already reserved for the same client. Increase ref count.
 
62
            item.iRefCount++;
 
63
            iStorageMap[aStorageName] = item;
 
64
            success = true;
 
65
        }
 
66
    }
 
67
    else
 
68
    {
 
69
        // No reservations for the storage. Add a new entry to the storage
 
70
        // reservation map.
 
71
        iStorageMap.insert(aStorageName, aClientId);
 
72
        success = true;
 
73
    }
 
74
 
 
75
    return success;
 
76
}
 
77
 
 
78
bool StorageBooker::reserveStorages(const QStringList &aStorageNames,
 
79
                                    const QString &aClientId)
 
80
{
 
81
    FUNCTION_CALL_TRACE;
 
82
 
 
83
    QMutexLocker locker(&iMutex);
 
84
 
 
85
    bool success = false;
 
86
    if (storagesAvailable(aStorageNames, aClientId))
 
87
    {
 
88
        foreach (QString storage, aStorageNames)
 
89
        {
 
90
            reserveStorage(storage, aClientId);
 
91
        }
 
92
        success = true;
 
93
    }
 
94
    else
 
95
    {
 
96
        success = false;
 
97
    }
 
98
 
 
99
    return success;
 
100
}
 
101
 
 
102
unsigned StorageBooker::releaseStorage(const QString &aStorageName)
 
103
{
 
104
    FUNCTION_CALL_TRACE;
 
105
 
 
106
    QMutexLocker locker(&iMutex);
 
107
 
 
108
    unsigned remainingRefCount = 0;
 
109
 
 
110
    if (iStorageMap.contains(aStorageName))
 
111
    {
 
112
        StorageMapItem item = iStorageMap[aStorageName];
 
113
        item.iRefCount--;
 
114
        remainingRefCount = item.iRefCount;
 
115
 
 
116
        if (remainingRefCount == 0)
 
117
        {
 
118
            iStorageMap.remove(aStorageName);
 
119
        }
 
120
        else
 
121
        {
 
122
            iStorageMap[aStorageName] = item;
 
123
        }
 
124
    } // no else
 
125
 
 
126
    return remainingRefCount;
 
127
}
 
128
 
 
129
void StorageBooker::releaseStorages(const QStringList &aStorageNames)
 
130
{
 
131
    FUNCTION_CALL_TRACE;
 
132
 
 
133
    QMutexLocker locker(&iMutex);
 
134
 
 
135
    foreach (QString storage, aStorageNames)
 
136
    {
 
137
        releaseStorage(storage);
 
138
    }
 
139
}
 
140
 
 
141
bool StorageBooker::isStorageAvailable(const QString &aStorageName,
 
142
                                       const QString &aClientId) const
 
143
{
 
144
    FUNCTION_CALL_TRACE;
 
145
 
 
146
    QMutexLocker locker(&iMutex);
 
147
 
 
148
    return (!iStorageMap.contains(aStorageName) ||
 
149
            (!aClientId.isEmpty() &&
 
150
             (aClientId == iStorageMap[aStorageName].iClientId)));
 
151
 
 
152
}
 
153
 
 
154
bool StorageBooker::storagesAvailable(const QStringList &aStorageNames,
 
155
                                      const QString &aClientId) const
 
156
{
 
157
    FUNCTION_CALL_TRACE;
 
158
 
 
159
    QMutexLocker locker(&iMutex);
 
160
 
 
161
    foreach (QString storage, aStorageNames)
 
162
    {
 
163
        if (!isStorageAvailable(storage, aClientId))
 
164
            return false;
 
165
    }
 
166
 
 
167
    return true;
 
168
}