Beta merge 2
[profile/ivi/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 <Emf_Mapi_Message.h>
24 #include <time.h>
25
26
27 using namespace DPL;
28
29 namespace TizenApis {
30 namespace Platform {
31 namespace Messaging {
32
33 Conversation::Conversation()
34 {
35
36 }
37
38 Conversation::Conversation(unsigned int threadId, Api::Messaging::MessageType msgType)
39 {
40         LogDebug("Enter");
41         
42         if (msgType == Api::Messaging::EMAIL)
43         {
44                 makeConversationFromEmailThreadId(threadId);
45         }
46         else
47         {
48                 makeConversationFromMsgId(threadId, msgType);
49         }
50 }
51         
52 Conversation::Conversation(std::string threadId, Api::Messaging::MessageType msgType)
53 {
54         LogDebug("Enter");
55         
56         std::istringstream stream(threadId);
57         
58         LogDebug("threadId : " << threadId);
59         
60         unsigned int number = 0;
61         stream >> number;
62         makeConversationFromMsgId(number, msgType);
63 }
64
65 Conversation::Conversation(unsigned int threadIndex)
66 {
67         makeConversationFromThreadIndex(threadIndex);
68 }
69
70 Conversation::Conversation(msg_thread_view_t msg_thread)
71 {
72         makeConversationFromThread(msg_thread);
73 }
74
75 void Conversation::makeConversationFromThread(msg_thread_view_t msg_thread)
76 {
77         LogDebug("Enter");
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;
85         int addressCount = 0;
86         int index = 0;
87         
88         m_result = true;
89
90         try 
91         {
92                 if (msg_thread_view_get_thread_id(msg_thread) >= 0)
93                 {
94                         m_Id = msg_thread_view_get_thread_id(msg_thread);
95                 }
96                 
97                 switch(msg_thread_view_get_message_type(msg_thread))
98                 {
99                         case MSG_TYPE_SMS:
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");
108                                 break;
109                         case MSG_TYPE_MMS:
110                         case MSG_TYPE_MMS_JAVA:
111                         case MSG_TYPE_MMS_NOTI:
112                                 m_type = Api::Messaging::MMS;;
113                                 LogDebug("Type:MMS");
114                                 break;
115 //                      default:
116                                 // Todo email / chat
117                 } 
118                 
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);
122
123                 if (tempString != NULL)
124                 {
125                         m_preview = tempString; 
126                         LogDebug("preview" << m_preview);
127                 }
128
129                 
130                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
131                 
132                 if (err != MSG_SUCCESS)
133                 {
134                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
135                 }
136
137                 lastMsgIndex = convViewList.nCount - 1;
138                 m_messageCount = convViewList.nCount;
139                 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
140
141                 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
142                 {
143                         m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
144                         LogDebug("message id" << m_lastMessageId);
145                 }
146                 else
147                 {
148                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message id fail");
149                 }
150
151                 if (msg_get_message(handle, m_lastMessageId, msgForLastIndex, &sendOpt) != MSG_SUCCESS)
152                 {
153                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get message fail");
154                 }
155
156                 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
157
158                 if (msg_get_direction_info(msgForLastIndex) == MSG_DIRECTION_TYPE_MT)
159                 {
160                         addressCount = msg_get_address_count(msgForLastIndex);
161
162                         if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
163                         {
164                                 tempString = (char*)msg_get_ith_address(msgForLastIndex, 0);
165                                 
166                                 if (tempString != NULL)
167                                 {
168                                         m_from = tempString;
169                                 }
170                                 else
171                                 {
172                                         LogDebug("address is null ");
173                                 }
174                         }
175                         else 
176                         {
177                                 LogDebug("address count index fail");
178                         }
179                         LogDebug("from" << m_from);
180                 }
181                 else
182                 {
183                         LogDebug("Msg direction: MO, from will be omitted");
184                 }
185
186
187                 tempString = (char*)msg_get_subject(msgForLastIndex);
188
189                 if (tempString != NULL)
190                 {
191                         m_subject = tempString;
192                 }
193
194                 LogDebug("subject" << m_subject);
195                 
196
197                 addressCount = msg_get_address_count(msgForLastIndex);
198
199
200                 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
201                 {
202                         for (index = 0; index < addressCount; index++)
203                         {
204                                 tempString = (char*)msg_get_ith_address(msgForLastIndex, index);
205
206                                 if (tempString != NULL)
207                                 {
208                                         m_to.push_back(tempString);
209                                 }
210                         }
211                         LogDebug("address count" << addressCount);
212                 }
213                 else
214                 {
215                         LogDebug("address fetch fail" << addressCount);
216                 }
217
218
219
220         }
221         catch (const WrtDeviceApis::Commons::Exception& ex) 
222         {
223                 m_result = false;
224                 LogError("Exception: " << ex.GetMessage());
225         }
226         
227         if (convViewList.msgInfo != NULL) 
228         {
229                 msg_release_message_list(&convViewList);
230         }
231
232         if (msgForLastIndex != NULL)
233         {
234                 msg_release_message(&msgForLastIndex);
235         }
236 }
237
238
239
240 // It is worth for below function to do test for fetching all conversations
241 void Conversation::makeConversationFromThreadIndex(unsigned int threadIndex)
242 {
243         LogDebug("Enter");
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;
251         int index = 0;
252         
253         m_result = true;
254
255         try 
256         {
257                 if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
258                 {
259                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get thread view fail" );
260                 }
261
262                 if (threadViewList.nCount < 0 || threadIndex >= (unsigned int)threadViewList.nCount)
263                 {
264                         LogDebug(threadIndex << ":" << threadViewList.nCount);
265                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "invalid index reference" );
266                 }
267
268                 
269                 if (msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]) >= 0)
270                 {
271                         m_Id = msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[threadIndex]);
272                 }
273                 
274                 switch(msg_thread_view_get_message_type(threadViewList.msgThreadInfo[threadIndex]))
275                 {
276                         case MSG_TYPE_SMS:
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");
285                                 break;
286                         case MSG_TYPE_MMS:
287                         case MSG_TYPE_MMS_JAVA:
288                         case MSG_TYPE_MMS_NOTI:
289                                 m_type = Api::Messaging::MMS;;
290                                 LogDebug("Type:MMS");
291                                 break;
292 //                      default:
293                                 // Todo email / chat
294                 } 
295                 
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]);
299
300                 if (tempString != NULL)
301                 {
302                         m_preview = tempString; 
303                         LogDebug("preview" << m_preview);
304                 }
305
306                 
307                 err = msg_get_conversation_view_list(handle, m_Id, &convViewList);
308                 
309                 if (err != MSG_SUCCESS)
310                 {
311                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get conversation(msg) view list fail");
312                 }
313
314                 lastMsgIndex = convViewList.nCount - 1;
315                 m_messageCount = convViewList.nCount;
316                 m_read = msg_is_read(convViewList.msgInfo[lastMsgIndex]);
317                 
318                 LogDebug("lastMsgIndex:" << lastMsgIndex << ",message count:" << m_messageCount << ",read" << m_read);
319
320                 if (msg_get_direction_info(convViewList.msgInfo[lastMsgIndex]) == MSG_DIRECTION_TYPE_MT)
321                 {
322                         addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
323
324                         if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
325                         {
326                     tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], 0);
327                                 
328                                 if (tempString != NULL)
329                                 {
330                                         m_from = tempString;
331                                 }
332                                 else
333                                 {
334                                         LogDebug("address is null ");
335                                 }
336                         }
337                         else 
338                         {
339                                 LogDebug("address count index fail");
340                         }
341                         LogDebug("from" << m_from);
342                 }
343                 else
344                 {
345                         LogDebug("Msg direction: MO, from will be omitted");
346                 }
347
348
349                 tempString = (char*)msg_get_subject(convViewList.msgInfo[lastMsgIndex]);
350
351                 if (tempString != NULL)
352                 {
353                         m_subject = tempString;
354                 }
355
356                 LogDebug("subject" << m_subject);
357                 
358
359                 addressCount = msg_get_address_count(convViewList.msgInfo[lastMsgIndex]);
360
361
362                 if (addressCount > 0 && addressCount < MAX_TO_ADDRESS_CNT )
363                 {
364                         for (index = 0; index < addressCount; index++)
365                         {
366                                 tempString = (char*)msg_get_ith_address(convViewList.msgInfo[lastMsgIndex], index);
367
368                                 if (tempString != NULL)
369                                 {
370                                         m_to.push_back(tempString);
371                                 }
372                         }
373                         LogDebug("address count" << addressCount);
374                 }
375                 else
376                 {
377                         LogDebug("address fetch fail" << addressCount);
378                 }
379
380                 if (msg_get_message_id(convViewList.msgInfo[lastMsgIndex]) >= 0 )
381                 {
382                         m_lastMessageId = msg_get_message_id(convViewList.msgInfo[lastMsgIndex]);
383                 }
384
385                 LogDebug("message id" << m_lastMessageId);
386
387         }
388         catch (const WrtDeviceApis::Commons::Exception& ex) 
389         {
390                 m_result = false;
391                 LogError("Exception: " << ex.GetMessage());
392         }
393         if (convViewList.msgInfo != NULL) 
394         {
395                 msg_release_message_list(&convViewList);
396         }
397
398         if (threadViewList.msgThreadInfo != NULL)
399         {
400                 msg_release_thread_view_list(&threadViewList);
401         }
402
403
404 }
405
406 bool Conversation::makeConversationFromMsgId(unsigned int threadId, Api::Messaging::MessageType msgType)
407 {
408         LogDebug("Enter");
409         MSG_HANDLE_T handle = MsgGetCommonHandle();
410         MSG_THREAD_VIEW_LIST_S threadViewList = {0, NULL};
411         int conversationId = 0; 
412         int index = 0;
413         m_result = false;
414         
415         if (msg_get_thread_view_list(handle, NULL, &threadViewList) != MSG_SUCCESS)
416         {
417                 LogDebug("get thread view fail");
418
419                 return m_result;
420         }
421
422         conversationId = Api::Messaging::IMessaging::getInstance().getConversationId(threadId, msgType);
423         
424         for(index = 0; index < threadViewList.nCount; index++)
425         {
426                 if (conversationId == msg_thread_view_get_thread_id(threadViewList.msgThreadInfo[index]))
427                 {
428                         LogDebug("Found" << conversationId);
429                         makeConversationFromThreadIndex(index);
430                         break;
431                 }
432         }
433
434         
435         return m_result;
436 }
437
438 bool Conversation::makeConversationFromEmailThreadId(unsigned int emailTreadId)
439 {
440         LogDebug("Enter");
441         
442         emf_mail_list_item_t *resultMail = NULL;
443         emf_mail_list_item_t *mailList = NULL;
444         int accountId = 0;
445         int index = 0;
446         int count = 0;
447         emf_mailbox_t mailbox = {};
448         emf_mail_t *mail = NULL;
449
450         
451         m_result = true;
452
453         try 
454         {
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) 
457                 {
458                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email thread fail" );
459                 }
460
461                 
462                 if (email_get_mail(&mailbox, resultMail->mail_id, &mail) != EMF_ERROR_NONE)
463                 {
464                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
465                 }
466
467
468                 // account Id
469                 m_unreadMessages = 0;
470                 
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)
473                 {
474                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "get email data fail" );
475                 }
476
477                 // unread msg count
478                 accountId = resultMail->account_id;     
479
480                 for (index = 0; index < count; index++)
481                 {
482                         if (mailList[index].flags_seen_field)
483                         {
484                                 m_unreadMessages++;
485                         }
486                         LogDebug(mailList[index].flags_seen_field);
487                 }
488                 
489                 // message count
490                 m_messageCount = resultMail->thread_item_count;
491                 // id
492                 m_Id = emailTreadId;
493                 // type
494                 m_type = Api::Messaging::EMAIL;
495
496                 // time horrible split 
497                 // 2011 11 08 00 35 45
498                 if (resultMail->datetime[0] != '\0')
499                 {
500                         char buf[MAX_DATETIME_STRING_LENGTH];
501                         char *targetBuf = resultMail->datetime;
502                         struct tm timeinfo;
503
504                         memset(buf, 0x00, sizeof(buf));
505                         targetBuf += snprintf(buf, sizeof(buf), "%.4s", targetBuf);
506                         timeinfo.tm_year = atoi(buf) - 1900;
507
508                         memset(buf, 0x00, sizeof(buf));
509                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
510                         timeinfo.tm_mon =  atoi(buf) - 1;
511
512                         memset(buf, 0x00, sizeof(buf));
513                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
514                         timeinfo.tm_mday =  atoi(buf);
515
516
517                         memset(buf, 0x00, sizeof(buf));
518                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
519                         timeinfo.tm_hour = atoi(buf);
520  
521                         memset(buf, 0x00, sizeof(buf));
522                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
523                         timeinfo.tm_min = atoi(buf);
524
525
526                         memset(buf, 0x00, sizeof(buf));
527                         targetBuf += snprintf(buf, sizeof(buf), "%.2s", targetBuf);
528                         timeinfo.tm_sec = atoi(buf);
529
530                         m_time = mktime(&timeinfo);
531                 }
532         
533                 // preview
534                 // not support 
535 #if 0
536                 if (resultMail->previewBodyText[0] != '\0')
537                 {
538                         m_preview = resultMail->previewBodyText; 
539                         LogDebug(m_preview);
540
541                 }
542                 if (mail->head->previewBodyText != NULL)
543                 {
544                         LogDebug(mail->head->previewBodyText);
545                         m_preview = mail->head->previewBodyText;
546                 }
547 #endif          
548                 // read
549                 m_read = (bool)resultMail->flags_seen_field; 
550
551
552                 // from
553                 if (resultMail->from[0] != '\0')
554                 {
555                         m_from = resultMail->from; 
556                         LogDebug(m_from);
557                 }
558
559                 // subject
560                 if (resultMail->subject[0] != '\0');
561                 {
562                         m_subject = resultMail->subject;
563                         LogDebug(m_subject);            
564                 }
565
566                 // to, cc, bcc
567                 if (mail->head!= NULL)
568                 {
569                         if (mail->head->bcc != NULL)
570                         {
571                                 LogDebug(mail->head->bcc);
572                                 m_bcc.push_back(mail->head->bcc);
573                         }
574
575                         if (mail->head->to != NULL)
576                         {
577                                 LogDebug(mail->head->to);
578                                 m_to.push_back(mail->head->to);
579                         }
580
581                         if (mail->head->cc != NULL)
582                         {
583                                 LogDebug(mail->head->cc);
584                                 m_cc.push_back(mail->head->cc);
585                         }
586
587                 }
588         
589                 m_lastMessageId = resultMail->mail_id;
590                 LogDebug(m_lastMessageId);              
591                 m_result = true;
592         }
593         catch (const WrtDeviceApis::Commons::Exception& ex) 
594         {
595                 m_result = false;
596                 LogError("Exception: " << ex.GetMessage());
597         }
598
599         if (resultMail != NULL)
600         {
601                 free(resultMail);
602         }
603         
604         if (mail != NULL)
605         {
606                 email_free_mail(&mail , 1);
607         }
608
609         return m_result;
610
611 }
612 Conversation::~Conversation()
613 {
614 }
615
616 #if 0
617 // setter
618 void Conversation::setId(unsigned int id)
619 {
620         m_id = id;
621 }
622 void Conversation::setType(unsigned short type)
623 {
624         m_type = type;
625 }
626 void Conversation::setTime(time_t time)
627 {
628         m_time = time;
629 }
630 void Conversation::setMessageCount(unsigned long messageCount)
631 {
632         m_messageCount = messageCount;
633 }
634 void Conversation::setUnreadMessages(unsigned long unreadMessages)
635 {
636         m_unreadMessages = unreadMessages;
637 }
638 void Conversation::setPreview(unsigned long preview)
639 {
640         m_preview = preview;
641 }
642 void Conversation::setRead(bool read)
643 {
644         m_read = read;
645 }
646 void Conversation::setFrom(std::string from)
647 {
648         m_from = from;
649 }
650 void Conversation::setTo(std::vector<std::string> to)
651 {
652         m_to = to;
653 }
654 void Conversation::setCC(std::vector<std::string> cc)
655 {
656         m_cc = cc;
657 }
658 void Conversation::setBCC(std::vector<std::string> bcc)
659 {
660         m_bcc = bcc;
661 }
662 void Conversation::setLastMessageId(unsigned int id)
663 {
664         m_lastMessageId = id;
665 }
666 #endif
667 // getter
668 unsigned int Conversation::getId()
669 {
670         return m_Id;
671 }
672 unsigned short Conversation::getType()
673 {
674         return m_type;
675 }
676 time_t Conversation::getTime()
677 {
678         return m_time;
679 }
680 unsigned long Conversation::getMessageCount()
681 {
682         return m_messageCount;
683 }
684 unsigned long Conversation::getUnreadMessages()
685 {
686         return m_unreadMessages;
687 }
688 std::string Conversation::getPreview()
689 {
690         return m_preview;
691 }
692
693 std::string Conversation::getSubject()
694 {
695         return m_subject;
696 }
697
698
699 bool Conversation::getRead()
700 {
701         return m_read;
702 }
703 std::string Conversation::getFrom()
704 {
705         return m_from;
706 }
707 std::vector<std::string> Conversation::getTo()
708 {
709         return m_to;
710 }
711 std::vector<std::string> Conversation::getCC()
712 {
713         return m_cc;
714 }
715 std::vector<std::string> Conversation::getBCC()
716 {
717         return m_bcc;
718 }
719 unsigned int Conversation::getLastMessageId()
720 {
721         return m_lastMessageId;
722 }
723
724 bool Conversation::getResult()
725 {
726         return m_result;
727 }
728
729 }
730 }
731 }
732