blob: 61650f0eea679659c4364afca87cb1d852bee930 [file] [log] [blame]
Alexandre Lision745e4d62015-03-22 20:03:10 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision745e4d62015-03-22 20:03:10 -04003 * 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 Lision745e4d62015-03-22 20:03:10 -040018 */
19#import "RingWizardWC.h"
20
Alexandre Lision54187a22016-04-04 09:45:52 -040021//Cocoa
22#import <AddressBook/AddressBook.h>
23#import <Quartz/Quartz.h>
24
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040025//Qt
26#import <QUrl>
Alexandre Lision54187a22016-04-04 09:45:52 -040027#import <QPixmap>
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040028
29//LRC
Alexandre Lision745e4d62015-03-22 20:03:10 -040030#import <accountmodel.h>
31#import <protocolmodel.h>
Alexandre Lision54187a22016-04-04 09:45:52 -040032#import <profilemodel.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040033#import <QItemSelectionModel>
34#import <account.h>
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040035#import <certificate.h>
Alexandre Lision54187a22016-04-04 09:45:52 -040036#import <profilemodel.h>
37#import <profile.h>
38#import <person.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040039
40#import "AppDelegate.h"
Alexandre Lision34607032016-02-08 16:16:49 -050041#import "Constants.h"
Alexandre Lision261f1b92016-04-04 12:35:34 -040042#import "views/NSImage+Extensions.h"
Alexandre Lision34607032016-02-08 16:16:49 -050043#import "views/NSColor+RingTheme.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040044
Alexandre Lision08abfac2015-09-22 12:20:51 -040045@implementation RingWizardWC {
Alexandre Lision54187a22016-04-04 09:45:52 -040046
Alexandre Lision54187a22016-04-04 09:45:52 -040047 __unsafe_unretained IBOutlet NSButton* photoView;
Alexandre Lision33942092016-02-03 12:19:09 -050048 __unsafe_unretained IBOutlet NSTextField* nicknameField;
49 __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
50 __unsafe_unretained IBOutlet NSTextField* indicationLabel;
51 __unsafe_unretained IBOutlet NSButton* createButton;
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040052
Alexandre Lision08abfac2015-09-22 12:20:51 -040053 Account* accountToCreate;
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040054}
55
Alexandre Lision08abfac2015-09-22 12:20:51 -040056NSInteger const NICKNAME_TAG = 1;
Alexandre Lision745e4d62015-03-22 20:03:10 -040057
58- (void)windowDidLoad {
59 [super windowDidLoad];
60
Alexandre Lision33942092016-02-03 12:19:09 -050061 [nicknameField setTag:NICKNAME_TAG];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040062
Alexandre Lision34607032016-02-08 16:16:49 -050063 [self.window setBackgroundColor:[NSColor ringGreyHighlight]];
64
65 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
Alexandre Lision76d59692016-01-20 18:06:05 -050066
67 if(![appDelegate checkForRingAccount]) {
Alexandre Lision34607032016-02-08 16:16:49 -050068 accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING);
Alexandre Lision54187a22016-04-04 09:45:52 -040069
Alexandre Lision33942092016-02-03 12:19:09 -050070 [nicknameField setStringValue:NSFullUserName()];
Alexandre Lision34607032016-02-08 16:16:49 -050071 [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:nicknameField]];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -040072 }
73
Alexandre Lision54187a22016-04-04 09:45:52 -040074 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 Lision745e4d62015-03-22 20:03:10 -040083}
84
Alexandre Lision54187a22016-04-04 09:45:52 -040085- (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 Lision34607032016-02-08 16:16:49 -0500104- (IBAction)shareRingID:(id)sender {
Alexandre Lision54187a22016-04-04 09:45:52 -0400105 auto sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[nicknameField stringValue]]];
Alexandre Lision34607032016-02-08 16:16:49 -0500106 [sharingServicePicker showRelativeToRect:[sender bounds]
107 ofView:sender
108 preferredEdge:NSMinYEdge];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400109}
110
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400111- (IBAction)createRingAccount:(id)sender
112{
Alexandre Lision33942092016-02-03 12:19:09 -0500113 [nicknameField setHidden:YES];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400114 [progressBar setHidden:NO];
Alexandre Lision34607032016-02-08 16:16:49 -0500115 [createButton setHidden:YES];
Alexandre Lision54187a22016-04-04 09:45:52 -0400116 [photoView setHidden:YES];
Alexandre Lision34607032016-02-08 16:16:49 -0500117 [progressBar startAnimation:nil];
Alexandre Lision922380d2015-09-15 10:25:17 -0400118 [indicationLabel setStringValue:NSLocalizedString(@"Just a moment...",
119 @"Indication for user")];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400120
Alexandre Lision54187a22016-04-04 09:45:52 -0400121 if (auto profile = ProfileModel::instance().selectedProfile()) {
122 profile->person()->setFormattedName([[nicknameField stringValue] UTF8String]);
123 QPixmap p;
Alexandre Lision261f1b92016-04-04 12:35:34 -0400124 auto smallImg = [NSImage imageResize:[photoView image] newSize:{100,100}];
125 if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) {
Alexandre Lision54187a22016-04-04 09:45:52 -0400126 profile->person()->setPhoto(QVariant(p));
127 }
128 profile->save();
129 }
130
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400131 QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lision745e4d62015-03-22 20:03:10 -0400132
133 [self setCallback];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400134
Alexandre Lision08abfac2015-09-22 12:20:51 -0400135 [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
Alexandre Lision34607032016-02-08 16:16:49 -0500136 [self registerDefaultPreferences];
Alexandre Lision41981972015-06-04 13:27:33 -0400137}
138
139/**
Alexandre Lision34607032016-02-08 16:16:49 -0500140 * Set default values for preferences
Alexandre Lision41981972015-06-04 13:27:33 -0400141 */
Alexandre Lision34607032016-02-08 16:16:49 -0500142- (void) registerDefaultPreferences
Alexandre Lision41981972015-06-04 13:27:33 -0400143{
Alexandre Lision34607032016-02-08 16:16:49 -0500144 // enable AutoStartup
Alexandre Lision41981972015-06-04 13:27:33 -0400145 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
146 if (loginItemsRef == nil) return;
Alexandre Lision81c97212015-06-17 15:51:53 -0400147 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision41981972015-06-04 13:27:33 -0400148 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
149 if (itemRef) CFRelease(itemRef);
Alexandre Lision34607032016-02-08 16:16:49 -0500150
151 // enable Notifications
152 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400153}
154
155- (void) saveAccount
156{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400157 accountToCreate->setUpnpEnabled(YES); // Always active upnp
158 accountToCreate << Account::EditAction::SAVE;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400159}
160
161- (void) setCallback
162{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400163 QObject::connect(&AccountModel::instance(),
Alexandre Lision745e4d62015-03-22 20:03:10 -0400164 &AccountModel::accountStateChanged,
165 [=](Account *account, const Account::RegistrationState state) {
Alexandre Lision1abdf582016-02-09 14:21:19 -0500166 [self.window close];
167 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
168 [appDelegate showMainWindow];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400169 });
170}
171
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400172- (IBAction)goToApp:(id)sender
173{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400174 [self.window close];
Alexandre Lision34607032016-02-08 16:16:49 -0500175 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400176 [appDelegate showMainWindow];
177}
178
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400179#pragma mark - NSOpenSavePanelDelegate delegate methods
180
181- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError
182{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400183 return YES;
184}
185
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400186#pragma mark - NSTextFieldDelegate methods
187
188-(void)controlTextDidChange:(NSNotification *)notif
Alexandre Lision745e4d62015-03-22 20:03:10 -0400189{
Alexandre Lision33942092016-02-03 12:19:09 -0500190 NSTextField* textField = [notif object];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400191 // else it is NICKNAME_TAG field
Alexandre Lision34607032016-02-08 16:16:49 -0500192 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 Lision745e4d62015-03-22 20:03:10 -0400198}
199
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400200# pragma NSWindowDelegate methods
Alexandre Lision745e4d62015-03-22 20:03:10 -0400201
202- (void)windowWillClose:(NSNotification *)notification
203{
Alexandre Lision34607032016-02-08 16:16:49 -0500204 AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
Alexandre Lision76d59692016-01-20 18:06:05 -0500205 if ([appDelegate checkForRingAccount]) {
206 [appDelegate showMainWindow];
207 }
Alexandre Lision745e4d62015-03-22 20:03:10 -0400208}
209
210@end