blob: fce48a3e06fa099f3fd82071aaaee091ccd80329 [file] [log] [blame]
Alexandre Lision58cab672015-06-09 15:25:40 -04001/*
2 * Copyright (C) 2015 Savoir-Faire Linux Inc.
3 * 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#import "ChatVC.h"
32
33#import <QItemSelectionModel>
34#import <qstring.h>
35
36#import <media/media.h>
37#import <media/text.h>
38#import <media/textrecording.h>
39#import <callmodel.h>
40
41@interface MediaConnectionsHolder : NSObject
42
43@property QMetaObject::Connection newMediaAdded;
44@property QMetaObject::Connection newMessage;
45
46@end
47
48@implementation MediaConnectionsHolder
49
50@end
51
52@interface ChatVC ()
53
54@property (unsafe_unretained) IBOutlet NSTextView *chatView;
55@property (unsafe_unretained) IBOutlet NSTextField *messageField;
56@property (unsafe_unretained) IBOutlet NSButton *sendButton;
57
58@property MediaConnectionsHolder* mediaHolder;
59
60@end
61
62@implementation ChatVC
63@synthesize messageField,chatView,sendButton, mediaHolder;
64
65- (void)awakeFromNib
66{
67 NSLog(@"Init ChatVC");
68
69 [self.view setWantsLayer:YES];
70 [self.view setLayer:[CALayer layer]];
71 [self.view.layer setBackgroundColor:[NSColor blackColor].CGColor];
72
73 mediaHolder = [[MediaConnectionsHolder alloc] init];
74
75 QObject::connect(CallModel::instance()->selectionModel(),
76 &QItemSelectionModel::currentChanged,
77 [=](const QModelIndex &current, const QModelIndex &previous) {
78 [self setupChat];
79 });
Alexandre Lision98f67cd2015-06-22 14:27:34 -040080
81 // Override default style to add interline space
82 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
83 paragraphStyle.lineSpacing = 8;
84 [chatView setDefaultParagraphStyle:paragraphStyle];
Alexandre Lision58cab672015-06-09 15:25:40 -040085}
86
87- (void) setupChat
88{
89 QObject::disconnect(mediaHolder.newMediaAdded);
90 QObject::disconnect(mediaHolder.newMessage);
91
92 QModelIndex callIdx = CallModel::instance()->selectionModel()->currentIndex();
93
94 if (!callIdx.isValid())
95 return;
96
97 Call* call = CallModel::instance()->getCall(callIdx);
98
99 /* check if text media is already present */
100 if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::IN)) {
101 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::IN);
102 [self parseChatModel:text->recording()->instantMessagingModel()];
103 } else if (call->hasMedia(Media::Media::Type::TEXT, Media::Media::Direction::OUT)) {
104 Media::Text *text = call->firstMedia<Media::Text>(Media::Media::Direction::OUT);
105 [self parseChatModel:text->recording()->instantMessagingModel()];
106 } else {
107 /* monitor media for messaging text messaging */
108 mediaHolder.newMediaAdded = QObject::connect(call,
109 &Call::mediaAdded,
110 [self] (Media::Media* media) {
111 if (media->type() == Media::Media::Type::TEXT) {
112 QObject::disconnect(mediaHolder.newMediaAdded);
113 [self parseChatModel:((Media::Text*)media)->recording()->instantMessagingModel()];
114
115 }
116 });
117 }
118}
119
120- (void) parseChatModel:(QAbstractItemModel *)model
121{
122 QObject::disconnect(mediaHolder.newMessage);
123 [self.messageField setStringValue:@""];
124 self.message = @"";
125 [self.chatView.textStorage.mutableString setString:@""];
126
127 /* put all the messages in the im model into the text view */
128 for (int row = 0; row < model->rowCount(); ++row) {
129 [self appendNewMessage:model->index(row, 0)];
130 }
131
132 /* append new messages */
133 mediaHolder.newMessage = QObject::connect(model,
134 &QAbstractItemModel::rowsInserted,
135 [self, model] (const QModelIndex &parent, int first, int last) {
136 for (int row = first; row <= last; ++row) {
137 QModelIndex idx = model->index(row, 0, parent);
138 [self appendNewMessage:idx];
139 }
140 }
141 );
142}
143
144- (void) appendNewMessage:(const QModelIndex&) msgIdx
145{
146 if (!msgIdx.isValid())
147 return;
148
Alexandre Lision98f67cd2015-06-22 14:27:34 -0400149 NSString* message = msgIdx.data(Qt::DisplayRole).value<QString>().toNSString();
150 NSString* author = msgIdx.data((int)Media::TextRecording::Role::AuthorDisplayname).value<QString>().toNSString();
151
152 NSMutableAttributedString* attr = [[NSMutableAttributedString alloc] initWithString:
153 [NSString stringWithFormat:@"%@: %@\n",author, message]];
154
155 // put in bold type author name
156 [attr applyFontTraits:NSBoldFontMask range: NSMakeRange(0, [author length])];
157
Alexandre Lision58cab672015-06-09 15:25:40 -0400158 [[chatView textStorage] appendAttributedString:attr];
Alexandre Lision98f67cd2015-06-22 14:27:34 -0400159
160 // reapply paragraph style on all the text
161 NSRange range = NSMakeRange(0,[chatView textStorage].length);
162 [[self.chatView textStorage] addAttribute:NSParagraphStyleAttributeName
163 value:chatView.defaultParagraphStyle
164 range:range];
165
Alexandre Lision58cab672015-06-09 15:25:40 -0400166 [chatView scrollRangeToVisible:NSMakeRange([[chatView string] length], 0)];
167
168}
169
170- (IBAction)sendMessage:(id)sender {
171
172 QModelIndex callIdx = CallModel::instance()->selectionModel()->currentIndex();
173 Call* call = CallModel::instance()->getCall(callIdx);
174
175 /* make sure there is text to send */
176 NSString* text = self.message;
177 if (text && text.length > 0) {
Alexandre Lision21d20632015-07-22 15:49:44 -0400178 QMap<QString, QString> messages;
179 messages["text/plain"] = QString::fromNSString(text);
180 call->addOutgoingMedia<Media::Text>()->send(messages);
Alexandre Lision58cab672015-06-09 15:25:40 -0400181 // Empty the text after sending it
182 [self.messageField setStringValue:@""];
183 self.message = @"";
184 }
185}
186
187#pragma mark - NSTextFieldDelegate
188
189- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
190{
191 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
192 [self sendMessage:nil];
193 return YES;
194 }
195 return NO;
196}
197
198@end