2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 <email-api-mail.h>
26 // this is msgInternalTypes
27 #define MAX_THREAD_DATA_LEN 128
35 Conversation::Conversation()
40 Conversation::Conversation(unsigned int threadId, Api::Messaging::MessageType msgType)
44 if (msgType == Api::Messaging::EMAIL)
46 makeConversationFromEmailThreadId(threadId);
48 else if(msgType == Api::Messaging::EMPTY_MESSAGE)
54 makeConversationFromMsgId(threadId, msgType);
58 Conversation::Conversation(std::string threadId, Api::Messaging::MessageType msgType)
62 std::istringstream stream(threadId);
64 LogDebug("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_struct_list_s *addr_list = NULL;
95 msg_struct_t msgInfo = NULL;
96 msg_struct_t sendOpt = NULL;
98 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
99 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
101 msg_error_t err = MSG_SUCCESS;
102 unsigned int lastMsgIndex = 0;
103 char *tempString = NULL;
104 int addressCount = 0;
108 char msgData[128] = {0,};
115 msg_get_int_value(msg_thread, MSG_THREAD_ID_INT, &tempInt);
119 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
123 case MSG_TYPE_SMS_CB:
124 case MSG_TYPE_SMS_JAVACB:
125 case MSG_TYPE_SMS_WAPPUSH:
126 case MSG_TYPE_SMS_MWI:
127 case MSG_TYPE_SMS_SYNCML:
128 case MSG_TYPE_SMS_REJECT:
129 m_type = Api::Messaging::SMS;
130 LogDebug("Type:SMS");
133 case MSG_TYPE_MMS_JAVA:
134 case MSG_TYPE_MMS_NOTI:
135 m_type = Api::Messaging::MMS;;
136 LogDebug("Type:MMS");
141 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
144 // get thread unread count
145 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
146 m_unreadMessages = tempInt;
148 // get thread preview
149 msg_get_str_value(msg_thread, MSG_THREAD_MSG_DATA_STR, msgData, 128);
150 tempString = msgData; // convert char to string
152 if (tempString != NULL)
154 m_preview = tempString;
155 LogDebug("preview" << m_preview);
158 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
160 if (err != MSG_SUCCESS)
162 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
165 // get thread message count
166 lastMsgIndex = convViewList.nCount - 1;
167 m_messageCount = convViewList.nCount;
169 // get thread read status
170 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_READ_BOOL, &tempBool);
173 // get thread last message Id
174 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_ID_INT, &tempInt);
175 m_lastMessageId = tempInt;
178 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
180 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
183 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_DIRECTION_INT, &tempInt);
186 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
188 nToCnt = addr_list->nCount;
190 if (type == MSG_DIRECTION_TYPE_MT)
193 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
195 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
196 msg_get_str_value(addr_list->msg_struct_info[m_lastMessageId], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
198 if (strNumber != NULL)
200 m_from = strNumber; // convert char to string
204 LogDebug("address is null ");
209 LogDebug("address count index fail");
215 LogDebug("Msg direction: MO, from will be omitted");
218 // get thread subject
219 char strTemp[MAX_SUBJECT_LEN] = {0};
220 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
221 tempString = strTemp;
222 if (tempString != NULL)
224 m_subject = tempString; // convert char to string
228 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
230 for (index = 0; index < nToCnt; index++)
232 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
233 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
234 tempString = strNumber;
235 if (tempString != NULL)
237 m_to.push_back(tempString);
243 LogDebug("address fetch fail" << addressCount);
246 catch (const WrtDeviceApis::Commons::Exception& ex)
249 LogError("Exception: " << ex.GetMessage());
252 msg_release_list_struct(&convViewList);
253 msg_release_struct(&msgInfo);
254 msg_release_struct(&sendOpt);
260 // It is worth for below function to do test for fetching all conversations
261 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
265 MSG_HANDLE_T handle = MsgGetCommonHandle();
266 MSG_LIST_S convViewList = {0, NULL};
267 MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
268 msg_error_t err = MSG_SUCCESS;
269 unsigned int lastMsgIndex = 0;
270 char *tempString = NULL;
271 int addressCount = 0;
278 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
280 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
283 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
285 LogDebug(threadIndex << ":" << threadViewList.nCount);
286 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
290 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
292 m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
295 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
298 case MSG_TYPE_SMS_CB:
299 case MSG_TYPE_SMS_JAVACB:
300 case MSG_TYPE_SMS_WAPPUSH:
301 case MSG_TYPE_SMS_MWI:
302 case MSG_TYPE_SMS_SYNCML:
303 case MSG_TYPE_SMS_REJECT:
304 m_type = Api::Messaging::SMS;
305 LogDebug("Type:SMS");
308 case MSG_TYPE_MMS_JAVA:
309 case MSG_TYPE_MMS_NOTI:
310 m_type = Api::Messaging::MMS;;
311 LogDebug("Type:MMS");
317 m_time = *(msg_thread_view_get_time(threadViewList.msgThreadInfo[threadIndex]));
318 m_unreadMessages = msg_thread_view_get_unread_cnt(threadViewList.msgThreadInfo[threadIndex]);
319 tempString = (char*)msg_thread_view_get_data(threadViewList.msgThreadInfo[threadIndex]);
321 if (tempString != NULL)
323 m_preview = tempString;
324 LogDebug("preview" << m_preview);
328 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
330 if (err != MSG_SUCCESS)
332 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
335 lastMsgIndex = convViewList.nCount - 1;
336 m_messageCount = convViewList.nCount;
337 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
339 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
341 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
343 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
345 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
347 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
349 if (tempString != NULL)
355 LogDebug("address is null ");
360 LogDebug("address count index fail");
362 LogDebug("from" << m_from);
366 LogDebug("Msg direction: MO, from will be omitted");
370 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
372 if (tempString != NULL)
374 m_subject = tempString;
377 LogDebug("subject" << m_subject);
380 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
383 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
385 for (index = 0; index < addressCount; index++)
387 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
389 if (tempString != NULL)
391 m_to.push_back(tempString);
394 LogDebug("address count" << addressCount);
398 LogDebug("address fetch fail" << addressCount);
401 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
403 m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
406 LogDebug("message id" << m_lastMessageId);
409 catch (const WrtDeviceApis::Commons::Exception& ex)
412 LogError("Exception: " << ex.GetMessage());
414 if (convViewList.msgInfo != NULL)
416 msg_release_list_struct(&convViewList);
419 if (threadViewList.msgThreadInfo != NULL)
421 msg_release_thread_view_list(&threadViewList);
427 bool Conversation::makeConversationFromMsgId(unsigned int msgId, Api::Messaging::MessageType msgType)
431 msg_handle_t handle = MsgGetCommonHandle();
433 msg_struct_t msgInfo = NULL;
434 msg_struct_t sendOpt = NULL;
436 msg_struct_t msg_thread = NULL;
438 msg_struct_list_s convViewList;
439 msg_struct_list_s *addr_list = NULL;
441 msg_error_t err = MSG_SUCCESS;
448 unsigned int lastMsgIndex = 0;
450 char *tempString = NULL;
451 char msgData[MAX_THREAD_DATA_LEN] = {0,};
456 // get msg from msgId
457 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
458 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
460 if (msg_get_message(handle, (msg_message_id_t)msgId, msgInfo, sendOpt) != MSG_SUCCESS)
462 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
465 // get threadId from message
466 msg_get_int_value(msgInfo, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
467 m_Id = (unsigned int)tempInt;
468 LogDebug("m_Id : " << m_Id);
471 msg_thread = msg_create_struct(MSG_STRUCT_THREAD_INFO);
472 err = msg_get_thread(handle, m_Id, msg_thread);
475 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
479 case MSG_TYPE_SMS_CB:
480 case MSG_TYPE_SMS_JAVACB:
481 case MSG_TYPE_SMS_WAPPUSH:
482 case MSG_TYPE_SMS_MWI:
483 case MSG_TYPE_SMS_SYNCML:
484 case MSG_TYPE_SMS_REJECT:
485 m_type = Api::Messaging::SMS;
486 LogDebug("Type:SMS");
489 case MSG_TYPE_MMS_JAVA:
490 case MSG_TYPE_MMS_NOTI:
491 m_type = Api::Messaging::MMS;;
492 LogDebug("Type:MMS");
497 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
499 LogDebug("m_time : " << m_time);
501 // get thread unread count
502 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
503 m_unreadMessages = tempInt;
504 LogDebug("m_unreadMessages : " << m_unreadMessages);
506 // get thread preview
507 msg_get_str_value(msg_thread, MSG_THREAD_MSG_DATA_STR, msgData, MAX_THREAD_DATA_LEN);
508 tempString = msgData; // convert char to string
510 if (tempString != NULL)
512 m_preview = tempString;
514 LogDebug("m_preview : " << m_preview);
516 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
518 if (err != MSG_SUCCESS)
520 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
523 // get thread message count
524 lastMsgIndex = convViewList.nCount - 1;
525 m_messageCount = convViewList.nCount;
526 LogDebug("m_messageCount : " << m_messageCount);
528 // get thread read status
529 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_READ_BOOL, &tempBool);
531 LogDebug("m_read : " << m_read);
533 // get thread last message Id
534 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_ID_INT, &tempInt);
535 m_lastMessageId = tempInt;
536 LogDebug("m_lastMessageId : " << m_lastMessageId);
539 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
541 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
544 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_MESSAGE_DIRECTION_INT, &tempInt);
546 LogDebug("direction : " << type);
548 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
549 nToCnt = addr_list->nCount;
551 if (type == MSG_DIRECTION_TYPE_MT)
554 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
556 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
557 msg_get_str_value(addr_list->msg_struct_info[nToCnt-1], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
559 if (strNumber != NULL)
561 m_from = strNumber; // convert char to string
565 LogDebug("address is null ");
570 LogDebug("address count index fail");
576 LogDebug("Msg direction: MO, from will be omitted");
578 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
580 for (int index = 0; index < nToCnt; index++)
582 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
583 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
584 tempString = strNumber;
585 if (tempString != NULL)
587 LogDebug("m_to : " << tempString);
588 m_to.push_back(tempString);
594 LogDebug("address fetch fail");
598 // get thread subject
599 char strTemp[MAX_SUBJECT_LEN] = {0};
600 msg_get_str_value(msgInfo, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
601 tempString = strTemp;
602 if (tempString != NULL)
604 m_subject = tempString; // convert char to string
606 LogDebug("m_subject : " << m_subject);
609 catch (const WrtDeviceApis::Commons::Exception& ex)
612 LogError("Exception: " << ex.GetMessage());
614 catch (const WrtDeviceApis::Commons::UnknownException& ex)
617 LogError("Exception: " << ex.GetMessage());
620 msg_release_list_struct(&convViewList);
621 msg_release_struct(&msgInfo);
622 msg_release_struct(&sendOpt);
623 msg_release_struct(&msg_thread);
628 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
632 email_mail_list_item_t *resultMail = NULL;
633 email_mail_list_item_t *mailList = NULL;
637 email_mail_data_t* mailData = NULL;
642 // Todo : will be re-implemented as using email_get_thread_information_ex
643 if(email_get_thread_information_ex(emailTreadId, &resultMail) != EMAIL_ERROR_NONE)
645 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
654 if (email_get_mail_data(resultMail->mail_id,&mailData) != EMAIL_ERROR_NONE)
656 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
660 m_unreadMessages = 0;
662 LogDebug("start email_get_mail_list");
663 if (email_get_mail_list(mailData->account_id, 0, emailTreadId, 0, resultMail->thread_item_count,
664 EMAIL_SORT_DATETIME_HIGH, &mailList, &count) != EMAIL_ERROR_NONE)
666 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
668 LogDebug("end email_get_mail_list");
671 for (index = 0; index < count; index++)
673 if (mailList[index].flags_seen_field)
677 LogDebug(mailList[index].flags_seen_field);
681 m_messageCount = resultMail->thread_item_count;
685 m_type = Api::Messaging::EMAIL;
687 // time horrible split
688 // 2011 11 08 00 35 45
691 m_time = resultMail->date_time;
694 if (resultMail->datetime[0] != '\0')
696 char buf[MAX_DATETIME_STRING_LENGTH];
697 char *targetBuf = resultMail->datetime;
700 memset(buf, 0x00, sizeof(buf));
701 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
702 timeinfo.tm_year = atoi(buf) - 1900;
704 memset(buf, 0x00, sizeof(buf));
705 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
706 timeinfo.tm_mon = atoi(buf) - 1;
708 memset(buf, 0x00, sizeof(buf));
709 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
710 timeinfo.tm_mday = atoi(buf);
713 memset(buf, 0x00, sizeof(buf));
714 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
715 timeinfo.tm_hour = atoi(buf);
717 memset(buf, 0x00, sizeof(buf));
718 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
719 timeinfo.tm_min = atoi(buf);
721 memset(buf, 0x00, sizeof(buf));
722 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
723 timeinfo.tm_sec = atoi(buf);
725 m_time = mktime(&timeinfo);
733 if (resultMail->previewBodyText[0] != '\0')
735 m_preview = resultMail->previewBodyText;
739 if (mail->head->previewBodyText != NULL)
741 LogDebug(mail->head->previewBodyText);
742 m_preview = mail->head->previewBodyText;
746 m_read = (bool)resultMail->flags_seen_field;
749 if (resultMail->from[0] != '\0')
751 m_from = resultMail->from;
756 if (resultMail->subject[0] != '\0');
758 m_subject = resultMail->subject;
762 if (mailData->full_address_to != NULL)
764 LogDebug("Header To " << mailData->full_address_to);
765 m_to.push_back(mailData->full_address_to);
768 if (mailData->full_address_bcc != NULL)
770 LogDebug("Header Bcc " << mailData->full_address_bcc);
771 m_to.push_back(mailData->full_address_bcc);
774 if (mailData->full_address_cc != NULL)
776 LogDebug("Header CC " << mailData->full_address_cc);
777 m_to.push_back(mailData->full_address_cc);
780 m_lastMessageId = resultMail->mail_id;
781 LogDebug(m_lastMessageId);
784 catch (const WrtDeviceApis::Commons::Exception& ex)
787 LogError("Exception: " << ex.GetMessage());
790 if (mailData != NULL)
792 email_free_mail_data(&mailData , 1);
795 if (resultMail != NULL)
803 Conversation::~Conversation()
809 void Conversation::setId(unsigned int id)
813 void Conversation::setType(unsigned short type)
817 void Conversation::setTime(time_t time)
821 void Conversation::setMessageCount(unsigned long messageCount)
823 m_messageCount = messageCount;
825 void Conversation::setUnreadMessages(unsigned long unreadMessages)
827 m_unreadMessages = unreadMessages;
829 void Conversation::setPreview(unsigned long preview)
833 void Conversation::setRead(bool read)
837 void Conversation::setFrom(std::string from)
841 void Conversation::setTo(std::vector<std::string> to)
845 void Conversation::setCC(std::vector<std::string> cc)
849 void Conversation::setBCC(std::vector<std::string> bcc)
853 void Conversation::setLastMessageId(unsigned int id)
855 m_lastMessageId = id;
859 void Conversation::setId(const unsigned int id)
865 unsigned int Conversation::getId()
869 unsigned short Conversation::getType()
873 time_t Conversation::getTime()
877 unsigned long Conversation::getMessageCount()
879 return m_messageCount;
881 unsigned long Conversation::getUnreadMessages()
883 return m_unreadMessages;
885 std::string Conversation::getPreview()
890 std::string Conversation::getSubject()
896 bool Conversation::getRead()
900 std::string Conversation::getFrom()
904 std::vector<std::string> Conversation::getTo()
908 std::vector<std::string> Conversation::getCC()
912 std::vector<std::string> Conversation::getBCC()
916 unsigned int Conversation::getLastMessageId()
918 return m_lastMessageId;
921 bool Conversation::getResult()