Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 18 | */ |
| 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 Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 30 | #import "MessagesVC.h" |
| 31 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 32 | @interface MediaConnectionsHolder : NSObject |
| 33 | |
| 34 | @property QMetaObject::Connection newMediaAdded; |
| 35 | @property QMetaObject::Connection newMessage; |
| 36 | |
| 37 | @end |
| 38 | |
| 39 | @implementation MediaConnectionsHolder |
| 40 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 41 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 42 | @end |
| 43 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 44 | @interface ChatVC () <MessagesVCDelegate> |
| 45 | { |
| 46 | IBOutlet MessagesVC* messagesViewVC; |
| 47 | } |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 48 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 49 | @property (unsafe_unretained) IBOutlet NSTextField *messageField; |
| 50 | @property (unsafe_unretained) IBOutlet NSButton *sendButton; |
| 51 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 52 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 53 | @property MediaConnectionsHolder* mediaHolder; |
| 54 | |
| 55 | @end |
| 56 | |
| 57 | @implementation ChatVC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 58 | |
| 59 | |
| 60 | @synthesize messageField,sendButton, mediaHolder; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 61 | |
| 62 | - (void)awakeFromNib |
| 63 | { |
| 64 | NSLog(@"Init ChatVC"); |
| 65 | |
| 66 | [self.view setWantsLayer:YES]; |
| 67 | [self.view setLayer:[CALayer layer]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 68 | [self.view.layer setBackgroundColor:[NSColor controlColor].CGColor]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 69 | |
| 70 | mediaHolder = [[MediaConnectionsHolder alloc] init]; |
| 71 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 72 | QObject::connect(CallModel::instance().selectionModel(), |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 73 | &QItemSelectionModel::currentChanged, |
| 74 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 75 | [self setupChat]; |
| 76 | }); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 77 | messagesViewVC.delegate = self; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 80 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 81 | - (void) setupChat |
| 82 | { |
| 83 | QObject::disconnect(mediaHolder.newMediaAdded); |
| 84 | QObject::disconnect(mediaHolder.newMessage); |
| 85 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 86 | QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex(); |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 87 | |
| 88 | if (!callIdx.isValid()) |
| 89 | return; |
| 90 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 91 | Call* call = CallModel::instance().getCall(callIdx); |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 92 | |
| 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 Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 108 | } |
| 109 | }); |
| 110 | } |
| 111 | } |
| 112 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 113 | #pragma mark - MessagesVC delegate |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 114 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 115 | -(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 Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 129 | } |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 132 | - (void) parseChatModel:(QAbstractItemModel *)model |
| 133 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 134 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 135 | [messagesViewVC setUpViewWithModel:model]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 138 | - (void) takeFocus |
| 139 | { |
| 140 | [self.view.window makeFirstResponder:self.messageField]; |
| 141 | } |
| 142 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 143 | - (IBAction)sendMessage:(id)sender { |
| 144 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 145 | QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex(); |
| 146 | Call* call = CallModel::instance().getCall(callIdx); |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 147 | |
| 148 | /* make sure there is text to send */ |
| 149 | NSString* text = self.message; |
| 150 | if (text && text.length > 0) { |
Alexandre Lision | 21d2063 | 2015-07-22 15:49:44 -0400 | [diff] [blame] | 151 | QMap<QString, QString> messages; |
| 152 | messages["text/plain"] = QString::fromNSString(text); |
| 153 | call->addOutgoingMedia<Media::Text>()->send(messages); |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 154 | // 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 |