ChatVC.mm 2.3 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1
/*
2
 *  Copyright (C) 2015-2017 Savoir-faire Linux Inc.
Alexandre Lision's avatar
Alexandre Lision committed
3
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4
 *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Alexandre Lision's avatar
Alexandre Lision committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  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"

23
#import "MessagesVC.h"
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
24
#import "NSString+Extensions.h"
25

26
@interface ChatVC ()
27
{
28 29 30 31
    IBOutlet MessagesVC* messagesViewVC;

    std::string convUid_;
    lrc::api::ConversationModel* convModel_;
32
}
Alexandre Lision's avatar
Alexandre Lision committed
33 34 35 36 37 38 39

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

@end

@implementation ChatVC
40 41


42
@synthesize messageField,sendButton;
Alexandre Lision's avatar
Alexandre Lision committed
43

44
-(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model
Alexandre Lision's avatar
Alexandre Lision committed
45
{
46 47
    convUid_ = convUid;
    convModel_ = model;
Alexandre Lision's avatar
Alexandre Lision committed
48

49
    [messagesViewVC setConversationUid:convUid_ model:convModel_];
Alexandre Lision's avatar
Alexandre Lision committed
50 51
}

Alexandre Lision's avatar
Alexandre Lision committed
52 53 54 55 56
- (void) takeFocus
{
    [self.view.window makeFirstResponder:self.messageField];
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
57 58 59 60
- (void)setMessage:(NSString *)newValue {
    _message = [newValue removeEmptyLinesAtBorders];
}

Alexandre Lision's avatar
Alexandre Lision committed
61 62 63 64
- (IBAction)sendMessage:(id)sender {
    /* make sure there is text to send */
    NSString* text = self.message;
    if (text && text.length > 0) {
65
        convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision's avatar
Alexandre Lision committed
66
        self.message = @"";
67
        [messageField setStringValue:@""];
Alexandre Lision's avatar
Alexandre Lision committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }
}

#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