update tizen source
[framework/messaging/msg-service.git] / framework / submit-handler / MsgSubmitHandler.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 <stdio.h>
32 #include <string.h>
33
34 #include "MsgDebug.h"
35 #include "MsgUtilFile.h"
36 #include "MsgException.h"
37 #include "MsgGconfWrapper.h"
38 #include "MsgPluginManager.h"
39 #include "MsgStorageHandler.h"
40 #include "MsgSubmitHandler.h"
41
42
43 /*==================================================================================================
44                                      FUNCTION IMPLEMENTATION
45 ==================================================================================================*/
46 MSG_ERROR_T MsgSubmitReq(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
47 {
48         MSG_ERROR_T err = MSG_SUCCESS;
49
50         static int reqId = 1;
51
52         pReqInfo->reqId = reqId;
53         reqId++;
54
55         MSG_DEBUG("==== Msg ID = [%d] ====", pReqInfo->msgInfo.msgId);
56         MSG_DEBUG("==== Folder ID = [%d] ====", pReqInfo->msgInfo.folderId);
57         MSG_DEBUG("==== Main Type = [%d] ====", pReqInfo->msgInfo.msgType.mainType);
58         MSG_DEBUG("==== Sub Type = [%d] ====", pReqInfo->msgInfo.msgType.subType);
59         MSG_DEBUG("==== Class Type = [%d] ====", pReqInfo->msgInfo.msgType.classType);
60         MSG_DEBUG("==== Message Data = [%s] ====", pReqInfo->msgInfo.msgData);
61         MSG_DEBUG("==== Message Text = [%s] ====", pReqInfo->msgInfo.msgText);
62
63         MSG_DEBUG("==== bSetting = [%d] ====", pReqInfo->sendOptInfo.bSetting);
64
65         if (pReqInfo->msgInfo.msgType.mainType == MSG_SMS_TYPE)
66         {
67                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
68                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
69                 MSG_DEBUG("==== bReplyPath = [%d] ====", pReqInfo->sendOptInfo.option.smsSendOptInfo.bReplyPath);
70
71                 err = MsgSubmitReqSMS(pReqInfo);
72         }
73         else if (pReqInfo->msgInfo.msgType.mainType == MSG_MMS_TYPE)
74         {
75                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
76                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
77                 MSG_DEBUG("==== bReadReq = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.bReadReq);
78                 MSG_DEBUG("==== priority = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.priority);
79                 MSG_DEBUG("==== expiryTime = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.expiryTime.time);
80
81                 err = MsgSubmitReqMMS(pReqInfo, bScheduled);
82         }
83
84         return err;
85 }
86
87
88 MSG_ERROR_T MsgSubmitReqSMS(MSG_REQUEST_INFO_S *pReqInfo)
89 {
90         MSG_ERROR_T err = MSG_SUCCESS;
91
92         // submit request based on msgType;
93         MSG_MAIN_TYPE_T mainType = pReqInfo->msgInfo.msgType.mainType;
94         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
95
96         if (plg == NULL)
97         {
98                 THROW(MsgException::PLUGIN_ERROR, "No plugin for %d type", mainType);
99         }
100
101         MSG_REQUEST_INFO_S tmpInfo = {};
102
103         bool bReqSentStatus = false;
104
105         for (int i = 0; i < pReqInfo->msgInfo.nAddressCnt; i++)
106         {
107                 MsgCopyReqInfo(pReqInfo, i, &tmpInfo);
108
109                 // If MSG ID > 0 -> MSG in DRAFT
110                 // Move Folder to OUTBOX
111                 if (pReqInfo->msgInfo.msgPort.valid == false)
112                 {
113                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
114
115                         if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID))
116                         {
117                                 err = MsgStoUpdateMessage(&(tmpInfo.msgInfo), &(tmpInfo.sendOptInfo));
118                         }
119
120                         if (err != MSG_SUCCESS)
121                         {
122                                 MSG_DEBUG("MsgStoUpdateMessage() Fail");
123                                 continue;
124                         }
125                 }
126
127                 if (i == (pReqInfo->msgInfo.nAddressCnt -1))
128                 {
129                         // Request Sent Status Callback
130                         bReqSentStatus = true;
131                 }
132
133                 err = plg->submitReq(&tmpInfo, bReqSentStatus);
134
135                 if (err != MSG_SUCCESS)
136                 {
137                         break;
138                 }
139
140                 if (i == (pReqInfo->msgInfo.nAddressCnt -1))
141                 {
142                         // Copy MSG ID
143                         pReqInfo->msgInfo.msgId = tmpInfo.msgInfo.msgId;
144                 }
145         }
146
147         return err;
148 }
149
150
151 MSG_ERROR_T MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
152 {
153         MSG_ERROR_T err = MSG_SUCCESS;
154
155         MSG_RECIPIENTS_LIST_S pRecipientList;
156
157         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
158         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
159
160         if(!plg)
161         {
162                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
163                 MSG_DEBUG("No Plugin for %d type", msgMainType);
164
165                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
166         }
167
168         //      If MSG ID > 0 -> MSG in DRAFT
169         // Move Folder to OUTBOX
170         /* reject_msg_support */
171         if(!bScheduled)
172         {
173                 if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS)
174                 {
175                         char fileName[MAX_COMMON_INFO_SIZE+1] = {0};
176
177                         // copy whole of MMS PDU filepath to msgData
178                         strncpy(fileName, pReqInfo->msgInfo.msgData, MAX_COMMON_INFO_SIZE);
179                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
180                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, MSG_IPC_DATA_PATH"%s", fileName);
181
182                         MSG_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
183
184                         // submit request
185                         plg->submitReq(pReqInfo, false);
186
187                         if(err != MSG_SUCCESS)
188                         {
189                                 MSG_DEBUG("Update Network Status : [%d]", err);
190                                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo),MSG_NETWORK_SEND_FAIL);
191                         }
192
193                         return err;
194                 }
195                 else if((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS))
196                 {
197                         // update address list in the ase of existing message
198                         if(pReqInfo->msgInfo.msgId > 0)
199                         {
200                                 err = MsgStoGetOrgAddressList(&(pReqInfo->msgInfo));
201
202                                 if(err != MSG_SUCCESS)
203                                         MSG_DEBUG("[WARNING]MsgStoGetOrgAddressList returned not a MSG_SUCCESS");
204                         }
205
206                         if(pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
207                                 MSG_ADDRESS_INFO_S addrInfo = {0,};
208                                 int addrIdx = 0;
209
210                                 err = MsgStoGetAddrInfo(pReqInfo->msgInfo.msgId, &addrInfo);
211
212                                 if (err == MSG_SUCCESS) {
213                                         for (int i = 0; i < pReqInfo->msgInfo.nAddressCnt; i++) {
214                                                 //if (pReqInfo->msgInfo.addressList[i].threadId == addrInfo.threadId) {
215                                                 if (!strcmp(pReqInfo->msgInfo.addressList[i].addressVal, addrInfo.addressVal)) {
216                                                         addrIdx = i;
217                                                         MSG_DEBUG("addrIdx = %d, address = [%s]", addrIdx, pReqInfo->msgInfo.addressList[i].addressVal);
218                                                         break;
219                                                 }
220                                         }
221                                 } else {
222                                         MSG_DEBUG("[Error]MsgStoGetAddrInfo is failed");
223                                 }
224
225                                 pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
226                                 err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo), addrIdx);
227
228                                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
229                         } else {
230                                 //new message case
231                                 MSG_DEBUG("New Message");
232                                 for(int i = 0; i < pReqInfo->msgInfo.nAddressCnt; i++) {
233                                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
234                                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo), i);
235                                         //pReqInfo->msgInfo.msgId = 0;
236                                 }
237
238                                 if (pReqInfo->msgInfo.msgId != pReqInfo->msgInfo.referenceId) {
239                                         MSG_DEBUG("Add Multi-recipient Message");
240                                         //multi-recipient mms case
241                                         pReqInfo->msgInfo.msgId = pReqInfo->msgInfo.referenceId;
242                                 }
243                         }
244                 //      pReqInfo->msgInfo.msgId = pReqInfo->msgInfo.referenceId;
245                 }
246                 else if(pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS)
247                 {
248                         err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
249                         if(err != MSG_SUCCESS)
250                                 return err;
251
252                         err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
253                         if(err != MSG_SUCCESS)
254                                 return MSG_ERR_PLUGIN_STORAGE;
255
256                         pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
257
258                         for(int i = 0; i < pRecipientList.recipientCnt; i++)
259                         {
260                                 pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
261                                 pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
262                                 pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
263                                 strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
264                         }
265
266                         char subject[MAX_SUBJECT_LEN+1];
267
268                         err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
269                         if(err != MSG_SUCCESS)
270                                 MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
271
272                         strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
273
274                         err = plg->composeReadReport(&(pReqInfo->msgInfo));
275                 }
276                 else if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
277                 {
278                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
279                 }
280         }
281
282         /* reject_msg_support */
283
284         if(err != MSG_SUCCESS)
285         {
286                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
287                 return err;
288         }
289
290         switch(pReqInfo->msgInfo.msgType.subType)
291         {
292                 case MSG_SENDREQ_MMS:
293                 case MSG_FORWARD_MMS:
294                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
295                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
296                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d.mms", pReqInfo->msgInfo.referenceId);
297                         break;
298
299                 case MSG_READREPLY_MMS:
300                 case MSG_READRECIND_MMS:
301                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
302                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, MSG_DATA_PATH"%d.mms", pReqInfo->msgInfo.msgId);
303                         break;
304                 default:
305                         break;
306         }
307
308         // update content location from db
309         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
310                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
311         /* reject_msg_support */
312         else if(pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
313                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
314         /* reject_msg_support */
315
316         // Check SIM is present or not
317         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
318
319         if(simStatus == MSG_SIM_STATUS_NOT_FOUND)
320         {
321                 MSG_DEBUG("SIM is not present...");
322                 MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
323
324                 return MSG_ERR_NO_SIM;
325         }
326
327         if(err == MSG_SUCCESS)
328                 err = plg->submitReq(pReqInfo, false);
329
330         if(err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
331                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
332
333         if (err != MSG_SUCCESS)
334         {
335                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
336                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
337                 else
338                         MsgStoUpdateNetworkStatus(&(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
339         }
340
341
342         return err;
343 }
344
345
346 MSG_ERROR_T MsgCancelReq(MSG_REQUEST_ID_T reqId)
347 {
348         MSG_ERROR_T err = MSG_SUCCESS;
349
350         return err;
351 }
352
353
354 MSG_ERROR_T MsgUpdateSentMsg(MSG_MESSAGE_ID_T MsgId, MSG_NETWORK_STATUS_T Status)
355 {
356         MSG_ERROR_T err = MSG_SUCCESS;
357
358         bool bKeepCopy = true;
359
360         // Move Msg to SENTBOX
361         if (Status == MSG_NETWORK_SEND_SUCCESS)
362         {
363                 MSG_DEBUG(" In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
364                 if (bKeepCopy == true)
365                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
366                 else
367                         err = MsgStoDeleteMessage(MsgId, false);
368         }
369
370         return err;
371 }
372
373
374 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
375 {
376         MSG_BEGIN();
377
378         memset(pDest, 0x00, sizeof(pDest));
379
380         // Copy Request ID
381         pDest->reqId = pSrc->reqId;
382
383         // Copy Msg Info
384         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
385         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
386
387         pDest->msgInfo.referenceId = pSrc->msgInfo.referenceId;
388
389         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
390         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
391         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
392
393         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
394
395         pDest->msgInfo.nAddressCnt = 1;
396
397         pDest->msgInfo.addressList[0].threadId = pSrc->msgInfo.addressList[addrIdx].threadId;
398         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
399         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
400         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
401         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
402         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
403
404         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
405         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
406
407         pDest->msgInfo.scheduledTime = pSrc->msgInfo.scheduledTime;
408         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
409         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
410         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
411         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
412         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
413         pDest->msgInfo.priority = pSrc->msgInfo.priority;
414         pDest->msgInfo.direction = pSrc->msgInfo.direction;
415
416         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
417
418         if (pDest->msgInfo.msgPort.valid == true)
419         {
420                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
421                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
422         }
423
424         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
425         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
426
427         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
428
429         if (pDest->msgInfo.bTextSms == true)
430         {
431                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
432                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
433         }
434
435         // Set Sending Info
436         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
437
438         if (pDest->sendOptInfo.bSetting == true)
439         {
440                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
441                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
442
443                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE)
444                 {
445                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
446                 }
447                 else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE)
448                 {
449                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
450                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
451
452                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
453                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
454
455                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
456                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
457                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
458                 }
459         }
460
461         MSG_END();
462 }
463