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