blob: 066e48f8f023edd49938045c8d07dd6c43c402c0 [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
Anthony Léonard2382b562017-12-13 15:51:28 -050044-(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model
Alexandre Lision58cab672015-06-09 15:25:40 -040045{
Anthony Léonard2382b562017-12-13 15:51:28 -050046 convUid_ = convUid;
47 convModel_ = model;
Alexandre Lision58cab672015-06-09 15:25:40 -040048
Anthony Léonard2382b562017-12-13 15:51:28 -050049 [messagesViewVC setConversationUid:convUid_ model:convModel_];
Alexandre Lision58cab672015-06-09 15:25:40 -040050}
51
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040052- (void) takeFocus
53{
54 [self.view.window makeFirstResponder:self.messageField];
55}
56
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040057- (void)setMessage:(NSString *)newValue {
58 _message = [newValue removeEmptyLinesAtBorders];
59}
60
Alexandre Lision58cab672015-06-09 15:25:40 -040061- (IBAction)sendMessage:(id)sender {
Alexandre Lision58cab672015-06-09 15:25:40 -040062 /* make sure there is text to send */
63 NSString* text = self.message;
64 if (text && text.length > 0) {
Anthony Léonard2382b562017-12-13 15:51:28 -050065 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision58cab672015-06-09 15:25:40 -040066 self.message = @"";
Anthony Léonard2382b562017-12-13 15:51:28 -050067 [messageField setStringValue:@""];
Alexandre Lision58cab672015-06-09 15:25:40 -040068 }
69}
70
71#pragma mark - NSTextFieldDelegate
72
73- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
74{
75 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
76 [self sendMessage:nil];
77 return YES;
78 }
79 return NO;
80}
81
82@end