af8db272773ce6ef5edb0e9875b3bcfe2a304a03
[platform/framework/native/messaging.git] / src / FMsg_MsgUtil.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FMsg_MsgUtil.cpp
19  * @brief               This is the implementation file for the %_MsgUtil class.
20  *
21  * This file contains the implementation of the %_MsgUtil class.
22  */
23
24 // includes
25 #include <messages.h>
26 #include <email.h>
27 #include <FBaseUtilStringUtil.h>
28 #include <FIoFile.h>
29 #include <FMsgMmsMessage.h>
30 #include <FMsgSmsMessage.h>
31 #include <FMsgEmailMessage.h>
32 #include <FBaseSysLog.h>
33 #include <FBase_StringConverter.h>
34 #include "FMsg_SmsMessageImpl.h"
35 #include "FMsg_MsgUtil.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Io;
41
42 namespace Tizen { namespace Messaging
43 {
44
45 result
46 _MsgUtil::CheckAddressValidity(const Tizen::Base::String& address, _MessageType type)
47 {
48         result r = E_SUCCESS;
49         int addressLen = 0;
50
51         switch (type)
52         {
53         case _MSG_SMS:
54                 // fall through
55         case _MSG_MMS:
56         {
57                 addressLen = address.GetLength();
58                 if (address[0] == L'+')
59                 {
60                         addressLen--;
61                 }
62
63                 if (addressLen < MIN_LENGTH_PHONE || addressLen > MAX_LENGTH_PHONE)
64                 {
65                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The Address length (%d) is too short or too long.", addressLen);
66                         r = E_INVALID_ADDRESS;
67                         goto CATCH;
68                 }
69
70                 for (int index = 0; index < addressLen; index++)
71                 {
72                         if (address[0] == L'+')
73                         {
74                                 if (!Character::IsDigit(address[index + 1]))
75                                 {
76                                         r = E_INVALID_ADDRESS;
77                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The phone number contains a non-digit string.");
78                                         goto CATCH;
79                                 }
80                         }
81                         else
82                         {
83                                 if (!Character::IsDigit(address[index]))
84                                 {
85                                         r = E_INVALID_ADDRESS;
86                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The phone number contains a non-digit string.");
87                                         goto CATCH;
88                                 }
89                         }
90                 }
91
92                 break;
93         }
94
95         case _MSG_EMAIL:
96         {
97                 addressLen = address.GetLength();
98
99                 // size check
100                 if (addressLen < 5 || addressLen > MAX_LENGTH_EMAIL)      // "a@a.a"
101                 {
102                         r = E_INVALID_ADDRESS;
103                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The length (%d) of email address is too short or too long.", addressLen);
104                         goto CATCH;
105                 }
106
107                 // check number of '@' char and '@' position
108                 int pos = 0;
109                 int posByLast = 0;
110
111                 r = address.IndexOf(L'@', 0, pos);
112                 r = address.LastIndexOf(L'@', addressLen - 1, posByLast);
113
114                 if (pos <= 0 || pos != posByLast)
115                 {
116                         r = E_INVALID_ADDRESS;
117                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] There is not a @ character or there are multiple @ characters.");
118                         goto CATCH;
119                 }
120
121                 break;
122         }
123
124         default:
125                 r = E_INVALID_ARG;
126                 SysLogException(NID_MSG, r, "[E_INVALID_ARG] There is not a such type of address (%d).", type);
127                 return r;
128         }
129
130         return E_SUCCESS;
131
132 CATCH:
133         return r;
134 }
135
136 result
137 _MsgUtil::CheckEmailAddressValidity(const RecipientList& recipientList)
138 {
139         result r = E_SUCCESS;
140         RecipientList* pRecipientList = null;
141         IList* pToList = null;
142         IList* pCcList = null;
143         IList* pBccList = null;
144         int count = 0;
145         int index = 0;
146         String* pItem = null;
147
148         pRecipientList = const_cast< RecipientList* >(&recipientList);
149
150         // TO
151         count = 0;  // initialize the count value
152         pToList = pRecipientList->GetListN(RECIPIENT_TYPE_TO);
153         if (!pToList)
154         {
155                 r = GetLastResult();
156                 if (IsFailed(r))
157                 {
158                         SysLogException(NID_MSG, r, "[%s] Failed to get TO list.", GetErrorMessage(r));
159                         goto CATCH;
160                 }
161                 else
162                 {
163                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in TO list is not valid.");
164                         SysLog(
165                                 NID_MSG,
166                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_TO) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
167                         r = E_INVALID_ADDRESS;
168                         goto CATCH;
169                 }
170         }
171         else
172         {
173                 count = pToList->GetCount();
174         }
175
176         if (count > 0)
177         {
178                 for (index = 0; index < count; index++)
179                 {
180                         pItem = dynamic_cast< String* >(pToList->GetAt(index));
181                         if (pItem != null)
182                         {
183                                 r = CheckAddressValidity(*pItem, _MSG_EMAIL);
184                         }
185                         else
186                         {
187                                 r = E_INVALID_ADDRESS;
188
189                         }
190                         if (IsFailed(r))
191                         {
192                                 SysLogException(NID_MSG, r, "[%s] There is invalid email address in TO list. Please check the RecipientList first.",
193                                                                  GetErrorMessage(
194                                                                          r));
195                                 goto CATCH;
196                         }
197                 }
198         }
199
200         // CC
201         count = 0;  // initialize the count value
202         pCcList = pRecipientList->GetListN(RECIPIENT_TYPE_CC);
203         if (!pCcList)
204         {
205                 r = GetLastResult();
206                 if (IsFailed(r))
207                 {
208                         SysLogException(NID_MSG, r, "[%s] Failed to get CC list.", GetErrorMessage(r));
209                         goto CATCH;
210                 }
211                 else
212                 {
213                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in CC list is not valid.");
214                         SysLog(
215                                 NID_MSG,
216                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_CC) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
217                         r = E_INVALID_ADDRESS;
218                         goto CATCH;
219                 }
220         }
221         else
222         {
223                 count = pCcList->GetCount();
224         }
225
226         if (count > 0)
227         {
228                 for (index = 0; index < count; index++)
229                 {
230                         pItem = dynamic_cast< String* >(pCcList->GetAt(index));
231                         if (pItem != null)
232                         {
233                                 r = CheckAddressValidity(*pItem, _MSG_EMAIL);
234                         }
235                         else
236                         {
237                                 r = E_INVALID_ADDRESS;
238
239                         }
240
241                         if (IsFailed(r))
242                         {
243                                 SysLogException(NID_MSG, r, "[%s] There is invalid email address in CC list. Please check the RecipientList first.",
244                                                                  GetErrorMessage(
245                                                                          r));
246                                 goto CATCH;
247                         }
248                 }
249         }
250
251         // BCC
252         count = 0;  // initialize the count value
253         pBccList = pRecipientList->GetListN(RECIPIENT_TYPE_BCC);
254         if (!pBccList)
255         {
256                 r = GetLastResult();
257                 if (IsFailed(r))
258                 {
259                         SysLogException(NID_MSG, r, "[%s] Failed to get BCC list.", GetErrorMessage(r));
260                         goto CATCH;
261                 }
262                 else
263                 {
264                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in BCC list is not valid.");
265                         SysLog(
266                                 NID_MSG,
267                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_BCC) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
268                         r = E_INVALID_ADDRESS;
269                         goto CATCH;
270                 }
271         }
272         else
273         {
274                 count = pBccList->GetCount();
275         }
276
277         if (count > 0)
278         {
279                 for (index = 0; index < count; index++)
280                 {
281                         pItem = dynamic_cast< String* >(pBccList->GetAt(index));
282                         if (pItem != null)
283                         {
284                                 r = CheckAddressValidity(*pItem, _MSG_EMAIL);
285                         }
286                         else
287                         {
288                                 r = E_INVALID_ADDRESS;
289
290                         }
291
292                         if (IsFailed(r))
293                         {
294                                 SysLogException(NID_MSG, r, "[%s] There is invalid email address in BCC list. Please check the RecipientList first.",
295                                                                  GetErrorMessage(
296                                                                          r));
297                                 goto CATCH;
298                         }
299                 }
300         }
301
302         // free the memories
303         if (pToList)
304         {
305                 if (pToList->GetCount() > 0)
306                 {
307                         pToList->RemoveAll(true);
308                 }
309                 delete pToList;
310                 pToList = null;
311         }
312         if (pCcList)
313         {
314                 if (pCcList->GetCount() > 0)
315                 {
316                         pCcList->RemoveAll(true);
317                 }
318                 delete pCcList;
319                 pCcList = null;
320         }
321         if (pBccList)
322         {
323                 if (pBccList->GetCount() > 0)
324                 {
325                         pBccList->RemoveAll(true);
326                 }
327                 delete pBccList;
328                 pBccList = null;
329         }
330
331         return E_SUCCESS;
332
333 CATCH:
334         if (pToList)
335         {
336                 if (pToList->GetCount() > 0)
337                 {
338                         pToList->RemoveAll(true);
339                 }
340                 delete pToList;
341                 pToList = null;
342         }
343         if (pCcList)
344         {
345                 if (pCcList->GetCount() > 0)
346                 {
347                         pCcList->RemoveAll(true);
348                 }
349                 delete pCcList;
350                 pCcList = null;
351         }
352         if (pBccList)
353         {
354                 if (pBccList->GetCount() > 0)
355                 {
356                         pBccList->RemoveAll(true);
357                 }
358                 delete pBccList;
359                 pBccList = null;
360         }
361
362         return r;
363 }
364
365 result
366 _MsgUtil::CheckPhoneNumberValidity(_MessageType type, const RecipientList& recipientList)
367 {
368         result r = E_SUCCESS;
369         RecipientList* pRecipientList = null;
370         IList* pToList = null;
371         IList* pCcList = null;
372         IList* pBccList = null;
373         int count = 0;
374         int index = 0;
375         String* pItem = null;
376         int addressLen = 0;
377
378         pRecipientList = const_cast< RecipientList* >(&recipientList);
379
380         // TO
381         count = 0;  // initialize the count value
382         pToList = pRecipientList->GetListN(RECIPIENT_TYPE_TO);
383         if (!pToList)
384         {
385                 r = GetLastResult();
386                 if (IsFailed(r))
387                 {
388                         SysLogException(NID_MSG, r, "[%s] Failed to get TO list.", GetErrorMessage(r));
389                         goto CATCH;
390                 }
391                 else
392                 {
393                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in TO list is not valid.");
394                         SysLog(
395                                 NID_MSG,
396                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_TO) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
397                         r = E_INVALID_ADDRESS;
398                         goto CATCH;
399                 }
400         }
401         else
402         {
403                 count = pToList->GetCount();
404         }
405
406         if (count > 0)
407         {
408                 for (index = 0; index < count; index++)
409                 {
410                         pItem = dynamic_cast< String* >(pToList->GetAt(index));
411                         SysTryCatch(NID_MSG, pItem != null, r = E_INVALID_ADDRESS, r, "To List Null"); 
412                         r = CheckAddressValidity(*pItem, type);
413
414                         if (type == _MSG_MMS && r != E_SUCCESS)  //Check If MMS might have email address
415                         {
416                                 addressLen = pItem->GetLength();
417
418                                 // size check
419                                 if (addressLen < 5 || addressLen > MAX_LENGTH_EMAIL)      // "a@a.a"
420                                 {
421                                         r = E_INVALID_ADDRESS;
422                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The length (%d) of email address is too short or too long.", addressLen);
423                                         goto CATCH;
424                                 }
425
426                                 // check number of '@' char and '@' position
427                                 int pos = 0;
428                                 int posByLast = 0;
429
430                                 r = pItem->IndexOf(L'@', 0, pos);
431                                 r = pItem->LastIndexOf(L'@', addressLen - 1, posByLast);
432
433                                 if (pos <= 0 || pos != posByLast)
434                                 {
435                                         r = E_INVALID_ADDRESS;
436                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] There is not a @ character or there are multiple @ characters.");
437                                         goto CATCH;
438                                 }
439                         }
440
441                         if (IsFailed(r))
442                         {
443                                 SysLogException(NID_MSG, r, "[%s] There is invalid phone number in TO list. Please check the RecipientList first.",
444                                                                  GetErrorMessage(
445                                                                          r));
446                                 goto CATCH;
447                         }
448                 }
449         }
450
451         // CC
452         count = 0;  // initialize the count value
453         pCcList = pRecipientList->GetListN(RECIPIENT_TYPE_CC);
454         if (!pCcList)
455         {
456                 r = GetLastResult();
457                 if (IsFailed(r))
458                 {
459                         SysLogException(NID_MSG, r, "[%s] Failed to get CC list.", GetErrorMessage(r));
460                         goto CATCH;
461                 }
462                 else
463                 {
464                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in CC list is not valid.");
465                         SysLog(
466                                 NID_MSG,
467                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_CC) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
468                         r = E_INVALID_ADDRESS;
469                         goto CATCH;
470                 }
471         }
472         else
473         {
474                 count = pCcList->GetCount();
475         }
476
477         if (count > 0)
478         {
479                 for (index = 0; index < count; index++)
480                 {
481                         pItem = dynamic_cast< String* >(pCcList->GetAt(index));
482                         SysTryCatch(NID_MSG, pItem != null, r = E_INVALID_ADDRESS, r, "To List Null"); 
483                         r = CheckAddressValidity(*pItem, type);
484
485
486                         if (type == _MSG_MMS && r != E_SUCCESS)  //Check If MMS might have email address
487                         {
488                                 addressLen = pItem->GetLength();
489
490                                 // size check
491                                 if (addressLen < 5 || addressLen > MAX_LENGTH_EMAIL)      // "a@a.a"
492                                 {
493                                         r = E_INVALID_ADDRESS;
494                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The length (%d) of email address is too short or too long.", addressLen);
495                                         goto CATCH;
496                                 }
497
498                                 // check number of '@' char and '@' position
499                                 int pos = 0;
500                                 int posByLast = 0;
501
502                                 r = pItem->IndexOf(L'@', 0, pos);
503                                 r = pItem->LastIndexOf(L'@', addressLen - 1, posByLast);
504
505                                 if (pos <= 0 || pos != posByLast)
506                                 {
507                                         r = E_INVALID_ADDRESS;
508                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] There is not a @ character or there are multiple @ characters.");
509                                         goto CATCH;
510                                 }
511                         }
512
513                         if (IsFailed(r))
514                         {
515                                 SysLogException(NID_MSG, r, "[%s] There is invalid phone number in CC list. Please check the RecipientList first.",
516                                                                  GetErrorMessage(
517                                                                          r));
518                                 goto CATCH;
519                         }
520                 }
521         }
522
523         // BCC
524         count = 0;  // initialize the count value
525         pBccList = pRecipientList->GetListN(RECIPIENT_TYPE_BCC);
526         if (!pBccList)
527         {
528                 r = GetLastResult();
529                 if (IsFailed(r))
530                 {
531                         SysLogException(NID_MSG, r, "[%s] Failed to get BCC list.", GetErrorMessage(r));
532                         goto CATCH;
533                 }
534                 else
535                 {
536                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] One of the address in BCC list is not valid.");
537                         SysLog(
538                                 NID_MSG,
539                                 "Strange!! RecipientList::GetListN(RECIPIENT_TYPE_BCC) returns null. But the last result (by GetLastResult) is E_SUCCESS. So just return E_INVALID_ADDRESS.");
540                         r = E_INVALID_ADDRESS;
541                         goto CATCH;
542                 }
543         }
544         else
545         {
546                 count = pBccList->GetCount();
547         }
548
549         if (count > 0)
550         {
551                 for (index = 0; index < count; index++)
552                 {
553                         pItem = dynamic_cast< String* >(pBccList->GetAt(index));
554                         SysTryCatch(NID_MSG, pItem != null, r = E_INVALID_ADDRESS, r, "To List Null"); 
555                         r = CheckAddressValidity(*pItem, type);
556
557
558                         if (type == _MSG_MMS && r != E_SUCCESS)  //Check If MMS might have email address
559                         {
560                                 addressLen = pItem->GetLength();
561
562                                 // size check
563                                 if (addressLen < 5 || addressLen > MAX_LENGTH_EMAIL)      // "a@a.a"
564                                 {
565                                         r = E_INVALID_ADDRESS;
566                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] The length (%d) of email address is too short or too long.", addressLen);
567                                         goto CATCH;
568                                 }
569
570                                 // check number of '@' char and '@' position
571                                 int pos = 0;
572                                 int posByLast = 0;
573
574                                 r = pItem->IndexOf(L'@', 0, pos);
575                                 r = pItem->LastIndexOf(L'@', addressLen - 1, posByLast);
576
577                                 if (pos <= 0 || pos != posByLast)
578                                 {
579                                         r = E_INVALID_ADDRESS;
580                                         SysLogException(NID_MSG, r, "[E_INVALID_ADDRESS] There is not a @ character or there are multiple @ characters.");
581                                         goto CATCH;
582                                 }
583                         }
584
585                         if (IsFailed(r))
586                         {
587                                 SysLogException(NID_MSG, r, "[%s] There is invalid phone number in BCC list. Please check the RecipientList first.",
588                                                                  GetErrorMessage(
589                                                                          r));
590                                 goto CATCH;
591                         }
592                 }
593         }
594
595         // free the memories
596         if (pToList)
597         {
598                 if (pToList->GetCount() > 0)
599                 {
600                         pToList->RemoveAll(true);
601                 }
602                 delete pToList;
603                 pToList = null;
604         }
605         if (pCcList)
606         {
607                 if (pCcList->GetCount() > 0)
608                 {
609                         pCcList->RemoveAll(true);
610                 }
611                 delete pCcList;
612                 pCcList = null;
613         }
614         if (pBccList)
615         {
616                 if (pBccList->GetCount() > 0)
617                 {
618                         pBccList->RemoveAll(true);
619                 }
620                 delete pBccList;
621                 pBccList = null;
622         }
623
624         return E_SUCCESS;
625
626 CATCH:
627         if (pToList)
628         {
629                 if (pToList->GetCount() > 0)
630                 {
631                         pToList->RemoveAll(true);
632                 }
633                 delete pToList;
634                 pToList = null;
635         }
636         if (pCcList)
637         {
638                 if (pCcList->GetCount() > 0)
639                 {
640                         pCcList->RemoveAll(true);
641                 }
642                 delete pCcList;
643                 pCcList = null;
644         }
645         if (pBccList)
646         {
647                 if (pBccList->GetCount() > 0)
648                 {
649                         pBccList->RemoveAll(true);
650                 }
651                 delete pBccList;
652                 pBccList = null;
653         }
654
655         return r;
656 }
657
658 bool
659 _MsgUtil::IsValidImageFile(const String& filePath)
660 {
661         bool isValid = false;
662
663         String delimiter(L";");
664         String fileExtensionTable(L"jpg;jpeg;gif;png;bmp;wbmp;tif;tiff;swf;svg;svgz");
665         String fileExtension = Tizen::Io::File::GetFileExtension(filePath);
666         Tizen::Base::Utility::StringTokenizer strTok(fileExtensionTable, delimiter);
667         String token;
668
669         while (strTok.HasMoreTokens())
670         {
671                 strTok.GetNextToken(token);
672                 if (token.Equals(fileExtension, false))
673                 {
674                         isValid = true;
675                         break;
676                 }
677         }
678
679         return isValid;
680 }
681
682 bool
683 _MsgUtil::IsValidAudioFile(const String& filePath)
684 {
685         bool isValid = false;
686
687         String delimiter(L";");
688         String fileExtensionTable(L"mid;midi;spm;imy;amr;mmf;xmf;mxmf;3gp;3gpp;mp4;asf;ra;ram;rv;rm;mp3;aac;wav;3ga;m4a;wma");
689         String fileExtension = Tizen::Io::File::GetFileExtension(filePath);
690         Tizen::Base::Utility::StringTokenizer strTok(fileExtensionTable, delimiter);
691         String token;
692
693         while (strTok.HasMoreTokens())
694         {
695                 strTok.GetNextToken(token);
696                 if (token.Equals(fileExtension, false))
697                 {
698                         isValid = true;
699                         break;
700                 }
701         }
702
703         return isValid;
704 }
705
706 bool
707 _MsgUtil::IsValidVideoFile(const String& filePath)
708 {
709
710         bool isValid = false;
711
712         String delimiter(L";");
713         String fileExtensionTable(L"3gp;3gpp;mp4;asf;wmv;ra;ram;rv;rm");
714         String fileExtension = Tizen::Io::File::GetFileExtension(filePath);
715         Tizen::Base::Utility::StringTokenizer strTok(fileExtensionTable, delimiter);
716         String token;
717
718         while (strTok.HasMoreTokens())
719         {
720                 strTok.GetNextToken(token);
721                 if (token.Equals(fileExtension, false))
722                 {
723                         isValid = true;
724                         break;
725                 }
726         }
727
728         return isValid;
729 }
730
731
732 bool
733 _MsgUtil::IsValidVcardFile(const String& filePath)
734 {
735         bool isValid = false;
736         String fileExtension = Tizen::Io::File::GetFileExtension(filePath);
737
738         if (fileExtension.Equals(L"vcf", false))
739         {
740                 isValid = true;
741         }
742
743         return isValid;
744 }
745
746 bool
747 _MsgUtil::IsValidVcalendarFile(const String& filePath)
748 {
749         bool isValid = false;
750         String fileExtension = Tizen::Io::File::GetFileExtension(filePath);
751
752         if (fileExtension.Equals(L"vcs", false))
753         {
754                 isValid = true;
755         }
756
757         return isValid;
758 }
759
760 bool
761 _MsgUtil::CheckMediaFormat(const MmsAttachmentFormat format, const String& filePath)
762 {
763         bool isValid = false;
764         switch (format)
765         {
766         case MMS_IMAGE:
767                 isValid = IsValidImageFile(filePath);
768                 break;
769
770         case MMS_AUDIO:
771                 isValid = IsValidAudioFile(filePath);
772                 break;
773
774         case MMS_VIDEO:
775                 isValid = IsValidVideoFile(filePath);
776                 break;
777
778         case MMS_VCARD:
779                 isValid = IsValidVcardFile(filePath);
780                 break;
781
782         case MMS_VCALENDAR:
783                 isValid = IsValidVcalendarFile(filePath);
784                 break;
785
786         default:
787                 break;
788         }
789         return isValid;
790 }
791
792 bool
793 _MsgUtil::IsValidPhoneNumber(const Tizen::Base::String& phoneNumber)
794 {
795         int length = 0;
796
797         length = phoneNumber.GetLength();
798         if (phoneNumber[0] == L'+')
799         {
800                 length--;
801         }
802
803         if (length < MIN_LENGTH_PHONE || length > MAX_LENGTH_PHONE)
804         {
805                 SysLog(NID_MSG, "The length of the phone number (%d) is too short or too long.", length);
806                 return false;
807         }
808
809         for (int index = 0; index < length; index++)
810         {
811                 if (phoneNumber[0] == L'+')
812                 {
813                         if (!Character::IsDigit(phoneNumber[index + 1]))
814                         {
815                                 SysLog(NID_MSG, "The phone number contains a non-digit string.");
816                                 return false;
817                         }
818                 }
819                 else
820                 {
821                         if (!Character::IsDigit(phoneNumber[index]))
822                         {
823                                 SysLog(NID_MSG, "The phone number contains a non-digit string.");
824                                 return false;
825                         }
826                 }
827         }
828
829         return true;
830 }
831
832 SmsMessageBoxType
833 _MsgUtil::GetSmsMessageBoxType(messages_message_box_e folderId)
834 {
835         SmsMessageBoxType type = SMS_MESSAGE_BOX_TYPE_NONE;
836         switch (folderId)
837         {
838         case MESSAGES_MBOX_INBOX:
839                 type = SMS_MESSAGE_BOX_TYPE_INBOX;
840                 break;
841
842         case MESSAGES_MBOX_OUTBOX:
843                 type = SMS_MESSAGE_BOX_TYPE_OUTBOX;
844                 break;
845
846         case MESSAGES_MBOX_SENTBOX:
847                 type = SMS_MESSAGE_BOX_TYPE_SENTBOX;
848                 break;
849
850 //      case MESSAGES_MBOX_ALL:
851 //              type = SMS_MESSAGE_BOX_TYPE_ALL;
852 //              break;
853
854         default:
855                 type = SMS_MESSAGE_BOX_TYPE_NONE;
856                 break;
857         }
858
859         return type;
860 }
861
862 messages_message_box_e
863 _MsgUtil::GetMsgFolderId(const SmsMessageBoxType& type)
864 {
865         messages_message_box_e folderId;
866
867         switch (type)
868         {
869         case SMS_MESSAGE_BOX_TYPE_INBOX:
870                 folderId = MESSAGES_MBOX_INBOX;
871                 break;
872
873         case SMS_MESSAGE_BOX_TYPE_OUTBOX:
874                 folderId = MESSAGES_MBOX_OUTBOX;
875                 break;
876
877         case SMS_MESSAGE_BOX_TYPE_SENTBOX:
878                 folderId = MESSAGES_MBOX_SENTBOX;
879                 break;
880
881         case SMS_MESSAGE_BOX_TYPE_ALL:
882                 folderId = MESSAGES_MBOX_ALL;
883                 break;
884
885         default:
886                 folderId = (messages_message_box_e) -1;
887                 break;
888         }
889
890         return folderId;
891 }
892
893 SmsMessageBoxType
894 _MsgUtil::GetMsgBoxType(const messages_message_box_e& folderId)
895 {
896         SmsMessageBoxType type;
897
898         switch (folderId)
899         {
900         case MESSAGES_MBOX_INBOX:
901                 type = SMS_MESSAGE_BOX_TYPE_INBOX;
902                 break;
903
904         case MESSAGES_MBOX_OUTBOX:
905                 type = SMS_MESSAGE_BOX_TYPE_OUTBOX;
906                 break;
907
908         case MESSAGES_MBOX_SENTBOX:
909                 type = SMS_MESSAGE_BOX_TYPE_SENTBOX;
910                 break;
911
912         case MESSAGES_MBOX_ALL:
913                 type = SMS_MESSAGE_BOX_TYPE_ALL;
914                 break;
915
916         default:
917                 type = SMS_MESSAGE_BOX_TYPE_NONE;
918                 break;
919         }
920
921         return type;
922 }
923
924 int
925 _MsgUtil::GetSmsMessage(const SmsMessage& message, const RecipientList& recipientList, messages_message_h& smsMsg)
926 {
927         int err = MESSAGES_ERROR_NONE;
928         const char* pMsgBody = null;
929         result r = E_SUCCESS;
930
931         //create new message
932         err = messages_create_message(MESSAGES_TYPE_SMS, &smsMsg);
933         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to create message object");
934
935         if (message.GetText().GetLength() == 0)
936         {
937                 Tizen::Base::String emptyText(L" ");
938                 pMsgBody = _StringConverter::CopyToCharArrayN(emptyText);
939         }
940         else
941         {
942                 pMsgBody = _StringConverter::CopyToCharArrayN(message.GetText());
943         }
944
945         SysTryCatch(NID_MSG, pMsgBody != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "memory allocation failed.");
946
947         err = messages_set_text(smsMsg, pMsgBody);
948         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set message text");
949
950         err = AddMessageAddress(recipientList, RECIPIENT_TYPE_TO, smsMsg, MESSAGES_RECIPIENT_TO);
951         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set message To address");
952
953         if (pMsgBody)
954         {
955                 delete[] pMsgBody;
956                 pMsgBody = null;
957         }
958
959         return err;
960
961 CATCH:
962
963         if (pMsgBody)
964         {
965                 delete[] pMsgBody;
966                 pMsgBody = null;
967         }
968
969         return err;
970 }
971
972 int
973 _MsgUtil::GetEmailMessage(const EmailMessage& message, const RecipientList& recipientList, email_h& emailMsg)
974 {
975         int err = EMAIL_ERROR_NONE;
976         result r = E_SUCCESS;
977         int attachNum = 0;
978         const char* pSubject = null;
979         const char* pMsgBody = null;
980         Tizen::Base::Collection::IList* pAttachmentList = null;
981
982         pSubject = _StringConverter::CopyToCharArrayN(message.GetSubject());
983         pMsgBody = _StringConverter::CopyToCharArrayN(message.GetText());
984
985         err = email_set_subject(emailMsg, pSubject);
986         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add subject failed");
987         err = email_set_body(emailMsg, pMsgBody);
988         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add body failed");
989
990         err = AddEmailMessageAddress(recipientList, RECIPIENT_TYPE_TO, emailMsg, EMAIL_RECIPIENT_TYPE_TO);
991         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add recipient failed");
992         err = AddEmailMessageAddress(recipientList, RECIPIENT_TYPE_CC, emailMsg, EMAIL_RECIPIENT_TYPE_CC);
993         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add recipient failed");
994         err = AddEmailMessageAddress(recipientList, RECIPIENT_TYPE_BCC, emailMsg, EMAIL_RECIPIENT_TYPE_BCC);
995         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add recipient failed");
996
997         pAttachmentList = message.GetAttachmentListN();
998
999         if (pAttachmentList)
1000         {
1001                 attachNum = pAttachmentList->GetCount();
1002         }
1003
1004         if (attachNum >= 1)
1005         {
1006                 String* pAttachPath = null;
1007                 char* pTempAttachPath = null;
1008
1009                 for (int index = 0; index < attachNum; index++)
1010                 {
1011                         pAttachPath = dynamic_cast< String* >(pAttachmentList->GetAt(index));
1012                         SysTryCatch(NID_MSG, pAttachPath != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add attachment failed");                    
1013                         pTempAttachPath = _StringConverter::CopyToCharArrayN(*pAttachPath);
1014                         err = email_add_attach(emailMsg, pTempAttachPath);
1015                         if (pTempAttachPath)
1016                         {
1017                                 delete[] pTempAttachPath;
1018                                 pTempAttachPath = null;
1019                         }
1020                         SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] email add attachment failed");
1021                 }
1022         }
1023
1024         if (pAttachmentList)
1025         {
1026                 if (pAttachmentList->GetCount() > 0)
1027                 {
1028                         pAttachmentList->RemoveAll(true);
1029                 }
1030                 delete pAttachmentList;
1031                 pAttachmentList = null;
1032         }
1033
1034         if (pMsgBody)
1035         {
1036                 delete[] pMsgBody;
1037                 pMsgBody = null;
1038         }
1039
1040         if (pSubject)
1041         {
1042                 delete[] pSubject;
1043                 pSubject = null;
1044         }
1045
1046         return err;
1047
1048 CATCH:
1049         if (pMsgBody)
1050         {
1051                 delete[] pMsgBody;
1052                 pMsgBody = null;
1053         }
1054
1055         if (pSubject)
1056         {
1057                 delete[] pSubject;
1058                 pSubject = null;
1059         }
1060
1061         if (pAttachmentList)
1062         {
1063                 if (pAttachmentList->GetCount() > 0)
1064                 {
1065                         pAttachmentList->RemoveAll(true);
1066                 }
1067                 delete pAttachmentList;
1068                 pAttachmentList = null;
1069         }
1070
1071         return err;
1072 }
1073
1074 int
1075 _MsgUtil::AddEmailMessageAddress(const RecipientList& recipientList, RecipientType type, email_h& emailMsg, email_recipient_type_e recipientType)
1076 {
1077         String* pTempRecipient = null;
1078         IList* pRecipientList = null;
1079         char* pEmailAddr = null;
1080         int count = 0;
1081         int err = EMAIL_ERROR_NONE;
1082         result r = E_SUCCESS;
1083
1084         pRecipientList = (const_cast< RecipientList& >(recipientList)).GetListN(type);
1085         if (!pRecipientList)
1086         {
1087                 r = GetLastResult();
1088                 SysLogException(NID_MSG, r, "[%s] Failed to get recipient list", GetErrorMessage(r));
1089                 goto CATCH;
1090         }
1091
1092         count = pRecipientList->GetCount();
1093         for (int index = 0; index < count; index++)
1094         {
1095                 pTempRecipient = dynamic_cast< String* >(pRecipientList->GetAt(index));
1096                 if (!pTempRecipient)
1097                 {
1098                 r = GetLastResult();
1099                 err = EMAIL_ERROR_OPERATION_FAILED;
1100                 SysLogException(NID_MSG, r, "[%s] Failed to get a recipient", GetErrorMessage(r));
1101                 goto CATCH;
1102                 }
1103
1104                 pEmailAddr = _StringConverter::CopyToCharArrayN(*pTempRecipient);
1105                 err = email_add_recipient(emailMsg, recipientType, pEmailAddr);
1106
1107                 if (pEmailAddr)
1108                 {
1109                         delete[] pEmailAddr;
1110                         pEmailAddr = null;
1111                 }
1112                 SysTryCatch(NID_MSG, err == EMAIL_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set message address");
1113
1114         }
1115
1116         // deallocate
1117         if (pRecipientList->GetCount() > 0)
1118         {
1119                 pRecipientList->RemoveAll(true);
1120         }
1121         delete pRecipientList;
1122         pRecipientList = null;
1123
1124         return err;
1125
1126 CATCH:
1127         SetLastResult(r);
1128         if (pRecipientList)
1129         {
1130                 if (pRecipientList->GetCount() > 0)
1131                 {
1132                         pRecipientList->RemoveAll(true);
1133                 }
1134                 delete pRecipientList;
1135                 pRecipientList = null;
1136         }
1137
1138         return err;
1139 }
1140
1141 int
1142 _MsgUtil::AddMessageAddress(const RecipientList& recipientList, RecipientType type, messages_message_h& messageMsg, messages_recipient_type_e recipientType)
1143 {
1144         String* pTempRecipient = null;
1145         IList* pRecipientList = null;
1146         char* pPhoneNum = null;
1147         int count = 0;
1148         int err = MESSAGES_ERROR_NONE;
1149         result r = E_SUCCESS;
1150
1151         pRecipientList = (const_cast< RecipientList& >(recipientList)).GetListN(type);
1152         if (!pRecipientList)
1153         {
1154                 r = GetLastResult();
1155                 SysLogException(NID_MSG, r, "[%s] Failed to get recipient list", GetErrorMessage(r));
1156                 goto CATCH;
1157         }
1158
1159         count = pRecipientList->GetCount();
1160         for (int index = 0; index < count; index++)
1161         {
1162                 pTempRecipient = dynamic_cast< String* >(pRecipientList->GetAt(index));
1163                 if (!pTempRecipient)
1164                 {
1165                         r = GetLastResult();
1166                         err = MESSAGES_ERROR_OPERATION_FAILED;
1167                         SysLogException(NID_MSG, r, "[%s] Failed to get a recipient", GetErrorMessage(r));
1168                         goto CATCH;
1169                 }
1170
1171                 pPhoneNum = _StringConverter::CopyToCharArrayN(*pTempRecipient);
1172                 err = messages_add_address(messageMsg, pPhoneNum, recipientType);
1173
1174                 if (pPhoneNum)
1175                 {
1176                         delete[] pPhoneNum;
1177                         pPhoneNum = null;
1178                 }
1179                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set message address");
1180
1181         }
1182
1183         // deallocate
1184         if (pRecipientList->GetCount() > 0)
1185         {
1186                 pRecipientList->RemoveAll(true);
1187         }
1188         delete pRecipientList;
1189         pRecipientList = null;
1190
1191         return err;
1192
1193 CATCH:
1194         SetLastResult(r);
1195         if (pRecipientList)
1196         {
1197                 if (pRecipientList->GetCount() > 0)
1198                 {
1199                         pRecipientList->RemoveAll(true);
1200                 }
1201                 delete pRecipientList;
1202                 pRecipientList = null;
1203         }
1204
1205         return err;
1206 }
1207
1208 Tizen::Base::String
1209 _MsgUtil::GetConcatenatedRecipientListString(const RecipientList& recipientList, RecipientType type,
1210                                                                                          const Tizen::Base::String& delim)
1211 {
1212         String concatenatedString;
1213         String* pTempRecipient = null;
1214         IList* pRecipientList = null;
1215         int count = 0;
1216         result r = E_SUCCESS;
1217
1218         ClearLastResult();
1219
1220         pRecipientList = (const_cast< RecipientList& >(recipientList)).GetListN(type);
1221         if (!pRecipientList)
1222         {
1223                 r = GetLastResult();
1224                 SysLogException(NID_MSG, r, "[%s] Failed to get recipient list", GetErrorMessage(r));
1225                 goto CATCH;
1226         }
1227
1228         concatenatedString.Clear();
1229
1230         count = pRecipientList->GetCount();
1231         for (int index = 0; index < count; index++)
1232         {
1233                 pTempRecipient = dynamic_cast< String* >(pRecipientList->GetAt(index));
1234                 if (!pTempRecipient)
1235                 {
1236                         r = GetLastResult();
1237                         SysLogException(NID_MSG, r, "[%s] Failed to get a recipient", GetErrorMessage(r));
1238                         goto CATCH;
1239                 }
1240
1241                 concatenatedString.Append(*pTempRecipient);
1242                 if (index < count - 1)
1243                 {
1244                         concatenatedString.Append(delim);
1245                 }
1246         }
1247
1248         // deallocate
1249         if (pRecipientList->GetCount() > 0)
1250         {
1251                 pRecipientList->RemoveAll(true);
1252         }
1253         delete pRecipientList;
1254         pRecipientList = null;
1255
1256         return concatenatedString;
1257
1258 CATCH:
1259         SetLastResult(r);
1260         // deallocate
1261         if (pRecipientList)
1262         {
1263                 if (pRecipientList->GetCount() > 0)
1264                 {
1265                         pRecipientList->RemoveAll(true);
1266                 }
1267                 delete pRecipientList;
1268                 pRecipientList = null;
1269         }
1270
1271         return concatenatedString;
1272 }
1273
1274 int
1275 _MsgUtil::GetMmsMessage(const MmsMessage& message, const RecipientList& recipientList, messages_message_h& mmsMsg)
1276 {
1277         int err = MESSAGES_ERROR_NONE;
1278         result r = E_SUCCESS;
1279         const char* pSubject = null;
1280         const char* pMsgBody = null;
1281         const char* pFilePath = null;
1282         String filePath;
1283
1284         pSubject = _StringConverter::CopyToCharArrayN(message.GetSubject());
1285         pMsgBody = _StringConverter::CopyToCharArrayN(message.GetText());
1286
1287         //create new message
1288         err = messages_create_message(MESSAGES_TYPE_MMS, &mmsMsg);
1289         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Insufficient memory");
1290
1291         err = messages_set_text(mmsMsg, pMsgBody);
1292         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set mms text");
1293
1294         err = messages_mms_set_subject(mmsMsg, pSubject);
1295         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to set mms body");
1296
1297         err = AddMessageAddress(recipientList, RECIPIENT_TYPE_TO, mmsMsg, MESSAGES_RECIPIENT_TO);
1298         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to add To address");
1299
1300         err = AddMessageAddress(recipientList, RECIPIENT_TYPE_CC, mmsMsg, MESSAGES_RECIPIENT_CC);
1301         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to add Cc address");
1302
1303         err = AddMessageAddress(recipientList, RECIPIENT_TYPE_BCC, mmsMsg, MESSAGES_RECIPIENT_BCC);
1304         SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "failed to add Bcc address");
1305
1306         // populate attachments
1307         filePath = message.GetAttachment(MMS_IMAGE);
1308         if (filePath.GetLength())
1309         {
1310                 pFilePath = _StringConverter::CopyToCharArrayN(filePath);
1311                 err = messages_mms_add_attachment(mmsMsg, MESSAGES_MEDIA_IMAGE, pFilePath);
1312                 delete[] pFilePath;
1313                 pFilePath = null;
1314                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "mms add attachment failed");
1315         }
1316
1317         //video attachment
1318         filePath = message.GetAttachment(MMS_VIDEO);
1319         if (filePath.GetLength())
1320         {
1321                 pFilePath = _StringConverter::CopyToCharArrayN(filePath);
1322                 err = messages_mms_add_attachment(mmsMsg, MESSAGES_MEDIA_VIDEO, pFilePath);
1323                 delete[] pFilePath;
1324                 pFilePath = null;
1325                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "mms add attachment failed");
1326         }
1327
1328         //audio attachment
1329         filePath = message.GetAttachment(MMS_AUDIO);
1330         if (filePath.GetLength())
1331         {
1332                 pFilePath = _StringConverter::CopyToCharArrayN(filePath);
1333                 err = messages_mms_add_attachment(mmsMsg, MESSAGES_MEDIA_AUDIO, pFilePath);
1334                 delete[] pFilePath;
1335                 pFilePath = null;
1336                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "mms add attachment failed");
1337         }
1338
1339         //vcard attachment
1340         filePath = message.GetAttachment(MMS_VCARD);
1341         if (filePath.GetLength())
1342         {
1343                 pFilePath = _StringConverter::CopyToCharArrayN(filePath);
1344                 err = messages_mms_add_attachment(mmsMsg, MESSAGES_MEDIA_UNKNOWN, pFilePath); // Not Suppourted Yet
1345                 delete[] pFilePath;
1346                 pFilePath = null;
1347                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "mms add attachment failed");
1348         }
1349
1350         //calander attachment
1351         filePath = message.GetAttachment(MMS_VCALENDAR);
1352         if (filePath.GetLength())
1353         {
1354                 pFilePath = _StringConverter::CopyToCharArrayN(filePath);
1355                 err = messages_mms_add_attachment(mmsMsg, MESSAGES_MEDIA_UNKNOWN, pFilePath); // Not Suppourted Yet
1356                 delete[] pFilePath;
1357                 pFilePath = null;
1358                 SysTryCatch(NID_MSG, err == MESSAGES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "mms add attachment failed");
1359         }
1360
1361         if (pMsgBody)
1362         {
1363                 delete[] pMsgBody;
1364                 pMsgBody = null;
1365         }
1366
1367         if (pSubject)
1368         {
1369                 delete[] pSubject;
1370                 pSubject = null;
1371         }
1372
1373         return err;
1374
1375 CATCH:
1376
1377         if (pMsgBody)
1378         {
1379                 delete[] pMsgBody;
1380                 pMsgBody = null;
1381         }
1382
1383         if (pSubject)
1384         {
1385                 delete[] pSubject;
1386                 pSubject = null;
1387         }
1388
1389         return err;
1390 }
1391
1392 Tizen::Base::DateTime
1393 _MsgUtil::ConvertTime(time_t* pTime)
1394 {
1395         DateTime dateTime;
1396         struct tm* pTempTime = null;
1397
1398         pTempTime = gmtime(pTime);
1399         if (pTempTime)
1400         {
1401                 dateTime.SetValue(pTempTime->tm_year + 1900, pTempTime->tm_mon + 1, pTempTime->tm_mday, pTempTime->tm_hour, pTempTime->tm_min, pTempTime->tm_sec);
1402         }
1403         return dateTime;
1404 }
1405
1406 SmsMessage*
1407 _MsgUtil::ConvertSmsMessageN(const SmsMessageBoxType& type, messages_message_h msg)
1408 {
1409         result r = E_SUCCESS;
1410         SmsMessage* pSmsMessage = null;
1411         char* pOriginalText = null;
1412         char* pBodyText = null;
1413         char* pMsgAddress = null;
1414         DateTime time;
1415         time_t tempTime = 0;
1416         bool hasMoreText = false;
1417         int messageId = 0;
1418         String body;
1419         messages_recipient_type_e recipientType = MESSAGES_RECIPIENT_TO;
1420         const int msgBodyLength = 160;
1421         int originalTextLength = 0;
1422
1423         pSmsMessage = new (std::nothrow) SmsMessage();
1424         SysTryCatch(NID_MSG, pSmsMessage != null, r = E_OUT_OF_MEMORY, r, "memory allocation failed");
1425
1426         // message ID
1427         messages_get_message_id(msg, &messageId);
1428         // body text
1429         messages_get_text(msg, &pOriginalText);
1430         SysTryCatch(NID_MSG, pOriginalText != null, r = E_SYSTEM, r, "sms get message body failed");
1431
1432         originalTextLength = strlen(pOriginalText);
1433         if (originalTextLength > msgBodyLength)
1434         {
1435                 pBodyText = (char*) calloc(msgBodyLength + 1, sizeof(char));   // include NULL character
1436                 SysTryCatch(NID_MSG, pBodyText != null, r = E_OUT_OF_MEMORY, r, "memory allocation failed");
1437                 strncpy(pBodyText, pOriginalText, msgBodyLength);
1438                 pBodyText[msgBodyLength] = '\0';  // add NULL character
1439                 free(const_cast< char* >(pOriginalText));
1440                 pOriginalText = null;
1441                 hasMoreText = true;
1442
1443         }
1444         else
1445         {
1446                 pBodyText = const_cast< char* >(pOriginalText);
1447                 pOriginalText = null;
1448         }
1449
1450         body = pBodyText;
1451
1452         // received/sent time
1453         messages_get_time(msg, &tempTime);
1454         time = _MsgUtil::ConvertTime(&tempTime);
1455
1456         switch (type)
1457         {
1458         case SMS_MESSAGE_BOX_TYPE_INBOX:
1459         {
1460                 // sender address
1461                 messages_get_address(msg, 0, &pMsgAddress, &recipientType);
1462                 SysTryCatch(NID_MSG, pMsgAddress != null, r = E_SYSTEM, r, "sms get message address failed");
1463                 String senderAddress(pMsgAddress);
1464                 free(pMsgAddress);
1465                 pMsgAddress = null;
1466                 _SmsMessageImpl::GetInstance(*pSmsMessage)->SetInboxMessage(messageId, body, hasMoreText, senderAddress, time);
1467                 break;
1468         }
1469
1470         case SMS_MESSAGE_BOX_TYPE_SENTBOX:
1471         {
1472                 // recipient list
1473                 int recipient_count = 0;
1474                 messages_get_address_count(msg, &recipient_count);
1475                 RecipientList recipients;
1476
1477                 for (int index = 0; index < recipient_count; index++)
1478                 {
1479                         messages_get_address(msg, index, &pMsgAddress, &recipientType);
1480                         SysTryCatch(NID_MSG, pMsgAddress != null, r = E_SYSTEM, r, "sms get message address failed");
1481                         String senderAddress(pMsgAddress);
1482                         free(pMsgAddress);
1483                         pMsgAddress = null;
1484                         r = recipients.Add(RECIPIENT_TYPE_TO, senderAddress);
1485                 }
1486                 _SmsMessageImpl::GetInstance(*pSmsMessage)->SetSentboxMessage(messageId, body, hasMoreText, recipients, time);
1487                 break;
1488         }
1489
1490         case SMS_MESSAGE_BOX_TYPE_OUTBOX:
1491         {
1492                 // recipient list
1493                 int recipient_count = 0;
1494                 messages_get_address_count(msg, &recipient_count);
1495                 RecipientList recipients;
1496
1497                 for (int index = 0; index < recipient_count; index++)
1498                 {
1499                         messages_get_address(msg, index, &pMsgAddress, &recipientType);
1500                         SysTryCatch(NID_MSG, pMsgAddress != null, r = E_SYSTEM, r, "sms get message address failed");
1501                         String senderAddress(pMsgAddress);
1502                         free(pMsgAddress);
1503                         pMsgAddress = null;
1504                         r = recipients.Add(RECIPIENT_TYPE_TO, senderAddress);
1505                 }
1506                 _SmsMessageImpl::GetInstance(*pSmsMessage)->SetOutboxMessage(messageId, body, hasMoreText, recipients, time);
1507                 break;
1508         }
1509
1510         default:
1511                 break;
1512         }
1513
1514         if (pBodyText)
1515         {
1516                 free(pBodyText);
1517                 pBodyText = null;
1518         }
1519
1520         return pSmsMessage;
1521
1522 CATCH:
1523
1524         if (pBodyText)
1525         {
1526                 free(pBodyText);
1527                 pBodyText = null;
1528         }
1529
1530         if (pSmsMessage)
1531         {
1532                 delete pSmsMessage;
1533         }
1534
1535         return NULL;
1536 }
1537
1538 } }  // Tizen::Messaging