blob: ec5b4d07b4af45f98b7ec01c5fe9d63ae7ff91fc [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;
47std::string accountToCreate;
48NSTimer* 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 Kostiuke2c4e082018-10-22 16:20:22 -040075 [self.delegate didCreateAccountWithSuccess: 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,
85 [self] (const std::string& 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();
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 Kostiukecaa3952018-07-13 16:00:34 -040099 self.accountModel->setAccountConfig(accountID, accountProperties);
100 QObject::disconnect(accountCreated);
Kateryna Kostiuke2c4e082018-10-22 16:20:22 -0400101 [self.delegate didCreateAccountWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400102 });
Kateryna Kostiuke2c4e082018-10-22 16:20:22 -0400103 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, [displayName UTF8String], "", "", "", [userNameField.stringValue UTF8String]);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400104
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 Kostiuke2c4e082018-10-22 16:20:22 -0400113 [self.delegate didCreateAccountWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400114}
115
116
117- (IBAction)editPhoto:(id)sender
118{
119 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400120#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400121 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 Kostiukecaa3952018-07-13 16:00:34 -0400127
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400128 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 Kostiukdc563242019-08-19 12:05:54 -0400137#endif
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400138 [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 Kostiuk256814e2018-09-04 14:47:33 -0400149 //do nothing when editing canceled
150 if (code == 0) {
151 return;
152 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400153 if (auto outputImage = [picker outputImage]) {
154 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400155 auto image = [picker inputImage];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400156 CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE);
157 outputImage = [outputImage imageResizeInsideMax: newSize];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400158 [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