Change license to APLv2.0
[platform/core/messaging/msg-service.git] / framework / submit-handler / MsgSubmitHandler.cpp
1 /*
2  * msg-service
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "MsgDebug.h"
24 #include "MsgUtilFile.h"
25 #include "MsgUtilStorage.h"
26 #include "MsgException.h"
27 #include "MsgGconfWrapper.h"
28 #include "MsgPluginManager.h"
29 #include "MsgStorageHandler.h"
30 #include "MsgSubmitHandler.h"
31
32
33 extern MsgDbHandler dbHandle;
34
35 /*==================================================================================================
36                                      FUNCTION IMPLEMENTATION
37 ==================================================================================================*/
38 msg_error_t MsgSubmitReq(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
39 {
40         msg_error_t err = MSG_SUCCESS;
41
42         static int reqId = 1;
43
44         pReqInfo->reqId = reqId;
45         reqId++;
46
47         MSG_DEBUG("==== Msg ID = [%d] ====", pReqInfo->msgInfo.msgId);
48         MSG_DEBUG("==== Folder ID = [%d] ====", pReqInfo->msgInfo.folderId);
49         MSG_DEBUG("==== Main Type = [%d] ====", pReqInfo->msgInfo.msgType.mainType);
50         MSG_DEBUG("==== Sub Type = [%d] ====", pReqInfo->msgInfo.msgType.subType);
51         MSG_DEBUG("==== Class Type = [%d] ====", pReqInfo->msgInfo.msgType.classType);
52         MSG_DEBUG("==== Message Data = [%s] ====", pReqInfo->msgInfo.msgData);
53         MSG_DEBUG("==== Message Text = [%s] ====", pReqInfo->msgInfo.msgText);
54
55         MSG_DEBUG("==== bSetting = [%d] ====", pReqInfo->sendOptInfo.bSetting);
56
57         if (pReqInfo->msgInfo.msgType.mainType == MSG_SMS_TYPE)
58         {
59                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
60                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
61                 MSG_DEBUG("==== bReplyPath = [%d] ====", pReqInfo->sendOptInfo.option.smsSendOptInfo.bReplyPath);
62
63                 err = MsgSubmitReqSMS(pReqInfo);
64         }
65         else if (pReqInfo->msgInfo.msgType.mainType == MSG_MMS_TYPE)
66         {
67                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
68                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
69                 MSG_DEBUG("==== bReadReq = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.bReadReq);
70                 MSG_DEBUG("==== priority = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.priority);
71                 MSG_DEBUG("==== expiryTime = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.expiryTime.time);
72
73                 err = MsgSubmitReqMMS(pReqInfo);
74         }
75
76         return err;
77 }
78
79
80 msg_error_t MsgSubmitReqSMS(MSG_REQUEST_INFO_S *pReqInfo)
81 {
82         msg_error_t err = MSG_SUCCESS;
83
84         // submit request based on msgType;
85         MSG_MAIN_TYPE_T mainType = pReqInfo->msgInfo.msgType.mainType;
86         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
87
88         if (plg == NULL) {
89                 MSG_DEBUG("No Plugin for %d type", mainType);
90                 return MSG_ERR_UNKNOWN;
91         }
92
93         // If MSG ID > 0 -> MSG in DRAFT
94         // Move Folder to OUTBOX
95         if (pReqInfo->msgInfo.msgPort.valid == false) {
96                 pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
97
98         if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID))
99                 err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
100         }
101
102         err = plg->submitReq(pReqInfo);
103
104         return err;
105 }
106
107
108 msg_error_t MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo)
109 {
110         msg_error_t err = MSG_SUCCESS;
111
112         MSG_RECIPIENTS_LIST_S pRecipientList;
113
114         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
115         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
116
117         if(!plg)
118         {
119                 MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
120                 MSG_DEBUG("No Plugin for %d type", msgMainType);
121
122                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
123         }
124
125         //      If MSG ID > 0 -> MSG in DRAFT
126         // Move Folder to OUTBOX
127         /* reject_msg_support */
128         MSG_DEBUG("Not scheduled MMS, pReqInfo->msgInfo.msgType.subType [%d]", pReqInfo->msgInfo.msgType.subType);
129
130         if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS)
131         {
132                 char fileName[MAX_COMMON_INFO_SIZE+1] = {0};
133
134                 // copy whole of MMS PDU filepath to msgData
135                 strncpy(fileName, pReqInfo->msgInfo.msgData, MAX_COMMON_INFO_SIZE);
136                 memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
137                 snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s/%s", MSG_IPC_DATA_PATH, fileName);
138
139                 MSG_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
140
141                 // submit request
142                 err = plg->submitReq(pReqInfo);
143
144                 if(err != MSG_SUCCESS)
145                 {
146                         MSG_DEBUG("Update Network Status : [%d]", err);
147                         MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo),MSG_NETWORK_SEND_FAIL);
148                 }
149
150                 return err;
151         }
152         else if((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS))
153         {
154                 // update address list in the ase of existing message
155                 if(pReqInfo->msgInfo.msgId > 0)
156                 {
157                         err = MsgStoGetOrgAddressList(&(pReqInfo->msgInfo));
158
159                         if(err != MSG_SUCCESS)
160                                 MSG_DEBUG("[WARNING]MsgStoGetOrgAddressList returned not a MSG_SUCCESS");
161                 }
162
163                 if(pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
164                         MSG_ADDRESS_INFO_S addrInfo = {0,};
165                         int addrIdx = 0;
166
167                         err = MsgStoGetAddrInfo(pReqInfo->msgInfo.msgId, &addrInfo);
168
169                         if (err == MSG_SUCCESS) {
170                                 for (int i = 0; i < pReqInfo->msgInfo.nAddressCnt; i++) {
171                                         //if (pReqInfo->msgInfo.addressList[i].threadId == addrInfo.threadId) {
172                                         if (!strcmp(pReqInfo->msgInfo.addressList[i].addressVal, addrInfo.addressVal)) {
173                                                 addrIdx = i;
174                                                 MSG_DEBUG("addrIdx = %d, address = [%s]", addrIdx, pReqInfo->msgInfo.addressList[i].addressVal);
175                                                 break;
176                                         }
177                                 }
178                         } else {
179                                 MSG_DEBUG("[Error]MsgStoGetAddrInfo is failed");
180                         }
181
182                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
183                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
184
185                         MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
186                 } else {
187                         //new message case
188                         MSG_DEBUG("New Message");
189                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
190                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
191                         //pReqInfo->msgInfo.msgId = 0;
192                 }
193         }
194         else if(pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS)
195         {
196                 err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
197                 if(err != MSG_SUCCESS)
198                         return err;
199
200                 err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
201                 if(err != MSG_SUCCESS)
202                         return MSG_ERR_PLUGIN_STORAGE;
203
204                 pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
205
206                 for(int i = 0; i < pRecipientList.recipientCnt; i++)
207                 {
208                         pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
209                         pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
210                         pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
211                         strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
212                 }
213
214                 char subject[MAX_SUBJECT_LEN+1];
215
216                 err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
217                 if(err != MSG_SUCCESS)
218                         MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
219
220                 strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
221
222                 err = plg->composeReadReport(&(pReqInfo->msgInfo));
223         }
224         else if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
225         {
226                 MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
227         }
228
229         /* reject_msg_support */
230
231         if(err != MSG_SUCCESS)
232         {
233                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
234                 return err;
235         }
236
237         switch(pReqInfo->msgInfo.msgType.subType)
238         {
239                 case MSG_SENDREQ_MMS:
240                 case MSG_FORWARD_MMS:
241                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
242                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
243                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s/%d.mms", MSG_DATA_PATH, pReqInfo->msgInfo.msgId);
244                         break;
245
246                 case MSG_READREPLY_MMS:
247                 case MSG_READRECIND_MMS:
248                         break;
249                 default:
250                         break;
251         }
252
253         // update content location from db
254         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
255                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
256         /* reject_msg_support */
257         else if(pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
258                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
259         /* reject_msg_support */
260
261         // Check SIM is present or not
262         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)MsgSettingGetInt(MSG_SIM_CHANGED);
263
264         if(simStatus == MSG_SIM_STATUS_NOT_FOUND)
265         {
266                 MSG_DEBUG("SIM is not present...");
267                 MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
268
269                 return MSG_ERR_NO_SIM;
270         }
271
272         if(err == MSG_SUCCESS)
273                 err = plg->submitReq(pReqInfo);
274
275         if(err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
276                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
277
278         if (err != MSG_SUCCESS)
279         {
280                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
281                         MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
282                 else
283                         MsgStoUpdateNetworkStatus(&dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
284         }
285
286
287         return err;
288 }
289
290
291 msg_error_t MsgCancelReq(msg_request_id_t reqId)
292 {
293         msg_error_t err = MSG_SUCCESS;
294
295         return err;
296 }
297
298
299 msg_error_t MsgUpdateSentMsg(msg_message_id_t MsgId, msg_network_status_t Status)
300 {
301         msg_error_t err = MSG_SUCCESS;
302
303         bool bKeepCopy = true;
304
305 #ifdef MSG_MMS_KEEPCOPY
306         MSG_SENDINGOPT_INFO_S sendOpt = {};
307
308         if (msgType.mainType == MSG_MMS_TYPE)
309         {
310                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
311                 {
312                         bKeepCopy = sendOpt.bKeepCopy;
313                 }
314                 else
315                 {
316                         ret = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
317                 }
318         }
319 #endif
320
321         // Move Msg to SENTBOX
322         if (Status == MSG_NETWORK_SEND_SUCCESS)
323         {
324                 MSG_DEBUG(" In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
325                 if (bKeepCopy == true)
326                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
327                 else
328                         err = MsgStoDeleteMessage(MsgId, false);
329         }
330
331         return err;
332 }
333
334
335 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
336 {
337         MSG_BEGIN();
338
339         memset(pDest, 0x00, sizeof(MSG_REQUEST_INFO_S));
340
341         // Copy Request ID
342         pDest->reqId = pSrc->reqId;
343
344         // Copy Msg Info
345         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
346
347         pDest->msgInfo.threadId = pSrc->msgInfo.threadId;
348
349         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
350
351         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
352         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
353         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
354
355         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
356
357         pDest->msgInfo.nAddressCnt = 1;
358
359         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
360         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
361         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
362         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
363         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
364
365         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
366         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
367
368         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
369         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
370         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
371         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
372         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
373         pDest->msgInfo.priority = pSrc->msgInfo.priority;
374         pDest->msgInfo.direction = pSrc->msgInfo.direction;
375
376         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
377
378         if (pDest->msgInfo.msgPort.valid == true)
379         {
380                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
381                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
382         }
383
384         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
385         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
386
387         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
388
389         if (pDest->msgInfo.bTextSms == true)
390         {
391                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
392                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
393         }
394
395         // Set Sending Info
396         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
397
398         if (pDest->sendOptInfo.bSetting == true)
399         {
400                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
401                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
402
403                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE)
404                 {
405                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
406                 }
407                 else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE)
408                 {
409                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
410                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
411
412                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
413                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
414
415                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
416                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
417                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
418                 }
419         }
420
421         MSG_END();
422 }
423