merge wrt-plugins-tizen_0.2.0-12
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / Conversation.cpp
1 /*
2 * Copyright (c) 2011 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
18 #include "Conversation.h"
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <Commons/Exception.h>
21 #include "MsgServiceHandleMgr.h"
22 #include <API/Messaging/IMessaging.h>
23 #include <Emf_Mapi_Message.h>
24 #include <time.h>
25
26
27 using namespace DPL;
28
29 namespace TizenApis {
30 namespace Platform {
31 namespace Messaging {
32
33 Conversation::Conversation()
34 {
35
36 }
37
38 Conversation::Conversation(unsigned int threadId, Api::Messaging::MessageType msgType)
39 {
40         LogDebug("Enter");
41         
42         if (msgType == Api::Messaging::EMAIL)
43         {
44                 makeConversationFromEmailThreadId(threadId);
45         }
46         else
47         {
48                 makeConversationFromMsgId(threadId, msgType);
49         }
50 }
51         
52 Conversation::Conversation(std::string threadId, Api::Messaging::MessageType msgType)
53 {
54         LogDebug("Enter");
55         
56         std::istringstream stream(threadId);
57         
58         LogDebug("threadId : " << threadId);
59         
60         unsigned int number = 0;
61         stream >> number;
62         makeConversationFromMsgId(number, msgType);
63 }
64
65 Conversation::Conversation(unsigned int threadIndex)
66 {
67         makeConversationFromThreadIndex(threadIndex);
68 }
69
70 Conversation::Conversation(msg_thread_view_t msg_thread)
71 {
72         makeConversationFromThread(msg_thread);
73 }
74
75 void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
76 {
77         LogDebug("Enter");
78         MSG_HANDLE_T handle = MsgGetCommonHandle();
79         MSG_LIST_S convViewList = {0, NULL};
80         msg_message_t msgForLastIndex = msg_new_message();
81         MSG_SENDINGOPT_S sendOpt = {0, };
82         MSG_ERROR_T err = MSG_SUCCESS;
83         unsigned int lastMsgIndex = 0;
84         char *tempString = NULL;
85         int addressCount = 0;
86         int index = 0;
87         
88         m_result = true;
89
90         try 
91         {
92                 if (msg_thread_view_get_thread_id(msg_thread) >= 0)
93                 {
94                         m_Id = msg_thread_view_get_thread_id(msg_thread);
95                 }
96                 
97                 switch(msg_thread_view_get_message_type(msg_thread))
98                 {
99                         case MSG_TYPE_SMS:
100                         case MSG_TYPE_SMS_CB:
101                         case MSG_TYPE_SMS_JAVACB:
102                         case MSG_TYPE_SMS_WAPPUSH:
103                         case MSG_TYPE_SMS_MWI:
104                         case MSG_TYPE_SMS_SYNCML:
105                         case MSG_TYPE_SMS_REJECT:
106                                 m_type = Api::Messaging::SMS;
107                                 LogDebug("Type:SMS");
108                                 break;
109                         case MSG_TYPE_MMS:
110                         case MSG_TYPE_MMS_JAVA:
111                         case MSG_TYPE_MMS_NOTI:
112                                 m_type = Api::Messaging::MMS;;
113                                 LogDebug("Type:MMS");
114                                 break;
115 //                      default:
116                                 // Todo email / chat
117                 } 
118                 
119                 m_time = *(msg_thread_view_get_time(msg_thread));
120                 m_unreadMessages = msg_thread_view_get_unread_cnt(msg_thread);
121                 tempString = (char*)msg_thread_view_get_data(msg_thread);
122
123                 if (tempString != NULL)
124                 {
125                         m_preview = tempString; 
126                         LogDebug("preview" << m_preview);
127                 }
128
129                 
130                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
131                 
132                 if (err != MSG_SUCCESS)
133                 {
134                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
135                 }
136
137                 lastMsgIndex = convViewList.nCount - 1;
138                 m_messageCount = convViewList.nCount;
139                 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
140
141                 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
142                 {
143                         m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
144                         LogDebug("message id" << m_lastMessageId);
145                 }
146                 else
147                 {
148                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message id fail");
149                 }
150
151                 if (msg_get_message(handle, m_lastMessageId, msgForLastIndex, &sendOpt) != MSG_SUCCESS)
152                 {
153                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
154                 }
155
156                 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
157
158                 if (msg_get_direction_info(msgForLastIndex) == MSG_DIRECTION_TYPE_MT)
159                 {
160                         addressCount = msg_get_address_count(msgForLastIndex);
161
162                         if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
163                         {
164                                 tempString = (char*)msg_get_ith_address(msgForLastIndex, 0);
165                                 
166                                 if (tempString != NULL)
167                                 {
168                                         m_from = tempString;
169                                 }
170                                 else
171                                 {
172                                         LogDebug("address is null ");
173                                 }
174                         }
175                         else 
176                         {
177                                 LogDebug("address count index fail");
178                         }
179                         LogDebug("from" << m_from);
180                 }
181                 else
182                 {
183                         LogDebug("Msg direction: MO, from will be omitted");
184                 }
185
186
187                 tempString = (char*)msg_get_subject(msgForLastIndex);
188
189                 if (tempString != NULL)
190                 {
191                         m_subject = tempString;
192                 }
193
194                 LogDebug("subject" << m_subject);
195                 
196
197                 addressCount = msg_get_address_count(msgForLastIndex);
198
199
200                 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
201                 {
202                         for (index = 0; index < addressCount; index++)
203                         {
204                                 tempString = (char*)msg_get_ith_address(msgForLastIndex, index);
205
206                                 if (tempString != NULL)
207                                 {
208                                         m_to.push_back(tempString);
209                                 }
210                         }
211                         LogDebug("address count" << addressCount);
212                 }
213                 else
214                 {
215                         LogDebug("address fetch fail" << addressCount);
216                 }
217
218
219
220         }
221         catch (const WrtDeviceApis::Commons::Exception& ex) 
222         {
223                 m_result = false;
224                 LogError("Exception: " << ex.GetMessage());
225         }
226         
227         if (convViewList.msgInfo != NULL) 
228         {
229                 msg_release_message_list(&convViewList);
230         }
231
232         if (msgForLastIndex != NULL)
233         {
234                 msg_release_message(&msgForLastIndex);
235         }
236 }
237
238
239
240 // It is worth for below function to do test for fetching all conversations
241 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
242 {
243         LogDebug("Enter");
244         MSG_HANDLE_T handle = MsgGetCommonHandle();
245         MSG_LIST_S convViewList = {0, NULL};
246         MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
247         MSG_ERROR_T err = MSG_SUCCESS;
248         unsigned int lastMsgIndex = 0;
249         char *tempString = NULL;
250         int addressCount = 0;
251         int index = 0;
252         
253         m_result = true;
254
255         try 
256         {
257                 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
258                 {
259                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
260                 }
261
262                 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
263                 {
264                         LogDebug(threadIndex << ":" << threadViewList.nCount);
265                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
266                 }
267
268                 
269                 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
270                 {
271                         m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
272                 }
273                 
274                 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
275                 {
276                         case MSG_TYPE_SMS:
277                         case MSG_TYPE_SMS_CB:
278                         case MSG_TYPE_SMS_JAVACB:
279                         case MSG_TYPE_SMS_WAPPUSH:
280                         case MSG_TYPE_SMS_MWI:
281                         case MSG_TYPE_SMS_SYNCML:
282                         case MSG_TYPE_SMS_REJECT:
283                                 m_type = Api::Messaging::SMS;
284                                 LogDebug("Type:SMS");
285                                 break;
286                         case MSG_TYPE_MMS:
287                         case MSG_TYPE_MMS_JAVA:
288                         case MSG_TYPE_MMS_NOTI:
289                                 m_type = Api::Messaging::MMS;;
290                                 LogDebug("Type:MMS");
291                                 break;
292 //                      default:
293                                 // Todo email / chat
294                 } 
295                 
296                 m_time = *(msg_thread_view_get_time(threadViewList.msgThreadInfo[threadIndex]));
297                 m_unreadMessages = msg_thread_view_get_unread_cnt(threadViewList.msgThreadInfo[threadIndex]);
298                 tempString = (char*)msg_thread_view_get_data(threadViewList.msgThreadInfo[threadIndex]);
299
300                 if (tempString != NULL)
301                 {
302                         m_preview = tempString; 
303                         LogDebug("preview" << m_preview);
304                 }
305
306                 
307                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
308                 
309                 if (err != MSG_SUCCESS)
310                 {
311                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
312                 }
313
314                 lastMsgIndex = convViewList.nCount - 1;
315                 m_messageCount = convViewList.nCount;
316                 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
317                 
318                 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
319
320                 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
321                 {
322                         addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
323
324                         if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
325                         {
326                     tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
327                                 
328                                 if (tempString != NULL)
329                                 {
330                                         m_from = tempString;
331                                 }
332                                 else
333                                 {
334                                         LogDebug("address is null ");
335                                 }
336                         }
337                         else 
338                         {
339                                 LogDebug("address count index fail");
340                         }
341                         LogDebug("from" << m_from);
342                 }
343                 else
344                 {
345                         LogDebug("Msg direction: MO, from will be omitted");
346                 }
347
348
349                 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
350
351                 if (tempString != NULL)
352                 {
353                         m_subject = tempString;
354                 }
355
356                 LogDebug("subject" << m_subject);
357                 
358
359                 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
360
361
362                 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
363                 {
364                         for (index = 0; index < addressCount; index++)
365                         {
366                                 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
367
368                                 if (tempString != NULL)
369                                 {
370                                         m_to.push_back(tempString);
371                                 }
372                         }
373                         LogDebug("address count" << addressCount);
374                 }
375                 else
376                 {
377                         LogDebug("address fetch fail" << addressCount);
378                 }
379
380                 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
381                 {
382                         m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
383                 }
384
385                 LogDebug("message id" << m_lastMessageId);
386
387         }
388         catch (const WrtDeviceApis::Commons::Exception& ex) 
389         {
390                 m_result = false;
391                 LogError("Exception: " << ex.GetMessage());
392         }
393         if (convViewList.msgInfo != NULL) 
394         {
395                 msg_release_message_list(&convViewList);
396         }
397
398         if (threadViewList.msgThreadInfo != NULL)
399         {
400                 msg_release_thread_view_list(&threadViewList);
401         }
402
403
404 }
405
406 bool Conversation::makeConversationFromMsgId(unsigned int threadId, Api::Messaging::MessageType msgType)
407 {
408         LogDebug("Enter");
409         MSG_HANDLE_T handle = MsgGetCommonHandle();
410         MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
411         int conversationId = 0; 
412         int index = 0;
413         m_result = false;
414         
415         if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
416         {
417                 LogDebug("get thread view fail");
418
419                 return m_result;
420         }
421
422         conversationId = Api::Messaging::IMessaging::getInstance().getConversationId(threadId, msgType);
423         
424         for(index = 0; index < threadViewList.nCount; index++)
425         {
426                 if (conversationId == msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[index]))
427                 {
428                         LogDebug("Found" << conversationId);
429                         makeConversationFromThreadIndex(index);
430                         break;
431                 }
432         }
433
434         
435         return m_result;
436 }
437
438 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
439 {
440         LogDebug("Enter");
441         
442         emf_mail_list_item_t *resultMail = NULL;
443         emf_mail_list_item_t *mailList = NULL;
444         int accountId = 0;
445         int index = 0;
446         int count = 0;
447         emf_mailbox_t mailbox = {};
448         emf_mail_t *mail = NULL;
449
450         
451         m_result = true;
452
453         try 
454         {
455
456                 // Todo : will be re-implemented as using email_get_thread_information_ex 
457                 if(email_get_thread_information_ex(emailTreadId, &resultMail) != EMF_ERROR_NONE) 
458                 {
459                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
460                 }
461
462                 if (!resultMail)
463                 {
464                         return NULL;
465                 }
466                 
467                 if (email_get_mail(&mailbox, resultMail->mail_id, &mail) != EMF_ERROR_NONE)
468                 {
469                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
470                 }
471                 
472                 // account Id
473                 m_unreadMessages = 0;
474
475                 LogDebug("start email_get_mail_list_ex");
476                 if (email_get_mail_list_ex(accountId, NULL, emailTreadId, 0, resultMail->thread_item_count, 
477                         EMF_SORT_DATETIME_HIGH, &mailList, &count) != EMF_ERROR_NONE)
478                 {
479                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
480                 }
481                 LogDebug("end email_get_mail_list_ex");
482                 
483                 // unread msg count
484                 accountId = resultMail->account_id;     
485
486                 for (index = 0; index < count; index++)
487                 {
488                         if (mailList[index].flags_seen_field)
489                         {
490                                 m_unreadMessages++;
491                         }
492                         LogDebug(mailList[index].flags_seen_field);
493                 }
494                 
495                 // message count
496                 m_messageCount = resultMail->thread_item_count;
497                 // id
498                 m_Id = emailTreadId;
499                 // type
500                 m_type = Api::Messaging::EMAIL;
501
502                 // time horrible split 
503                 // 2011 11 08 00 35 45
504                 if (resultMail->datetime[0] != '\0')
505                 {
506                         char buf[MAX_DATETIME_STRING_LENGTH];
507                         char *targetBuf = resultMail->datetime;
508                         struct tm timeinfo;
509
510                         memset(buf, 0x00, sizeof(buf));
511                         targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
512                         timeinfo.tm_year = atoi(buf) - 1900;
513
514                         memset(buf, 0x00, sizeof(buf));
515                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
516                         timeinfo.tm_mon =  atoi(buf) - 1;
517
518                         memset(buf, 0x00, sizeof(buf));
519                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
520                         timeinfo.tm_mday =  atoi(buf);
521
522
523                         memset(buf, 0x00, sizeof(buf));
524                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
525                         timeinfo.tm_hour = atoi(buf);
526  
527                         memset(buf, 0x00, sizeof(buf));
528                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
529                         timeinfo.tm_min = atoi(buf);
530
531
532                         memset(buf, 0x00, sizeof(buf));
533                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
534                         timeinfo.tm_sec = atoi(buf);
535
536                         m_time = mktime(&timeinfo);
537                 }
538         
539                 // preview
540                 // not support 
541 #if 0
542                 if (resultMail->previewBodyText[0] != '\0')
543                 {
544                         m_preview = resultMail->previewBodyText; 
545                         LogDebug(m_preview);
546
547                 }
548                 if (mail->head->previewBodyText != NULL)
549                 {
550                         LogDebug(mail->head->previewBodyText);
551                         m_preview = mail->head->previewBodyText;
552                 }
553 #endif  
554                 // read
555                 m_read = (bool)resultMail->flags_seen_field; 
556
557                 // from
558                 if (resultMail->from[0] != '\0')
559                 {
560                         m_from = resultMail->from; 
561                         LogDebug(m_from);
562                 }
563
564                 // subject
565                 if (resultMail->subject[0] != '\0');
566                 {
567                         m_subject = resultMail->subject;
568                         LogDebug(m_subject);            
569                 }
570
571                 // to, cc, bcc
572                 if (mail->head!= NULL)
573                 {
574                         if (mail->head->bcc != NULL)
575                         {
576                                 LogDebug(mail->head->bcc);
577                                 m_bcc.push_back(mail->head->bcc);
578                         }
579
580                         if (mail->head->to != NULL)
581                         {
582                                 LogDebug(mail->head->to);
583                                 m_to.push_back(mail->head->to);
584                         }
585
586                         if (mail->head->cc != NULL)
587                         {
588                                 LogDebug(mail->head->cc);
589                                 m_cc.push_back(mail->head->cc);
590                         }
591
592                 }
593         
594                 m_lastMessageId = resultMail->mail_id;
595                 LogDebug(m_lastMessageId);              
596                 m_result = true;
597         }
598         catch (const WrtDeviceApis::Commons::Exception& ex) 
599         {
600                 m_result = false;
601                 LogError("Exception: " << ex.GetMessage());
602         }
603
604         if (mail != NULL)
605         {
606                 email_free_mail(&mail , 1);
607         }
608         
609         if (resultMail != NULL)
610         {
611                 free(resultMail);
612         }
613
614
615         return m_result;
616
617 }
618 Conversation::~Conversation()
619 {
620 }
621
622 #if 0
623 // setter
624 void Conversation::setId(unsigned int id)
625 {
626         m_id = id;
627 }
628 void Conversation::setType(unsigned short type)
629 {
630         m_type = type;
631 }
632 void Conversation::setTime(time_t time)
633 {
634         m_time = time;
635 }
636 void Conversation::setMessageCount(unsigned long messageCount)
637 {
638         m_messageCount = messageCount;
639 }
640 void Conversation::setUnreadMessages(unsigned long unreadMessages)
641 {
642         m_unreadMessages = unreadMessages;
643 }
644 void Conversation::setPreview(unsigned long preview)
645 {
646         m_preview = preview;
647 }
648 void Conversation::setRead(bool read)
649 {
650         m_read = read;
651 }
652 void Conversation::setFrom(std::string from)
653 {
654         m_from = from;
655 }
656 void Conversation::setTo(std::vector<std::string> to)
657 {
658         m_to = to;
659 }
660 void Conversation::setCC(std::vector<std::string> cc)
661 {
662         m_cc = cc;
663 }
664 void Conversation::setBCC(std::vector<std::string> bcc)
665 {
666         m_bcc = bcc;
667 }
668 void Conversation::setLastMessageId(unsigned int id)
669 {
670         m_lastMessageId = id;
671 }
672 #endif
673 // getter
674 unsigned int Conversation::getId()
675 {
676         return m_Id;
677 }
678 unsigned short Conversation::getType()
679 {
680         return m_type;
681 }
682 time_t Conversation::getTime()
683 {
684         return m_time;
685 }
686 unsigned long Conversation::getMessageCount()
687 {
688         return m_messageCount;
689 }
690 unsigned long Conversation::getUnreadMessages()
691 {
692         return m_unreadMessages;
693 }
694 std::string Conversation::getPreview()
695 {
696         return m_preview;
697 }
698
699 std::string Conversation::getSubject()
700 {
701         return m_subject;
702 }
703
704
705 bool Conversation::getRead()
706 {
707         return m_read;
708 }
709 std::string Conversation::getFrom()
710 {
711         return m_from;
712 }
713 std::vector<std::string> Conversation::getTo()
714 {
715         return m_to;
716 }
717 std::vector<std::string> Conversation::getCC()
718 {
719         return m_cc;
720 }
721 std::vector<std::string> Conversation::getBCC()
722 {
723         return m_bcc;
724 }
725 unsigned int Conversation::getLastMessageId()
726 {
727         return m_lastMessageId;
728 }
729
730 bool Conversation::getResult()
731 {
732         return m_result;
733 }
734
735 }
736 }
737 }
738