Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame^] | 1 | /* |
| 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> |
| 23 | |
| 24 | //LRC |
| 25 | #import <api/lrc.h> |
| 26 | #import <api/newaccountmodel.h> |
| 27 | #import <account.h> |
| 28 | |
| 29 | //ring |
| 30 | #import "AddSIPAccountVC.h" |
| 31 | |
| 32 | @interface AddSIPAccountVC () { |
| 33 | __unsafe_unretained IBOutlet NSButton* photoView; |
| 34 | __unsafe_unretained IBOutlet NSImageView* addProfilePhotoImage; |
| 35 | __unsafe_unretained IBOutlet NSTextField* displayNameField; |
| 36 | __unsafe_unretained IBOutlet NSTextField* userNameField; |
| 37 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
| 38 | __unsafe_unretained IBOutlet NSTextField* serverField; |
| 39 | } |
| 40 | @end |
| 41 | |
| 42 | @implementation AddSIPAccountVC |
| 43 | |
| 44 | QMetaObject::Connection accountCreated; |
| 45 | std::string accountToCreate; |
| 46 | NSTimer* timeoutTimer; |
| 47 | @synthesize accountModel; |
| 48 | |
| 49 | - (void)viewDidLoad { |
| 50 | [super viewDidLoad]; |
| 51 | [self.view setAutoresizingMask: NSViewHeightSizable]; |
| 52 | } |
| 53 | |
| 54 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel { |
| 55 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 56 | { |
| 57 | self.accountModel = accountModel; |
| 58 | } |
| 59 | return self; |
| 60 | } |
| 61 | |
| 62 | -(void) show { |
| 63 | [photoView setWantsLayer: YES]; |
| 64 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 65 | photoView.layer.masksToBounds = YES; |
| 66 | [photoView setBordered:YES]; |
| 67 | [addProfilePhotoImage setWantsLayer: YES]; |
| 68 | } |
| 69 | |
| 70 | - (IBAction)cancel:(id)sender |
| 71 | { |
| 72 | [self.delegate close]; |
| 73 | } |
| 74 | |
| 75 | - (IBAction)addAccount:(id)sender |
| 76 | { |
| 77 | NSString* displayName = [displayNameField.stringValue isEqualToString:@""] ? @"SIP" : displayNameField.stringValue; |
| 78 | |
| 79 | QObject::disconnect(accountCreated); |
| 80 | accountCreated = QObject::connect(self.accountModel, |
| 81 | &lrc::api::NewAccountModel::accountAdded, |
| 82 | [self] (const std::string& accountID) { |
| 83 | if(accountID.compare(accountToCreate) != 0) { |
| 84 | return; |
| 85 | } |
| 86 | 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 | } |
| 99 | if(![userNameField.stringValue isEqualToString:@""]) { |
| 100 | accountProperties.username = [userNameField.stringValue UTF8String]; |
| 101 | } |
| 102 | self.accountModel->setAccountConfig(accountID, accountProperties); |
| 103 | QObject::disconnect(accountCreated); |
| 104 | [self.delegate close]; |
| 105 | }); |
| 106 | accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::SIP, [displayName UTF8String]); |
| 107 | |
| 108 | timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:5 |
| 109 | target:self |
| 110 | selector:@selector(addingAccountTimeout) userInfo:nil |
| 111 | repeats:NO]; |
| 112 | } |
| 113 | |
| 114 | -(void) addingAccountTimeout { |
| 115 | QObject::disconnect(accountCreated); |
| 116 | [self.delegate close]; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | - (IBAction)editPhoto:(id)sender |
| 121 | { |
| 122 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
| 123 | |
| 124 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 125 | withDelegate:self |
| 126 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 127 | contextInfo:nil]; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
| 132 | returnCode:(NSInteger) code |
| 133 | contextInfo:(void*) contextInfo |
| 134 | { |
| 135 | if (auto outputImage = [picker outputImage]) { |
| 136 | [photoView setBordered:NO]; |
| 137 | [photoView setImage:outputImage]; |
| 138 | [addProfilePhotoImage setHidden:YES]; |
| 139 | } else if(!photoView.image) { |
| 140 | [photoView setBordered:YES]; |
| 141 | [addProfilePhotoImage setHidden:NO]; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @end |