Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 22019 Savoir-faire Linux Inc. |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -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 | |
| 21 | //cocoa |
| 22 | #import <Quartz/Quartz.h> |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 23 | #import <AVFoundation/AVFoundation.h> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 24 | |
| 25 | //LRC |
| 26 | #import <api/lrc.h> |
| 27 | #import <api/newaccountmodel.h> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 28 | |
| 29 | //ring |
| 30 | #import "AddSIPAccountVC.h" |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 31 | #import "views/NSImage+Extensions.h" |
Kateryna Kostiuk | 2f2ef95 | 2019-05-07 11:04:41 -0400 | [diff] [blame] | 32 | #import "Constants.h" |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 33 | |
| 34 | @interface AddSIPAccountVC () { |
| 35 | __unsafe_unretained IBOutlet NSButton* photoView; |
| 36 | __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage; |
| 37 | __unsafe_unretained IBOutlet NSTextField* displayNameField; |
| 38 | __unsafe_unretained IBOutlet NSTextField* userNameField; |
| 39 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
| 40 | __unsafe_unretained IBOutlet NSTextField* serverField; |
| 41 | } |
| 42 | @end |
| 43 | |
| 44 | @implementation AddSIPAccountVC |
| 45 | |
| 46 | QMetaObject::Connection accountCreated; |
| 47 | std::string accountToCreate; |
| 48 | NSTimer* timeoutTimer; |
| 49 | @synthesize accountModel; |
| 50 | |
| 51 | - (void)viewDidLoad { |
| 52 | [super viewDidLoad]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 53 | [self.view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel { |
| 57 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 58 | { |
| 59 | self.accountModel = accountModel; |
| 60 | } |
| 61 | return self; |
| 62 | } |
| 63 | |
| 64 | -(void) show { |
| 65 | [photoView setWantsLayer: YES]; |
| 66 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 67 | photoView.layer.masksToBounds = YES; |
| 68 | [photoView setBordered:YES]; |
| 69 | [addProfilePhotoImage setWantsLayer: YES]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 70 | [self.delegate showView:self.view]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | - (IBAction)cancel:(id)sender |
| 74 | { |
Kateryna Kostiuk | a505b30 | 2019-12-02 16:20:37 -0500 | [diff] [blame^] | 75 | [self.delegate completedWithSuccess: NO]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | - (IBAction)addAccount:(id)sender |
| 79 | { |
| 80 | NSString* displayName = [displayNameField.stringValue isEqualToString:@""] ? @"SIP" : displayNameField.stringValue; |
| 81 | |
| 82 | QObject::disconnect(accountCreated); |
| 83 | accountCreated = QObject::connect(self.accountModel, |
| 84 | &lrc::api::NewAccountModel::accountAdded, |
| 85 | [self] (const std::string& accountID) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 86 | if([photoView image]) { |
| 87 | NSImage *avatarImage = [photoView image]; |
| 88 | auto imageToBytes = QByteArray::fromNSData([avatarImage TIFFRepresentation]).toBase64(); |
| 89 | std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length()); |
| 90 | self.accountModel->setAvatar(accountID, imageToString); |
| 91 | } |
| 92 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID); |
| 93 | if(![serverField.stringValue isEqualToString:@""]) { |
| 94 | accountProperties.hostname = [serverField.stringValue UTF8String]; |
| 95 | } |
| 96 | if(![passwordField.stringValue isEqualToString:@""]) { |
| 97 | accountProperties.password = [passwordField.stringValue UTF8String]; |
| 98 | } |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 99 | self.accountModel->setAccountConfig(accountID, accountProperties); |
| 100 | QObject::disconnect(accountCreated); |
Kateryna Kostiuk | a505b30 | 2019-12-02 16:20:37 -0500 | [diff] [blame^] | 101 | [self.delegate completedWithSuccess: YES]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 102 | }); |
Kateryna Kostiuk | e2c4e08 | 2018-10-22 16:20:22 -0400 | [diff] [blame] | 103 | accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, [displayName UTF8String], "", "", "", [userNameField.stringValue UTF8String]); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 104 | |
| 105 | timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5 |
| 106 | target:self |
| 107 | selector:@selector(addingAccountTimeout) userInfo:nil |
| 108 | repeats:NO]; |
| 109 | } |
| 110 | |
| 111 | -(void) addingAccountTimeout { |
| 112 | QObject::disconnect(accountCreated); |
Kateryna Kostiuk | a505b30 | 2019-12-02 16:20:37 -0500 | [diff] [blame^] | 113 | [self.delegate completedWithSuccess: YES]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | - (IBAction)editPhoto:(id)sender |
| 118 | { |
| 119 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
Kateryna Kostiuk | dc56324 | 2019-08-19 12:05:54 -0400 | [diff] [blame] | 120 | #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 121 | if (@available(macOS 10.14, *)) { |
| 122 | AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; |
| 123 | if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) |
| 124 | { |
| 125 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 126 | } |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 127 | |
Kateryna Kostiuk | 91b44e3 | 2018-09-28 17:08:02 -0400 | [diff] [blame] | 128 | if(authStatus == AVAuthorizationStatusNotDetermined) |
| 129 | { |
| 130 | [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { |
| 131 | if(!granted){ |
| 132 | [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey]; |
| 133 | } |
| 134 | }]; |
| 135 | } |
| 136 | } |
Kateryna Kostiuk | dc56324 | 2019-08-19 12:05:54 -0400 | [diff] [blame] | 137 | #endif |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 138 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 139 | withDelegate:self |
| 140 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 141 | contextInfo:nil]; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
| 146 | returnCode:(NSInteger) code |
| 147 | contextInfo:(void*) contextInfo |
| 148 | { |
Kateryna Kostiuk | 256814e | 2018-09-04 14:47:33 -0400 | [diff] [blame] | 149 | //do nothing when editing canceled |
| 150 | if (code == 0) { |
| 151 | return; |
| 152 | } |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 153 | if (auto outputImage = [picker outputImage]) { |
| 154 | [photoView setBordered:NO]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 155 | auto image = [picker inputImage]; |
Kateryna Kostiuk | 2f2ef95 | 2019-05-07 11:04:41 -0400 | [diff] [blame] | 156 | CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE); |
| 157 | outputImage = [outputImage imageResizeInsideMax: newSize]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 158 | [photoView setImage:outputImage]; |
| 159 | [addProfilePhotoImage setHidden:YES]; |
| 160 | } else if(!photoView.image) { |
| 161 | [photoView setBordered:YES]; |
| 162 | [addProfilePhotoImage setHidden:NO]; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @end |