Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2018-2019 by Savoir-faire Linux Inc. |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 3 | * 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 Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 23 | #import "NSString+Extensions.h" |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 24 | |
| 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 Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 42 | __unsafe_unretained IBOutlet NSButton* sendButton; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSButton* recordButton; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 44 | __unsafe_unretained IBOutlet NSButton* exitButton; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @end |
| 48 | |
| 49 | @implementation LeaveMessageVC |
| 50 | |
| 51 | bool isRecording = false; |
| 52 | int recordingTime = 0; |
| 53 | NSTimer* refreshDurationTimer; |
| 54 | lrc::api::AVModel* avModel; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 55 | NSMutableDictionary *filesToSend; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 56 | QString conversationUid; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 57 | lrc::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 Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 65 | [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 Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 78 | } |
| 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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 97 | QString file_name = avModel->startLocalRecorder(true); |
| 98 | if (file_name.isEmpty()) { |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 101 | filesToSend[conversationUid.toNSString()] = file_name.toNSString(); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 102 | isRecording = true; |
| 103 | recordButton.image = [NSImage imageNamed:@"ic_stoprecord.png"]; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 104 | recordButton.title = NSLocalizedString(@"Stop recording", @"Record message title"); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 105 | [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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 113 | avModel->stopLocalRecorder(QString::fromNSString(filesToSend[conversationUid.toNSString()])); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 114 | isRecording = false; |
| 115 | recordButton.image = [NSImage imageNamed:@"ic_action_audio.png"]; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 116 | recordButton.title = NSLocalizedString(@"Record a message", @"Record message title"); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 117 | [refreshDurationTimer invalidate]; |
| 118 | refreshDurationTimer = nil; |
| 119 | [timerBox setHidden:YES]; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 120 | [sendButton setHidden: NO]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | - (IBAction)sendMessage:(NSButton *)sender { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 125 | NSArray* pathURL = [filesToSend[conversationUid.toNSString()] componentsSeparatedByString: @"/"]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 126 | if([pathURL count] < 1) { |
| 127 | return; |
| 128 | } |
| 129 | NSString* name = [pathURL objectAtIndex: [pathURL count] - 1]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 130 | conversationModel->sendFile(conversationUid, QString::fromNSString(filesToSend[conversationUid.toNSString()]), QString::fromNSString(name)); |
| 131 | [filesToSend removeObjectForKey: conversationUid.toNSString()]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 132 | [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 Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 143 | recordButton.title = NSLocalizedString(@"Record a message", @"Record message title"); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 144 | recordingTime = 0; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 145 | [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 146 | isRecording = false; |
| 147 | [timerBox setHidden:YES]; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 148 | [sendButton setHidden: YES]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 149 | [refreshDurationTimer invalidate]; |
| 150 | refreshDurationTimer = nil; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 151 | [filesToSend removeObjectForKey: conversationUid.toNSString()]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | - (void)viewWillHide { |
| 155 | recordButton.image = [NSImage imageNamed:@"ic_action_audio.png"]; |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 156 | recordButton.title = NSLocalizedString(@"Record a message", @"Record message title"); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 157 | if(filesToSend[conversationUid.toNSString()]) { |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 158 | [sendButton setHidden: NO]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 159 | } else { |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 160 | [sendButton setHidden: YES]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 161 | } |
| 162 | recordingTime = 0; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 163 | [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 164 | 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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 185 | -(void)setConversationUID:(const QString&) convUid conversationModel:(lrc::api::ConversationModel*) convModel { |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 186 | conversationUid = convUid; |
| 187 | conversationModel = convModel; |
| 188 | [self updateView]; |
| 189 | } |
| 190 | |
| 191 | -(void) updateView { |
Kateryna Kostiuk | 3541ae2 | 2020-08-17 12:26:14 -0400 | [diff] [blame] | 192 | auto it = getConversationFromUid(conversationUid, *conversationModel); |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 193 | if (it != conversationModel->allFilteredConversations().end()) { |
Kateryna Kostiuk | dc72084 | 2020-09-10 16:44:02 -0400 | [diff] [blame] | 194 | @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 Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 199 | NSString *name = bestNameForConversation(*it, *conversationModel); |
Kateryna Kostiuk | e28a038 | 2019-10-25 16:29:08 -0400 | [diff] [blame] | 200 | |
| 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 Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 229 | } |
| 230 | [self show]; |
| 231 | } |
| 232 | |
| 233 | -(void) updateDurationLabel |
| 234 | { |
| 235 | recordingTime++; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 236 | [timerLabel setStringValue: [NSString formattedStringTimeFromSeconds: recordingTime]]; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @end |