multi-device: update Ring account creation wizard

This patch implements multi-device support

- The account creation wizzard has now two options "Existing Ring
  account" and "New Ring account".

  "Existing Ring account": Allows for fetching a Ring account archive
  from the DHT. Requires pin and password.

  "New Ring account": This is the previously existing wizard. It was
  modified to ask for a password which will be used to encrypt the
  account archive. This password is then required for exporting the
  account on the Ring.

- Creating a new Ring account with the "+" button now triggers the
  account creation wizard.

Change-Id: I6840d968e52de014ab0aa9dd6f5bc1ab31f9e9ec
Tuleap: #958
diff --git a/src/RingWizardWC.mm b/src/RingWizardWC.mm
index 61650f0..373eb80 100644
--- a/src/RingWizardWC.mm
+++ b/src/RingWizardWC.mm
@@ -22,188 +22,149 @@
 #import <AddressBook/AddressBook.h>
 #import <Quartz/Quartz.h>
 
-//Qt
-#import <QUrl>
-#import <QPixmap>
-
-//LRC
-#import <accountmodel.h>
-#import <protocolmodel.h>
-#import <profilemodel.h>
-#import <QItemSelectionModel>
-#import <account.h>
-#import <certificate.h>
-#import <profilemodel.h>
-#import <profile.h>
-#import <person.h>
 
 #import "AppDelegate.h"
 #import "Constants.h"
 #import "views/NSImage+Extensions.h"
 #import "views/NSColor+RingTheme.h"
+#import "RingWizardNewAccountVC.h"
+#import "RingWizardLinkAccountVC.h"
+#import "RingWizardChooseVC.h"
 
+
+@interface RingWizardWC ()
+
+@property (retain, nonatomic)IBOutlet NSView* container;
+
+@end
 @implementation RingWizardWC {
-
-    __unsafe_unretained IBOutlet NSButton* photoView;
-    __unsafe_unretained IBOutlet NSTextField* nicknameField;
-    __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
-    __unsafe_unretained IBOutlet NSTextField* indicationLabel;
-    __unsafe_unretained IBOutlet NSButton* createButton;
-
-    Account* accountToCreate;
+    IBOutlet RingWizardNewAccountVC* newAccountWC;
+    IBOutlet RingWizardLinkAccountVC* linkAccountWC;
+    IBOutlet RingWizardChooseVC* chooseActiontWC;
+    float initialHeight;
+    float currentHeight;
+    BOOL isCancelable;
 }
 
-NSInteger const NICKNAME_TAG        = 1;
-
-- (void)windowDidLoad {
+- (void)windowDidLoad
+{
     [super windowDidLoad];
 
-    [nicknameField setTag:NICKNAME_TAG];
-
     [self.window setBackgroundColor:[NSColor ringGreyHighlight]];
+    chooseActiontWC = [[RingWizardChooseVC alloc] initWithNibName:@"RingWizardChoose" bundle:nil];
+    [chooseActiontWC setDelegate:self];
+    linkAccountWC = [[RingWizardLinkAccountVC alloc] initWithNibName:@"RingWizardLinkAccount" bundle:nil];
+    [linkAccountWC setDelegate:self];
+    newAccountWC = [[RingWizardNewAccountVC alloc] initWithNibName:@"RingWizardNewAccount" bundle:nil];
+    [newAccountWC setDelegate:self];
+    initialHeight = self.window.frame.size.height;
+    currentHeight = self.window.frame.size.height;
+    isCancelable = NO;
+    [self showView:chooseActiontWC.view];
+}
 
-    AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
-
-    if(![appDelegate checkForRingAccount]) {
-        accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING);
-
-        [nicknameField setStringValue:NSFullUserName()];
-        [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:nicknameField]];
+- (void)removeSubviews
+{
+    while ([self.container.subviews count] > 0)
+    {
+        [[self.container.subviews firstObject] removeFromSuperview];
     }
-
-    NSData* imgData = [[[ABAddressBook sharedAddressBook] me] imageData];
-    if (imgData != nil) {
-        [photoView setImage:[[NSImage alloc] initWithData:imgData]];
-    } else
-        [photoView setImage:[NSImage imageNamed:@"default_user_icon"]];
-
-    [photoView setWantsLayer: YES];
-    photoView.layer.cornerRadius = photoView.frame.size.width / 2;
-    photoView.layer.masksToBounds = YES;
 }
 
