Commit 886cde17 authored by Alexandre Lision's avatar Alexandre Lision

accounts: refactor account panel

This commit simplifies the UI to manage (create, remove, backup,
restore) accounts. It includes the rewording of the previous
Import/Export feature to prevent confusion with the new multidevice
export.
Import is now 'Restore account'
Export is now 'Backup account'
Additionnal label have been added as well

Tuleap: #1152
Change-Id: I64506ce8a2d740f33eeb4719efb4bf8e9f2df746
parent 849514f5
......@@ -114,8 +114,10 @@ SET(ringclient_CONTROLLERS
src/AccSecurityVC.h
src/CertificateWC.mm
src/CertificateWC.h
src/PathPasswordWC.mm
src/PathPasswordWC.h
src/BackupAccountWC.mm
src/BackupAccountWC.h
src/RestoreAccountWC.mm
src/RestoreAccountWC.h
src/AudioPrefsVC.mm
src/AudioPrefsVC.h
src/AccountsVC.mm
......@@ -209,7 +211,8 @@ SET(ringclient_XIBS
PreferencesWindow
RingWizard
CertificateWindow
PathPasswordWindow
BackupAccountWindow
RestoreAccountWindow
ExportPasswordWindow
MigrateRingAccountsWindow
PersonLinker
......
......@@ -86,11 +86,6 @@ typedef NS_ENUM(NSInteger, TagViews) {
AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState);
}
- (IBAction)removeAccount:(id)sender {
AccountModel::instance().remove(AccountModel::instance().selectedAccount());
AccountModel::instance().save();
}
- (void)loadAccount
{
auto account = AccountModel::instance().selectedAccount();
......
......@@ -24,7 +24,6 @@
@interface AccRingVC ()
@property (assign) IBOutlet NSTextField *aliasTextField;
@property (assign) IBOutlet NSTextField *typeLabel;
@property (assign) IBOutlet NSTextField *bootstrapField;
@property (assign) IBOutlet NSTextField *hashField;
......@@ -39,7 +38,6 @@
@end
@implementation AccRingVC
@synthesize typeLabel;
@synthesize bootstrapField;
@synthesize hashField;
@synthesize aliasTextField;
......@@ -71,19 +69,12 @@ typedef NS_ENUM(NSInteger, TagViews) {
});
}
- (IBAction)removeAccount:(id)sender {
AccountModel::instance().remove(AccountModel::instance().selectedAccount());
AccountModel::instance().save();
}
- (void)loadAccount
{
auto account = AccountModel::instance().selectedAccount();
[self.aliasTextField setStringValue:account->alias().toNSString()];
[typeLabel setStringValue:@"RING"];
[allowUnknown setState:account->allowIncomingFromUnknown()];
[allowHistory setState:account->allowIncomingFromHistory()];
[allowContacts setState:account->allowIncomingFromContact()];
......
......@@ -37,10 +37,11 @@
#import "AccSecurityVC.h"
#import "AccRingVC.h"
#import "AccDevicesVC.h"
#import "PathPasswordWC.h"
#import "BackupAccountWC.h"
#import "RestoreAccountWC.h"
#import "RingWizardWC.h"
@interface AccountsVC () <PathPasswordDelegate>
@interface AccountsVC () <BackupAccountDelegate, RestoreAccountDelegate>
@property (assign) IBOutlet NSPopUpButton *protocolList;
......@@ -63,7 +64,7 @@
@property AccMediaVC* audioVC;
@property AccAdvancedVC* advancedVC;
@property AccSecurityVC* securityVC;
@property PathPasswordWC* passwordWC;
@property AbstractLoadingWC* accountModal;
@property RingWizardWC* wizard;
@end
......@@ -80,7 +81,7 @@
@synthesize accountsListView;
@synthesize accountDetailsView;
@synthesize treeController;
@synthesize passwordWC;
@synthesize accountModal;
@synthesize wizard;
NSInteger const TAG_CHECK = 100;
......@@ -167,58 +168,6 @@ NSInteger const TAG_TYPE = 400;
[self.ringDevicesTabItem setView:self.devicesVC.view];
}
- (void)createSIPAccount
{
QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
AccountModel::instance().protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
auto acc = AccountModel::instance().add([newAccName UTF8String], qIdx);
acc->setDisplayName(acc->alias());
acc->setDTMFType(DtmfType::OverSip);
AccountModel::instance().save();
}
- (void)createRingAccount
{
wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
[wizard showChooseWithCancelButton: YES];
// [wizard.window makeKeyAndOrderFront:self];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:wizard.window completionHandler:nil];
#else
[NSApp beginSheet: wizard.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[wizard showWindow:self];
}
- (IBAction)addAccount:(id)sender
{
QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
auto protocol = qvariant_cast<Account::Protocol> (AccountModel::instance().protocolModel()->data( qProtocolIdx, Qt::UserRole));
switch (protocol){
case Account::Protocol::SIP:{
[self createSIPAccount];
break;
}
case Account::Protocol::RING:{
[self createRingAccount];
break;
}
}
}
- (IBAction)protocolSelectedChanged:(id)sender
{
int index = [sender indexOfSelectedItem];
auto qIdx = AccountModel::instance().protocolModel()->index(index, 0);
AccountModel::instance().protocolModel()->selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
}
- (void) setupSIPPanels
{
// Start by removing all tabs
......@@ -244,46 +193,6 @@ NSInteger const TAG_TYPE = 400;
[configPanels insertTabViewItem:advancedTabItem atIndex:3];
}
- (IBAction)exportAccount:(id)sender
{
passwordWC = [[PathPasswordWC alloc] initWithDelegate:self actionCode:Action::ACTION_EXPORT];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:passwordWC.window completionHandler:nil];
#else
[NSApp beginSheet: passwordWC.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[passwordWC setAllowFileSelection:NO];
if(treeController.selectedNodes.count > 0) {
QStringList accounts;
for (id item : [treeController selectedNodes]) {
QModelIndex accIdx = [treeController toQIdx:item];
accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
}
[passwordWC setAccounts:accounts];
}
[passwordWC showWindow:self];
}
- (IBAction)importAccount:(id)sender
{
passwordWC = [[PathPasswordWC alloc] initWithDelegate:self actionCode:Action::ACTION_IMPORT];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:passwordWC.window completionHandler:nil];
#else
[NSApp beginSheet: passwordWC.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[passwordWC setAllowFileSelection:YES];
[passwordWC showWindow:self];
}
- (IBAction)toggleAccount:(NSButton*)sender {
NSInteger row = [accountsListView rowForView:sender];
auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
......@@ -383,28 +292,154 @@ NSInteger const TAG_TYPE = 400;
}
}
#pragma mark - Delete account
- (IBAction)removeAccount:(id)sender
{
AccountModel::instance().remove(AccountModel::instance().selectedAccount());
AccountModel::instance().save();
}
#pragma mark - NSMenuDelegate methods
#pragma mark - Advanced menu methods
- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
- (IBAction)advancedActionsClicked:(NSButton *)sender
{
auto qIdx = AccountModel::instance().protocolModel()->index(index, 0);
[item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
if (qIdx == AccountModel::instance().protocolModel()->selectionModel()->currentIndex()) {
[protocolList selectItem:item];
NSMenu* menu = [[NSMenu alloc] init];
auto backupItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Backup account", @"Contextual menu entry")
action:@selector(backupAccount:)
keyEquivalent:@""];
if(treeController.selectedNodes.count == 0) {
// Use a fake selector, to grey out the Backup entry in the menu
[backupItem setAction:@selector(disable:)];
}
return YES;
[menu insertItem:backupItem atIndex:0];
[menu insertItemWithTitle:NSLocalizedString(@"Restore account", @"Contextual menu entry")
action:@selector(restoreAccount:)
keyEquivalent:@""
atIndex:0];
[NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
}
- (void) backupAccount:(NSMenuItem*) sender
{
auto passwordWC = [[BackupAccountWC alloc] initWithDelegate:self];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:passwordWC.window completionHandler:nil];
#else
[NSApp beginSheet: passwordWC.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[passwordWC setAllowFileSelection:NO];
if(treeController.selectedNodes.count > 0) {
QStringList accounts;
for (id item : [treeController selectedNodes]) {
QModelIndex accIdx = [treeController toQIdx:item];
accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
}
[passwordWC setAccounts:accounts];
}
[passwordWC showWindow:self];
accountModal = passwordWC;
}
- (void) restoreAccount:(NSMenuItem*) sender
{
auto passwordWC = [[RestoreAccountWC alloc] initWithDelegate:self];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:passwordWC.window completionHandler:nil];
#else
[NSApp beginSheet: passwordWC.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[passwordWC setAllowFileSelection:YES];
[passwordWC showWindow:self];
accountModal = passwordWC;
}
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
- (NSEvent*) forgedEventForButton:(NSButton*) button
{
NSRect frame = [button frame];
NSPoint menuOrigin = [[button superview]
convertPoint:NSMakePoint(frame.origin.x, frame.origin.y)
toView:nil];
return [NSEvent mouseEventWithType:NSLeftMouseDown
location:menuOrigin
modifierFlags:NSLeftMouseDownMask // 0x100
timestamp:0
windowNumber:[[button window] windowNumber]
context:[[button window] graphicsContext]
eventNumber:0
clickCount:1
pressure:1];
}
#pragma mark - Account creation methods
- (IBAction)addAccountClicked:(NSButton *)sender
{
return AccountModel::instance().protocolModel()->rowCount();
NSMenu* menu = [[NSMenu alloc] init];
[menu insertItemWithTitle:NSLocalizedString(@"Create RING Account", @"Contextual menu entry")
action:@selector(createRingAccount:)
keyEquivalent:@""
atIndex:0];
[menu insertItemWithTitle:NSLocalizedString(@"Create SIP Account", @"Contextual menu entry")
action:@selector(createSIPAccount:)
keyEquivalent:@""
atIndex:0];
[NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
}
#pragma mark - PathPasswordDelegate methods
- (void)createSIPAccount:(NSMenuItem*) sender
{
auto acc = AccountModel::instance().add([NSLocalizedString(@"New SIP account", @"User label") UTF8String]);
acc->setDisplayName(acc->alias());
acc->setProtocol(Account::Protocol::SIP);
acc->setDTMFType(DtmfType::OverSip);
AccountModel::instance().save();
}
- (void)createRingAccount:(NSMenuItem*) sender
{
wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
[wizard showChooseWithCancelButton: YES];
// [wizard.window makeKeyAndOrderFront:self];
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
[self.view.window beginSheet:wizard.window completionHandler:nil];
#else
[NSApp beginSheet: wizard.window
modalForWindow: self.view.window
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
#endif
[wizard showWindow:self];
}
#pragma mark - BackupAccountDelegate methods
-(void) didCompleteExportWithPath:(NSURL*) fileUrl
{
[[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
}
#pragma mark - RestoreAccountDelegate methods
-(void) didCompleteImport
{
// Nothing to do here
}
@end
/*
* Copyright (C) 2016 Savoir-faire Linux Inc.
* Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import <Cocoa/Cocoa.h>
#import <QtCore/qdir.h>
#import "LoadingWCDelegate.h"
#import "AbstractLoadingWC.h"
@protocol BackupAccountDelegate <LoadingWCDelegate>
@optional
-(void) didCompleteExportWithPath:(NSURL*) path;
@end
@interface BackupAccountWC : AbstractLoadingWC
- (id)initWithDelegate:(id <LoadingWCDelegate>) del;
/**
* Allow the NSPathControl of this window to select files or not
*/
@property (nonatomic) BOOL allowFileSelection;
/**
* password string contained in passwordField.
* This is a KVO method to bind the text with the OK Button
* if password.length is > 0, button is enabled, otherwise disabled
*/
@property (retain) NSString* password;
/**
* Object uses to store account to exports
*/
@property (assign) QStringList accounts;
/**
* passwordConfirmation string contained in passwordConfirmationField.
*/
@property (retain) NSString* passwordConfirmation;
/**
* computed properties calculated by password string contained in
* passwordField and passwordCOnfirmation string contained
* inpasswordConfirmationField
* This is a KVO method to bind the text with the OK Button
* if password.length is > 0 AND passwordConfirmation.length > 0
* AND password isEqualsToString passwordCOnfirmationbutton is enabled,
* otherwise disabled
*/
@property (readonly) BOOL validatePasswords;
@end
/*
* Copyright (C) 2016 Savoir-faire Linux Inc.
* Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import "BackupAccountWC.h"
//LRC
#import <accountmodel.h>
//Ring
#import "views/ITProgressIndicator.h"
@interface BackupAccountWC() <NSTextFieldDelegate> {
__unsafe_unretained IBOutlet NSPathControl* path;
__unsafe_unretained IBOutlet NSSecureTextField* passwordField;
__unsafe_unretained IBOutlet NSSecureTextField* passwordConfirmationField;
__unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
}
@end
@implementation BackupAccountWC {
struct {
unsigned int didCompleteExport:1;
} delegateRespondsTo;
}
@synthesize accounts;
- (id)initWithDelegate:(id <LoadingWCDelegate>) del
{
return [self initWithDelegate:del actionCode:0];
}
- (id)initWithDelegate:(id <BackupAccountDelegate>) del actionCode:(NSInteger) code
{
return [super initWithWindowNibName:@"BackupAccountWindow" delegate:del actionCode:code];
}
- (void)windowDidLoad
{
[super windowDidLoad];
[path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
}
- (void)setDelegate:(id <BackupAccountDelegate>)aDelegate
{
if (self.delegate != aDelegate) {
[super setDelegate: aDelegate];
delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)];
}
}
- (BOOL)validatePasswords
{
BOOL result = (self.password.length != 0 && [self.password isEqualToString:self.passwordConfirmation]);
NSLog(@"ValidatesPasswords : %s", result ? "true" : "false");
return result;
}
+ (NSSet *)keyPathsForValuesAffectingValidatePasswords
{
return [NSSet setWithObjects:@"password", @"passwordConfirmation", nil];
}
- (void) setAllowFileSelection:(BOOL) b
{
_allowFileSelection = b;
[path setAllowedTypes:_allowFileSelection ? nil : [NSArray arrayWithObject:@"public.folder"]];
}
- (IBAction)completeAction:(id)sender
{
auto finalURL = [path.URL URLByAppendingPathComponent:@"accounts.ring"];
[self showLoading];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int result = AccountModel::instance().exportAccounts(accounts, finalURL.path.UTF8String, passwordField.stringValue.UTF8String);
switch (result) {
case 0:
if (delegateRespondsTo.didCompleteExport){
[((id<BackupAccountDelegate>)self.delegate) didCompleteExportWithPath:finalURL];
}
[self close];
break;
default:{
[self showError] ;
}break;
}
});
}
- (void)showLoading
{
[progressIndicator setNumberOfLines:30];
[progressIndicator setWidthOfLine:2];
[progressIndicator setLengthOfLine:5];
[progressIndicator setInnerMargin:20];
[super showLoading];
}
@end
......@@ -30,17 +30,17 @@ typedef NS_ENUM(NSUInteger, Action) {
ACTION_IMPORT = 1,
};
@protocol PathPasswordDelegate <LoadingWCDelegate>
@protocol RestoreAccountDelegate <LoadingWCDelegate>
@optional
-(void) didCompleteExportWithPath:(NSURL*) path;
-(void) didCompleteImport;
@end
@interface PathPasswordWC : AbstractLoadingWC
@interface RestoreAccountWC : AbstractLoadingWC
- (id)initWithDelegate:(id <LoadingWCDelegate>) del;
/**
* Allow the NSPathControl of this window to select files or not
......
......@@ -16,7 +16,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import "PathPasswordWC.h"
#import "RestoreAccountWC.h"
//LRC
#import <accountmodel.h>
......@@ -24,26 +24,29 @@
//Ring
#import "views/ITProgressIndicator.h"
@interface PathPasswordWC() <NSTextFieldDelegate>{
@interface RestoreAccountWC() <NSTextFieldDelegate> {
__unsafe_unretained IBOutlet NSPathControl* path;
__unsafe_unretained IBOutlet NSSecureTextField* passwordField;
__unsafe_unretained IBOutlet NSTextField* errorField;
__unsafe_unretained IBOutlet ITProgressIndicator* progressIndicator;
}
@end
@implementation PathPasswordWC {
@implementation RestoreAccountWC {
struct {
unsigned int didCompleteExport:1;
unsigned int didCompleteImport:1;
} delegateRespondsTo;
}
@synthesize accounts;
- (id)initWithDelegate:(id <PathPasswordDelegate>) del actionCode:(NSInteger) code
- (id)initWithDelegate:(id <LoadingWCDelegate>) del
{
return [super initWithWindowNibName:@"PathPasswordWindow" delegate:del actionCode:code];
return [self initWithDelegate:del actionCode:0];
}
- (id)initWithDelegate:(id <RestoreAccountDelegate>) del actionCode:(NSInteger) code
{
return [super initWithWindowNibName:@"RestoreAccountWindow" delegate:del actionCode:code];
}
- (void)windowDidLoad
......@@ -52,11 +55,10 @@
[path setURL: [NSURL fileURLWithPath:NSHomeDirectory()]];
}
- (void)setDelegate:(id <PathPasswordDelegate>)aDelegate
- (void)setDelegate:(id <RestoreAccountDelegate>)aDelegate
{
if (self.delegate != aDelegate) {
[super setDelegate: aDelegate];
delegateRespondsTo.didCompleteExport = [self.delegate respondsToSelector:@selector(didCompleteExportWithPath:)];
delegateRespondsTo.didCompleteImport = [self.delegate respondsToSelector:@selector(didCompleteWithImport)];
}