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 <email-api-mail.h>
27 // this is msgInternalTypes
28 #define MAX_THREAD_DATA_LEN 128
35 Conversation::Conversation()
40 Conversation::Conversation(unsigned int threadId, MessageType msgType)
46 makeConversationFromEmailThreadId(threadId);
48 else if(msgType == EMPTY_MESSAGE)
54 makeConversationFromMsgId(threadId, msgType);
58 Conversation::Conversation(std::string threadId, MessageType msgType)
62 std::istringstream stream(threadId);
64 LoggerD("threadId : " << threadId);
66 unsigned int number = 0;
68 result = makeConversationFromMsgId(number, msgType);
71 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "fail to make conversation");
75 Conversation::Conversation(unsigned int threadIndex)
77 makeConversationFromThreadIndex(threadIndex);
80 //Conversation::Conversation(msg_thread_view_t msg_thread)
81 Conversation::Conversation(msg_struct_t msg_thread)
83 makeConversationFromThread(msg_thread);
86 //void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
87 void Conversation::makeConversationFromThread(msg_struct_t msg_thread)
90 msg_handle_t handle = MsgGetCommonHandle();
92 msg_struct_list_s convViewList;
93 msg_list_handle_t addr_list = NULL;
94 msg_struct_t addr_info = 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_HND, (void **)&addr_list);
189 nToCnt = msg_list_length(addr_list);
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 addr_info = (msg_struct_t)msg_list_nth_data(addr_list, m_lastMessageId);
198 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
200 if (strNumber[0] != '\0')
202 m_from = strNumber; // convert char to string
206 LoggerD("address is null ");
211 LoggerD("address count index fail");
217 LoggerD("Msg direction: MO, from will be omitted");
220 // get thread subject
221 char strTemp[MAX_SUBJECT_LEN] = {0};
222 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
223 tempString = strTemp;
224 if (tempString != NULL)
226 m_subject = tempString; // convert char to string
230 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
232 for (index = 0; index < nToCnt; index++)
234 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
235 addr_info = (msg_struct_t)msg_list_nth_data(addr_list, index);
236 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
237 tempString = strNumber;
238 if (tempString != NULL)
240 m_to.push_back(tempString);
246 LoggerD("address fetch fail" << addressCount);
249 catch (const WrtDeviceApis::Commons::Exception& ex)
252 LoggerE("Exception: " << ex.GetMessage());
255 msg_release_list_struct(&convViewList);
256 msg_release_struct(&msgInfo);
257 msg_release_struct(&sendOpt);
263 // It is worth for below function to do test for fetching all conversations
264 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
268 MSG_HANDLE_T handle = MsgGetCommonHandle();
269 MSG_LIST_S convViewList = {0, NULL};
270 MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
271 msg_error_t err = MSG_SUCCESS;
272 unsigned int lastMsgIndex = 0;
273 char *tempString = NULL;
274 int addressCount = 0;
281 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
283 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
286 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
288 LoggerD(threadIndex << ":" << threadViewList.nCount);
289 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
293 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
295 m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
298 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
301 case MSG_TYPE_SMS_CB:
302 case MSG_TYPE_SMS_JAVACB:
303 case MSG_TYPE_SMS_WAPPUSH:
304 case MSG_TYPE_SMS_MWI:
305 case MSG_TYPE_SMS_SYNCML:
306 case MSG_TYPE_SMS_REJECT:
311 case MSG_TYPE_MMS_JAVA:
312 case MSG_TYPE_MMS_NOTI:
320 m_time = *(msg_thread_view_get_time(threadViewList.msgThreadInfo[threadIndex]));
321 m_unreadMessages = msg_thread_view_get_unread_cnt(threadViewList.msgThreadInfo[threadIndex]);
322 tempString = (char*)msg_thread_view_get_data(threadViewList.msgThreadInfo[threadIndex]);
324 if (tempString != NULL)
326 m_preview = tempString;
327 LoggerD("preview" << m_preview);
331 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
333 if (err != MSG_SUCCESS)
335 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
338 lastMsgIndex = convViewList.nCount - 1;
339 m_messageCount = convViewList.nCount;
340 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
342 LoggerD("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
344 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
346 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
348 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
350 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
352 if (tempString != NULL)
358 LoggerD("address is null ");
363 LoggerD("address count index fail");
365 LoggerD("from" << m_from);
369 LoggerD("Msg direction: MO, from will be omitted");
373 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
375 if (tempString != NULL)
377 m_subject = tempString;
380 LoggerD("subject" << m_subject);
383 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
386 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
388 for (index = 0; index < addressCount; index++)
390 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
392 if (tempString != NULL)
394 m_to.push_back(tempString);
397 LoggerD("address count" << addressCount);
401 LoggerD("address fetch fail" << addressCount);
404 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
406 m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
409 LoggerD("message id" << m_lastMessageId);
412 catch (const WrtDeviceApis::Commons::Exception& ex)
415 LoggerE("Exception: " << ex.GetMessage());
417 if (convViewList.msgInfo != NULL)
419 msg_release_list_struct(&convViewList);
422 if (threadViewList.msgThreadInfo != NULL)
424 msg_release_thread_view_list(&threadViewList);
430 bool Conversation::makeConversationFromMsgId(unsigned int msgId, MessageType msgType)
434 msg_handle_t handle = MsgGetCommonHandle();
436 msg_struct_t msgInfo = NULL;
437 msg_struct_t sendOpt = NULL;
439 msg_struct_t msg_thread = NULL;
441 msg_struct_list_s convViewList;
442 msg_list_handle_t addr_list = NULL;
443 msg_struct_t addr_info = NULL;
445 msg_error_t err = MSG_SUCCESS;
452 unsigned int lastMsgIndex = 0;
454 char *tempString = NULL;
455 char msgData[MAX_THREAD_DATA_LEN] = {0,};
460 // get msg from msgId
461 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
462 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
464 if (msg_get_message(handle, (msg_message_id_t)msgId, msgInfo, sendOpt) != MSG_SUCCESS)
466 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
469 // get threadId from message
470 msg_get_int_value(msgInfo, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
471 m_Id = (unsigned int)tempInt;
472 LoggerD("m_Id : " << m_Id);
475 msg_thread = msg_create_struct(MSG_STRUCT_THREAD_INFO);
476 err = msg_get_thread(handle, m_Id, msg_thread);
479 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
483 case MSG_TYPE_SMS_CB:
484 case MSG_TYPE_SMS_JAVACB:
485 case MSG_TYPE_SMS_WAPPUSH:
486 case MSG_TYPE_SMS_MWI:
487 case MSG_TYPE_SMS_SYNCML:
488 case MSG_TYPE_SMS_REJECT:
493 case MSG_TYPE_MMS_JAVA:
494 case MSG_TYPE_MMS_NOTI:
501 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
503 LoggerD("m_time : " << m_time);
505 // get thread unread count
506 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
507 m_unreadMessages = tempInt;
508 LoggerD("m_unreadMessages : " << m_unreadMessages);
510 // get thread preview
511 msg_get_str_value(msg_thread, MSG_THREAD_MSG_DATA_STR, msgData, MAX_THREAD_DATA_LEN);
512 tempString = msgData; // convert char to string
514 if (tempString != NULL)
516 m_preview = tempString;
518 LoggerD("m_preview : " << m_preview);
520 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
522 if (err != MSG_SUCCESS)
524 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
527 // get thread message count
528 lastMsgIndex = convViewList.nCount - 1;
529 m_messageCount = convViewList.nCount;
530 LoggerD("m_messageCount : " << m_messageCount);
532 // get thread read status
533 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_READ_BOOL, &tempBool);
535 LoggerD("m_read : " << m_read);
537 // get thread last message Id
538 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_ID_INT, &tempInt);
539 m_lastMessageId = tempInt;
540 LoggerD("m_lastMessageId : " << m_lastMessageId);
543 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
545 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
548 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_DIRECTION_INT, &tempInt);
550 LoggerD("direction : " << type);
552 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
553 nToCnt = msg_list_length(addr_list);
555 if (type == MSG_DIRECTION_TYPE_MT)
558 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
560 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
561 addr_info = (msg_struct_t)msg_list_nth_data(addr_list, nToCnt-1);
562 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
564 if (strNumber[0] != '\0')
566 m_from = strNumber; // convert char to string
570 LoggerD("address is null ");
575 LoggerD("address count index fail");
581 LoggerD("Msg direction: MO, from will be omitted");
583 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
585 for (int index = 0; index < nToCnt; index++)
587 addr_info = (msg_struct_t)msg_list_nth_data(addr_list, index);
588 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
589 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
590 tempString = strNumber;
591 if (tempString != NULL)
593 LoggerD("m_to : " << tempString);
594 m_to.push_back(tempString);
600 LoggerD("address fetch fail");
604 // get thread subject
605 char strTemp[MAX_SUBJECT_LEN] = {0};
606 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
607 tempString = strTemp;
608 if (tempString != NULL)
610 m_subject = tempString; // convert char to string
612 LoggerD("m_subject : " << m_subject);
615 catch (const WrtDeviceApis::Commons::UnknownException& ex)
618 LoggerE("Exception: " << ex.GetMessage());
620 catch (const WrtDeviceApis::Commons::Exception& ex)
623 LoggerE("Exception: " << ex.GetMessage());
626 msg_release_list_struct(&convViewList);
627 msg_release_struct(&msgInfo);
628 msg_release_struct(&sendOpt);
629 msg_release_struct(&msg_thread);
634 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
638 email_mail_list_item_t *resultMail = NULL;
639 email_mail_list_item_t *mailList = NULL;
643 email_mail_data_t* mailData = NULL;
648 // Todo : will be re-implemented as using email_get_thread_information_ex
649 if(email_get_thread_information_ex(emailTreadId, &resultMail) != EMAIL_ERROR_NONE)
651 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
660 if (email_get_mail_data(resultMail->mail_id,&mailData) != EMAIL_ERROR_NONE)
662 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
666 m_unreadMessages = 0;
668 LoggerD("start email_get_mail_list");
669 if (email_get_mail_list(mailData->account_id, 0, emailTreadId, 0, resultMail->thread_item_count,
670 EMAIL_SORT_DATETIME_HIGH, &mailList, &count) != EMAIL_ERROR_NONE)
672 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
674 LoggerD("end email_get_mail_list");
677 for (index = 0; index < count; index++)
679 if (mailList[index].flags_seen_field)
683 LoggerD(mailList[index].flags_seen_field);
687 m_messageCount = resultMail->thread_item_count;
693 // time horrible split
694 // 2011 11 08 00 35 45
696 m_time = resultMail->date_time;
699 m_read = (bool)resultMail->flags_seen_field;
702 if (resultMail->full_address_from[0] != '\0')
704 m_from = resultMail->full_address_from;
709 if (resultMail->subject[0] != '\0')
711 m_subject = resultMail->subject;
715 if (mailData->full_address_to != NULL)
717 LoggerD("Header To " << mailData->full_address_to);
718 m_to.push_back(mailData->full_address_to);
721 if (mailData->full_address_bcc != NULL)
723 LoggerD("Header Bcc " << mailData->full_address_bcc);
724 m_bcc.push_back(mailData->full_address_bcc);
727 if (mailData->full_address_cc != NULL)
729 LoggerD("Header CC " << mailData->full_address_cc);
730 m_cc.push_back(mailData->full_address_cc);
733 m_lastMessageId = resultMail->mail_id;
734 LoggerD(m_lastMessageId);
737 catch (const WrtDeviceApis::Commons::Exception& ex)
740 LoggerE("Exception: " << ex.GetMessage());
743 if (mailData != NULL)
745 email_free_mail_data(&mailData , 1);
748 if (resultMail != NULL)
756 Conversation::~Conversation()
762 void Conversation::setId(unsigned int id)
766 void Conversation::setType(unsigned short type)
770 void Conversation::setTime(time_t time)
774 void Conversation::setMessageCount(unsigned long messageCount)
776 m_messageCount = messageCount;
778 void Conversation::setUnreadMessages(unsigned long unreadMessages)
780 m_unreadMessages = unreadMessages;
782 void Conversation::setPreview(unsigned long preview)
786 void Conversation::setRead(bool read)
790 void Conversation::setFrom(std::string from)
794 void Conversation::setTo(std::vector<std::string> to)
798 void Conversation::setCC(std::vector<std::string> cc)
802 void Conversation::setBCC(std::vector<std::string> bcc)
806 void Conversation::setLastMessageId(unsigned int id)
808 m_lastMessageId = id;
812 void Conversation::setConvId(const int id)
818 int Conversation::getConvId()
822 unsigned short Conversation::getType()
826 time_t Conversation::getTime()
830 unsigned long Conversation::getMessageCount()
832 return m_messageCount;
834 unsigned long Conversation::getUnreadMessages()
836 return m_unreadMessages;
838 std::string Conversation::getPreview()
843 std::string Conversation::getSubject()
849 bool Conversation::getRead()
853 std::string Conversation::getFrom()
857 std::vector<std::string> Conversation::getTo()
861 std::vector<std::string> Conversation::getCC()
865 std::vector<std::string> Conversation::getBCC()
869 int Conversation::getLastMessageId()
871 return m_lastMessageId;
874 bool Conversation::getResult()