-- (IBAction) editPhoto:(id)sender
+#define minHeight 135
+- (void)showView: (NSView*) view
 {
-    auto pictureTaker = [IKPictureTaker pictureTaker];
-    [pictureTaker beginPictureTakerSheetForWindow:self.window
-                                     withDelegate:self
-                                   didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
-                                      contextInfo:nil];
-}
-
-- (void) pictureTakerDidEnd:(IKPictureTaker *) picker
-                 returnCode:(NSInteger) code
-                contextInfo:(void*) contextInfo
-{
-    if (auto outputImage = [picker outputImage]) {
-        [photoView setImage:outputImage];
-    } else
-        [photoView setImage:[NSImage imageNamed:@"default_user_icon"]];
-}
-
-- (IBAction)shareRingID:(id)sender {
-    auto sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[nicknameField stringValue]]];
-    [sharingServicePicker showRelativeToRect:[sender bounds]
-                                      ofView:sender
-                               preferredEdge:NSMinYEdge];
-}
-
-- (IBAction)createRingAccount:(id)sender
-{
-    [nicknameField setHidden:YES];
-    [progressBar setHidden:NO];
-    [createButton setHidden:YES];
-    [photoView setHidden:YES];
-    [progressBar startAnimation:nil];
-    [indicationLabel setStringValue:NSLocalizedString(@"Just a moment...",
-                                                      @"Indication for user")];
-
-    if (auto profile = ProfileModel::instance().selectedProfile()) {
-        profile->person()->setFormattedName([[nicknameField stringValue] UTF8String]);
-        QPixmap p;
-        auto smallImg = [NSImage imageResize:[photoView image] newSize:{100,100}];
-        if (p.loadFromData(QByteArray::fromNSData([smallImg TIFFRepresentation]))) {
-            profile->person()->setPhoto(QVariant(p));
-        }
-        profile->save();
+    [self removeSubviews];
+    NSRect frame = [self.container frame];
+    frame.size.height = MAX(minHeight, view.bounds.size.height);
+    [view setFrame:frame];
+    [self.container setFrame:frame];
+    float size = 0;
+    NSView *container = self.window.contentView;
+    for (NSView *child in container.subviews){
+        size += child.frame.size.height;
     }
-
-    QModelIndex qIdx =  AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
-
-    [self setCallback];
-
-    [self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
-    [self registerDefaultPreferences];
-}
-
-/**
- * Set default values for preferences
- */
-- (void) registerDefaultPreferences
-{
-    // enable AutoStartup
-    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
-    if (loginItemsRef == nil) return;
-    CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
-    LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
-    if (itemRef) CFRelease(itemRef);
-
-    // enable Notifications
-    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::Notifications];
-}
-
-- (void) saveAccount
-{
-    accountToCreate->setUpnpEnabled(YES); // Always active upnp
-    accountToCreate << Account::EditAction::SAVE;
-}
-
-- (void) setCallback
-{
-    QObject::connect(&AccountModel::instance(),
-                     &AccountModel::accountStateChanged,
-                     [=](Account *account, const Account::RegistrationState state) {
-                         [self.window close];
-                         AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
-                         [appDelegate showMainWindow];
-                     });
-}
-
-- (IBAction)goToApp:(id)sender
-{
-    [self.window close];
-    AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
-    [appDelegate showMainWindow];
-}
-
-#pragma mark - NSOpenSavePanelDelegate delegate methods
-
-- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError
-{
-    return YES;
-}
-
-#pragma mark - NSTextFieldDelegate methods
-
--(void)controlTextDidChange:(NSNotification *)notif
-{
-    NSTextField* textField = [notif object];
-    // else it is NICKNAME_TAG field
-    NSString* alias = textField.stringValue;
-    if ([alias isEqualToString:@""]) {
-        alias = NSLocalizedString(@"Unknown", @"Name used when user leave field empty");
+    if (currentHeight != size){
+        currentHeight = size;
+        NSRect frameWindows = self.window.frame;
+        frameWindows.size.height = currentHeight;
+        [self.window setFrame:frameWindows display:YES animate:YES];
     }
-    accountToCreate->setAlias([alias UTF8String]);
-    accountToCreate->setDisplayName([alias UTF8String]);
+    [self.container addSubview:view];
+}
+
+- (void)showChooseWithCancelButton:(BOOL)showCancel
+{
+    [self showView: chooseActiontWC.view];
+    [chooseActiontWC showCancelButton:showCancel];
+    isCancelable = showCancel;
+}
+
+- (void)showNewAccountVC
+{
+    [self showView: newAccountWC.view];
+    [newAccountWC show];
+}
+
+- (void)showLinkAccountVC
+{
+    [self showView: linkAccountWC.view];
+    [linkAccountWC show];
 }
 
 # pragma NSWindowDelegate methods
 
 - (void)windowWillClose:(NSNotification *)notification
 {
-    AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
-    if ([appDelegate checkForRingAccount]) {
-        [appDelegate showMainWindow];
+    if (!isCancelable){
+        AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
+        if ([appDelegate checkForRingAccount]) {
+            [appDelegate showMainWindow];
+        }
+    }
+}
+
+#pragma - WizardChooseDelegate methods
+
+- (void)didCompleteWithAction:(WizardAction)action
+{
+    if (action == WIZARD_ACTION_LINK){
+        [self showLinkAccountVC];
+    } else if (action == WIZARD_ACTION_NEW){
+        [self showNewAccountVC];
+    } else {
+        [self.window close];
+    }
+
+}
+
+#pragma - WizardCreateAccountDelegate methods
+
+- (void)didCreateAccountWithSuccess:(BOOL)success
+{
+    if (success) {
+        [self.window close];
+        if (!isCancelable){
+            AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
+            [appDelegate showMainWindow];
+        }
+    } else {
+        [self showChooseWithCancelButton:isCancelable];
+    }
+}
+
+#pragma - WizardLinkAccountDelegate methods
+
+- (void)didLinkAccountWithSuccess:(BOOL)success
+{
+    if (success) {
+        [self.window close];
+        if (!isCancelable){
+            AppDelegate* appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
+            [appDelegate showMainWindow];
+        }
+    } else {
+        [self showChooseWithCancelButton:isCancelable];
     }
 }