|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 //system includes |
|
42 #include <e32base.h> |
|
43 #include <s32mem.h> |
|
44 |
|
45 //user includes |
|
46 #include "cntsymbiansrvconnection.h" |
|
47 #include "cntsymbiantransformerror.h" |
|
48 |
|
49 // Constants |
|
50 // To be removed. Should be defined in a header file |
|
51 #define KCntSearchResultIdLists 99 |
|
52 #define KCntOpenDataBase 100 // = KCapabilityReadUserData |
|
53 |
|
54 _LIT(KCntServerExe,"CNTSRV.EXE"); // Name of the exe for the Contacts server. |
|
55 _LIT(KCntServerName,"CNTSRV"); // Name used to connect a session |
|
56 // to the Contacts server. |
|
57 |
|
58 /** Maximum number of asynchronous IPC calls. */ |
|
59 const TInt KAsyncMessageSlots=6; |
|
60 |
|
61 /* Contacts server version number. */ |
|
62 const TInt KCntServerMajorVersionNumber=1; |
|
63 const TInt KCntServerMinorVersionNumber=1; |
|
64 const TInt KCntServerBuildVersionNumber=1; |
|
65 |
|
66 const TInt KGranularityRank = 8; //2^8 = 256 bytes |
|
67 const TInt KDefaultPackagerSize = 3514; //Observed Techview Golden template size. |
|
68 |
|
69 /*! |
|
70 * The constructor |
|
71 */ |
|
72 CntSymbianSrvConnection::CntSymbianSrvConnection() : |
|
73 m_buffer(0), |
|
74 m_bufPtr(0,0,0), |
|
75 m_isInitialized(false) |
|
76 { |
|
77 } |
|
78 |
|
79 /*! |
|
80 * Destructor |
|
81 */ |
|
82 CntSymbianSrvConnection::~CntSymbianSrvConnection() |
|
83 { |
|
84 delete m_buffer; |
|
85 RHandleBase::Close(); |
|
86 } |
|
87 |
|
88 /*! |
|
89 * Query the SQL database |
|
90 * |
|
91 * \a sqlQuery An SQL query |
|
92 * \a error On return, contains the possible error. |
|
93 * \return the list of matched contact ids |
|
94 */ |
|
95 QList<QContactLocalId> CntSymbianSrvConnection::searchContacts(const QString& sqlQuery, |
|
96 QContactManager::Error& error) |
|
97 { |
|
98 QList<QContactLocalId> list; |
|
99 TPtrC queryPtr(reinterpret_cast<const TUint16*>(sqlQuery.utf16())); |
|
100 TRAPD(err, list = searchContactsL(queryPtr)); |
|
101 CntSymbianTransformError::transformError(err, error); |
|
102 return list; |
|
103 } |
|
104 |
|
105 /*! |
|
106 * The leaving function that queries the SQL database |
|
107 * |
|
108 * \a aSqlQuery An SQL query |
|
109 * \return the list of matched contact ids |
|
110 */ |
|
111 QList<QContactLocalId> CntSymbianSrvConnection::searchContactsL(const TDesC& aSqlQuery) |
|
112 { |
|
113 // Initialize connection if it is not initialized yet. |
|
114 if (!m_isInitialized) { |
|
115 ConnectSrvL(); |
|
116 OpenDatabaseL(); |
|
117 m_isInitialized = true; |
|
118 } |
|
119 |
|
120 // Fetch results from the server |
|
121 TIpcArgs args; |
|
122 args.Set(0, &GetReceivingBufferL()); |
|
123 args.Set(1, &aSqlQuery); |
|
124 TInt newBuffSize = SendReceive(KCntSearchResultIdLists, args); |
|
125 User::LeaveIfError(newBuffSize); |
|
126 if (newBuffSize > 0) |
|
127 { |
|
128 args.Set(0, &GetReceivingBufferL(newBuffSize)); |
|
129 args.Set(1,&aSqlQuery); |
|
130 User::LeaveIfError(newBuffSize = SendReceive(KCntSearchResultIdLists, args)); |
|
131 } |
|
132 |
|
133 // Unpack the contact ids into an list |
|
134 return UnpackCntIdArrayL(); |
|
135 } |
|
136 |
|
137 /*! |
|
138 * Connect to / create a cntsrv server session |
|
139 */ |
|
140 void CntSymbianSrvConnection::ConnectSrvL() |
|
141 { |
|
142 // Assume the server is already running and attempt to create a session |
|
143 // with a maximum of KAsyncMessageSlots message slots. |
|
144 TInt err = CreateSession(KCntServerName,Version(),KAsyncMessageSlots); |
|
145 |
|
146 // Server is not running |
|
147 if(err == KErrNotFound) { |
|
148 // Use the RProcess API to start the server. |
|
149 RProcess server; |
|
150 User::LeaveIfError(server.Create(KCntServerExe,KNullDesC)); |
|
151 |
|
152 //Enforce server to be at system default priority EPriorityForeground |
|
153 server.SetPriority(EPriorityForeground); |
|
154 |
|
155 // Synchronise with the server. |
|
156 TRequestStatus reqStatus; |
|
157 server.Rendezvous(reqStatus); |
|
158 server.Resume(); |
|
159 |
|
160 // Server will call the reciprocal static synchronisation call. |
|
161 User::WaitForRequest(reqStatus); |
|
162 server.Close(); |
|
163 User::LeaveIfError(reqStatus.Int()); |
|
164 |
|
165 // Create the server session. |
|
166 User::LeaveIfError(CreateSession(KCntServerName,Version(),KAsyncMessageSlots)); |
|
167 } else { |
|
168 User::LeaveIfError(err); |
|
169 } |
|
170 |
|
171 // Create IPC buffer |
|
172 m_buffer = CBufFlat::NewL(1 << KGranularityRank); |
|
173 m_maxBufferSize = KDefaultPackagerSize; |
|
174 |
|
175 } |
|
176 |
|
177 /*! |
|
178 * Open database |
|
179 */ |
|
180 void CntSymbianSrvConnection::OpenDatabaseL() |
|
181 { |
|
182 TIpcArgs args; |
|
183 args.Set(0, &KNullDesC); |
|
184 User::LeaveIfError(SendReceive(KCntOpenDataBase, args)); |
|
185 } |
|
186 |
|
187 /*! |
|
188 * Version of cntsrv |
|
189 */ |
|
190 TVersion CntSymbianSrvConnection::Version() const |
|
191 { |
|
192 return(TVersion(KCntServerMajorVersionNumber, |
|
193 KCntServerMinorVersionNumber, |
|
194 KCntServerBuildVersionNumber)); |
|
195 } |
|
196 |
|
197 /*! |
|
198 * Get the buffer reference to be used for IPC |
|
199 * |
|
200 * \a size size of the receiving buffer |
|
201 * \return a reference to the beginning of the buffer |
|
202 */ |
|
203 TDes8& CntSymbianSrvConnection::GetReceivingBufferL(int size) |
|
204 { |
|
205 if(size > m_buffer->Size()) { |
|
206 // Find next divisable by granularity size value. |
|
207 (size >>= KGranularityRank)++; |
|
208 m_maxBufferSize = size <<= 8; |
|
209 m_buffer->ResizeL(m_maxBufferSize); |
|
210 |
|
211 } else if(!size && m_buffer->Size() < m_maxBufferSize) { |
|
212 // Use the stored default size. |
|
213 m_buffer->ResizeL(m_maxBufferSize); |
|
214 } |
|
215 // The location of the whole buffer may have changed, because reallocation |
|
216 // may have taken place. Update both buffer pointers. |
|
217 m_bufPtr.Set(m_buffer->Ptr(0)); |
|
218 return m_bufPtr; |
|
219 } |
|
220 |
|
221 /*! |
|
222 * Unpack results from a buffer stream and store in a list |
|
223 * |
|
224 * \return list of matched contact ids |
|
225 */ |
|
226 QList<QContactLocalId> CntSymbianSrvConnection::UnpackCntIdArrayL() |
|
227 { |
|
228 RBufReadStream readStream; |
|
229 QList<QContactLocalId> list; |
|
230 TContactItemId item; |
|
231 |
|
232 readStream.Open(*m_buffer); |
|
233 int count = readStream.ReadInt32L(); |
|
234 for (int i=0; i<count; i++) { |
|
235 readStream >> item; |
|
236 list.append(item); |
|
237 } |
|
238 return list; |
|
239 } |