blob: 44dc18ff513aee70eb04bcbc82ace68695f10354 [file] [log] [blame]
Kateryna Kostiukecaa3952018-07-13 16:00:34 -04001/*
2 * Copyright (C) 22018 Savoir-faire Linux Inc.
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 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>
28#import <account.h>
29
30//ring
31#import "AddSIPAccountVC.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040032#import "views/NSImage+Extensions.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];
53 [self.view setAutoresizingMask: NSViewHeightSizable];
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];
70}
71
72- (IBAction)cancel:(id)sender
73{
74 [self.delegate close];
75}
76
77- (IBAction)addAccount:(id)sender
78{
79 NSString* displayName = [displayNameField.stringValue isEqualToString:@""] ? @"SIP" : displayNameField.stringValue;
80
81 QObject::disconnect(accountCreated);
82 accountCreated = QObject::connect(self.accountModel,
83 &lrc::api::NewAccountModel::accountAdded,
84 [self] (const std::string& accountID) {
85 if(accountID.compare(accountToCreate) != 0) {
86 return;
87 }
88 if([photoView image]) {
89 NSImage *avatarImage = [photoView image];
90 auto imageToBytes = QByteArray::fromNSData([avatarImage TIFFRepresentation]).toBase64();
91 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
92 self.accountModel->setAvatar(accountID, imageToString);
93 }
94 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
95 if(![serverField.stringValue isEqualToString:@""]) {
96 accountProperties.hostname = [serverField.stringValue UTF8String];
97 }
98 if(![passwordField.stringValue isEqualToString:@""]) {
99 accountProperties.password = [passwordField.stringValue UTF8String];
100 }
101 if(![userNameField.stringValue isEqualToString:@""]) {
102 accountProperties.username = [userNameField.stringValue UTF8String];
103 }
104 self.accountModel->setAccountConfig(accountID, accountProperties);
105 QObject::disconnect(accountCreated);
106 [self.delegate close];
107 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400108 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, [@"SIP" UTF8String]);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400109
110 timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5
111 target:self
112 selector:@selector(addingAccountTimeout) userInfo:nil
113 repeats:NO];
114}
115
116-(void) addingAccountTimeout {
117 QObject::disconnect(accountCreated);
118 [self.delegate close];
119}
120
121
122- (IBAction)editPhoto:(id)sender
123{
124 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400125 if (@available(macOS 10.14, *)) {
126 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
127 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
128 {
129 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
130 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400131
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400132 if(authStatus == AVAuthorizationStatusNotDetermined)
133 {
134 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
135 if(!granted){
136 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
137 }
138 }];
139 }
140 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400141 [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window]
142 withDelegate:self
143 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
144 contextInfo:nil];
145
146}
147
148- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
149 returnCode:(NSInteger) code
150 contextInfo:(void*) contextInfo
151{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400152 //do nothing when editing canceled
153 if (code == 0) {
154 return;
155 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400156 if (auto outputImage = [picker outputImage]) {
157 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400158 auto image = [picker inputImage];
159 CGFloat newSize = MIN(image.size.height, image.size.width);
160 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400161 [photoView setImage:outputImage];
162 [addProfilePhotoImage setHidden:YES];
163 } else if(!photoView.image) {
164 [photoView setBordered:YES];
165 [addProfilePhotoImage setHidden:NO];
166 }
167}
168
169@end