refactoring of messaging controller with new model

MessagesVC is now implemented using the new LRC model for
conversations.
 - Both views to display the messages (in call and off call)
   initialize their MessagesVC with the current conversation when
   needed.
 - A conversation caching system is introduced to not get the whole
   conversation::Info structure from LRC at each display request (once
   per message).

Change-Id: Ib520c1f88be78de37968d3d7741010f2c73f20ea
Reviewed-by: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
diff --git a/src/ChatVC.mm b/src/ChatVC.mm
index 7655f16..5a3a9ed 100644
--- a/src/ChatVC.mm
+++ b/src/ChatVC.mm
@@ -1,6 +1,7 @@
 /*
- *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
+ *  Copyright (C) 2015-2017 Savoir-faire Linux Inc.
  *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
+ *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -19,45 +20,25 @@
 
 #import "ChatVC.h"
 
-#import <QItemSelectionModel>
-#import <qstring.h>
-
-#import <media/media.h>
-#import <media/text.h>
-#import <media/textrecording.h>
-#import <callmodel.h>
-
 #import "MessagesVC.h"
 
-@interface MediaConnectionsHolder : NSObject
-
-@property QMetaObject::Connection newMediaAdded;
-@property QMetaObject::Connection newMessage;
-
-@end
-
-@implementation MediaConnectionsHolder
-
-
-@end
-
-@interface ChatVC () <MessagesVCDelegate>
+@interface ChatVC ()
 {
- IBOutlet MessagesVC* messagesViewVC;
+    IBOutlet MessagesVC* messagesViewVC;
+
+    std::string convUid_;
+    lrc::api::ConversationModel* convModel_;
 }
 
 @property (unsafe_unretained) IBOutlet NSTextField *messageField;
 @property (unsafe_unretained) IBOutlet NSButton *sendButton;
 
-
-@property MediaConnectionsHolder* mediaHolder;
-
 @end
 
 @implementation ChatVC
 
 
-@synthesize messageField,sendButton, mediaHolder;
+@synthesize messageField,sendButton;
 
 - (void)awakeFromNib
 {
@@ -66,73 +47,14 @@
     [self.view setWantsLayer:YES];
     [self.view setLayer:[CALayer layer]];
     [self.view.layer setBackgroundColor:[NSColor controlColor].CGColor];
-
-    mediaHolder = [[MediaConnectionsHolder alloc] init];
-
-    QObject::connect(CallModel::instance().selectionModel(),
-                     &QItemSelectionModel::currentChanged,
-                     [=](const QModelIndex &current, const QModelIndex &previous) {
-                         [self setupChat];
-                     });
-    messagesViewVC.delegate = self;
 }
 
-
-- (void) setupChat
+-(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model
 {
-    QObject::disconnect(mediaHolder.newMediaAdded);
-    QObject::disconnect(mediaHolder.newMessage);
+    convUid_ = convUid;
+    convModel_ = model;
 
-    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
-
-    if (!callIdx.isValid())
-        return;
-
-    Call* call = CallModel::instance().getCall(callIdx);
-
-    /* check if text media is already present */
-    if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
-        Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::IN);
-        [self parseChatModel:text->recording()->instantMessagingModel()];
-    } else if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
-        Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
-        [self parseChatModel:text->recording()->instantMessagingModel()];
-    } else {
-        /* monitor media for messaging text messaging */
-        mediaHolder.newMediaAdded = QObject::connect(call,
-                                                     &Call::mediaAdded,
-                                                     [self] (Media::Media* media) {
-                                                         if (media->type() == Media::Media::Type::TEXT) {
-                                                             QObject::disconnect(mediaHolder.newMediaAdded);
-                                                             [self parseChatModel:((Media::Text*)media)->recording()->instantMessagingModel()];
-                                                         }
-                                                     });
-    }
-}
-
-#pragma mark - MessagesVC delegate
-
--(void) newMessageAdded {
-
-    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
-    if (!callIdx.isValid())
-        return;
-    Call* call = CallModel::instance().getCall(callIdx);
-    if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
-        Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::IN);
-        auto textRecording = text->recording();
-        textRecording->setAllRead();
-    } else if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
-        Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
-        auto textRecording = text->recording();
-        textRecording->setAllRead();
-    }
-}
-
-- (void) parseChatModel:(QAbstractItemModel *)model
-
-{
-    [messagesViewVC setUpViewWithModel:model];
+    [messagesViewVC setConversationUid:convUid_ model:convModel_];
 }
 
 - (void) takeFocus
@@ -141,19 +63,13 @@
 }
 
 - (IBAction)sendMessage:(id)sender {
-
-    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
-    Call* call = CallModel::instance().getCall(callIdx);
-
     /* make sure there is text to send */
     NSString* text = self.message;
     if (text && text.length > 0) {
-        QMap<QString, QString> messages;
-        messages["text/plain"] = QString::fromNSString(text);
-        call->addOutgoingMedia<Media::Text>()->send(messages);
-        // Empty the text after sending it
-        [self.messageField setStringValue:@""];
+        convModel_->sendMessage(convUid_, std::string([text UTF8String]));
         self.message = @"";
+        [messageField setStringValue:@""];
+        [messagesViewVC newMessageSent];
     }
 }