1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#ifndef QCORE_SYMBIAN_P_H
43
#define QCORE_SYMBIAN_P_H
44
45
//
46
//  W A R N I N G
47
//  -------------
48
//
49
// This file is not part of the Qt API.  It exists for the convenience
50
// of other Qt classes.  This header file may change from version to
51
// version without notice, or even be removed.
52
//
53
// We mean it.
54
//
55
56
#include <e32std.h>
57
#include <QtCore/qglobal.h>
58
#include <QtCore/qmutex.h>
59
#include <qstring.h>
60
#include <qrect.h>
61
#include <qhash.h>
62
#include <qscopedpointer.h>
63
#include <f32file.h>
64
#include <es_sock.h>
65
66
#define QT_LSTRING2(x) L##x
67
#define QT_LSTRING(x) QT_LSTRING2(x)
68
69
#if defined(QT_LIBINFIX)
70
#  define QT_LIBINFIX_UNICODE QT_LSTRING(QT_LIBINFIX)
71
#else
72
#  define QT_LIBINFIX_UNICODE L""
73
#endif
74
75
QT_BEGIN_HEADER
76
77
QT_BEGIN_NAMESPACE
78
79
Q_CORE_EXPORT HBufC* qt_QString2HBufC(const QString& aString);
80
81
Q_CORE_EXPORT QString qt_TDesC2QString(const TDesC& aDescriptor);
82
inline QString qt_TDes2QString(const TDes& aDescriptor) { return qt_TDesC2QString(aDescriptor); }
83
84
static inline QSize qt_TSize2QSize(const TSize& ts)
85
{
86
    return QSize(ts.iWidth, ts.iHeight);
87
}
88
89
static inline TSize qt_QSize2TSize(const QSize& qs)
90
{
91
    return TSize(qs.width(), qs.height());
92
}
93
94
static inline QRect qt_TRect2QRect(const TRect& tr)
95
{
96
    return QRect(tr.iTl.iX, tr.iTl.iY, tr.Width(), tr.Height());
97
}
98
99
static inline TRect qt_QRect2TRect(const QRect& qr)
100
{
101
    return TRect(TPoint(qr.left(), qr.top()), TSize(qr.width(), qr.height()));
102
}
103
104
// Returned TPtrC is valid as long as the given parameter is valid and unmodified
105
static inline TPtrC qt_QString2TPtrC( const QString& string )
106
{
107
    return TPtrC16(static_cast<const TUint16*>(string.utf16()), string.length());
108
}
109
110
/*!
111
    \internal
112
    This class is a wrapper around the Symbian HBufC descriptor class.
113
    It makes sure that the heap allocated HBufC class is freed when it is
114
    destroyed.
115
*/
116
class Q_CORE_EXPORT QHBufC
117
{
118
public:
119
    QHBufC();
120
    QHBufC(const QHBufC &src);
121
    QHBufC(HBufC *src);
122
    QHBufC(const QString &src);
123
    ~QHBufC();
124
125
    inline operator HBufC *() { return m_hBufC; }
126
    inline operator const HBufC *() const { return m_hBufC; }
127
    inline HBufC *data() { return m_hBufC; }
128
    inline const HBufC *data() const { return m_hBufC; }
129
    inline HBufC & operator*() { return *m_hBufC; }
130
    inline const HBufC & operator*() const { return *m_hBufC; }
131
    inline HBufC * operator->() { return m_hBufC; }
132
    inline const HBufC * operator->() const { return m_hBufC; }
133
134
    inline bool operator==(const QHBufC &param) const { return data() == param.data(); }
135
    inline bool operator!=(const QHBufC &param) const { return data() != param.data(); }
136
137
private:
138
    HBufC *m_hBufC;
139
};
140
141
inline uint qHash(TUid uid)
142
{
143
    return qHash(uid.iUid);
144
}
145
146
Q_CORE_EXPORT RFs& qt_s60GetRFs();
147
Q_CORE_EXPORT RSocketServ& qt_symbianGetSocketServer();
148
149
// Defined in qlocale_symbian.cpp.
150
Q_CORE_EXPORT QByteArray qt_symbianLocaleName(int code);
151
152
template <typename R>
153
struct QScopedPointerRCloser
154
{
155
    static inline void cleanup(R *rPointer)
156
    {
157
        // Enforce a complete type.
158
        // If you get a compile error here, read the section on forward declared
159
        // classes in the QScopedPointer documentation.
160
        typedef char IsIncompleteType[ sizeof(R) ? 1 : -1 ];
161
        (void) sizeof(IsIncompleteType);
162
163
        if (rPointer)
164
            rPointer->Close();
165
    }
166
};
167
168
//Wrapper for RSocket so it can be used as a key in QHash or QMap
169
class QHashableSocket : public RSocket
170
{
171
public:
172
    bool operator==(const QHashableSocket &other) const
173
    {
174
        return SubSessionHandle() == other.SubSessionHandle()
175
            && Session().Handle() == other.Session().Handle();
176
    }
177
    bool operator<(const QHashableSocket &other) const
178
    {
179
        if (Session().Handle() == other.Session().Handle())
180
            return SubSessionHandle() < other.SubSessionHandle();
181
        return Session().Handle() < other.Session().Handle();
182
    }
183
};
184
185
uint qHash(const RSubSessionBase& key);
186
187
/*!
188
  \internal
189
  This class exists in QtCore for the benefit of QSocketNotifier, which uses integer
190
  file descriptors in its public API.
191
  So we need a way to map between int and RSocket.
192
  Additionally, it is used to host the global RSocketServ session
193
*/
194
class Q_CORE_EXPORT QSymbianSocketManager
195
{
196
public:
197
    QSymbianSocketManager();
198
    ~QSymbianSocketManager();
199
200
    /*!
201
      \internal
202
      \return handle to the socket server
203
    */
204
    RSocketServ& getSocketServer();
205
    /*!
206
      \internal
207
      Adds a symbian socket to the global map
208
      \param an open socket
209
      \return pseudo file descriptor, -1 if out of resources
210
    */
211
    int addSocket(const RSocket &sock);
212
    /*!
213
      \internal
214
      Removes a symbian socket from the global map
215
      \param an open socket
216
      \return true if the socket was in the map
217
    */
218
    bool removeSocket(const RSocket &sock);
219
    /*!
220
      \internal
221
      Get pseudo file descriptor for a socket
222
      \param an open socket
223
      \return integer handle, or -1 if not in map
224
    */
225
    int lookupSocket(const RSocket &sock) const;
226
    /*!
227
      \internal
228
      Get socket for a pseudo file descriptor
229
      \param an open socket fd
230
      \param sock (out) socket handle
231
      \return true on success or false if not in map
232
    */
233
    bool lookupSocket(int fd, RSocket& sock) const;
234
235
    /*!
236
      \internal
237
      Set the default connection to use for new sockets
238
      \param an open connection
239
    */
240
    void setDefaultConnection(RConnection* con);
241
    /*!
242
      \internal
243
      Get the default connection to use for new sockets
244
      \return the connection, or null pointer if there is none set
245
    */
246
    RConnection *defaultConnection() const;
247
248
    /*!
249
      \internal
250
      Add an opened connection to the active list
251
      \param an open connection
252
    */
253
    void addActiveConnection(TUint32 identifier);
254
255
    /*!
256
      \internal
257
      Remove a connection from the active list
258
      \param a closed connection
259
    */
260
    void removeActiveConnection(TUint32 identifier);
261
262
    /*!
263
      \internal
264
      Add an opened connection to the active list
265
      \param an open connection
266
    */
267
    QList<TUint32> activeConnections() const;
268
269
    /*!
270
      \internal
271
      Gets a reference to the singleton socket manager
272
    */
273
    static QSymbianSocketManager& instance();
274
private:
275
    int allocateSocket();
276
277
    const static int max_sockets = 0x20000; //covers all TCP and UDP ports, probably run out of memory first
278
    const static int socket_offset = 0x40000000; //hacky way of separating sockets from file descriptors
279
    int iNextSocket;
280
    QHash<QHashableSocket, int> socketMap;
281
    QHash<int, RSocket> reverseSocketMap;
282
    QHash<TUint32, int> activeConnectionsMap;
283
    mutable QMutex iMutex;
284
    RSocketServ iSocketServ;
285
    RConnection *iDefaultConnection;
286
};
287
288
template <typename T> class QScopedPointerResourceCloser
289
{
290
public:
291
    static inline void cleanup(T* pointer)
292
    {
293
        if (pointer)
294
            pointer->Close();
295
    }
296
};
297
298
/*typical use:
299
    RFile file;
300
    file.Open(...);
301
    QScopedResource<RFile> ptr(file);
302
    container.append(file); //this may throw std::bad_alloc, in which case file.Close() is called by destructor
303
    ptr.take(); //if we reach this line, ownership is transferred to the container
304
 */
305
template <typename T> class QScopedResource : public QScopedPointer<T, QScopedPointerResourceCloser<T> >
306
{
307
public:
308
    inline QScopedResource(T& resource) : QScopedPointer<T, QScopedPointerResourceCloser<T> >(&resource) {}
309
};
310
311
QT_END_NAMESPACE
312
313
QT_END_HEADER
314
315
#endif //QCORE_SYMBIAN_P_H