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;
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)
459 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
467 if (email_get_mail(&mailbox, resultMail->mail_id, &mail) != EMF_ERROR_NONE)
469 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
473 m_unreadMessages = 0;
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)
479 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
481 LogDebug("end email_get_mail_list_ex");
484 accountId = resultMail->account_id;
486 for (index = 0; index < count; index++)
488 if (mailList[index].flags_seen_field)
492 LogDebug(mailList[index].flags_seen_field);
496 m_messageCount = resultMail->thread_item_count;
500 m_type = Api::Messaging::EMAIL;
502 // time horrible split
503 // 2011 11 08 00 35 45
504 if (resultMail->datetime[0] != '\0')
506 char buf[MAX_DATETIME_STRING_LENGTH];
507 char *targetBuf = resultMail->datetime;
510 memset(buf, 0x00, sizeof(buf));
511 targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
512 timeinfo.tm_year = atoi(buf) - 1900;
514 memset(buf, 0x00, sizeof(buf));
515 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
516 timeinfo.tm_mon = atoi(buf) - 1;
518 memset(buf, 0x00, sizeof(buf));
519 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
520 timeinfo.tm_mday = atoi(buf);
523 memset(buf, 0x00, sizeof(buf));
524 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
525 timeinfo.tm_hour = atoi(buf);
527 memset(buf, 0x00, sizeof(buf));
528 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
529 timeinfo.tm_min = atoi(buf);
532 memset(buf, 0x00, sizeof(buf));
533 targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
534 timeinfo.tm_sec = atoi(buf);
536 m_time = mktime(&timeinfo);
542 if (resultMail->previewBodyText[0] != '\0')
544 m_preview = resultMail->previewBodyText;
548 if (mail->head->previewBodyText != NULL)
550 LogDebug(mail->head->previewBodyText);
551 m_preview = mail->head->previewBodyText;
555 m_read = (bool)resultMail->flags_seen_field;
558 if (resultMail->from[0] != '\0')
560 m_from = resultMail->from;
565 if (resultMail->subject[0] != '\0');
567 m_subject = resultMail->subject;
572 if (mail->head!= NULL)
574 if (mail->head->bcc != NULL)
576 LogDebug(mail->head->bcc);
577 m_bcc.push_back(mail->head->bcc);
580 if (mail->head->to != NULL)
582 LogDebug(mail->head->to);
583 m_to.push_back(mail->head->to);
586 if (mail->head->cc != NULL)
588 LogDebug(mail->head->cc);
589 m_cc.push_back(mail->head->cc);
594 m_lastMessageId = resultMail->mail_id;
595 LogDebug(m_lastMessageId);
598 catch (const WrtDeviceApis::Commons::Exception& ex)
601 LogError("Exception: " << ex.GetMessage());
606 email_free_mail(&mail , 1);
609 if (resultMail != NULL)
618 Conversation::~Conversation()
624 void Conversation::setId(unsigned int id)
628 void Conversation::setType(unsigned short type)
632 void Conversation::setTime(time_t time)
636 void Conversation::setMessageCount(unsigned long messageCount)
638 m_messageCount = messageCount;
640 void Conversation::setUnreadMessages(unsigned long unreadMessages)
642 m_unreadMessages = unreadMessages;
644 void Conversation::setPreview(unsigned long preview)
648 void Conversation::setRead(bool read)
652 void Conversation::setFrom(std::string from)
656 void Conversation::setTo(std::vector<std::string> to)
660 void Conversation::setCC(std::vector<std::string> cc)
664 void Conversation::setBCC(std::vector<std::string> bcc)
668 void Conversation::setLastMessageId(unsigned int id)
670 m_lastMessageId = id;
674 unsigned int Conversation::getId()
678 unsigned short Conversation::getType()
682 time_t Conversation::getTime()
686 unsigned long Conversation::getMessageCount()
688 return m_messageCount;
690 unsigned long Conversation::getUnreadMessages()
692 return m_unreadMessages;
694 std::string Conversation::getPreview()
699 std::string Conversation::getSubject()
705 bool Conversation::getRead()
709 std::string Conversation::getFrom()
713 std::vector<std::string> Conversation::getTo()
717 std::vector<std::string> Conversation::getCC()
721 std::vector<std::string> Conversation::getBCC()
725 unsigned int Conversation::getLastMessageId()
727 return m_lastMessageId;
730 bool Conversation::getResult()