Update change log and spec for wrt-plugins-tizen_0.4.38
[framework/web/wrt-plugins-tizen.git] / src / Messaging / Conversation.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17  
18
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>
26 #include <time.h>
27
28 // this is msgInternalTypes
29 #define MAX_THREAD_DATA_LEN     128
30
31 using namespace DPL;
32
33 namespace DeviceAPI {
34 namespace Messaging {
35
36 Conversation::Conversation()
37 {
38
39 }
40
41 Conversation::Conversation(unsigned int threadId, MessageType msgType)
42 {
43         LoggerD("Enter");
44         
45         if (msgType == EMAIL)
46         {
47                 makeConversationFromEmailThreadId(threadId);
48         }
49         else if((msgType == EMPTY_MESSAGE_CONVERSATION)||(msgType == EMPTY_EMAIL_CONVERSATION))
50         {
51                 makeEmptyConversation(threadId, msgType);
52         }
53         else
54         {
55                 makeConversationFromMsgId(threadId, msgType);
56         }
57 }
58         
59 Conversation::Conversation(std::string threadId, MessageType msgType)
60 {
61         LoggerD("Enter");
62         bool result = FALSE;
63         std::istringstream stream(threadId);
64         
65         LoggerD("threadId : " << threadId);
66         
67         unsigned int number = 0;
68         stream >> number;
69         result = makeConversationFromMsgId(number, msgType);
70         if(result == FALSE)
71         {
72                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,     "fail to make conversation");
73         }
74 }
75
76 Conversation::Conversation(unsigned int threadIndex)
77 {
78         makeConversationFromThreadIndex(threadIndex);
79 }
80
81 //Conversation::Conversation(msg_thread_view_t msg_thread)
82 Conversation::Conversation(msg_struct_t msg_thread)
83 {
84         makeConversationFromThread(msg_thread);
85 }
86
87 //void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
88 void Conversation::makeConversationFromThread(msg_struct_t msg_thread)  
89 {
90         LoggerD("Enter");
91         msg_handle_t handle = MsgGetCommonHandle();
92
93         msg_struct_list_s convViewList;
94         msg_struct_list_s *addr_list = NULL;
95
96         msg_struct_t msgInfo = NULL;
97         msg_struct_t sendOpt = NULL;
98         
99         msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
100         sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
101         
102         msg_error_t err = MSG_SUCCESS;
103         unsigned int lastMsgIndex = 0;
104         char *tempString = NULL;
105         int addressCount = 0;
106         int index = 0;
107         int tempInt;
108         bool tempBool;
109         char msgData[128] = {0,};
110         m_result = true;
111         int nToCnt;
112
113         try 
114         {
115                 //      get thread Id
116                 msg_get_int_value(msg_thread, MSG_THREAD_ID_INT, &tempInt);
117                 m_Id = tempInt;
118                 
119                 //      get thread type
120                 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
121                 switch(tempInt)
122                 {
123                         case MSG_TYPE_SMS:
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:
130                                 m_type = SMS;
131                                 LoggerD("Type:SMS");
132                                 break;
133                         case MSG_TYPE_MMS:
134                         case MSG_TYPE_MMS_JAVA:
135                         case MSG_TYPE_MMS_NOTI:
136                                 m_type = MMS;;
137                                 LoggerD("Type:MMS");
138                                 break;
139                 } 
140                 
141                 //      get thread time
142                 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
143                 m_time = tempInt;
144
145                 //      get thread unread count
146                 msg_get_int_value(msg_thread, MSG_THREAD_UNREAD_COUNT_INT, &tempInt);
147                 m_unreadMessages = tempInt;
148
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
152
153                 if (tempString != NULL)
154                 {
155                         m_preview = tempString; 
156                         LoggerD("preview" << m_preview);
157                 }
158
159                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
160                 
161                 if (err != MSG_SUCCESS)
162                 {
163                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
164                 }
165
166                 //      get thread message count
167                 lastMsgIndex = convViewList.nCount - 1;
168                 m_messageCount = convViewList.nCount;
169
170                 //      get thread read status
171                 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_READ_BOOL, &tempBool);
172                 m_read = tempBool;
173
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;
177
178                 //      get thread from
179                 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
180                 {
181                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
182                 }
183
184                 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_DIRECTION_INT, &tempInt);
185                 int type = tempInt;
186
187                 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
188
189                 nToCnt = addr_list->nCount;
190
191                 if (type == MSG_DIRECTION_TYPE_MT)
192                 {
193
194                         if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
195                         {
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);
198                                 
199                                 if (strNumber[0] != '\0')
200                                 {
201                                         m_from = strNumber;     // convert char to string
202                                 }
203                                 else
204                                 {
205                                         LoggerD("address is null ");
206                                 }
207                         }
208                         else 
209                         {
210                                 LoggerD("address count index fail");
211                         }
212                         
213                 }
214                 else
215                 {
216                         LoggerD("Msg direction: MO, from will be omitted");
217                 }
218
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)
224                 {
225                         m_subject = tempString; // convert char to string
226                 }
227
228                 //      get thread to           
229                 if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
230                 {
231                         for (index = 0; index < nToCnt; index++)
232                         {
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)
237                                 {
238                                         m_to.push_back(tempString);
239                                 }
240                         }
241                 }
242                 else
243                 {
244                         LoggerD("address fetch fail" << addressCount);
245                 }
246         }
247         catch (const WrtDeviceApis::Commons::Exception& ex) 
248         {
249                 m_result = false;
250                 LoggerE("Exception: " << ex.GetMessage());
251         }
252         
253         msg_release_list_struct(&convViewList);
254         msg_release_struct(&msgInfo);
255         msg_release_struct(&sendOpt);
256
257 }
258
259
260
261 // It is worth for below function to do test for fetching all conversations
262 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
263 {
264 /*
265         LoggerD("Enter");
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;
273         int index = 0;
274         
275         m_result = true;
276
277         try 
278         {
279                 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
280                 {
281                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
282                 }
283
284                 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
285                 {
286                         LoggerD(threadIndex << ":" << threadViewList.nCount);
287                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
288                 }
289
290                 
291                 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
292                 {
293                         m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
294                 }
295                 
296                 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
297                 {
298                         case MSG_TYPE_SMS:
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:
305                                 m_type = SMS;
306                                 LoggerD("Type:SMS");
307                                 break;
308                         case MSG_TYPE_MMS:
309                         case MSG_TYPE_MMS_JAVA:
310                         case MSG_TYPE_MMS_NOTI:
311                                 m_type = MMS;;
312                                 LoggerD("Type:MMS");
313                                 break;
314 //                      default:
315                                 // Todo email / chat
316                 } 
317                 
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]);
321
322                 if (tempString != NULL)
323                 {
324                         m_preview = tempString; 
325                         LoggerD("preview" << m_preview);
326                 }
327
328                 
329                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
330                 
331                 if (err != MSG_SUCCESS)
332                 {
333                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
334                 }
335
336                 lastMsgIndex = convViewList.nCount - 1;
337                 m_messageCount = convViewList.nCount;
338                 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
339                 
340                 LoggerD("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
341
342                 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
343                 {
344                         addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
345
346                         if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
347                         {
348                     tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
349                                 
350                                 if (tempString != NULL)
351                                 {
352                                         m_from = tempString;
353                                 }
354                                 else
355                                 {
356                                         LoggerD("address is null ");
357                                 }
358                         }
359                         else 
360                         {
361                                 LoggerD("address count index fail");
362                         }
363                         LoggerD("from" << m_from);
364                 }
365                 else
366                 {
367                         LoggerD("Msg direction: MO, from will be omitted");
368                 }
369
370
371                 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
372
373                 if (tempString != NULL)
374                 {
375                         m_subject = tempString;
376                 }
377
378                 LoggerD("subject" << m_subject);
379                 
380
381                 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
382
383
384                 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
385                 {
386                         for (index = 0; index < addressCount; index++)
387                         {
388                                 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
389
390                                 if (tempString != NULL)
391                                 {
392                                         m_to.push_back(tempString);
393                                 }
394                         }
395                         LoggerD("address count" << addressCount);
396                 }
397                 else
398                 {
399                         LoggerD("address fetch fail" << addressCount);
400                 }
401
402                 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
403                 {
404                         m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
405                 }
406
407                 LoggerD("message id" << m_lastMessageId);
408
409         }
410         catch (const WrtDeviceApis::Commons::Exception& ex) 
411         {
412                 m_result = false;
413                 LoggerE("Exception: " << ex.GetMessage());
414         }
415         if (convViewList.msgInfo != NULL) 
416         {
417                 msg_release_list_struct(&convViewList);
418         }
419
420         if (threadViewList.msgThreadInfo != NULL)
421         {
422                 msg_release_thread_view_list(&threadViewList);
423         }
424 */
425
426 }
427
428 bool Conversation::makeConversationFromMsgId(unsigned int msgId, MessageType msgType)
429 {
430         LoggerD("Enter");
431
432         msg_handle_t handle = MsgGetCommonHandle();
433
434         msg_struct_t msgInfo = NULL;
435         msg_struct_t sendOpt = NULL;    
436
437         msg_struct_t msg_thread = NULL;
438
439         msg_struct_list_s convViewList;
440         msg_struct_list_s *addr_list = NULL;
441         
442         msg_error_t err = MSG_SUCCESS;
443
444         int tempInt;
445         bool tempBool;
446
447         int nToCnt;
448         
449         unsigned int lastMsgIndex = 0;
450
451         char *tempString = NULL;
452         char msgData[MAX_THREAD_DATA_LEN] = {0,};
453         m_result = true;
454
455         try 
456         {
457                 //      get msg from msgId
458                 msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
459                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
460                 
461                 if (msg_get_message(handle, (msg_message_id_t)msgId, msgInfo, sendOpt) != MSG_SUCCESS)
462                 {
463                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
464                 }
465
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);
470
471                 // get Thread
472                 msg_thread = msg_create_struct(MSG_STRUCT_THREAD_INFO);
473                 err = msg_get_thread(handle, m_Id, msg_thread);
474
475                 //      get thread type
476                 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TYPE_INT, &tempInt);
477                 switch(tempInt)
478                 {
479                         case MSG_TYPE_SMS:
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:
486                                 m_type = SMS;
487                                 LoggerD("Type:SMS");
488                                 break;
489                         case MSG_TYPE_MMS:
490                         case MSG_TYPE_MMS_JAVA:
491                         case MSG_TYPE_MMS_NOTI:
492                                 m_type = MMS;;
493                                 LoggerD("Type:MMS");
494                                 break;
495                 } 
496
497                 //      get thread time
498                 msg_get_int_value(msg_thread, MSG_THREAD_MSG_TIME_INT, &tempInt);
499                 m_time = tempInt;
500                 LoggerD("m_time : " << m_time);
501
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);
506
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
510
511                 if (tempString != NULL)
512                 {
513                         m_preview = tempString; 
514                 }
515                 LoggerD("m_preview : " << m_preview);
516
517                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
518                         
519                 if (err != MSG_SUCCESS)
520                 {
521                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
522                 }
523
524                 //      get thread message count
525                 lastMsgIndex = convViewList.nCount - 1;
526                 m_messageCount = convViewList.nCount;
527                 LoggerD("m_messageCount : " << m_messageCount);
528
529                 //      get thread read status
530                 msg_get_bool_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_READ_BOOL, &tempBool);
531                 m_read = tempBool;
532                 LoggerD("m_read : " << m_read);
533
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);
538
539                 //      get thread from
540                 if (msg_get_message(handle, m_lastMessageId, msgInfo, sendOpt) != MSG_SUCCESS)
541                 {
542                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
543                 }
544
545                 msg_get_int_value(convViewList.msg_struct_info[lastMsgIndex], MSG_CONV_MSG_DIRECTION_INT, &tempInt);
546                 int type = tempInt;
547                 LoggerD("direction : " << type);
548                 m_direction = type;
549
550                 msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
551                 nToCnt = addr_list->nCount;
552
553                 if (type == MSG_DIRECTION_TYPE_MT)
554                 {
555                         
556                         if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
557                         {
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);
560
561                                 if (strNumber[0] != '\0')
562                                 {
563                                         m_from = strNumber; // convert char to string
564                                 }
565                                 else
566                                 {
567                                         LoggerD("address is null ");
568                                 }
569                         }               
570                         else 
571                         {
572                                 LoggerD("address count index fail");
573                         }
574
575                 }
576                 else
577                 {
578                         LoggerD("Msg direction: MO, from will be omitted");
579                         //      get thread to           
580                         if (nToCnt > 0 && nToCnt < MAX_TO_ADDRESS_CNT )
581                         {
582                                 for (int index = 0; index < nToCnt; index++)
583                                 {
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)
588                                         {
589                                                 LoggerD("m_to : " << tempString);
590                                                 m_to.push_back(tempString);
591                                         }
592                                 }
593                         }
594                         else
595                         {
596                                 LoggerD("address fetch fail");
597                         }
598                 }
599
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)
605                 {
606                         m_subject = tempString; // convert char to string
607                 }               
608                 LoggerD("m_subject : " << m_subject);
609                 
610         }
611         catch (const WrtDeviceApis::Commons::UnknownException& ex)
612         {
613                 m_result = false;
614                 LoggerE("Exception: " << ex.GetMessage());
615         }
616         catch (const WrtDeviceApis::Commons::Exception& ex)
617         {
618                 m_result = false;
619                 LoggerE("Exception: " << ex.GetMessage());
620         }
621
622         msg_release_list_struct(&convViewList);
623         msg_release_struct(&msgInfo);
624         msg_release_struct(&sendOpt);
625         msg_release_struct(&msg_thread);
626         
627         return m_result;
628 }
629
630 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
631 {
632         LoggerD("Enter");
633         
634         email_mail_list_item_t *resultMail = NULL;
635         email_mail_list_item_t *mailList = NULL;
636         int index = 0;
637         int count = 0;
638
639         email_mail_data_t* mailData = NULL;
640         m_result = true;
641
642         try 
643         {
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) 
646                 {
647                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
648                 }
649
650                 if (!resultMail)
651                 {
652                         return NULL;
653                 }
654
655                 //get mail data
656                 if (email_get_mail_data(resultMail->mail_id,&mailData) != EMAIL_ERROR_NONE)
657                 {
658                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
659                 }
660         
661                 // account Id
662                 m_unreadMessages = 0;
663
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)
667                 {
668                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
669                 }
670                 LoggerD("end email_get_mail_list");
671                 
672                 // unread msg count
673                 for (index = 0; index < count; index++)
674                 {
675                         if (mailList[index].flags_seen_field)
676                         {
677                                 m_unreadMessages++;
678                         }
679                         LoggerD(mailList[index].flags_seen_field);
680                 }
681                 
682                 // message count
683                 m_messageCount = resultMail->thread_item_count;
684                 // id
685                 m_Id = emailTreadId;
686                 // type
687                 m_type = EMAIL;
688
689                 // time horrible split 
690                 // 2011 11 08 00 35 45
691
692                 m_time = resultMail->date_time;
693
694                 // read
695                 m_read = (bool)resultMail->flags_seen_field;
696
697                 // from
698                 if (resultMail->full_address_from[0] != '\0')
699                 {
700                         LoggerD("Header From " << resultMail->full_address_from);
701                         m_from = EmailUtils::stripAddress(resultMail->full_address_from);
702                         LoggerD("email From " << m_from);
703                 }
704
705                 // subject
706                 if (resultMail->subject[0] != '\0')
707                 {
708                         m_subject = resultMail->subject;
709                         LoggerD(m_subject);             
710                 }
711
712                 if (mailData->full_address_to != NULL)
713                 {
714                         LoggerD("Header To " << mailData->full_address_to);
715                         m_to = EmailUtils::stripAddressLine(mailData->full_address_to).getRecipients();
716                 }
717
718                 if (mailData->full_address_bcc != NULL)
719                 {
720                         LoggerD("Header Bcc " << mailData->full_address_bcc);
721                         m_bcc = EmailUtils::stripAddressLine(mailData->full_address_bcc).getRecipients();
722                 }
723
724                 if (mailData->full_address_cc != NULL)
725                 {
726                         LoggerD("Header CC " << mailData->full_address_cc);
727                         m_cc = EmailUtils::stripAddressLine(mailData->full_address_cc).getRecipients();
728                 }
729                 
730                 m_lastMessageId = resultMail->mail_id;
731                 LoggerD(m_lastMessageId);               
732                 m_result = true;
733         }
734         catch (const WrtDeviceApis::Commons::Exception& ex) 
735         {
736                 m_result = false;
737                 LoggerE("Exception: " << ex.GetMessage());
738         }
739
740         if (mailData != NULL)
741         {
742                 email_free_mail_data(&mailData , 1);
743         }
744         
745         if (resultMail != NULL)
746         {
747                 free(resultMail);
748         }
749
750         return m_result;
751
752 }
753
754 void Conversation::makeEmptyConversation(unsigned int threadIndex, MessageType msgType)
755 {
756         LoggerD("Enter");
757         if(msgType == EMPTY_MESSAGE_CONVERSATION)
758         {
759                 m_type = SMS;
760                 m_Id = 0;
761         }
762         else
763         {
764                 m_type = EMAIL;
765                 m_Id = threadIndex;
766         }
767         m_time = 0;
768         m_messageCount = 0;
769         m_unreadMessages = 0;
770         m_preview = "";
771         m_read = false;
772         m_from = "";
773         m_subject = "";
774         m_lastMessageId = 0;
775         m_direction = 0;
776         m_result = true;
777 }
778
779
780 Conversation::~Conversation()
781 {
782 }
783
784 #if 0
785 // setter
786 void Conversation::setId(unsigned int id)
787 {
788         m_id = id;
789 }
790 void Conversation::setType(unsigned short type)
791 {
792         m_type = type;
793 }
794 void Conversation::setTime(time_t time)
795 {
796         m_time = time;
797 }
798 void Conversation::setMessageCount(unsigned long messageCount)
799 {
800         m_messageCount = messageCount;
801 }
802 void Conversation::setUnreadMessages(unsigned long unreadMessages)
803 {
804         m_unreadMessages = unreadMessages;
805 }
806 void Conversation::setPreview(unsigned long preview)
807 {
808         m_preview = preview;
809 }
810 void Conversation::setRead(bool read)
811 {
812         m_read = read;
813 }
814 void Conversation::setFrom(std::string from)
815 {
816         m_from = from;
817 }
818 void Conversation::setTo(std::vector<std::string> to)
819 {
820         m_to = to;
821 }
822 void Conversation::setCC(std::vector<std::string> cc)
823 {
824         m_cc = cc;
825 }
826 void Conversation::setBCC(std::vector<std::string> bcc)
827 {
828         m_bcc = bcc;
829 }
830 void Conversation::setLastMessageId(unsigned int id)
831 {
832         m_lastMessageId = id;
833 }
834 #endif
835
836 void Conversation::setConvId(const int id)
837 {
838         m_Id = id;
839 }
840
841 // getter
842 int Conversation::getConvId()
843 {
844         return m_Id;
845 }
846 unsigned short Conversation::getType()
847 {
848         return m_type;
849 }
850 time_t Conversation::getTime()
851 {
852         return m_time;
853 }
854 unsigned long Conversation::getMessageCount()
855 {
856         return m_messageCount;
857 }
858 unsigned long Conversation::getUnreadMessages()
859 {
860         return m_unreadMessages;
861 }
862 std::string Conversation::getPreview()
863 {
864         return m_preview;
865 }
866
867 std::string Conversation::getSubject()
868 {
869         return m_subject;
870 }
871
872
873 bool Conversation::getRead()
874 {
875         return m_read;
876 }
877 std::string Conversation::getFrom()
878 {
879         return m_from;
880 }
881 std::vector<std::string> Conversation::getTo()
882 {
883         return m_to;
884 }
885 std::vector<std::string> Conversation::getCC()
886 {
887         return m_cc;
888 }
889 std::vector<std::string> Conversation::getBCC()
890 {
891         return m_bcc;
892 }
893 int Conversation::getLastMessageId()
894 {
895         return m_lastMessageId;
896 }
897
898 bool Conversation::getResult()
899 {
900         return m_result;
901 }
902
903 int Conversation::getDirection()
904 {
905         return m_direction;
906 }
907
908
909 }
910 }
911