update tizen source
[framework/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginMain.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include <glib.h>
32 #include <pthread.h>
33 #include <stdio.h>
34 #include <time.h>
35
36 #include "MmsPluginTypes.h"
37 #include "MmsPluginMain.h"
38 #include "MmsPluginTransport.h"
39 #include "MsgDebug.h"
40 #include "MsgException.h"
41
42 #include "MmsPluginMain.h"
43 #include "MmsPluginMessage.h"
44 #include "MmsPluginStorage.h"
45 #include "MmsPluginInternal.h"
46 #include "MmsPluginEventHandler.h"
47 #include "MsgGconfWrapper.h"
48 #include "MmsPluginCodec.h"
49 #include "MsgUtilFile.h"
50 #include "MmsPluginUserAgent.h"
51
52
53 /*==================================================================================================
54                                                         FUNCTION IMPLEMENTATION
55 ==================================================================================================*/
56 MSG_ERROR_T MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
57 {
58         if (pPluginHandle == NULL) {
59                 MSG_DEBUG("MMS plugin: create handler error ");
60                 return MSG_ERR_NULL_POINTER;
61         } else {
62                 pPluginHandle->pfInitialize = MmsInitialize;
63                 pPluginHandle->pfFinalize = MmsFinalize;
64                 pPluginHandle->pfRegisterListener = MmsRegisterListener;
65                 pPluginHandle->pfSubmitRequest = MmsSubmitRequest;
66                 pPluginHandle->pfAddMessage = MmsAddMessage;
67                 pPluginHandle->pfProcessReceivedInd = MmsProcessReceivedInd;
68                 pPluginHandle->pfUpdateMessage = MmsUpdateMessage;
69                 pPluginHandle->pfGetMmsMessage = MmsGetMmsMessage;
70                 pPluginHandle->pfUpdateRejectStatus = MmsUpdateRejectStatus;
71                 pPluginHandle->pfComposeReadReport = MmsComposeReadReport;
72                 pPluginHandle->pfRestoreMsg = MmsRestoreMsg;
73
74                 MSG_DEBUG("MMS plugin: create handler OK");
75                 MSG_DEBUG ("MMS plugin %p", pPluginHandle);
76         }
77
78         return MSG_SUCCESS;
79 }
80
81
82 MSG_ERROR_T MmsPlgDestroyHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
83 {
84         MSG_BEGIN();
85
86         if (pPluginHandle != NULL) {
87                 free(pPluginHandle);
88                 pPluginHandle = NULL;
89         }
90
91         MSG_END();
92
93         return MSG_SUCCESS;
94 }
95
96
97 MSG_ERROR_T MmsInitialize()
98 {
99         MSG_BEGIN();
100
101         // remove temp files
102         MsgMmsInitDir();
103
104         MSG_END();
105
106         return MSG_SUCCESS;
107 }
108
109
110 MSG_ERROR_T MmsFinalize()
111 {
112         MSG_BEGIN();
113
114         MSG_END();
115
116         return MSG_SUCCESS;
117 }
118
119
120 MSG_ERROR_T MmsRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
121 {
122         MSG_BEGIN();
123
124         MmsPluginEventHandler::instance()->registerListener(pListener);
125
126         MSG_END();
127
128         return MSG_SUCCESS;
129 }
130
131
132 MSG_ERROR_T MmsSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo, bool bReqCb)
133 {
134         MSG_BEGIN();
135
136         try {
137                 MmsPluginTransport::instance()->submitRequest(pReqInfo);
138         } catch (MsgException& e) {
139                 MSG_FATAL("%s", e.what());
140                 return MSG_ERR_PLUGIN_TRANSPORT;
141         } catch (exception& e) {
142                 MSG_FATAL("%s", e.what());
143                 return MSG_ERR_PLUGIN_TRANSPORT;
144         }
145
146         MSG_END();
147
148         return MSG_SUCCESS;
149 }
150
151
152 MSG_ERROR_T MmsAddMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char *pFileData)
153 {
154         MSG_BEGIN();
155
156         try {
157                 MmsPluginStorage::instance()->addMessage(pMsgInfo, pSendOptInfo, pFileData);
158         } catch (MsgException& e) {
159                 MSG_FATAL("%s", e.what());
160                 return MSG_ERR_PLUGIN_TRANSPORT;
161         } catch (exception& e) {
162                 MSG_FATAL("%s", e.what());
163                 return MSG_ERR_PLUGIN_TRANSPORT;
164         }
165
166         MSG_END();
167
168         return MSG_SUCCESS;
169 }
170
171
172 MSG_ERROR_T MmsProcessReceivedInd(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_REQUEST_INFO_S *pRequest, bool *bReject)
173 {
174         MSG_BEGIN();
175
176         MSG_DEBUG("MMS Plugin ProcessReceivedInd");
177
178         try {
179                 MmsPluginInternal::instance()->processReceivedInd(pMsgInfo, pRequest, bReject);
180         } catch (MsgException& e) {
181                 MSG_FATAL("%s", e.what());
182                 return MSG_ERR_PLUGIN_TRANSPORT;
183         } catch (exception& e) {
184                 MSG_FATAL("%s", e.what());
185                 return MSG_ERR_PLUGIN_TRANSPORT;
186         }
187
188         MSG_END();
189
190         return MSG_SUCCESS;
191 }
192
193
194 MSG_ERROR_T MmsUpdateMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo,  char *pFileData)
195 {
196         MSG_BEGIN();
197
198         MSG_ERROR_T err = MSG_SUCCESS;
199
200         try {
201                 if (pMsgInfo->networkStatus == MSG_NETWORK_NOT_SEND) {
202                         err = MmsPluginStorage::instance()->updateMessage(pMsgInfo, pSendOptInfo, pFileData);
203                 } else {
204                         //[Update Message ID & File path only in case of retrieve. Else update Message ID]
205                         if (pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS) {
206                                 err = MmsPluginStorage::instance()->updateConfMessage(pMsgInfo);
207                         } else {
208                                 err = MmsPluginStorage::instance()->updateMsgServerID(pMsgInfo, pSendOptInfo);
209                         }
210                 }
211         } catch (MsgException& e) {
212                 MSG_FATAL("%s", e.what());
213                 return MSG_ERR_PLUGIN_STORAGE;
214         } catch (exception& e) {
215                 MSG_FATAL("%s", e.what());
216                 return MSG_ERR_PLUGIN_STORAGE;
217         }
218
219         MSG_END();
220
221         return err;
222 }
223
224
225 MSG_ERROR_T MmsGetMmsMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, MMS_MESSAGE_DATA_S *pMmsMsg, char **pDestMsg)
226 {
227         MSG_BEGIN();
228
229         MSG_ERROR_T     err = MSG_SUCCESS;
230
231         try {
232                 err = MmsPluginStorage::instance()->plgGetMmsMessage(pMsg, pSendOptInfo, pMmsMsg, pDestMsg);
233         } catch (MsgException& e) {
234                 MSG_FATAL("%s", e.what());
235                 return MSG_ERR_PLUGIN_STORAGE;
236         } catch (exception& e) {
237                 MSG_FATAL("%s", e.what());
238                 return MSG_ERR_PLUGIN_STORAGE;
239         }
240
241         MSG_END();
242
243         return err;
244 }
245
246
247 MSG_ERROR_T MmsUpdateRejectStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
248 {
249         MSG_BEGIN();
250
251         MSG_ERROR_T err = MSG_SUCCESS;
252
253         try {
254                 char szTrID[MMS_TR_ID_LEN + 1] = {0x00};
255                 bool bReportAllowed;
256
257                 err = MmsPluginStorage::instance()->getTrID(pMsgInfo,szTrID,sizeof(szTrID));
258                 if (err != MSG_SUCCESS)
259                         MSG_DEBUG("MmsPlgUpdRejectStatus : Get MMS Transacation id Failed");
260
261                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
262                 MsgSettingGetBool(MMS_SEND_REPORT_ALLOWED, &bReportAllowed);
263                 if (MmsPluginInternal::instance()->encodeNotifyRespInd(szTrID, MSG_DELIVERY_REPORT_REJECTED, bReportAllowed, pMsgInfo->msgData)) {
264                         MSG_DEBUG("MmsPlgUpdRejectStatus : Encode Notify Response Success");
265                         pMsgInfo->dataSize = strlen(pMsgInfo->msgData);
266                         pMsgInfo->bTextSms = true;
267                 } else
268                         MSG_DEBUG("MmsPlgSetRejectStatus : Encode Notify Response Failed");
269         } catch (MsgException& e) {
270                 MSG_FATAL("%s", e.what());
271                 return MSG_ERR_PLUGIN_STORAGE;
272         } catch (exception& e) {
273                 MSG_FATAL("%s", e.what());
274                 return MSG_ERR_PLUGIN_STORAGE;
275         }
276
277         if (err != MSG_SUCCESS)
278                 MSG_DEBUG("MmsPlgSetRejectStatus : Update MMS Message Failed");
279
280         MSG_END();
281
282         return err;
283 }
284
285
286 MSG_ERROR_T MmsComposeReadReport(MSG_MESSAGE_INFO_S *pMsgInfo)
287 {
288         MSG_BEGIN();
289
290         try {
291                 MmsPluginStorage::instance()->composeReadReport(pMsgInfo);
292         } catch (MsgException& e) {
293                 MSG_FATAL("%s", e.what());
294                 return MSG_ERR_PLUGIN_STORAGE;
295         } catch (exception& e) {
296                 MSG_FATAL("%s", e.what());
297                 return MSG_ERR_PLUGIN_STORAGE;
298         }
299
300         MSG_END();
301
302         return MSG_SUCCESS;
303 }
304
305
306 MSG_ERROR_T MmsRestoreMsg(MSG_MESSAGE_INFO_S *pMsgInfo, char *pRcvBody, int rcvdBodyLen, char *filePath)
307 {
308         MSG_BEGIN();
309
310         if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
311                 FILE *pFile;
312                 _MmsInitHeader();
313                 _MmsRegisterDecodeBuffer(gszMmsLoadBuf1,  gszMmsLoadBuf2, MSG_MMS_DECODE_BUFFER_MAX);
314
315                 if ((pFile = MsgOpenFile(pMsgInfo->msgData, "rb+")) == NULL) {
316                         MSG_DEBUG("File Open Error: %s", pMsgInfo->msgData);
317                 } else {
318                         //Decode Header
319                         if (!MmsBinaryDecodeMsgHeader(pFile, rcvdBodyLen))
320                                 MSG_DEBUG("Decoding Header Failed \r\n");
321
322                         MsgCloseFile(pFile);
323                 }
324         } else {
325                 MSG_DEBUG(":::%d :%s ",rcvdBodyLen, pRcvBody);
326
327                 if (filePath) {
328                         snprintf(filePath, MAX_FULL_PATH_SIZE, MSG_DATA_PATH"BODY_%lu.DATA", random() % 1000000000 + 1);
329                 } else {
330                         return MSG_ERR_NULL_POINTER;
331                 }
332
333                 // create temp file
334                 if (!MsgOpenCreateAndOverwriteFile(filePath, (char*)pRcvBody,rcvdBodyLen))
335                         return MSG_ERR_PLUGIN_STORAGE;
336         }
337
338         MSG_END();
339
340         return MSG_SUCCESS;
341 }
342