blob: 9f19035e9f598b4336b9fca3da561fbec10293cb [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
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040030 QString convUid_;
Anthony Léonard2382b562017-12-13 15:51:28 -050031 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
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040044-(void)setConversationUid:(const QString&)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
Kateryna Kostiuk5acaefd2020-03-25 11:14:25 -040061- (void) clearData {
62 [messagesViewVC clearData];
63}
64
Alexandre Lision58cab672015-06-09 15:25:40 -040065- (IBAction)sendMessage:(id)sender {
Alexandre Lision58cab672015-06-09 15:25:40 -040066 /* make sure there is text to send */
67 NSString* text = self.message;
68 if (text && text.length > 0) {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040069 convModel_->sendMessage(convUid_, QString::fromNSString(text));
Alexandre Lision58cab672015-06-09 15:25:40 -040070 self.message = @"";
Anthony Léonard2382b562017-12-13 15:51:28 -050071 [messageField setStringValue:@""];
Alexandre Lision58cab672015-06-09 15:25:40 -040072 }
73}
74
75#pragma mark - NSTextFieldDelegate
76
77- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
78{
79 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
80 [self sendMessage:nil];
81 return YES;
82 }
83 return NO;
84}
85
86@end