ChatVC.mm 5.52 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1
/*
Alexandre Lision's avatar
Alexandre Lision committed
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision's avatar
Alexandre Lision committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *  Author: Alexandre Lision <alexandre.lision@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
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#import "ChatVC.h"

#import <QItemSelectionModel>
#import <qstring.h>

#import <media/media.h>
#import <media/text.h>
#import <media/textrecording.h>
#import <callmodel.h>

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
30 31
#import "MessagesVC.h"

Alexandre Lision's avatar
Alexandre Lision committed
32 33 34 35 36 37 38 39 40
@interface MediaConnectionsHolder : NSObject

@property QMetaObject::Connection newMediaAdded;
@property QMetaObject::Connection newMessage;

@end

@implementation MediaConnectionsHolder

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
41

Alexandre Lision's avatar
Alexandre Lision committed
42 43
@end

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
44 45 46 47
@interface ChatVC () <MessagesVCDelegate>
{
 IBOutlet MessagesVC* messagesViewVC;
}
Alexandre Lision's avatar
Alexandre Lision committed
48 49 50 51

@property (unsafe_unretained) IBOutlet NSTextField *messageField;
@property (unsafe_unretained) IBOutlet NSButton *sendButton;

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
52

Alexandre Lision's avatar
Alexandre Lision committed
53 54 55 56 57
@property MediaConnectionsHolder* mediaHolder;

@end

@implementation ChatVC
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
58 59 60


@synthesize messageField,sendButton, mediaHolder;
Alexandre Lision's avatar
Alexandre Lision committed
61 62 63 64 65 66 67

- (void)awakeFromNib
{
    NSLog(@"Init ChatVC");

    [self.view setWantsLayer:YES];
    [self.view setLayer:[CALayer layer]];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
68
    [self.view.layer setBackgroundColor:[NSColor controlColor].CGColor];
Alexandre Lision's avatar
Alexandre Lision committed
69 70 71

    mediaHolder = [[MediaConnectionsHolder alloc] init];

Alexandre Lision's avatar
Alexandre Lision committed
72
    QObject::connect(CallModel::instance().selectionModel(),
Alexandre Lision's avatar
Alexandre Lision committed
73 74 75 76
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         [self setupChat];
                     });
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
77
    messagesViewVC.delegate = self;
Alexandre Lision's avatar
Alexandre Lision committed
78 79
}

Alexandre Lision's avatar
Alexandre Lision committed
80

Alexandre Lision's avatar
Alexandre Lision committed
81 82 83 84 85
- (void) setupChat
{
    QObject::disconnect(mediaHolder.newMediaAdded);
    QObject::disconnect(mediaHolder.newMessage);

Alexandre Lision's avatar
Alexandre Lision committed
86
    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
Alexandre Lision's avatar
Alexandre Lision committed
87 88 89 90

    if (!callIdx.isValid())
        return;

Alexandre Lision's avatar
Alexandre Lision committed
91
    Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision's avatar
Alexandre Lision committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    /* 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()];
                                                         }
                                                     });
    }
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
113
#pragma mark - MessagesVC delegate
Alexandre Lision's avatar
Alexandre Lision committed
114

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
115
-(void) newMessageAdded {
Alexandre Lision's avatar
Alexandre Lision committed
116

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
117 118
    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
    if (!callIdx.isValid())
Alexandre Lision's avatar
Alexandre Lision committed
119
        return;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
120 121 122 123 124 125 126 127 128 129 130
    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();
    }
}
Alexandre Lision's avatar
Alexandre Lision committed
131

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
132
- (void) parseChatModel:(QAbstractItemModel *)model
Alexandre Lision's avatar
Alexandre Lision committed
133

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
134 135
{
    [messagesViewVC setUpViewWithModel:model];
Alexandre Lision's avatar
Alexandre Lision committed
136 137
}

Alexandre Lision's avatar
Alexandre Lision committed
138 139 140 141 142
- (void) takeFocus
{
    [self.view.window makeFirstResponder:self.messageField];
}

Alexandre Lision's avatar
Alexandre Lision committed
143 144
- (IBAction)sendMessage:(id)sender {

Alexandre Lision's avatar
Alexandre Lision committed
145 146
    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
    Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision's avatar
Alexandre Lision committed
147 148 149 150

    /* make sure there is text to send */
    NSString* text = self.message;
    if (text && text.length > 0) {
Alexandre Lision's avatar
Alexandre Lision committed
151 152 153
        QMap<QString, QString> messages;
        messages["text/plain"] = QString::fromNSString(text);
        call->addOutgoingMedia<Media::Text>()->send(messages);
Alexandre Lision's avatar
Alexandre Lision committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        // Empty the text after sending it
        [self.messageField setStringValue:@""];
        self.message = @"";
    }
}

#pragma mark - NSTextFieldDelegate

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
    if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
        [self sendMessage:nil];
        return YES;
    }
    return NO;
}

@end