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 <Emf_Mapi_Message.h>
33 Conversation::Conversation()
38 Conversation::Conversation(unsigned int threadId, Api::Messaging::MessageType msgType)
42 if (msgType == Api::Messaging::EMAIL)
44 makeConversationFromEmailThreadId(threadId);
48 makeConversationFromMsgId(threadId, msgType);
52 Conversation::Conversation(std::string threadId, Api::Messaging::MessageType msgType)
56 std::istringstream stream(threadId);
58 LogDebug("threadId : " << threadId);
60 unsigned int number = 0;
62 makeConversationFromMsgId(number, msgType);
65 Conversation::Conversation(unsigned int threadIndex)
67 makeConversationFromThreadIndex(threadIndex);
70 Conversation::Conversation(msg_thread_view_t msg_thread)
72 makeConversationFromThread(msg_thread);
75 void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
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;
92 if (msg_thread_view_get_thread_id(msg_thread) >= 0)
94 m_Id = msg_thread_view_get_thread_id(msg_thread);
97 switch(msg_thread_view_get_message_type(msg_thread))
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");
110 case MSG_TYPE_MMS_JAVA:
111 case MSG_TYPE_MMS_NOTI:
112 m_type = Api::Messaging::MMS;;
113 LogDebug("Type:MMS");
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);
123 if (tempString != NULL)
125 m_preview = tempString;
126 LogDebug("preview" << m_preview);
130 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
132 if (err != MSG_SUCCESS)
134 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
137 lastMsgIndex = convViewList.nCount - 1;
138 m_messageCount = convViewList.nCount;
139 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
141 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
143 m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
144 LogDebug("message id" << m_lastMessageId);
148 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message id fail");
151 if (msg_get_message(handle, m_lastMessageId, msgForLastIndex, &sendOpt) != MSG_SUCCESS)
153 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
156 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
158 if (msg_get_direction_info(msgForLastIndex) == MSG_DIRECTION_TYPE_MT)
160 addressCount = msg_get_address_count(msgForLastIndex);
162 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
164 tempString = (char*)msg_get_ith_address(msgForLastIndex, 0);
166 if (tempString != NULL)
172 LogDebug("address is null ");
177 LogDebug("address count index fail");
179 LogDebug("from" << m_from);
183 LogDebug("Msg direction: MO, from will be omitted");
187 tempString = (char*)msg_get_subject(msgForLastIndex);
189 if (tempString != NULL)
191 m_subject = tempString;
194 LogDebug("subject" << m_subject);
197 addressCount = msg_get_address_count(msgForLastIndex);
200 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
202 for (index = 0; index < addressCount; index++)
204 tempString = (char*)msg_get_ith_address(msgForLastIndex, index);
206 if (tempString != NULL)
208 m_to.push_back(tempString);
211 LogDebug("address count" << addressCount);
215 LogDebug("address fetch fail" << addressCount);
221 catch (const WrtDeviceApis::Commons::Exception& ex)
224 LogError("Exception: " << ex.GetMessage());
227 if (convViewList.msgInfo != NULL)
229 msg_release_message_list(&convViewList);
232 if (msgForLastIndex != NULL)
234 msg_release_message(&msgForLastIndex);
240 // It is worth for below function to do test for fetching all conversations
241 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
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;
257 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
259 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
262 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
264 LogDebug(threadIndex << ":" << threadViewList.nCount);
265 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
269 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
271 m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
274 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
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");
287 case MSG_TYPE_MMS_JAVA:
288 case MSG_TYPE_MMS_NOTI:
289 m_type = Api::Messaging::MMS;;
290 LogDebug("Type:MMS");
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]);
300 if (tempString != NULL)
302 m_preview = tempString;
303 LogDebug("preview" << m_preview);
307 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
309 if (err != MSG_SUCCESS)
311 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
314 lastMsgIndex = convViewList.nCount - 1;
315 m_messageCount = convViewList.nCount;
316 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
318 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
320 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
322 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
324 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
326 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
328 if (tempString != NULL)
334 LogDebug("address is null ");
339 LogDebug("address count index fail");
341 LogDebug("from" << m_from);
345 LogDebug("Msg direction: MO, from will be omitted");
349 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
351 if (tempString != NULL)
353 m_subject = tempString;
356 LogDebug("subject" << m_subject);
359 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
362 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
364 for (index = 0; index < addressCount; index++)
366 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
368 if (tempString != NULL)
370 m_to.push_back(tempString);
373 LogDebug("address count" << addressCount);
377 LogDebug("address fetch fail" << addressCount);
380 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
382 m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
385 LogDebug("message id" << m_lastMessageId);
388 catch (const WrtDeviceApis::Commons::Exception& ex)
391 LogError("Exception: " << ex.GetMessage());
393 if (convViewList.msgInfo != NULL)
395 msg_release_message_list(&convViewList);
398 if (threadViewList.msgThreadInfo != NULL)
400 msg_release_thread_view_list(&threadViewList);
406 bool Conversation::makeConversationFromMsgId(unsigned int threadId, Api::Messaging::MessageType msgType)
409 MSG_HANDLE_T handle = MsgGetCommonHandle();
410 MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
411 int conversationId = 0;
415 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
417 LogDebug("get thread view fail");
422 conversationId = Api::Messaging::IMessaging::getInstance().getConversationId(threadId, msgType);
424 for(index = 0; index < threadViewList.nCount; index++)
426 if (conversationId == msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[index]))
428 LogDebug("Found" << conversationId);
429 makeConversationFromThreadIndex(index);
438 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
442 emf_mail_list_item_t *resultMail = NULL;
443 emf_mail_list_item_t *mailList = NULL;
447 emf_mailbox_t mailbox = {};
448 emf_mail_t *mail = NULL;
455 // Todo : will be re-implemented as using email_get_thread_information_ex
456 if(email_get_thread_information_ex(emailTreadId, &resultMail) != EMF_ERROR_NONE)
458 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
462 if (email_get_mail(&mailbox, resultMail->mail_id, &mail) != EMF_ERROR_NONE)
464 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
469 m_unreadMessages = 0;
471 if (email_get_mail_list_ex(accountId, NULL, emailTreadId, 0, resultMail->thread_item_count,
472 EMF_SORT_DATETIME_HIGH, &mailList, &count) != EMF_ERROR_NONE)
474 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
478 accountId = resultMail->account_id;
480 for (index = 0; index < count; index++)
482 if (mailList[index].flags_seen_field)
486 LogDebug(mailList[index].flags_seen_field);
490 m_messageCount = resultMail->thread_item_count;
494 m_type = Api::Messaging::EMAIL;
496 // time horrible split
497 // 2011 11 08 00 35 45
498 if (resultMail->datetime[0] != '\0')
500 char buf[MAX_DATETIME_STRING_LENGTH];
501 char *targetBuf = resultMail->datetime;
504 memset(buf, 0x00, sizeof(buf));
505 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
506 timeinfo.tm_year = atoi(buf) - 1900;
508 memset(buf, 0x00, sizeof(buf));
509 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
510 timeinfo.tm_mon = atoi(buf) - 1;
512 memset(buf, 0x00, sizeof(buf));
513 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
514 timeinfo.tm_mday = atoi(buf);
517 memset(buf, 0x00, sizeof(buf));
518 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
519 timeinfo.tm_hour = atoi(buf);
521 memset(buf, 0x00, sizeof(buf));
522 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
523 timeinfo.tm_min = atoi(buf);
526 memset(buf, 0x00, sizeof(buf));
527 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
528 timeinfo.tm_sec = atoi(buf);
530 m_time = mktime(&timeinfo);
536 if (resultMail->previewBodyText[0] != '\0')
538 m_preview = resultMail->previewBodyText;
542 if (mail->head->previewBodyText != NULL)
544 LogDebug(mail->head->previewBodyText);
545 m_preview = mail->head->previewBodyText;
549 m_read = (bool)resultMail->flags_seen_field;
553 if (resultMail->from[0] != '\0')
555 m_from = resultMail->from;
560 if (resultMail->subject[0] != '\0');
562 m_subject = resultMail->subject;
567 if (mail->head!= NULL)
569 if (mail->head->bcc != NULL)
571 LogDebug(mail->head->bcc);
572 m_bcc.push_back(mail->head->bcc);
575 if (mail->head->to != NULL)
577 LogDebug(mail->head->to);
578 m_to.push_back(mail->head->to);
581 if (mail->head->cc != NULL)
583 LogDebug(mail->head->cc);
584 m_cc.push_back(mail->head->cc);
589 m_lastMessageId = resultMail->mail_id;
590 LogDebug(m_lastMessageId);
593 catch (const WrtDeviceApis::Commons::Exception& ex)
596 LogError("Exception: " << ex.GetMessage());
599 if (resultMail != NULL)
606 email_free_mail(&mail , 1);
612 Conversation::~Conversation()
618 void Conversation::setId(unsigned int id)
622 void Conversation::setType(unsigned short type)
626 void Conversation::setTime(time_t time)
630 void Conversation::setMessageCount(unsigned long messageCount)
632 m_messageCount = messageCount;
634 void Conversation::setUnreadMessages(unsigned long unreadMessages)
636 m_unreadMessages = unreadMessages;
638 void Conversation::setPreview(unsigned long preview)
642 void Conversation::setRead(bool read)
646 void Conversation::setFrom(std::string from)
650 void Conversation::setTo(std::vector<std::string> to)
654 void Conversation::setCC(std::vector<std::string> cc)
658 void Conversation::setBCC(std::vector<std::string> bcc)
662 void Conversation::setLastMessageId(unsigned int id)
664 m_lastMessageId = id;
668 unsigned int Conversation::getId()
672 unsigned short Conversation::getType()
676 time_t Conversation::getTime()
680 unsigned long Conversation::getMessageCount()
682 return m_messageCount;
684 unsigned long Conversation::getUnreadMessages()
686 return m_unreadMessages;
688 std::string Conversation::getPreview()
693 std::string Conversation::getSubject()
699 bool Conversation::getRead()
703 std::string Conversation::getFrom()
707 std::vector<std::string> Conversation::getTo()
711 std::vector<std::string> Conversation::getCC()
715 std::vector<std::string> Conversation::getBCC()
719 unsigned int Conversation::getLastMessageId()
721 return m_lastMessageId;
724 bool Conversation::getResult()