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