Commit 2382b56e authored by Anthony Léonard's avatar Anthony Léonard Committed by Kateryna Kostiuk

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's avatarKateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
parent be339114
......@@ -17,6 +17,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import <Cocoa/Cocoa.h>
#import <api/conversation.h>
#import <api/conversationmodel.h>
@interface ChatVC : NSViewController <NSTextFieldDelegate>
......@@ -27,6 +29,7 @@
*/
@property (retain) NSString* message;
- (void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel*)model;
- (void) takeFocus;
@end
/*
* 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;
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);
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()];
}
});
}
}
convUid_ = convUid;
convModel_ = model;
#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];
}
}
......
......@@ -18,6 +18,8 @@
*/
#import <Cocoa/Cocoa.h>
#import <api/conversation.h>
#import <api/conversationmodel.h>
@interface ConversationVC : NSViewController
......@@ -32,4 +34,6 @@
*/
@property (retain) NSString* message;
- (void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel*)model;
@end
This diff is collapsed.
......@@ -18,6 +18,8 @@
*/
#import <Cocoa/Cocoa.h>
#import <api/conversationmodel.h>
#import <api/conversation.h>
@protocol MessagesVCDelegate
......@@ -27,7 +29,9 @@
@interface MessagesVC : NSViewController
-(void)setUpViewWithModel: (QAbstractItemModel*) model;
-(void)setConversationUid:(const std::string)convUid model:(const lrc::api::ConversationModel*)model;
-(void)newMessageSent;
@property (retain, nonatomic) id <MessagesVCDelegate> delegate;
@end
This diff is collapsed.
......@@ -147,6 +147,16 @@ NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIde
[currentCallVC animateIn];
[offlineVC animateOut];
});
QObject::connect(&lrc_->getBehaviorController(),
&lrc::api::BehaviorController::showChatView,
[self](const std::string& accountId,
const lrc::api::conversation::Info& convInfo){
auto& accInfo = lrc_->getAccountModel().getAccountInfo(accountId);
[offlineVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
[offlineVC animateIn];
[currentCallVC animateOut];
});
}
/**
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment