blob: 7655f16c2f8e9f2700515921d1968f19a7b21900 [file] [log] [blame]
Alexandre Lision58cab672015-06-09 15:25:40 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision58cab672015-06-09 15:25:40 -04003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Alexandre Lision58cab672015-06-09 15:25:40 -040018 */
19
20#import "ChatVC.h"
21
22#import <QItemSelectionModel>
23#import <qstring.h>
24
25#import <media/media.h>
26#import <media/text.h>
27#import <media/textrecording.h>
28#import <callmodel.h>
29
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040030#import "MessagesVC.h"
31
Alexandre Lision58cab672015-06-09 15:25:40 -040032@interface MediaConnectionsHolder : NSObject
33
34@property QMetaObject::Connection newMediaAdded;
35@property QMetaObject::Connection newMessage;
36
37@end
38
39@implementation MediaConnectionsHolder
40
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040041
Alexandre Lision58cab672015-06-09 15:25:40 -040042@end
43
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040044@interface ChatVC () <MessagesVCDelegate>
45{
46 IBOutlet MessagesVC* messagesViewVC;
47}
Alexandre Lision58cab672015-06-09 15:25:40 -040048
Alexandre Lision58cab672015-06-09 15:25:40 -040049@property (unsafe_unretained) IBOutlet NSTextField *messageField;
50@property (unsafe_unretained) IBOutlet NSButton *sendButton;
51
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040052
Alexandre Lision58cab672015-06-09 15:25:40 -040053@property MediaConnectionsHolder* mediaHolder;
54
55@end
56
57@implementation ChatVC
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040058
59
60@synthesize messageField,sendButton, mediaHolder;
Alexandre Lision58cab672015-06-09 15:25:40 -040061
62- (void)awakeFromNib
63{
64 NSLog(@"Init ChatVC");
65
66 [self.view setWantsLayer:YES];
67 [self.view setLayer:[CALayer layer]];
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040068 [self.view.layer setBackgroundColor:[NSColor controlColor].CGColor];
Alexandre Lision58cab672015-06-09 15:25:40 -040069
70 mediaHolder = [[MediaConnectionsHolder alloc] init];
71
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040072 QObject::connect(CallModel::instance().selectionModel(),
Alexandre Lision58cab672015-06-09 15:25:40 -040073 &QItemSelectionModel::currentChanged,
74 [=](const QModelIndex &current, const QModelIndex &previous) {
75 [self setupChat];
76 });
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040077 messagesViewVC.delegate = self;
Alexandre Lision58cab672015-06-09 15:25:40 -040078}
79
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040080
Alexandre Lision58cab672015-06-09 15:25:40 -040081- (void) setupChat
82{
83 QObject::disconnect(mediaHolder.newMediaAdded);
84 QObject::disconnect(mediaHolder.newMessage);
85
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040086 QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
Alexandre Lision58cab672015-06-09 15:25:40 -040087
88 if (!callIdx.isValid())
89 return;
90
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040091 Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision58cab672015-06-09 15:25:40 -040092
93 /* check if text media is already present */
94 if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
95 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::IN);
96 [self parseChatModel:text->recording()->instantMessagingModel()];
97 } else if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
98 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
99 [self parseChatModel:text->recording()->instantMessagingModel()];
100 } else {
101 /* monitor media for messaging text messaging */
102 mediaHolder.newMediaAdded = QObject::connect(call,
103 &Call::mediaAdded,
104 [self] (Media::Media* media) {
105 if (media->type() == Media::Media::Type::TEXT) {
106 QObject::disconnect(mediaHolder.newMediaAdded);
107 [self parseChatModel:((Media::Text*)media)->recording()->instantMessagingModel()];
Alexandre Lision58cab672015-06-09 15:25:40 -0400108 }
109 });
110 }
111}
112
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -0400113#pragma mark - MessagesVC delegate
Alexandre Lision58cab672015-06-09 15:25:40 -0400114
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -0400115-(void) newMessageAdded {
116
117 QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
118 if (!callIdx.isValid())
119 return;
120 Call* call = CallModel::instance().getCall(callIdx);
121 if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
122 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::IN);
123 auto textRecording = text->recording();
124 textRecording->setAllRead();
125 } else if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
126 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
127 auto textRecording = text->recording();
128 textRecording->setAllRead();
Alexandre Lision58cab672015-06-09 15:25:40 -0400129 }
Alexandre Lision58cab672015-06-09 15:25:40 -0400130}
131
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -0400132- (void) parseChatModel:(QAbstractItemModel *)model
133
Alexandre Lision58cab672015-06-09 15:25:40 -0400134{
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -0400135 [messagesViewVC setUpViewWithModel:model];
Alexandre Lision58cab672015-06-09 15:25:40 -0400136}
137
Alexandre Lision16d9c0a2015-08-10 12:05:15 -0400138- (void) takeFocus
139{
140 [self.view.window makeFirstResponder:self.messageField];
141}
142
Alexandre Lision58cab672015-06-09 15:25:40 -0400143- (IBAction)sendMessage:(id)sender {
144
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400145 QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
146 Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision58cab672015-06-09 15:25:40 -0400147
148 /* make sure there is text to send */
149 NSString* text = self.message;
150 if (text && text.length > 0) {
Alexandre Lision21d20632015-07-22 15:49:44 -0400151 QMap<QString, QString> messages;
152 messages["text/plain"] = QString::fromNSString(text);
153 call->addOutgoingMedia<Media::Text>()->send(messages);
Alexandre Lision58cab672015-06-09 15:25:40 -0400154 // Empty the text after sending it
155 [self.messageField setStringValue:@""];
156 self.message = @"";
157 }
158}
159
160#pragma mark - NSTextFieldDelegate
161
162- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
163{
164 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
165 [self sendMessage:nil];
166 return YES;
167 }
168 return NO;
169}
170
171@end