0
|
1 |
/*
|
|
2 |
* Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
// Include Files
|
|
21 |
|
|
22 |
#include <e32std.h> // GLDEF_C
|
|
23 |
#include <e32base.h>
|
|
24 |
#include "MsgSession.h"
|
|
25 |
#include "sysini.h"
|
|
26 |
|
|
27 |
#define KInitialMessageBufferSize 1024
|
|
28 |
|
|
29 |
TVersion MessageRelayVersion = TVersion(1, 0, 0);
|
|
30 |
|
|
31 |
|
|
32 |
/****************************************************************************
|
|
33 |
* CMessageNotifier
|
|
34 |
*****************************************************************************
|
|
35 |
*
|
|
36 |
*
|
|
37 |
*
|
|
38 |
****************************************************************************/
|
|
39 |
class CMessageNotifier : public CActive
|
|
40 |
{
|
|
41 |
CMessageSession *iOwner;
|
|
42 |
|
|
43 |
void ConstructL(CMessageSession *aOwner);
|
|
44 |
|
|
45 |
CMessageNotifier();
|
|
46 |
|
|
47 |
|
|
48 |
public:
|
|
49 |
static CMessageNotifier *NewL(CMessageSession *aOwner);
|
|
50 |
static CMessageNotifier *NewLC(CMessageSession *aOwner);
|
|
51 |
|
|
52 |
void RunL();
|
|
53 |
void DoCancel();
|
|
54 |
|
|
55 |
void StartReceiveL();
|
|
56 |
|
|
57 |
TRequestStatus *GetStatusPtr() { return &iStatus;}
|
|
58 |
};
|
|
59 |
|
|
60 |
CMessageNotifier::CMessageNotifier() :
|
|
61 |
CActive(EPriorityNormal)
|
|
62 |
{
|
|
63 |
|
|
64 |
}
|
|
65 |
|
|
66 |
void CMessageNotifier::ConstructL(CMessageSession *aOwner)
|
|
67 |
{
|
|
68 |
iOwner = aOwner;
|
|
69 |
CActiveScheduler::Add(this);
|
|
70 |
}
|
|
71 |
|
|
72 |
CMessageNotifier *CMessageNotifier::NewL(CMessageSession *aOwner)
|
|
73 |
{
|
|
74 |
CMessageNotifier *self = CMessageNotifier::NewLC(aOwner);
|
|
75 |
CleanupStack::Pop();
|
|
76 |
return self;
|
|
77 |
}
|
|
78 |
|
|
79 |
CMessageNotifier *CMessageNotifier::NewLC(CMessageSession *aOwner)
|
|
80 |
{
|
|
81 |
CMessageNotifier *self = new (ELeave) CMessageNotifier();
|
|
82 |
CleanupStack::PushL(self);
|
|
83 |
self->ConstructL(aOwner);
|
|
84 |
return self;
|
|
85 |
}
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
void CMessageNotifier::RunL()
|
|
90 |
{
|
|
91 |
/* RunL(...) is called only when a message is received. */
|
|
92 |
|
|
93 |
ASSERT(iOwner != NULL);
|
|
94 |
ASSERT(iOwner->iListener != NULL);
|
|
95 |
|
|
96 |
if (iStatus.Int() == KErrNone) {
|
|
97 |
if (iOwner->iMessageBuffer->Length() > 0) {
|
|
98 |
iOwner->iListener->ReceiveMessageL(iOwner->iMessageBuffer->Des());
|
|
99 |
}
|
|
100 |
StartReceiveL();
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
|
|
105 |
void CMessageNotifier::DoCancel()
|
|
106 |
{
|
|
107 |
iOwner->SendReceive(KCancelReceive, TIpcArgs());
|
|
108 |
}
|
|
109 |
|
|
110 |
void CMessageNotifier::StartReceiveL()
|
|
111 |
{
|
|
112 |
iStatus = KRequestPending;
|
|
113 |
SetActive();
|
|
114 |
iOwner->iMessagePtr->Zero();
|
|
115 |
TIpcArgs args(iOwner->iMessagePtr);
|
|
116 |
iOwner->SendReceive(KReceive, args, iStatus);
|
|
117 |
}
|
|
118 |
|
|
119 |
/****************************************************************************
|
|
120 |
* CMessageSession
|
|
121 |
*****************************************************************************
|
|
122 |
*
|
|
123 |
*
|
|
124 |
*
|
|
125 |
****************************************************************************/
|
|
126 |
EXPORT_C CMessageSession::CMessageSession() :
|
|
127 |
RSessionBase()
|
|
128 |
{
|
|
129 |
|
|
130 |
}
|
|
131 |
|
|
132 |
EXPORT_C void CMessageSession::ConstructL()
|
|
133 |
{
|
|
134 |
iMessageBuffer = HBufC8::NewL(KMaxSmsBufferSize);
|
|
135 |
iMessagePtr = new (ELeave) TPtr8(iMessageBuffer->Des());
|
|
136 |
|
|
137 |
iListener = NULL;
|
|
138 |
iIncomingNotify = CMessageNotifier::NewL(this);
|
|
139 |
}
|
|
140 |
|
|
141 |
EXPORT_C CMessageSession *CMessageSession::NewL()
|
|
142 |
{
|
|
143 |
CMessageSession *self = CMessageSession::NewLC();
|
|
144 |
CleanupStack::Pop();
|
|
145 |
return self;
|
|
146 |
}
|
|
147 |
|
|
148 |
EXPORT_C CMessageSession *CMessageSession::NewLC()
|
|
149 |
{
|
|
150 |
CMessageSession *self = new (ELeave) CMessageSession;
|
|
151 |
CleanupStack::PushL(self);
|
|
152 |
self->ConstructL();
|
|
153 |
return self;
|
|
154 |
}
|
|
155 |
|
|
156 |
EXPORT_C CMessageSession::~CMessageSession()
|
|
157 |
{
|
|
158 |
StopListeningL();
|
|
159 |
delete iIncomingNotify;
|
|
160 |
delete iMessageBuffer;
|
|
161 |
delete iMessagePtr;
|
|
162 |
}
|
|
163 |
|
|
164 |
/* Attaches to server. */
|
|
165 |
EXPORT_C void CMessageSession::AttachL()
|
|
166 |
{
|
|
167 |
User::LeaveIfError(CreateSession(KRelayServiceName, MessageRelayVersion));
|
|
168 |
}
|
|
169 |
|
|
170 |
/* Sends a message. */
|
|
171 |
EXPORT_C void CMessageSession::SendMessageL(const TPtr8 &aMessage, TMessageType aType)
|
|
172 |
{
|
|
173 |
TIpcArgs args( aMessage.Length(), &aMessage, aType );
|
|
174 |
User::LeaveIfError( SendReceive( KNewMessage, args ) );
|
|
175 |
}
|
|
176 |
|
|
177 |
/* Listen messages of given type in a given port. One
|
|
178 |
session may listen to one port only. */
|
|
179 |
EXPORT_C void CMessageSession::ListenMessagesL(TMessageType aType, TUint aPort)
|
|
180 |
{
|
|
181 |
ASSERT(iListener != NULL);
|
|
182 |
TIpcArgs args( iMessagePtr, aType, aPort);
|
|
183 |
User::LeaveIfError(SendReceive(KStartListening, args));
|
|
184 |
iIncomingNotify->StartReceiveL();
|
|
185 |
}
|
|
186 |
|
|
187 |
/* Stops listening message. */
|
|
188 |
EXPORT_C void CMessageSession::StopListeningL()
|
|
189 |
{
|
|
190 |
User::LeaveIfError(SendReceive(KStopListening, TIpcArgs()));
|
|
191 |
iIncomingNotify->Cancel();
|
|
192 |
}
|
|
193 |
|
|
194 |
/* Sets the message listener. Listener's ReceiveMessageL(...)
|
|
195 |
is called, when a new message is received. */
|
|
196 |
EXPORT_C void CMessageSession::SetListener(CMessageListener *aListener)
|
|
197 |
{
|
|
198 |
iListener = aListener;
|
|
199 |
}
|
|
200 |
|
|
201 |
/* Returns ETrue, is session set to listening state. */
|
|
202 |
EXPORT_C TBool CMessageSession::IsListening() const
|
|
203 |
{
|
|
204 |
if( iIncomingNotify->IsActive() )
|
|
205 |
return ETrue;
|
|
206 |
else
|
|
207 |
return EFalse;
|
|
208 |
}
|
|
209 |
|
|
210 |
/* Returns ETrue, if file based SMS should be used.
|
|
211 |
Reads the settings from c:\system\data\wma.ini file.
|
|
212 |
File format:
|
|
213 |
# Starts a comment line
|
|
214 |
# Set UseSocketOutput=1 to use native sockets implementation,
|
|
215 |
# UseSocketOutput=0 to use file based SMS messaging.
|
|
216 |
UseSocketOutput=0 */
|
|
217 |
EXPORT_C TBool CMessageSession::UseFileSMS()
|
|
218 |
{
|
|
219 |
/* The config file. */
|
|
220 |
_LIT(KWMAIniFile, "c:\\system\\data\\wma.ini");
|
|
221 |
|
|
222 |
|
|
223 |
/* The write mode; 0 == file (default), 1 == socket. */
|
|
224 |
int mode = 0;
|
|
225 |
CSysIni *ini = NULL;
|
|
226 |
|
|
227 |
/* Open the config file. NewLC leaves, if the file is not found, in which
|
|
228 |
case we default to writing to file. */
|
|
229 |
TRAPD(err, ini = CSysIni::NewL(KWMAIniFile));
|
|
230 |
CleanupStack::PushL(ini);
|
|
231 |
switch ( err )
|
|
232 |
{
|
|
233 |
case KErrNone:
|
|
234 |
if (ini->SeekBool("UseSocketOutput"))
|
|
235 |
mode = 1; // Use socket output
|
|
236 |
break;
|
|
237 |
case KErrNotFound:
|
|
238 |
mode = 0;
|
|
239 |
break;
|
|
240 |
default:
|
|
241 |
User::Panic(_L("CMessageSession::UseFileSms() panic:"), err);
|
|
242 |
}
|
|
243 |
|
|
244 |
//delete ini;
|
|
245 |
CleanupStack::PopAndDestroy();
|
|
246 |
|
|
247 |
if (mode == 1)
|
|
248 |
return EFalse;
|
|
249 |
else
|
|
250 |
return ETrue;
|
|
251 |
|
|
252 |
}
|
|
253 |
|
|
254 |
/****************************************************************************
|
|
255 |
* CMessageListener
|
|
256 |
*****************************************************************************
|
|
257 |
*
|
|
258 |
*
|
|
259 |
*
|
|
260 |
****************************************************************************/
|
|
261 |
EXPORT_C void CMessageListener::ConstructL()
|
|
262 |
{
|
|
263 |
|
|
264 |
}
|
|
265 |
|
|
266 |
EXPORT_C CMessageListener *CMessageListener::NewL()
|
|
267 |
{
|
|
268 |
CMessageListener *self = CMessageListener::NewLC();
|
|
269 |
CleanupStack::Pop();
|
|
270 |
return self;
|
|
271 |
}
|
|
272 |
|
|
273 |
EXPORT_C CMessageListener *CMessageListener::NewLC()
|
|
274 |
{
|
|
275 |
CMessageListener *self = new (ELeave) CMessageListener;
|
|
276 |
CleanupStack::PushL(self);
|
|
277 |
self->ConstructL();
|
|
278 |
return self;
|
|
279 |
}
|
|
280 |
|
|
281 |
EXPORT_C CMessageListener::~CMessageListener()
|
|
282 |
{
|
|
283 |
|
|
284 |
}
|
|
285 |
|
|
286 |
/* Called when a message is received. */
|
|
287 |
EXPORT_C void CMessageListener::ReceiveMessageL(const TPtr8 &/*aMessage*/)
|
|
288 |
{
|
|
289 |
|
|
290 |
}
|
|
291 |
|
|
292 |
// End of file
|