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