2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "Conversation.h"
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <Commons/Exception.h>
22 #include "MsgServiceHandleMgr.h"
23 #include "IMessaging.h"
24 #include "EmailUtils.h"
25 #include <email-api-mail.h>
28 // this is msgInternalTypes
29 #define MAX_THREAD_DATA_LEN 128
36 Conversation::Conversation()
41 Conversation::Conversation(unsigned int threadId, MessageType msgType)
47 makeConversationFromEmailThreadId(threadId);
49 else if((msgType == EMPTY_MESSAGE_CONVERSATION)||(msgType == EMPTY_EMAIL_CONVERSATION))
51 makeEmptyConversation(threadId, msgType);
55 makeConversationFromMsgId(threadId, msgType);
59 Conversation::Conversation(std::string threadId, MessageType msgType)
63 std::istringstream stream(threadId);
65 LoggerD("threadId : " << threadId);
67 unsigned int number = 0;
69 result = makeConversationFromMsgId(number, msgType);
72 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "fail to make conversation");
76 Conversation::Conversation(unsigned int threadIndex)
78 makeConversationFromThreadIndex(threadIndex);
81 //Conversation::Conversation(msg_thread_view_t msg_thread)
82 Conversation::Conversation(msg_struct_t msg_thread)
84 makeConversationFromThread(msg_thread);
87 //void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
88 void Conversation::makeConversationFromThread(msg_struct_t msg_thread)
91 msg_handle_t handle = MsgGetCommonHandle();
93 msg_struct_list_s convViewList;
94 msg_struct_list_s *addr_list = NULL;
96 msg_struct_t msgInfo = NULL;
97 msg_struct_t sendOpt = NULL;
99 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
100 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
102 msg_error_t err = MSG_SUCCESS;
103 unsigned int lastMsgIndex = 0;
104 char *tempString = NULL;
105 int addressCount = 0;
109 char msgData[128] = {0,};
116 msg_get_int_value(msg_thread, MSG_THREAD_ID_INT, &tempInt);
120 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
124 case MSG_TYPE_SMS_CB:
125 case MSG_TYPE_SMS_JAVACB:
126 case MSG_TYPE_SMS_WAPPUSH:
127 case MSG_TYPE_SMS_MWI:
128 case MSG_TYPE_SMS_SYNCML:
129 case MSG_TYPE_SMS_REJECT:
134 case MSG_TYPE_MMS_JAVA:
135 case MSG_TYPE_MMS_NOTI:
142 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
145 // get thread unread count
146 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
147 m_unreadMessages = tempInt;
149 // get thread preview
150 msg_get_str_value(msg_thread, MSG_THREAD_MSG_DATA_STR, msgData, 128);
151 tempString = msgData; // convert char to string
153 if (tempString != NULL)
155 m_preview = tempString;
156 LoggerD("preview" << m_preview);
159 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
161 if (err != MSG_SUCCESS)
163 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
166 // get thread message count
167 lastMsgIndex = convViewList.nCount - 1;
168 m_messageCount = convViewList.nCount;
170 // get thread read status
171 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_READ_BOOL, &tempBool);
174 // get thread last message Id
175 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_ID_INT, &tempInt);
176 m_lastMessageId = tempInt;
179 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
181 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
184 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_DIRECTION_INT, &tempInt);
187 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
189 nToCnt = addr_list->nCount;
191 if (type == MSG_DIRECTION_TYPE_MT)
194 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
196 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
197 msg_get_str_value(addr_list->msg_struct_info[m_lastMessageId], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
199 if (strNumber[0] != '\0')
201 m_from = strNumber; // convert char to string
205 LoggerD("address is null ");
210 LoggerD("address count index fail");
216 LoggerD("Msg direction: MO, from will be omitted");
219 // get thread subject
220 char strTemp[MAX_SUBJECT_LEN] = {0};
221 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
222 tempString = strTemp;
223 if (tempString != NULL)
225 m_subject = tempString; // convert char to string
229 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
231 for (index = 0; index < nToCnt; index++)
233 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
234 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
235 tempString = strNumber;
236 if (tempString != NULL)
238 m_to.push_back(tempString);
244 LoggerD("address fetch fail" << addressCount);
247 catch (const WrtDeviceApis::Commons::Exception& ex)
250 LoggerE("Exception: " << ex.GetMessage());
253 msg_release_list_struct(&convViewList);
254 msg_release_struct(&msgInfo);
255 msg_release_struct(&sendOpt);
261 // It is worth for below function to do test for fetching all conversations
262 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
266 MSG_HANDLE_T handle = MsgGetCommonHandle();
267 MSG_LIST_S convViewList = {0, NULL};
268 MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
269 msg_error_t err = MSG_SUCCESS;
270 unsigned int lastMsgIndex = 0;
271 char *tempString = NULL;
272 int addressCount = 0;
279 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
281 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
284 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
286 LoggerD(threadIndex << ":" << threadViewList.nCount);
287 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
291 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
293 m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
296 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
299 case MSG_TYPE_SMS_CB:
300 case MSG_TYPE_SMS_JAVACB:
301 case MSG_TYPE_SMS_WAPPUSH:
302 case MSG_TYPE_SMS_MWI:
303 case MSG_TYPE_SMS_SYNCML:
304 case MSG_TYPE_SMS_REJECT:
309 case MSG_TYPE_MMS_JAVA:
310 case MSG_TYPE_MMS_NOTI:
318 m_time = *(msg_thread_view_get_time(threadViewList.msgThreadInfo[threadIndex]));
319 m_unreadMessages = msg_thread_view_get_unread_cnt(threadViewList.msgThreadInfo[threadIndex]);
320 tempString = (char*)msg_thread_view_get_data(threadViewList.msgThreadInfo[threadIndex]);
322 if (tempString != NULL)
324 m_preview = tempString;
325 LoggerD("preview" << m_preview);
329 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
331 if (err != MSG_SUCCESS)
333 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
336 lastMsgIndex = convViewList.nCount - 1;
337 m_messageCount = convViewList.nCount;
338 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
340 LoggerD("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
342 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
344 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
346 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
348 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
350 if (tempString != NULL)
356 LoggerD("address is null ");
361 LoggerD("address count index fail");
363 LoggerD("from" << m_from);
367 LoggerD("Msg direction: MO, from will be omitted");
371 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
373 if (tempString != NULL)
375 m_subject = tempString;
378 LoggerD("subject" << m_subject);
381 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
384 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
386 for (index = 0; index < addressCount; index++)
388 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
390 if (tempString != NULL)
392 m_to.push_back(tempString);
395 LoggerD("address count" << addressCount);
399 LoggerD("address fetch fail" << addressCount);
402 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
404 m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
407 LoggerD("message id" << m_lastMessageId);
410 catch (const WrtDeviceApis::Commons::Exception& ex)
413 LoggerE("Exception: " << ex.GetMessage());
415 if (convViewList.msgInfo != NULL)
417 msg_release_list_struct(&convViewList);
420 if (threadViewList.msgThreadInfo != NULL)
422 msg_release_thread_view_list(&threadViewList);
428 bool Conversation::makeConversationFromMsgId(unsigned int msgId, MessageType msgType)
432 msg_handle_t handle = MsgGetCommonHandle();
434 msg_struct_t msgInfo = NULL;
435 msg_struct_t sendOpt = NULL;
437 msg_struct_t msg_thread = NULL;
439 msg_struct_list_s convViewList;
440 msg_struct_list_s *addr_list = NULL;
442 msg_error_t err = MSG_SUCCESS;
449 unsigned int lastMsgIndex = 0;
451 char *tempString = NULL;
452 char msgData[MAX_THREAD_DATA_LEN] = {0,};
457 // get msg from msgId
458 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
459 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
461 if (msg_get_message(handle, (msg_message_id_t)msgId, msgInfo, sendOpt) != MSG_SUCCESS)
463 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
466 // get threadId from message
467 msg_get_int_value(msgInfo, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
468 m_Id = (unsigned int)tempInt;
469 LoggerD("m_Id : " << m_Id);
472 msg_thread = msg_create_struct(MSG_STRUCT_THREAD_INFO);
473 err = msg_get_thread(handle, m_Id, msg_thread);
476 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
480 case MSG_TYPE_SMS_CB:
481 case MSG_TYPE_SMS_JAVACB:
482 case MSG_TYPE_SMS_WAPPUSH:
483 case MSG_TYPE_SMS_MWI:
484 case MSG_TYPE_SMS_SYNCML:
485 case MSG_TYPE_SMS_REJECT:
490 case MSG_TYPE_MMS_JAVA:
491 case MSG_TYPE_MMS_NOTI:
498 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
500 LoggerD("m_time : " << m_time);
502 // get thread unread count
503 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
504 m_unreadMessages = tempInt;
505 LoggerD("m_unreadMessages : " << m_unreadMessages);
507 // get thread preview
508 msg_get_str_value(msg_thread, MSG_THREAD_MSG_DATA_STR, msgData, MAX_THREAD_DATA_LEN);
509 tempString = msgData; // convert char to string
511 if (tempString != NULL)
513 m_preview = tempString;
515 LoggerD("m_preview : " << m_preview);
517 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
519 if (err != MSG_SUCCESS)
521 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
524 // get thread message count
525 lastMsgIndex = convViewList.nCount - 1;
526 m_messageCount = convViewList.nCount;
527 LoggerD("m_messageCount : " << m_messageCount);
529 // get thread read status
530 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_READ_BOOL, &tempBool);
532 LoggerD("m_read : " << m_read);
534 // get thread last message Id
535 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_ID_INT, &tempInt);
536 m_lastMessageId = tempInt;
537 LoggerD("m_lastMessageId : " << m_lastMessageId);
540 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
542 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
545 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_DIRECTION_INT, &tempInt);
547 LoggerD("direction : " << type);
550 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
551 nToCnt = addr_list->nCount;
553 if (type == MSG_DIRECTION_TYPE_MT)
556 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
558 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
559 msg_get_str_value(addr_list->msg_struct_info[nToCnt-1], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
561 if (strNumber[0] != '\0')
563 m_from = strNumber; // convert char to string
567 LoggerD("address is null ");
572 LoggerD("address count index fail");
578 LoggerD("Msg direction: MO, from will be omitted");
580 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
582 for (int index = 0; index < nToCnt; index++)
584 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
585 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
586 tempString = strNumber;
587 if (tempString != NULL)
589 LoggerD("m_to : " << tempString);
590 m_to.push_back(tempString);
596 LoggerD("address fetch fail");
600 // get thread subject
601 char strTemp[MAX_SUBJECT_LEN] = {0};
602 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
603 tempString = strTemp;
604 if (tempString != NULL)
606 m_subject = tempString; // convert char to string
608 LoggerD("m_subject : " << m_subject);
611 catch (const WrtDeviceApis::Commons::UnknownException& ex)
614 LoggerE("Exception: " << ex.GetMessage());
616 catch (const WrtDeviceApis::Commons::Exception& ex)
619 LoggerE("Exception: " << ex.GetMessage());
622 msg_release_list_struct(&convViewList);
623 msg_release_struct(&msgInfo);
624 msg_release_struct(&sendOpt);
625 msg_release_struct(&msg_thread);
630 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
634 email_mail_list_item_t *resultMail = NULL;
635 email_mail_list_item_t *mailList = NULL;
639 email_mail_data_t* mailData = NULL;
644 // Todo : will be re-implemented as using email_get_thread_information_ex
645 if(email_get_thread_information_ex(emailTreadId, &resultMail) != EMAIL_ERROR_NONE)
647 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
656 if (email_get_mail_data(resultMail->mail_id,&mailData) != EMAIL_ERROR_NONE)
658 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
662 m_unreadMessages = 0;
664 LoggerD("start email_get_mail_list");
665 if (email_get_mail_list(mailData->account_id, 0, emailTreadId, 0, resultMail->thread_item_count,
666 EMAIL_SORT_DATETIME_HIGH, &mailList, &count) != EMAIL_ERROR_NONE)
668 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
670 LoggerD("end email_get_mail_list");
673 for (index = 0; index < count; index++)
675 if (mailList[index].flags_seen_field)
679 LoggerD(mailList[index].flags_seen_field);
683 m_messageCount = resultMail->thread_item_count;
689 // time horrible split
690 // 2011 11 08 00 35 45
692 m_time = resultMail->date_time;
695 m_read = (bool)resultMail->flags_seen_field;
698 if (resultMail->full_address_from[0] != '\0')
700 LoggerD("Header From " << resultMail->full_address_from);
701 m_from = EmailUtils::stripAddress(resultMail->full_address_from);
702 LoggerD("email From " << m_from);
706 if (resultMail->subject[0] != '\0')
708 m_subject = resultMail->subject;
712 if (mailData->full_address_to != NULL)
714 LoggerD("Header To " << mailData->full_address_to);
715 m_to = EmailUtils::stripAddressLine(mailData->full_address_to).getRecipients();
718 if (mailData->full_address_bcc != NULL)
720 LoggerD("Header Bcc " << mailData->full_address_bcc);
721 m_bcc = EmailUtils::stripAddressLine(mailData->full_address_bcc).getRecipients();
724 if (mailData->full_address_cc != NULL)
726 LoggerD("Header CC " << mailData->full_address_cc);
727 m_cc = EmailUtils::stripAddressLine(mailData->full_address_cc).getRecipients();
730 m_lastMessageId = resultMail->mail_id;
731 LoggerD(m_lastMessageId);
734 catch (const WrtDeviceApis::Commons::Exception& ex)
737 LoggerE("Exception: " << ex.GetMessage());
740 if (mailData != NULL)
742 email_free_mail_data(&mailData , 1);
745 if (resultMail != NULL)
754 void Conversation::makeEmptyConversation(unsigned int threadIndex, MessageType msgType)
757 if(msgType == EMPTY_MESSAGE_CONVERSATION)
769 m_unreadMessages = 0;
780 Conversation::~Conversation()
786 void Conversation::setId(unsigned int id)
790 void Conversation::setType(unsigned short type)
794 void Conversation::setTime(time_t time)
798 void Conversation::setMessageCount(unsigned long messageCount)
800 m_messageCount = messageCount;
802 void Conversation::setUnreadMessages(unsigned long unreadMessages)
804 m_unreadMessages = unreadMessages;
806 void Conversation::setPreview(unsigned long preview)
810 void Conversation::setRead(bool read)
814 void Conversation::setFrom(std::string from)
818 void Conversation::setTo(std::vector<std::string> to)
822 void Conversation::setCC(std::vector<std::string> cc)
826 void Conversation::setBCC(std::vector<std::string> bcc)
830 void Conversation::setLastMessageId(unsigned int id)
832 m_lastMessageId = id;
836 void Conversation::setConvId(const int id)
842 int Conversation::getConvId()
846 unsigned short Conversation::getType()
850 time_t Conversation::getTime()
854 unsigned long Conversation::getMessageCount()
856 return m_messageCount;
858 unsigned long Conversation::getUnreadMessages()
860 return m_unreadMessages;
862 std::string Conversation::getPreview()
867 std::string Conversation::getSubject()
873 bool Conversation::getRead()
877 std::string Conversation::getFrom()
881 std::vector<std::string> Conversation::getTo()
885 std::vector<std::string> Conversation::getCC()
889 std::vector<std::string> Conversation::getBCC()
893 int Conversation::getLastMessageId()
895 return m_lastMessageId;
898 bool Conversation::getResult()
903 int Conversation::getDirection()