Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@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. |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 18 | */ |
| 19 | #import "RingWizardWC.h" |
| 20 | |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 21 | //Cocoa |
| 22 | #import <AddressBook/AddressBook.h> |
| 23 | #import <Quartz/Quartz.h> |
| 24 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 25 | //Qt |
| 26 | #import <QUrl> |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 27 | #import <QPixmap> |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 28 | |
| 29 | //LRC |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 30 | #import <accountmodel.h> |
| 31 | #import <protocolmodel.h> |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 32 | #import <profilemodel.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 33 | #import <QItemSelectionModel> |
| 34 | #import <account.h> |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 35 | #import <certificate.h> |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 36 | #import <profilemodel.h> |
| 37 | #import <profile.h> |
| 38 | #import <person.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 39 | |
| 40 | #import "AppDelegate.h" |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 41 | #import "Constants.h" |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 42 | #import "views/NSImage+Extensions.h" |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 43 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 44 | |
Alexandre Lision | 08abfac | 2015-09-22 12:20:51 -0400 | [diff] [blame] | 45 | @implementation RingWizardWC { |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 46 | |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSButton* photoView; |
Alexandre Lision | 3394209 | 2016-02-03 12:19:09 -0500 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet NSTextField* nicknameField; |
| 49 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
| 50 | __unsafe_unretained IBOutlet NSTextField* indicationLabel; |
| 51 | __unsafe_unretained IBOutlet NSButton* createButton; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 52 | |
Alexandre Lision | 08abfac | 2015-09-22 12:20:51 -0400 | [diff] [blame] | 53 | Account* accountToCreate; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Alexandre Lision | 08abfac | 2015-09-22 12:20:51 -0400 | [diff] [blame] | 56 | NSInteger const NICKNAME_TAG = 1; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 57 | |
| 58 | - (void)windowDidLoad { |
| 59 | [super windowDidLoad]; |
| 60 | |
Alexandre Lision | 3394209 | 2016-02-03 12:19:09 -0500 | [diff] [blame] | 61 | [nicknameField setTag:NICKNAME_TAG]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 62 | |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 63 | [self.window setBackgroundColor:[NSColor ringGreyHighlight]]; |
| 64 | |
| 65 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 66 | |
| 67 | if(![appDelegate checkForRingAccount]) { |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 68 | accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING); |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 69 | |
Alexandre Lision | 3394209 | 2016-02-03 12:19:09 -0500 | [diff] [blame] | 70 | [nicknameField setStringValue:NSFullUserName()]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 71 | [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:nicknameField]]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 74 | NSData* imgData = [[[ABAddressBook sharedAddressBook] me] imageData]; |
| 75 | if (imgData != nil) { |
| 76 | [photoView setImage:[[NSImage alloc] initWithData:imgData]]; |
| 77 | } else |
| 78 | [photoView setImage:[NSImage imageNamed:@"default_user_icon"]]; |
| 79 | |
| 80 | [photoView setWantsLayer: YES]; |
| 81 | photoView.layer.cornerRadius = photoView.frame.size.width / 2; |
| 82 | photoView.layer.masksToBounds = YES; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 85 | - (IBAction) editPhoto:(id)sender |
| 86 | { |
| 87 | auto pictureTaker = [IKPictureTaker pictureTaker]; |
| 88 | [pictureTaker beginPictureTakerSheetForWindow:self.window |
| 89 | withDelegate:self |
| 90 | didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:) |
| 91 | contextInfo:nil]; |
| 92 | } |
| 93 | |
| 94 | - (void) pictureTakerDidEnd:(IKPictureTaker *) picker |
| 95 | returnCode:(NSInteger) code |
| 96 | contextInfo:(void*) contextInfo |
| 97 | { |
| 98 | if (auto outputImage = [picker outputImage]) { |
| 99 | [photoView setImage:outputImage]; |
| 100 | } else |
| 101 | [photoView setImage:[NSImage imageNamed:@"default_user_icon"]]; |
| 102 | } |
| 103 | |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 104 | - (IBAction)shareRingID:(id)sender { |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 105 | auto sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[nicknameField stringValue]]]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 106 | [sharingServicePicker showRelativeToRect:[sender bounds] |
| 107 | ofView:sender |
| 108 | preferredEdge:NSMinYEdge]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 111 | - (IBAction)createRingAccount:(id)sender |
| 112 | { |
Alexandre Lision | 3394209 | 2016-02-03 12:19:09 -0500 | [diff] [blame] | 113 | [nicknameField setHidden:YES]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 114 | [progressBar setHidden:NO]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 115 | [createButton setHidden:YES]; |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 116 | [photoView setHidden:YES]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 117 | [progressBar startAnimation:nil]; |
Alexandre Lision | 922380d | 2015-09-15 10:25:17 -0400 | [diff] [blame] | 118 | [indicationLabel setStringValue:NSLocalizedString(@"Just a moment...", |
| 119 | @"Indication for user")]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 120 | |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 121 | if (auto profile = ProfileModel::instance().selectedProfile()) { |
| 122 | profile->person()->setFormattedName([[nicknameField stringValue] UTF8String]); |
| 123 | QPixmap p; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 124 | auto smallImg = [NSImage imageResize:[photoView image] newSize:{100,100}]; |
| 125 | if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) { |
Alexandre Lision | 54187a2 | 2016-04-04 09:45:52 -0400 | [diff] [blame] | 126 | profile->person()->setPhoto(QVariant(p)); |
| 127 | } |
| 128 | profile->save(); |
| 129 | } |
| 130 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 131 | QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 132 | |
| 133 | [self setCallback]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 134 | |
Alexandre Lision | 08abfac | 2015-09-22 12:20:51 -0400 | [diff] [blame] | 135 | [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 136 | [self registerDefaultPreferences]; |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 140 | * Set default values for preferences |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 141 | */ |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 142 | - (void) registerDefaultPreferences |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 143 | { |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 144 | // enable AutoStartup |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 145 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 146 | if (loginItemsRef == nil) return; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 147 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 4198197 | 2015-06-04 13:27:33 -0400 | [diff] [blame] | 148 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 149 | if (itemRef) CFRelease(itemRef); |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 150 | |
| 151 | // enable Notifications |
| 152 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | - (void) saveAccount |
| 156 | { |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 157 | accountToCreate->setUpnpEnabled(YES); // Always active upnp |
| 158 | accountToCreate << Account::EditAction::SAVE; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | - (void) setCallback |
| 162 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 163 | QObject::connect(&AccountModel::instance(), |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 164 | &AccountModel::accountStateChanged, |
| 165 | [=](Account *account, const Account::RegistrationState state) { |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 166 | [self.window close]; |
| 167 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
| 168 | [appDelegate showMainWindow]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 169 | }); |
| 170 | } |
| 171 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 172 | - (IBAction)goToApp:(id)sender |
| 173 | { |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 174 | [self.window close]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 175 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 176 | [appDelegate showMainWindow]; |
| 177 | } |
| 178 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 179 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 180 | |
| 181 | - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError |
| 182 | { |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 183 | return YES; |
| 184 | } |
| 185 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 186 | #pragma mark - NSTextFieldDelegate methods |
| 187 | |
| 188 | -(void)controlTextDidChange:(NSNotification *)notif |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 189 | { |
Alexandre Lision | 3394209 | 2016-02-03 12:19:09 -0500 | [diff] [blame] | 190 | NSTextField* textField = [notif object]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 191 | // else it is NICKNAME_TAG field |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 192 | NSString* alias = textField.stringValue; |
| 193 | if ([alias isEqualToString:@""]) { |
| 194 | alias = NSLocalizedString(@"Unknown", @"Name used when user leave field empty"); |
| 195 | } |
| 196 | accountToCreate->setAlias([alias UTF8String]); |
| 197 | accountToCreate->setDisplayName([alias UTF8String]); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 200 | # pragma NSWindowDelegate methods |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 201 | |
| 202 | - (void)windowWillClose:(NSNotification *)notification |
| 203 | { |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 204 | AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 205 | if ([appDelegate checkForRingAccount]) { |
| 206 | [appDelegate showMainWindow]; |
| 207 | } |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @end |