Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
| 3 | * Author: Loïc Siret <loic.siret@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 | #import "RingWizardNewAccountVC.h" |
| 21 | |
| 22 | |
| 23 | //Cocoa |
| 24 | #import <AddressBook/AddressBook.h> |
| 25 | #import <Quartz/Quartz.h> |
| 26 | |
| 27 | //Qt |
| 28 | #import <QUrl> |
| 29 | #import <QPixmap> |
| 30 | |
| 31 | //LRC |
| 32 | #import <accountmodel.h> |
| 33 | #import <protocolmodel.h> |
| 34 | #import <profilemodel.h> |
| 35 | #import <QItemSelectionModel> |
| 36 | #import <account.h> |
| 37 | #import <certificate.h> |
| 38 | #import <profilemodel.h> |
| 39 | #import <profile.h> |
| 40 | #import <person.h> |
| 41 | |
| 42 | #import "AppDelegate.h" |
| 43 | #import "Constants.h" |
| 44 | #import "views/NSImage+Extensions.h" |
| 45 | #import "views/NSColor+RingTheme.h" |
| 46 | |
| 47 | @interface RingWizardNewAccountVC () |
| 48 | @end |
| 49 | |
| 50 | @implementation RingWizardNewAccountVC |
| 51 | { |
| 52 | __unsafe_unretained IBOutlet NSButton* photoView; |
| 53 | __unsafe_unretained IBOutlet NSTextField* nicknameField; |
| 54 | __unsafe_unretained IBOutlet NSSecureTextField* passwordField; |
| 55 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
| 56 | __unsafe_unretained IBOutlet NSTextField* indicationLabel; |
| 57 | __unsafe_unretained IBOutlet NSTextField* passwordLabel; |
| 58 | __unsafe_unretained IBOutlet NSButton* createButton; |
| 59 | __unsafe_unretained IBOutlet NSButton* cancelButton; |
| 60 | Account* accountToCreate; |
| 61 | NSTimer* errorTimer; |
| 62 | QMetaObject::Connection stateChanged; |
| 63 | } |
| 64 | |
| 65 | NSInteger const NICKNAME_TAG = 1; |
| 66 | |
| 67 | |
| 68 | - (void)show |
| 69 | { |
| 70 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
| 71 | [nicknameField setTag:NICKNAME_TAG]; |
| 72 | [nicknameField setStringValue:NSFullUserName()]; |
| 73 | [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:nicknameField]]; |
| 74 | |
| 75 | NSData* imgData = [[[ABAddressBook sharedAddressBook] me] imageData]; |
| 76 | if (imgData != nil) { |
| 77 | [photoView setImage:[[NSImage alloc] initWithData:imgData]]; |
| 78 | } else |
| 79 | [photoView setImage:[NSImage imageNamed:@"default_user_icon"]]; |
| 80 | |
| 81 | [photoView setWantsLayer: YES]; |
| 82 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 83 | photoView.layer.masksToBounds = YES; |
| 84 | } |
| 85 | |
| 86 | - (IBAction)editPhoto:(id)sender |
| 87 | { |
| 88 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
| 89 | |
| 90 | [pictureTaker beginPictureTakerSheetForWindow:[self.delegate window] |
| 91 | withDelegate:self |
| 92 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 93 | contextInfo:nil]; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | - (void)pictureTakerDidEnd:(IKPictureTaker *) picker |
| 98 | returnCode:(NSInteger) code |
| 99 | contextInfo:(void*) contextInfo |
| 100 | { |
| 101 | if (auto outputImage = [picker outputImage]) { |
| 102 | [photoView setImage:outputImage]; |
| 103 | } else |
| 104 | [photoView setImage:[NSImage imageNamed:@"default_user_icon"]]; |
| 105 | } |
| 106 | |
| 107 | - (IBAction)createRingAccount:(id)sender |
| 108 | { |
| 109 | [nicknameField setHidden:YES]; |
| 110 | [progressBar setHidden:NO]; |
| 111 | [createButton setHidden:YES]; |
| 112 | [photoView setHidden:YES]; |
| 113 | [passwordField setHidden:YES]; |
| 114 | [passwordLabel setHidden:YES]; |
| 115 | [cancelButton setHidden:YES]; |
| 116 | [progressBar startAnimation:nil]; |
| 117 | [indicationLabel setStringValue:NSLocalizedString(@"Just a moment...", |
| 118 | @"Indication for user")]; |
| 119 | |
| 120 | if ([self.alias isEqualToString:@""]) { |
| 121 | self.alias = NSLocalizedString(@"Unknown", @"Name used when user leave field empty"); |
| 122 | } |
| 123 | accountToCreate = AccountModel::instance().add(QString::fromNSString(self.alias), Account::Protocol::RING); |
| 124 | |
| 125 | accountToCreate->setAlias([self.alias UTF8String]); |
| 126 | accountToCreate->setDisplayName([self.alias UTF8String]); |
| 127 | |
| 128 | if (auto profile = ProfileModel::instance().selectedProfile()) { |
| 129 | profile->person()->setFormattedName([self.alias UTF8String]); |
| 130 | QPixmap p; |
| 131 | auto smallImg = [NSImage imageResize:[photoView image] newSize:{100,100}]; |
| 132 | if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) { |
| 133 | profile->person()->setPhoto(QVariant(p)); |
Alexandre Lision | 849514f | 2016-10-25 14:07:51 -0400 | [diff] [blame^] | 134 | } else { |
| 135 | auto defaultAvatar = [NSImage imageResize:[NSImage imageNamed:@"default_user_icon"] newSize:{100,100}]; |
| 136 | p.loadFromData(QByteArray::fromNSData([defaultAvatar TIFFRepresentation])); |
| 137 | profile->person()->setPhoto(QVariant(p)); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 138 | } |
| 139 | profile->save(); |
| 140 | } |
| 141 | |
| 142 | QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
| 143 | |
| 144 | [self setCallback]; |
| 145 | |
| 146 | [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1]; |
| 147 | [self registerDefaultPreferences]; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set default values for preferences |
| 152 | */ |
| 153 | - (void)registerDefaultPreferences |
| 154 | { |
| 155 | // enable AutoStartup |
| 156 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 157 | if (loginItemsRef == nil) return; |
| 158 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
| 159 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 160 | if (itemRef) CFRelease(itemRef); |
| 161 | |
| 162 | // enable Notifications |
| 163 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications]; |
| 164 | } |
| 165 | |
| 166 | - (void)saveAccount |
| 167 | { |
| 168 | accountToCreate->setArchivePassword(QString::fromNSString(passwordField.stringValue)); |
| 169 | accountToCreate->setUpnpEnabled(YES); // Always active upnp |
| 170 | accountToCreate << Account::EditAction::SAVE; |
| 171 | } |
| 172 | |
| 173 | - (void)setCallback |
| 174 | { |
| 175 | errorTimer = [NSTimer scheduledTimerWithTimeInterval:30 |
| 176 | target:self |
| 177 | selector:@selector(didCreateFailed) userInfo:nil |
| 178 | repeats:NO]; |
| 179 | stateChanged = QObject::connect(&AccountModel::instance(), |
| 180 | &AccountModel::accountStateChanged, |
| 181 | [=](Account *account, const Account::RegistrationState state) { |
| 182 | switch(state){ |
| 183 | case Account::RegistrationState::READY: |
| 184 | case Account::RegistrationState::TRYING: |
| 185 | case Account::RegistrationState::UNREGISTERED:{ |
| 186 | accountToCreate<< Account::EditAction::RELOAD; |
| 187 | QObject::disconnect(stateChanged); |
| 188 | [self.delegate didCreateAccountWithSuccess:YES]; |
| 189 | break; |
| 190 | } |
| 191 | case Account::RegistrationState::ERROR: |
| 192 | QObject::disconnect(stateChanged); |
| 193 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 194 | break; |
| 195 | case Account::RegistrationState::INITIALIZING: |
| 196 | case Account::RegistrationState::COUNT__:{ |
| 197 | //DO Nothing |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | - (void)didCreateFailed |
| 205 | { |
| 206 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 207 | } |
| 208 | |
| 209 | - (IBAction)cancel:(id)sender |
| 210 | { |
| 211 | [self.delegate didCreateAccountWithSuccess:NO]; |
| 212 | } |
| 213 | |
| 214 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 215 | |
| 216 | - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError |
| 217 | { |
| 218 | return YES; |
| 219 | } |
| 220 | |
| 221 | -(void)controlTextDidChange:(NSNotification *)notif |
| 222 | { |
| 223 | NSTextField* textField = [notif object]; |
| 224 | // else it is NICKNAME_TAG field |
| 225 | NSString* alias = textField.stringValue; |
| 226 | if ([alias isEqualToString:@""]) { |
| 227 | alias = NSLocalizedString(@"Unknown", @"Name used when user leave field empty"); |
| 228 | } |
| 229 | self.alias = alias; |
| 230 | } |
| 231 | |
| 232 | @end |