wizard: add photo selection

Allow the user to define a photo that will be sent as a public
profile when making a call.
Remove menu entry 'Setup Ring' showing the wizard in top menu bar

Tuleap: #530
Change-Id: I5fdd82000b72008563eb61fae61a4ee1eb406c65
diff --git a/src/RingWizardWC.mm b/src/RingWizardWC.mm
index f83bc78..569ffaf 100644
--- a/src/RingWizardWC.mm
+++ b/src/RingWizardWC.mm
@@ -18,21 +18,33 @@
  */
 #import "RingWizardWC.h"
 
+//Cocoa
+#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/NSColor+RingTheme.h"
 
 @implementation RingWizardWC {
+
+
+    __unsafe_unretained IBOutlet NSButton* photoView;
     __unsafe_unretained IBOutlet NSTextField* nicknameField;
     __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
     __unsafe_unretained IBOutlet NSTextField* indicationLabel;
@@ -66,18 +78,24 @@
 
     if(![appDelegate checkForRingAccount]) {
         accountToCreate = AccountModel::instance().add(QString::fromNSString(NSFullUserName()), Account::Protocol::RING);
+
         [nicknameField setStringValue:NSFullUserName()];
         [self controlTextDidChange:[NSNotification notificationWithName:@"PlaceHolder" object:nicknameField]];
-    } else {
-        [indicationLabel setStringValue:NSLocalizedString(@"Ring is already ready to work",
-                                                          @"Display message to user")];
-        auto accList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
-        [self displayHash:accList[0]->username().toNSString()];
     }
 
     [caListPathControl setDelegate:self];
     [certificatePathControl setDelegate:self];
     [pvkPathControl setDelegate:self];
+
+    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;
 }
 
 - (void) displayHash:(NSString* ) hash
@@ -99,8 +117,27 @@
     [createButton setAction:@selector(goToApp:)];
 }
 
+- (IBAction) editPhoto:(id)sender
+{
+    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 {
-    NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[nicknameField stringValue]]];
+    auto sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[nicknameField stringValue]]];
     [sharingServicePicker showRelativeToRect:[sender bounds]
                                       ofView:sender
                                preferredEdge:NSMinYEdge];
@@ -111,10 +148,20 @@
     [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;
+        if (p.loadFromData(QByteArray::fromNSData([[photoView image] TIFFRepresentation]))) {
+            profile->person()->setPhoto(QVariant(p));
+        }
+        profile->save();
+    }
+
     QModelIndex qIdx =  AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
 
     [self setCallback];
@@ -217,9 +264,7 @@
     [self->certificatePathControl setURL:fileURL];
     accountToCreate->setTlsCertificate([[fileURL path] UTF8String]);
 
-    auto cert = accountToCreate->tlsCertificate();
-
-    if (cert) {
+    if (auto cert = accountToCreate->tlsCertificate()) {
         [pvkContainer setHidden:!cert->requirePrivateKey()];
     } else {
         [pvkContainer setHidden:YES];