blob: da7d67a182fd1f9891a509ff817b36de831ece36 [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lisionf5fc4792015-03-17 09:15:43 -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 Lisionf5fc4792015-03-17 09:15:43 -040018 */
19
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040020#import "AccountsVC.h"
21
Alexandre Lision0c56cb62016-02-12 18:04:08 -050022// Qt
23#import <QItemSelectionModel>
Alexandre Lision91d11e52015-03-20 17:42:05 -040024#import <QSortFilterProxyModel>
Alexandre Lision0c56cb62016-02-12 18:04:08 -050025#import <QtCore/qdir.h>
26#import <QtCore/qstandardpaths.h>
27
28// LRC
Alexandre Lision91d11e52015-03-20 17:42:05 -040029#import <accountmodel.h>
30#import <protocolmodel.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040031#import <account.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040032
33#import "QNSTreeController.h"
34#import "AccGeneralVC.h"
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050035#import "AccMediaVC.h"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040036#import "AccAdvancedVC.h"
37#import "AccSecurityVC.h"
38#import "AccRingVC.h"
Alexandre Lision45f1f542016-08-25 15:16:17 -040039#import "AccDevicesVC.h"
Alexandre Lision886cde12016-10-25 17:39:49 -040040#import "BackupAccountWC.h"
41#import "RestoreAccountWC.h"
Loïc Siretfcb4ca62016-09-21 17:12:09 -040042#import "RingWizardWC.h"
Kateryna Kostiuk77e93902017-05-30 16:38:11 -040043#import "AccBannedContactsVC.h"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040044
Alexandre Lision886cde12016-10-25 17:39:49 -040045@interface AccountsVC () <BackupAccountDelegate, RestoreAccountDelegate>
Alexandre Lisionc1f96662016-03-23 17:24:19 -040046
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040047@property (assign) IBOutlet NSPopUpButton *protocolList;
48
49@property (assign) IBOutlet NSTabView *configPanels;
50@property (retain) IBOutlet NSTabViewItem *generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050051@property (retain) IBOutlet NSTabViewItem *mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040052@property (retain) IBOutlet NSTabViewItem *advancedTabItem;
53@property (retain) IBOutlet NSTabViewItem *securityTabItem;
54@property (retain) IBOutlet NSTabViewItem *ringTabItem;
Alexandre Lision45f1f542016-08-25 15:16:17 -040055@property (retain) IBOutlet NSTabViewItem *ringDevicesTabItem;
Kateryna Kostiuk77e93902017-05-30 16:38:11 -040056@property (retain) IBOutlet NSTabViewItem *bannedListTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040057
58@property QNSTreeController *treeController;
59@property (assign) IBOutlet NSOutlineView *accountsListView;
60@property (assign) IBOutlet NSTabView *accountDetailsView;
Alexandre Lisionc1f96662016-03-23 17:24:19 -040061@property (unsafe_unretained) IBOutlet NSButton* exportAccountButton;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040062
63@property AccRingVC* ringVC;
Alexandre Lision45f1f542016-08-25 15:16:17 -040064@property AccDevicesVC* devicesVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040065@property AccGeneralVC* generalVC;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050066@property AccMediaVC* audioVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040067@property AccAdvancedVC* advancedVC;
Kateryna Kostiuk77e93902017-05-30 16:38:11 -040068@property AccBannedContactsVC* bannedContactsVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040069@property AccSecurityVC* securityVC;
Alexandre Lision886cde12016-10-25 17:39:49 -040070@property AbstractLoadingWC* accountModal;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040071@property RingWizardWC* wizard;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072
73@end
74
75@implementation AccountsVC
76@synthesize protocolList;
77@synthesize configPanels;
78@synthesize generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050079@synthesize mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040080@synthesize advancedTabItem;
81@synthesize securityTabItem;
82@synthesize ringTabItem;
Alexandre Lision45f1f542016-08-25 15:16:17 -040083@synthesize ringDevicesTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040084@synthesize accountsListView;
85@synthesize accountDetailsView;
86@synthesize treeController;
Alexandre Lision886cde12016-10-25 17:39:49 -040087@synthesize accountModal;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040088@synthesize wizard;
Kateryna Kostiuk77e93902017-05-30 16:38:11 -040089@synthesize bannedListTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040090
Alexandre Lision0c56cb62016-02-12 18:04:08 -050091NSInteger const TAG_CHECK = 100;
92NSInteger const TAG_NAME = 200;
93NSInteger const TAG_STATUS = 300;
94NSInteger const TAG_TYPE = 400;
95
Kateryna Kostiukab499f42018-04-16 12:27:33 -040096QMetaObject::Connection accountChangedConnection, selectedAccountChangedConnection, accountTypeChangedConnection;
97
Alexandre Lisionc1f96662016-03-23 17:24:19 -040098
Kateryna Kostiuk67735232018-05-10 15:05:32 -040099- (void)loadView {
100 [super loadView];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400101 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400102 [treeController setAvoidsEmptySelection:NO];
103 [treeController setAlwaysUsesMultipleValuesMarker:YES];
104 [treeController setChildrenKeyPath:@"children"];
105
106 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
107 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
108 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
109
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400110 QObject::disconnect(accountChangedConnection);
111 QObject::disconnect(selectedAccountChangedConnection);
112
113 accountChangedConnection = QObject::connect(&AccountModel::instance(),
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400114 &QAbstractItemModel::dataChanged,
115 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
116 [accountsListView reloadDataForRowIndexes:
117 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
118 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
119 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400120
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400121 selectedAccountChangedConnection = QObject::connect(AccountModel::instance().selectionModel(),
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400122 &QItemSelectionModel::currentChanged,
123 [=](const QModelIndex &current, const QModelIndex &previous) {
124 [accountDetailsView setHidden:!current.isValid()];
125 if(!current.isValid()) {
126 [accountsListView deselectAll:nil];
127 return;
128 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500129
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400130 [treeController setSelectionQModelIndex:current];
131 });
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400132 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400133
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400134
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400135 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400136 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400137 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400138 QObject::disconnect(accountTypeChangedConnection);
139 accountTypeChangedConnection = QObject::connect(AccountModel::instance().protocolModel()->selectionModel(),
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400140 &QItemSelectionModel::currentChanged,
141 [=](const QModelIndex &current, const QModelIndex &previous) {
142 if (!current.isValid()) {
143 return;
144 }
145 [protocolList removeAllItems];
146 [protocolList addItemWithTitle:AccountModel::instance().protocolModel()->data(current, Qt::DisplayRole).toString().toNSString()];
147 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400148
149 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
150 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
151 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
152 [self.generalTabItem setView:self.generalVC.view];
153
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500154 self.audioVC = [[AccMediaVC alloc] initWithNibName:@"AccMedia" bundle:nil];
155 [[self.audioVC view] setFrame:[self.mediaTabItem.view frame]];
156 [[self.audioVC view] setBounds:[self.mediaTabItem.view bounds]];
157 [self.mediaTabItem setView:self.audioVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400158
159 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
160 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
161 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
162 [self.advancedTabItem setView:self.advancedVC.view];
163
164 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
165 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
166 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
167 [self.securityTabItem setView:self.securityVC.view];
168
169 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
170 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
171 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
172 [self.ringTabItem setView:self.ringVC.view];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400173
174 self.devicesVC = [[AccDevicesVC alloc] initWithNibName:@"AccDevices" bundle:nil];
175 [[self.devicesVC view] setFrame:[self.ringDevicesTabItem.view frame]];
176 [[self.devicesVC view] setBounds:[self.ringDevicesTabItem.view bounds]];
177 [self.ringDevicesTabItem setView:self.devicesVC.view];
Kateryna Kostiuk77e93902017-05-30 16:38:11 -0400178
179 self.bannedContactsVC = [[AccBannedContactsVC alloc] initWithNibName:@"AccBannedContacts" bundle:nil];
180 [[self.bannedContactsVC view] setFrame:[self.bannedListTabItem.view frame]];
181 [[self.bannedContactsVC view] setBounds:[self.bannedListTabItem.view bounds]];
182 [self.bannedListTabItem setView:self.bannedContactsVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183}
184
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400185- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400187 // Start by removing all tabs
188 for(NSTabViewItem* item in configPanels.tabViewItems) {
189 [configPanels removeTabViewItem:item];
190 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400191 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500192 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
193 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
194 [configPanels insertTabViewItem:securityTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400195}
196
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400197- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400198{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400199 // Start by removing all tabs
200 for(NSTabViewItem* item in configPanels.tabViewItems) {
201 [configPanels removeTabViewItem:item];
202 }
203
204 [configPanels insertTabViewItem:ringTabItem atIndex:0];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400205 [configPanels insertTabViewItem:ringDevicesTabItem atIndex:1];
206 [configPanels insertTabViewItem:mediaTabItem atIndex:2];
207 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
Kateryna Kostiuk77e93902017-05-30 16:38:11 -0400208 [configPanels insertTabViewItem:bannedListTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400209}
Loïc Siret31d5cc02016-09-08 14:38:24 -0400210
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500211- (IBAction)toggleAccount:(NSButton*)sender {
212 NSInteger row = [accountsListView rowForView:sender];
213 auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
214 accountToToggle->setEnabled(sender.state);
215 accountToToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400216}
217
218#pragma mark - NSOutlineViewDelegate methods
219
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400220- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
221{
222 return YES;
223}
224
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500225- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400226{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500227 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400228}
229
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500230- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400231{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500232 NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400233
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400234 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
235 if(!qIdx.isValid())
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500236 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400237
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500238 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
239 NSTextField* stateLabel = [result viewWithTag:TAG_STATUS];
240 NSButton* checkButton = [result viewWithTag:TAG_CHECK];
241 NSTextField* typeLabel = [result viewWithTag:TAG_TYPE];
Alexandre Lision0ab46082015-08-11 12:10:21 -0400242
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500243 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
244 auto humanState = account->toHumanStateName();
245
246 [nameLabel setStringValue:account->alias().toNSString()];
247 [stateLabel setStringValue:humanState.toNSString()];
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500248
249 switch (account->protocol()) {
250 case Account::Protocol::SIP:
251 [typeLabel setStringValue:@"SIP"];
252 break;
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500253 case Account::Protocol::RING:
254 [typeLabel setStringValue:@"RING"];
255 break;
256 default:
257 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400258 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500259
260 switch (account->registrationState()) {
261 case Account::RegistrationState::READY:
262 [stateLabel setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
263 break;
264 case Account::RegistrationState::TRYING:
265 [stateLabel setTextColor:[NSColor redColor]];
266 break;
267 case Account::RegistrationState::UNREGISTERED:
268 [stateLabel setTextColor:[NSColor blackColor]];
269 break;
270 case Account::RegistrationState::ERROR:
271 [stateLabel setTextColor:[NSColor redColor]];
272 break;
273 default:
274 [stateLabel setTextColor:[NSColor blackColor]];
275 break;
276 }
277
278 [checkButton setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
279
280 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400281}
282
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400283- (void)outlineViewSelectionDidChange:(NSNotification *)notification
284{
285 // ask the tree controller for the current selection
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400286 [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400287 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400288 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400289 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400290 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
291 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400292
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500293 switch (acc->protocol()) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400294 case Account::Protocol::SIP:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400295 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400296 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400297 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400298 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400299 break;
300 default:
301 break;
302 }
303
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400304 [self.accountDetailsView setHidden:NO];
305 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400306 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400307 }
308}
309
Alexandre Lision886cde12016-10-25 17:39:49 -0400310#pragma mark - Delete account
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400311
Alexandre Lision886cde12016-10-25 17:39:49 -0400312- (IBAction)removeAccount:(id)sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400313{
Alexandre Lision886cde12016-10-25 17:39:49 -0400314 AccountModel::instance().remove(AccountModel::instance().selectedAccount());
315 AccountModel::instance().save();
316}
317
318#pragma mark - Advanced menu methods
319
320- (IBAction)advancedActionsClicked:(NSButton *)sender
321{
322 NSMenu* menu = [[NSMenu alloc] init];
323
324 auto backupItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Backup account", @"Contextual menu entry")
325 action:@selector(backupAccount:)
326 keyEquivalent:@""];
327
328 if(treeController.selectedNodes.count == 0) {
329 // Use a fake selector, to grey out the Backup entry in the menu
330 [backupItem setAction:@selector(disable:)];
Alexandre Lision274c22c2016-06-30 11:57:43 -0400331 }
Alexandre Lision886cde12016-10-25 17:39:49 -0400332
333 [menu insertItem:backupItem atIndex:0];
334
335 [menu insertItemWithTitle:NSLocalizedString(@"Restore account", @"Contextual menu entry")
336 action:@selector(restoreAccount:)
337 keyEquivalent:@""
338 atIndex:0];
339
340 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400341}
342
Alexandre Lision886cde12016-10-25 17:39:49 -0400343- (void) backupAccount:(NSMenuItem*) sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400344{
Alexandre Lision886cde12016-10-25 17:39:49 -0400345 auto passwordWC = [[BackupAccountWC alloc] initWithDelegate:self];
346#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
347 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
348#else
349 [NSApp beginSheet: passwordWC.window
350 modalForWindow: self.view.window
351 modalDelegate: self
352 didEndSelector: nil
353 contextInfo: nil];
354#endif
355 [passwordWC setAllowFileSelection:NO];
356 if(treeController.selectedNodes.count > 0) {
357 QStringList accounts;
358 for (id item : [treeController selectedNodes]) {
359 QModelIndex accIdx = [treeController toQIdx:item];
360 accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
361 }
362 [passwordWC setAccounts:accounts];
363 }
364 [passwordWC showWindow:self];
365 accountModal = passwordWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400366}
367
Alexandre Lision886cde12016-10-25 17:39:49 -0400368- (void) restoreAccount:(NSMenuItem*) sender
369{
370 auto passwordWC = [[RestoreAccountWC alloc] initWithDelegate:self];
371#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
372 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
373#else
374 [NSApp beginSheet: passwordWC.window
375 modalForWindow: self.view.window
376 modalDelegate: self
377 didEndSelector: nil
378 contextInfo: nil];
379#endif
380 [passwordWC setAllowFileSelection:YES];
381 [passwordWC showWindow:self];
382 accountModal = passwordWC;
383}
384
385- (NSEvent*) forgedEventForButton:(NSButton*) button
386{
387 NSRect frame = [button frame];
388 NSPoint menuOrigin = [[button superview]
389 convertPoint:NSMakePoint(frame.origin.x, frame.origin.y)
390 toView:nil];
391 return [NSEvent mouseEventWithType:NSLeftMouseDown
392 location:menuOrigin
393 modifierFlags:NSLeftMouseDownMask // 0x100
394 timestamp:0
395 windowNumber:[[button window] windowNumber]
396 context:[[button window] graphicsContext]
397 eventNumber:0
398 clickCount:1
399 pressure:1];
400}
401
402
403#pragma mark - Account creation methods
404
405- (IBAction)addAccountClicked:(NSButton *)sender
406{
407 NSMenu* menu = [[NSMenu alloc] init];
408 [menu insertItemWithTitle:NSLocalizedString(@"Create RING Account", @"Contextual menu entry")
409 action:@selector(createRingAccount:)
410 keyEquivalent:@""
411 atIndex:0];
412 [menu insertItemWithTitle:NSLocalizedString(@"Create SIP Account", @"Contextual menu entry")
413 action:@selector(createSIPAccount:)
414 keyEquivalent:@""
415 atIndex:0];
416
417 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
418}
419
420- (void)createSIPAccount:(NSMenuItem*) sender
421{
422 auto acc = AccountModel::instance().add([NSLocalizedString(@"New SIP account", @"User label") UTF8String]);
423 acc->setDisplayName(acc->alias());
424 acc->setProtocol(Account::Protocol::SIP);
425 acc->setDTMFType(DtmfType::OverSip);
426 AccountModel::instance().save();
427}
428
429- (void)createRingAccount:(NSMenuItem*) sender
430{
431 wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
432 [wizard showChooseWithCancelButton: YES];
433 // [wizard.window makeKeyAndOrderFront:self];
434#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
435 [self.view.window beginSheet:wizard.window completionHandler:nil];
436#else
437 [NSApp beginSheet: wizard.window
438 modalForWindow: self.view.window
439 modalDelegate: self
440 didEndSelector: nil
441 contextInfo: nil];
442#endif
443 [wizard showWindow:self];
444}
445
446#pragma mark - BackupAccountDelegate methods
447
Loïc Siret31d5cc02016-09-08 14:38:24 -0400448-(void) didCompleteExportWithPath:(NSURL*) fileUrl
449{
450 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
451}
452
Alexandre Lision886cde12016-10-25 17:39:49 -0400453#pragma mark - RestoreAccountDelegate methods
454
455-(void) didCompleteImport
456{
457 // Nothing to do here
458}
459
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400460@end