blob: b902e7581d83b539ca560b6fe31381242329635c [file] [log] [blame]
Alexandre Lision58cab672015-06-09 15:25:40 -04001/*
Anthony Léonard2382b562017-12-13 15:51:28 -05002 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
Alexandre Lision58cab672015-06-09 15:25:40 -04003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Anthony Léonard2382b562017-12-13 15:51:28 -05004 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Alexandre Lision58cab672015-06-09 15:25:40 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Alexandre Lision58cab672015-06-09 15:25:40 -040019 */
20
21#import "ChatVC.h"
22
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040023#import "MessagesVC.h"
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040024#import "NSString+Extensions.h"
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040025
Anthony Léonard2382b562017-12-13 15:51:28 -050026@interface ChatVC ()
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040027{
Anthony Léonard2382b562017-12-13 15:51:28 -050028 IBOutlet MessagesVC* messagesViewVC;
29
30 std::string convUid_;
31 lrc::api::ConversationModel* convModel_;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040032}
Alexandre Lision58cab672015-06-09 15:25:40 -040033
Alexandre Lision58cab672015-06-09 15:25:40 -040034@property (unsafe_unretained) IBOutlet NSTextField *messageField;
35@property (unsafe_unretained) IBOutlet NSButton *sendButton;
36
Alexandre Lision58cab672015-06-09 15:25:40 -040037@end
38
39@implementation ChatVC
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040040
41
Anthony Léonard2382b562017-12-13 15:51:28 -050042@synthesize messageField,sendButton;
Alexandre Lision58cab672015-06-09 15:25:40 -040043
44- (void)awakeFromNib
45{
46 NSLog(@"Init ChatVC");
47
48 [self.view setWantsLayer:YES];
49 [self.view setLayer:[CALayer layer]];
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040050 [self.view.layer setBackgroundColor:[NSColor controlColor].CGColor];
Alexandre Lision58cab672015-06-09 15:25:40 -040051}
52
Anthony Léonard2382b562017-12-13 15:51:28 -050053-(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model
Alexandre Lision58cab672015-06-09 15:25:40 -040054{
Anthony Léonard2382b562017-12-13 15:51:28 -050055 convUid_ = convUid;
56 convModel_ = model;
Alexandre Lision58cab672015-06-09 15:25:40 -040057
Anthony Léonard2382b562017-12-13 15:51:28 -050058 [messagesViewVC setConversationUid:convUid_ model:convModel_];
Alexandre Lision58cab672015-06-09 15:25:40 -040059}
60
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040061- (void) takeFocus
62{
63 [self.view.window makeFirstResponder:self.messageField];
64}
65
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040066- (void)setMessage:(NSString *)newValue {
67 _message = [newValue removeEmptyLinesAtBorders];
68}
69
Alexandre Lision58cab672015-06-09 15:25:40 -040070- (IBAction)sendMessage:(id)sender {
Alexandre Lision58cab672015-06-09 15:25:40 -040071 /* make sure there is text to send */
72 NSString* text = self.message;
73 if (text && text.length > 0) {
Anthony Léonard2382b562017-12-13 15:51:28 -050074 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision58cab672015-06-09 15:25:40 -040075 self.message = @"";
Anthony Léonard2382b562017-12-13 15:51:28 -050076 [messageField setStringValue:@""];
Alexandre Lision58cab672015-06-09 15:25:40 -040077 }
78}
79
80#pragma mark - NSTextFieldDelegate
81
82- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
83{
84 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
85 [self sendMessage:nil];
86 return YES;
87 }
88 return NO;
89}
90
91@end