blob: 5867969304d5801b9dcaa7a9872af282f70b1bd1 [file] [log] [blame]
Kateryna Kostiukecaa3952018-07-13 16:00:34 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 22019 Savoir-faire Linux Inc.
Kateryna Kostiukecaa3952018-07-13 16:00:34 -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
21//cocoa
22#import <Quartz/Quartz.h>
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -040023#import <AVFoundation/AVFoundation.h>
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040024
25//LRC
26#import <api/lrc.h>
27#import <api/newaccountmodel.h>
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040028
29//ring
30#import "AddSIPAccountVC.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040031#import "views/NSImage+Extensions.h"
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -040032#import "Constants.h"
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040033
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
46QMetaObject::Connection accountCreated;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040047QString accountToCreate;
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040048NSTimer* timeoutTimer;
49@synthesize accountModel;
50
51- (void)viewDidLoad {
52 [super viewDidLoad];
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040053 [self.view setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040054}
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 Kostiukc7e68f32019-10-09 16:15:45 -040070 [self.delegate showView:self.view];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040071}
72
73- (IBAction)cancel:(id)sender
74{
Kateryna Kostiuka505b302019-12-02 16:20:37 -050075 [self.delegate completedWithSuccess: NO];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040076}
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,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040085 [self] (const QString& accountID) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040086 if([photoView image]) {
87 NSImage *avatarImage = [photoView image];
88 auto imageToBytes = QByteArray::fromNSData([avatarImage TIFFRepresentation]).toBase64();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040089 self.accountModel->setAvatar(accountID, QString(imageToBytes));
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040090 }
91 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
92 if(![serverField.stringValue isEqualToString:@""]) {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040093 accountProperties.hostname = QString::fromNSString(serverField.stringValue);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040094 }
95 if(![passwordField.stringValue isEqualToString:@""]) {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040096 accountProperties.password = QString::fromNSString(passwordField.stringValue);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040097 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040098 self.accountModel->setAccountConfig(accountID, accountProperties);
99 QObject::disconnect(accountCreated);
Kateryna Kostiuka505b302019-12-02 16:20:37 -0500100 [self.delegate completedWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400101 });
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400102 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, QString::fromNSString(displayName), "", "", "", QString::fromNSString(userNameField.stringValue));
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400103
104 timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5
105 target:self
106 selector:@selector(addingAccountTimeout) userInfo:nil
107 repeats:NO];
108}
109
110-(void) addingAccountTimeout {
111 QObject::disconnect(accountCreated);
Kateryna Kostiuka505b302019-12-02 16:20:37 -0500112 [self.delegate completedWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400113}
114
115
116- (IBAction)editPhoto:(id)sender
117{
118 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400119#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400120 if (@available(macOS 10.14, *)) {
121 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
122 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
123 {
124 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
125 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400126
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400127 if(authStatus == AVAuthorizationStatusNotDetermined)
128 {
129 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
130 if(!granted){
131 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
132 }
133 }];
134 }
135 }
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400136#endif
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400137 [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window]
138 withDelegate:self
139 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
140 contextInfo:nil];
141
142}
143
144- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
145 returnCode:(NSInteger) code
146 contextInfo:(void*) contextInfo
147{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400148 //do nothing when editing canceled
149 if (code == 0) {
150 return;
151 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400152 if (auto outputImage = [picker outputImage]) {
153 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400154 auto image = [picker inputImage];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400155 CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE);
156 outputImage = [outputImage imageResizeInsideMax: newSize];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400157 [photoView setImage:outputImage];
158 [addProfilePhotoImage setHidden:YES];
159 } else if(!photoView.image) {
160 [photoView setBordered:YES];
161 [addProfilePhotoImage setHidden:NO];
162 }
163}
164
165@end