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