blob: 0de219d9e6aca69900241180be7962d7f306fd12 [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 Kostiukecaa3952018-07-13 16:00:34 -040032
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
45QMetaObject::Connection accountCreated;
46std::string accountToCreate;
47NSTimer* 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 Kostiuke2c4e082018-10-22 16:20:22 -040073 [self.delegate didCreateAccountWithSuccess: NO];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040074}
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 Kostiukecaa3952018-07-13 16:00:34 -040084 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 Kostiukecaa3952018-07-13 16:00:34 -040097 self.accountModel->setAccountConfig(accountID, accountProperties);
98 QObject::disconnect(accountCreated);
Kateryna Kostiuke2c4e082018-10-22 16:20:22 -040099 [self.delegate didCreateAccountWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400100 });
Kateryna Kostiuke2c4e082018-10-22 16:20:22 -0400101 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, [displayName UTF8String], "", "", "", [userNameField.stringValue UTF8String]);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400102
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 Kostiuke2c4e082018-10-22 16:20:22 -0400111 [self.delegate didCreateAccountWithSuccess: YES];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400112}
113
114
115- (IBAction)editPhoto:(id)sender
116{
117 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400118 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 Kostiukecaa3952018-07-13 16:00:34 -0400124
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400125 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 Kostiukecaa3952018-07-13 16:00:34 -0400134 [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 Kostiuk256814e2018-09-04 14:47:33 -0400145 //do nothing when editing canceled
146 if (code == 0) {
147 return;
148 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400149 if (auto outputImage = [picker outputImage]) {
150 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400151 auto image = [picker inputImage];
152 CGFloat newSize = MIN(image.size.height, image.size.width);
153 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400154 [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