1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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);
qt_TDes2QString(const TDes & aDescriptor)82 inline QString qt_TDes2QString(const TDes& aDescriptor) { return qt_TDesC2QString(aDescriptor); }
83
qt_TSize2QSize(const TSize & ts)84 static inline QSize qt_TSize2QSize(const TSize& ts)
85 {
86 return QSize(ts.iWidth, ts.iHeight);
87 }
88
qt_QSize2TSize(const QSize & qs)89 static inline TSize qt_QSize2TSize(const QSize& qs)
90 {
91 return TSize(qs.width(), qs.height());
92 }
93
qt_TRect2QRect(const TRect & tr)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
qt_QRect2TRect(const QRect & qr)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
qt_QString2TPtrC(const QString & string)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; }
data()127 inline HBufC *data() { return m_hBufC; }
data()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 ¶m) const { return data() == param.data(); }
135 inline bool operator!=(const QHBufC ¶m) const { return data() != param.data(); }
136
137 private:
138 HBufC *m_hBufC;
139 };
140
qHash(TUid uid)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 {
cleanupQScopedPointerRCloser155 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:
cleanup(T * pointer)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:
QScopedResource(T & resource)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
316