ChatVC.mm 6.85 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1
/*
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *  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>

@interface MediaConnectionsHolder : NSObject

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

@end

@implementation MediaConnectionsHolder

@end

@interface ChatVC ()

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

@property MediaConnectionsHolder* mediaHolder;

@end

@implementation ChatVC
@synthesize messageField,chatView,sendButton, mediaHolder;

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

    [self.view setWantsLayer:YES];
    [self.view setLayer:[CALayer layer]];
    [self.view.layer setBackgroundColor:[NSColor blackColor].CGColor];

    mediaHolder = [[MediaConnectionsHolder alloc] init];

64
    QObject::connect(CallModel::instance().selectionModel(),
Alexandre Lision's avatar
Alexandre Lision committed
65 66 67 68
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         [self setupChat];
                     });
69 70 71 72 73

    // Override default style to add interline space
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle  alloc] init];
    paragraphStyle.lineSpacing = 8;
    [chatView setDefaultParagraphStyle:paragraphStyle];
Alexandre Lision's avatar
Alexandre Lision committed
74 75
}

Alexandre Lision's avatar
Alexandre Lision committed
76

Alexandre Lision's avatar
Alexandre Lision committed
77 78 79 80 81
- (void) setupChat
{
    QObject::disconnect(mediaHolder.newMediaAdded);
    QObject::disconnect(mediaHolder.newMessage);

82
    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
Alexandre Lision's avatar
Alexandre Lision committed
83 84 85 86

    if (!callIdx.isValid())
        return;

87
    Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision's avatar
Alexandre Lision committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

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

                                                         }
                                                     });
    }
}

- (void) parseChatModel:(QAbstractItemModel *)model
{
    QObject::disconnect(mediaHolder.newMessage);
    [self.messageField setStringValue:@""];
    self.message = @"";
    [self.chatView.textStorage.mutableString setString:@""];

    /* put all the messages in the im model into the text view */
    for (int row = 0; row < model->rowCount(); ++row) {
        [self appendNewMessage:model->index(row, 0)];
    }

    /* append new messages */
    mediaHolder.newMessage = QObject::connect(model,
                                              &QAbstractItemModel::rowsInserted,
                                              [self, model] (const QModelIndex &parent, int first, int last) {
126 127 128 129
                                                  for (int row = first; row <= last; ++row) {
                                                      [self appendNewMessage:model->index(row, 0, parent)];
                                                  }
                                              });
Alexandre Lision's avatar
Alexandre Lision committed
130 131 132 133 134 135 136
}

- (void) appendNewMessage:(const QModelIndex&) msgIdx
{
    if (!msgIdx.isValid())
        return;

137 138 139 140 141 142 143 144 145
    NSString* message = msgIdx.data(Qt::DisplayRole).value<QString>().toNSString();
    NSString* author = msgIdx.data((int)Media::TextRecording::Role::AuthorDisplayname).value<QString>().toNSString();

    NSMutableAttributedString* attr = [[NSMutableAttributedString alloc] initWithString:
                                [NSString stringWithFormat:@"%@: %@\n",author, message]];

    // put in bold type author name
    [attr applyFontTraits:NSBoldFontMask range: NSMakeRange(0, [author length])];

Alexandre Lision's avatar
Alexandre Lision committed
146
    [[chatView textStorage] appendAttributedString:attr];
147 148 149 150 151 152 153

    // reapply paragraph style on all the text
    NSRange range = NSMakeRange(0,[chatView textStorage].length);
    [[self.chatView textStorage] addAttribute:NSParagraphStyleAttributeName
                                        value:chatView.defaultParagraphStyle
                                        range:range];

Alexandre Lision's avatar
Alexandre Lision committed
154 155 156 157
    [chatView scrollRangeToVisible:NSMakeRange([[chatView string] length], 0)];

}

Alexandre Lision's avatar
Alexandre Lision committed
158 159 160 161 162
- (void) takeFocus
{
    [self.view.window makeFirstResponder:self.messageField];
}

Alexandre Lision's avatar
Alexandre Lision committed
163 164
- (IBAction)sendMessage:(id)sender {

165 166
    QModelIndex callIdx = CallModel::instance().selectionModel()->currentIndex();
    Call* call = CallModel::instance().getCall(callIdx);
Alexandre Lision's avatar
Alexandre Lision committed
167 168 169 170

    /* make sure there is text to send */
    NSString* text = self.message;
    if (text && text.length > 0) {
171 172 173
        QMap<QString, QString> messages;
        messages["text/plain"] = QString::fromNSString(text);
        call->addOutgoingMedia<Media::Text>()->send(messages);
Alexandre Lision's avatar
Alexandre Lision committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        // 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