blob: 5495b8aafb440dca9e5c2fa9be478990358646b2 [file] [log] [blame]
Kateryna Kostiukf6317422018-09-27 17:08:20 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2018-2019 by Savoir-faire Linux Inc.
Kateryna Kostiukf6317422018-09-27 17:08:20 -04003 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#import "LeaveMessageVC.h"
21#import "views/NSColor+RingTheme.h"
22#import "utils.h"
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -040023#import "NSString+Extensions.h"
Kateryna Kostiukf6317422018-09-27 17:08:20 -040024
25//lrc
26#import <api/avmodel.h>
27#import <api/conversationmodel.h>
28
29#import <QuartzCore/QuartzCore.h>
30#import "delegates/ImageManipulationDelegate.h"
31
32//Qt
33#import <QtMacExtras/qmacfunctions.h>
34#import <QPixmap>
35#import <globalinstances.h>
36
37@interface LeaveMessageVC () {
38 __unsafe_unretained IBOutlet NSImageView* personPhoto;
39 __unsafe_unretained IBOutlet NSTextField* infoLabel;
40 __unsafe_unretained IBOutlet NSBox* timerBox;
41 __unsafe_unretained IBOutlet NSTextField* timerLabel;
Kateryna Kostiuke28a0382019-10-25 16:29:08 -040042 __unsafe_unretained IBOutlet NSButton* sendButton;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040043 __unsafe_unretained IBOutlet NSButton* recordButton;
Kateryna Kostiuke28a0382019-10-25 16:29:08 -040044 __unsafe_unretained IBOutlet NSButton* exitButton;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040045}
46
47@end
48
49@implementation LeaveMessageVC
50
51bool isRecording = false;
52int recordingTime = 0;
53NSTimer* refreshDurationTimer;
54lrc::api::AVModel* avModel;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040055NSMutableDictionary *filesToSend;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040056QString conversationUid;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040057lrc::api::ConversationModel* conversationModel;
58
59- (void)loadView {
60 [super loadView];
61 [personPhoto setWantsLayer:YES];
62 personPhoto.layer.masksToBounds =true;
63 personPhoto.layer.cornerRadius = personPhoto.frame.size.width * 0.5;
64 filesToSend = [[NSMutableDictionary alloc] init];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -040065 [self setButtonShadow:sendButton];
66 [self setButtonShadow:exitButton];
67 [self setButtonShadow:recordButton];
68}
69
70-(void) setButtonShadow:(NSButton*) button {
71 button.wantsLayer = YES;
72 button.layer.masksToBounds = NO;
73 button.shadow = [[NSShadow alloc] init];
74 button.layer.shadowOpacity = 0.8;
75 button.layer.shadowColor = [[NSColor grayColor] CGColor];
76 button.layer.shadowOffset = NSMakeSize(-0.5, 1);
77 button.layer.shadowRadius = 1;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040078}
79
80-(void) setAVModel: (lrc::api::AVModel*) avmodel {
81 avModel = avmodel;
82}
83
84-(void) initFrame {
85 [self.view setFrame:self.view.superview.bounds];
86 [self.view setHidden:YES];
87 self.view.layer.position = self.view.frame.origin;
88}
89
90- (IBAction)cancel:(id)sender {
91 [self exit];
92}
93
94- (IBAction)recordMessage:(NSButton *)sender {
95 if (!isRecording) {
96 [self clearData];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040097 QString file_name = avModel->startLocalRecorder(true);
98 if (file_name.isEmpty()) {
Kateryna Kostiukf6317422018-09-27 17:08:20 -040099 return;
100 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400101 filesToSend[conversationUid.toNSString()] = file_name.toNSString();
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400102 isRecording = true;
103 recordButton.image = [NSImage imageNamed:@"ic_stoprecord.png"];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400104 recordButton.title = NSLocalizedString(@"Stop recording", @"Record message title");
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400105 [timerBox setHidden:NO];
106 if (refreshDurationTimer == nil)
107 refreshDurationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
108 target:self
109 selector:@selector(updateDurationLabel)
110 userInfo:nil
111 repeats:YES];
112 } else {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400113 avModel->stopLocalRecorder(QString::fromNSString(filesToSend[conversationUid.toNSString()]));
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400114 isRecording = false;
115 recordButton.image = [NSImage imageNamed:@"ic_action_audio.png"];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400116 recordButton.title = NSLocalizedString(@"Record a message", @"Record message title");
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400117 [refreshDurationTimer invalidate];
118 refreshDurationTimer = nil;
119 [timerBox setHidden:YES];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400120 [sendButton setHidden: NO];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400121 }
122}
123
124- (IBAction)sendMessage:(NSButton *)sender {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400125 NSArray* pathURL = [filesToSend[conversationUid.toNSString()] componentsSeparatedByString: @"/"];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400126 if([pathURL count] < 1) {
127 return;
128 }
129 NSString* name = [pathURL objectAtIndex: [pathURL count] - 1];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400130 conversationModel->sendFile(conversationUid, QString::fromNSString(filesToSend[conversationUid.toNSString()]), QString::fromNSString(name));
131 [filesToSend removeObjectForKey: conversationUid.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400132 [self exit];
133}
134
135- (void) exit {
136 [self clearData];
137 [self hide];
138 [self.delegate messageCompleted];
139}
140
141- (void)clearData {
142 recordButton.image = [NSImage imageNamed:@"ic_action_audio.png"];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400143 recordButton.title = NSLocalizedString(@"Record a message", @"Record message title");
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400144 recordingTime = 0;
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400145 [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400146 isRecording = false;
147 [timerBox setHidden:YES];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400148 [sendButton setHidden: YES];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400149 [refreshDurationTimer invalidate];
150 refreshDurationTimer = nil;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400151 [filesToSend removeObjectForKey: conversationUid.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400152}
153
154- (void)viewWillHide {
155 recordButton.image = [NSImage imageNamed:@"ic_action_audio.png"];
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400156 recordButton.title = NSLocalizedString(@"Record a message", @"Record message title");
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400157 if(filesToSend[conversationUid.toNSString()]) {
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400158 [sendButton setHidden: NO];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400159 } else {
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400160 [sendButton setHidden: YES];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400161 }
162 recordingTime = 0;
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400163 [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400164 isRecording = false;
165 [timerBox setHidden:YES];
166 [refreshDurationTimer invalidate];
167 refreshDurationTimer = nil;
168}
169
170-(void) hide {
171 if(self.view.frame.origin.x < 0) {
172 return;
173 }
174 [self viewWillHide];
175 [self.view setHidden:YES];
176}
177
178-(void) show {
179 if(self.view.frame.origin.x < 0) {
180 return;
181 }
182 [self.view setHidden:NO];
183}
184
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400185-(void)setConversationUID:(const QString&) convUid conversationModel:(lrc::api::ConversationModel*) convModel {
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400186 conversationUid = convUid;
187 conversationModel = convModel;
188 [self updateView];
189}
190
191-(void) updateView {
Kateryna Kostiuk3541ae22020-08-17 12:26:14 -0400192 auto it = getConversationFromUid(conversationUid, *conversationModel);
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400193 if (it != conversationModel->allFilteredConversations().end()) {
Kateryna Kostiukdc720842020-09-10 16:44:02 -0400194 @autoreleasepool {
195 auto& imgManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
196 QVariant photo = imgManip.conversationPhoto(*it, conversationModel->owner, QSize(120, 120), NO);
197 [personPhoto setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
198 }
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400199 NSString *name = bestNameForConversation(*it, *conversationModel);
Kateryna Kostiuke28a0382019-10-25 16:29:08 -0400200
201 NSFont *fontName = [NSFont systemFontOfSize: 20.0 weight: NSFontWeightSemibold];
202 NSFont *otherFont = [NSFont systemFontOfSize: 20.0 weight: NSFontWeightThin];
203 NSColor *color = [NSColor textColor];
204 NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
205 [style setLineBreakMode:NSLineBreakByWordWrapping];
206 [style setAlignment:NSCenterTextAlignment];
207 NSDictionary *nameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
208 fontName, NSFontAttributeName,
209 style, NSParagraphStyleAttributeName,
210 color, NSForegroundColorAttributeName,
211 nil];
212 NSDictionary *otherAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
213 otherFont, NSFontAttributeName,
214 style, NSParagraphStyleAttributeName,
215 color, NSForegroundColorAttributeName,
216 nil];
217 NSAttributedString* attributedName = [[NSAttributedString alloc] initWithString:name attributes:nameAttrs];
218 NSString *str = [NSString stringWithFormat: @"%@%@\n%@",
219 @" ",
220 NSLocalizedString(@"appears to be busy.", @"Peer busy message"),
221 NSLocalizedString(@"Would you like to leave a message?", @"Peer busy message")];
222 NSAttributedString* attributedOther= [[NSAttributedString alloc] initWithString: str attributes: otherAttrs];
223 NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
224 [result appendAttributedString:attributedName];
225 [result appendAttributedString:attributedOther];
226 NSRange range = NSMakeRange(0, [result length]);
227 [result addAttribute:NSParagraphStyleAttributeName value:style range: range];
228 [infoLabel setAttributedStringValue: result];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400229 }
230 [self show];
231}
232
233-(void) updateDurationLabel
234{
235 recordingTime++;
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400236 [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400237}
238
239@end