Commit b3f7ed66 authored by Alexandre Lision's avatar Alexandre Lision

wizard: allow certificate selection

When first opening Ring, let the user choose its own certificate/pvk file if
he wants to for its ring account.

Issue: #79099
Change-Id: Ic4b23c3b5bc8553eb841663aa15757cb884cdf65
parent 24805d5c
......@@ -115,8 +115,11 @@
- (void) showMainWindow
{
if(self.ringWindowController == nil)
if(self.ringWindowController == nil) {
self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"];
}
self.wizard = nil;
[self.ringWindowController.window makeKeyAndOrderFront:self];
}
......
/*
* Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
* Copyright (C) 2015 Savoir-faire Linux Inc.
* Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -30,6 +30,6 @@
#import <Cocoa/Cocoa.h>
@interface RingWizardWC : NSWindowController <NSWindowDelegate>
@interface RingWizardWC : NSWindowController <NSWindowDelegate, NSPathControlDelegate, NSOpenSavePanelDelegate>
@end
/*
* Copyright (C) 2015 Savoir-Faire Linux Inc.
* Copyright (C) 2015 Savoir-faire Linux Inc.
* Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
......@@ -29,48 +29,79 @@
*/
#import "RingWizardWC.h"
//Qt
#import <QUrl>
//LRC
#import <accountmodel.h>
#import <protocolmodel.h>
#import <QItemSelectionModel>
#import <account.h>
#import <certificate.h>
#import "AppDelegate.h"
#define PVK_PASSWORD_TAG 0
#define NICKNAME_TAG 1
@interface RingWizardWC () {
__unsafe_unretained IBOutlet NSButton *goToAppButton;
__unsafe_unretained IBOutlet NSTextField *nickname;
__unsafe_unretained IBOutlet NSProgressIndicator *progressBar;
__unsafe_unretained IBOutlet NSTextField *indicationLabel;
__unsafe_unretained IBOutlet NSButton *createButton;
__unsafe_unretained IBOutlet NSButton *showCustomCertsButton;
IBOutlet NSView *securityContainer;
__unsafe_unretained IBOutlet NSSecureTextField *passwordField;
__unsafe_unretained IBOutlet NSView *pvkContainer;
__unsafe_unretained IBOutlet NSPathControl *certificatePathControl;
__unsafe_unretained IBOutlet NSPathControl *caListPathControl;
__unsafe_unretained IBOutlet NSPathControl *pvkPathControl;
BOOL isExpanded;
}
@interface RingWizardWC ()
@property (unsafe_unretained) IBOutlet NSButton *goToAppButton;
@property (unsafe_unretained) IBOutlet NSTextField *nickname;
@property (unsafe_unretained) IBOutlet NSProgressIndicator *progressBar;
@property (unsafe_unretained) IBOutlet NSTextField *indicationLabel;
@property (unsafe_unretained) IBOutlet NSButton *createButton;
@property Account* accountToCreate;
@end
@implementation RingWizardWC
@synthesize goToAppButton;
@synthesize nickname;
@synthesize progressBar;
@synthesize indicationLabel;
@synthesize createButton;
@synthesize accountToCreate;
- (void)windowDidLoad {
[super windowDidLoad];
[passwordField setTag:PVK_PASSWORD_TAG];
[nickname setTag:NICKNAME_TAG];
isExpanded = false;
[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSStatusWindowLevel];
[self.window makeMainWindow];
[self checkForRingAccount];
if(![self checkForRingAccount]) {
accountToCreate = AccountModel::instance()->add("", Account::Protocol::RING);
} else {
[indicationLabel setStringValue:@"Ring is already ready to work"];
auto accList = AccountModel::instance()->getAccountsByProtocol(Account::Protocol::RING);
[self displayHash:accList[0]->username().toNSString()];
[showCustomCertsButton setHidden:YES];
}
[caListPathControl setDelegate:self];
[certificatePathControl setDelegate:self];
[pvkPathControl setDelegate:self];
}
- (void) checkForRingAccount
- (BOOL) checkForRingAccount
{
for (int i = 0 ; i < AccountModel::instance()->rowCount() ; ++i) {
QModelIndex idx = AccountModel::instance()->index(i);
Account* acc = AccountModel::instance()->getAccountByModelIndex(idx);
if(acc->protocol() == Account::Protocol::RING) {
[indicationLabel setStringValue:@"Ring is already ready to work"];
[self displayHash:acc->username().toNSString()];
return YES;
}
}
return false;
}
- (void) displayHash:(NSString* ) hash
......@@ -85,13 +116,12 @@
NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
[createButton setTitle:@"Share by mail"];
//[createButton setImage:emailSharingService.image];
[createButton setAlternateImage:emailSharingService.alternateImage];
[createButton setAction:@selector(shareByEmail)];
}
- (IBAction)createRingAccount:(id)sender {
- (IBAction)createRingAccount:(id)sender
{
[nickname setHidden:YES];
[progressBar setHidden:NO];
[createButton setEnabled:NO];
......@@ -100,6 +130,11 @@
QModelIndex qIdx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
[self setCallback];
if (isExpanded) {
// retract panel
[self chooseOwnCertificates:nil];
[showCustomCertsButton setHidden:YES];
}
[self performSelector:@selector(saveAccount) withObject:nil afterDelay:1];
[self registerAutoStartup];
......@@ -119,13 +154,8 @@
- (void) saveAccount
{
NSString* newAccName = @"My Ring";
Account* newAcc = AccountModel::instance()->add([newAccName UTF8String], Account::Protocol::RING);
newAcc->setAlias([[nickname stringValue] UTF8String]);
newAcc->setDisplayName([[nickname stringValue] UTF8String]);
newAcc->setUpnpEnabled(YES); // Always active upnp
newAcc << Account::EditAction::SAVE;
accountToCreate->setUpnpEnabled(YES); // Always active upnp
accountToCreate << Account::EditAction::SAVE;
}
- (void) setCallback
......@@ -141,7 +171,31 @@
});
}
- (IBAction)goToApp:(id)sender {
- (IBAction)chooseOwnCertificates:(NSButton*)sender
{
if (isExpanded) {
[securityContainer removeFromSuperview];
NSRect frame = [self.window frame];
frame.size = CGSizeMake(securityContainer.frame.size.width, frame.size.height - securityContainer.frame.size.height);
frame.origin.y = frame.origin.y + securityContainer.frame.size.height;
[self.window setFrame:frame display:YES animate:YES];
isExpanded = false;
[sender setImage:[NSImage imageNamed:@"NSAddTemplate"]];
} else {
NSRect frame = [self.window frame];
frame.size = CGSizeMake(securityContainer.frame.size.width, frame.size.height + securityContainer.frame.size.height);
frame.origin.y = frame.origin.y - securityContainer.frame.size.height;
[self.window setFrame:frame display:YES animate:YES];
[securityContainer setFrameOrigin:CGPointMake(0, 50)];
[self.window.contentView addSubview:securityContainer];
isExpanded = true;
[sender setImage:[NSImage imageNamed:@"NSRemoveTemplate"]];
}
}
- (IBAction)goToApp:(id)sender
{
[self.window close];
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
[appDelegate showMainWindow];
......@@ -149,52 +203,133 @@
- (void) shareByEmail
{
/*
Create the array of items to share.
Start with just the content of the text view. If there's an image, add that too.
*/
NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:[nickname stringValue], nil];
NSSharingService* emailSharingService = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
/*
Perform the service using the array of items.
*/
[emailSharingService performWithItems:shareItems];
}
#pragma mark - NSPathControl delegate methods
# pragma NSWindowDelegate methods
- (IBAction)caListPathControlSingleClick:(id)sender
{
NSURL* fileURL;
if ([sender isKindOfClass:[NSMenuItem class]]) {
fileURL = nil;
} else {
fileURL = [[sender clickedPathComponentCell] URL];
}
[self->caListPathControl setURL:fileURL];
accountToCreate->setTlsCaListCertificate([[fileURL path] UTF8String]);
- (BOOL)windowShouldClose:(id)sender
}
- (IBAction)certificatePathControlSingleClick:(id)sender
{
NSLog(@"windowShouldClose");
return YES;
NSURL* fileURL;
if ([sender isKindOfClass:[NSMenuItem class]]) {
fileURL = nil;
} else {
fileURL = [[sender clickedPathComponentCell] URL];
}
[self->certificatePathControl setURL:fileURL];
accountToCreate->setTlsCertificate([[fileURL path] UTF8String]);
auto cert = accountToCreate->tlsCertificate();
if (cert) {
[pvkContainer setHidden:!cert->requirePrivateKey()];
} else {
[pvkContainer setHidden:YES];
}
}
- (void)windowDidBecomeKey:(NSNotification *)notification
- (IBAction)pvkFilePathControlSingleClick:(id)sender
{
NSLog(@"windowDidBecomeKey");
NSURL* fileURL;
if ([sender isKindOfClass:[NSMenuItem class]]) {
fileURL = nil;
} else {
fileURL = [[sender clickedPathComponentCell] URL];
}
[self->pvkPathControl setURL:fileURL];
accountToCreate->setTlsPrivateKey([[fileURL path] UTF8String]);
if(accountToCreate->tlsCertificate()->requirePrivateKeyPassword()) {
[passwordField setHidden:NO];
} else {
[passwordField setHidden:YES];
}
}
- (void)windowDidResignKey:(NSNotification *)notification
/*
Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave.
*/
- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
{
NSLog(@"windowDidResignKey");
NSLog(@"willDisplayOpenPanel");
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setResolvesAliases:YES];
if(pathControl == caListPathControl) {
[openPanel setTitle:NSLocalizedString(@"Choose a CA list", @"Open panel title")];
} else if (pathControl == certificatePathControl) {
[openPanel setTitle:NSLocalizedString(@"Choose a certificate", @"Open panel title")];
} else {
[openPanel setTitle:NSLocalizedString(@"Choose a private key file", @"Open panel title")];
}
[openPanel setPrompt:NSLocalizedString(@"Choose", @"Open panel prompt for 'Choose a file'")];
[openPanel setDelegate:self];
}
- (void)windowDidBecomeMain:(NSNotification *)notification
- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu
{
NSLog(@"windowDidBecomeMain");
NSMenuItem *item;
if(pathControl == caListPathControl) {
item = [menu addItemWithTitle:@"Remove value" action:@selector(caListPathControlSingleClick:) keyEquivalent:@""];
} else if (pathControl == certificatePathControl) {
item = [menu addItemWithTitle:@"Remove value" action:@selector(certificatePathControlSingleClick:) keyEquivalent:@""];
} else {
item = [menu addItemWithTitle:@"Remove value" action:@selector(pvkFilePathControlSingleClick:) keyEquivalent:@""];
}
[item setTarget:self]; // or whatever target you want
}
- (void)windowDidResignMain:(NSNotification *)notification
#pragma mark - NSOpenSavePanelDelegate delegate methods
- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError
{
NSLog(@"windowDidResignMain");
[self.window close];
return YES;
}
#pragma mark - NSTextFieldDelegate methods
-(void)controlTextDidChange:(NSNotification *)notif
{
NSTextField *textField = [notif object];
if (textField.tag == PVK_PASSWORD_TAG) {
accountToCreate->setTlsPassword([textField.stringValue UTF8String]);
return;
}
// else it is NICKNAME_TAG field
if ([textField.stringValue isEqualToString:@""]) {
[createButton setEnabled:NO];
} else {
[createButton setEnabled:YES];
}
accountToCreate->setAlias([textField.stringValue UTF8String]);
accountToCreate->setDisplayName([textField.stringValue UTF8String]);
}
# pragma NSWindowDelegate methods
- (void)windowWillClose:(NSNotification *)notification
{
//NSLog(@"windowWillClose");
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
[appDelegate showMainWindow];
}
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment