blob: d0d14d2078a477be357fcef5fbe3e2478bfd0976 [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
Alexandre Lisionc1f96662016-03-23 17:24:19 -040096
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040097- (void)awakeFromNib
98{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040099 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400100 [treeController setAvoidsEmptySelection:NO];
101 [treeController setAlwaysUsesMultipleValuesMarker:YES];
102 [treeController setChildrenKeyPath:@"children"];
103
104 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
105 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
106 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
107
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400108 QObject::connect(&AccountModel::instance(),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400109 &QAbstractItemModel::dataChanged,
110 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111 [accountsListView reloadDataForRowIndexes:
112 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
113 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400114 });
115
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500116 QObject::connect(AccountModel::instance().selectionModel(),
117 &QItemSelectionModel::currentChanged,
118 [=](const QModelIndex &current, const QModelIndex &previous) {
119 [accountDetailsView setHidden:!current.isValid()];
120 if(!current.isValid()) {
121 [accountsListView deselectAll:nil];
122 return;
123 }
124
125 [treeController setSelectionQModelIndex:current];
126 });
127
128
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400129 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400130
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400131 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400132 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400133 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision274c22c2016-06-30 11:57:43 -0400134 QObject::connect(AccountModel::instance().protocolModel()->selectionModel(),
135 &QItemSelectionModel::currentChanged,
136 [=](const QModelIndex &current, const QModelIndex &previous) {
137 if (!current.isValid()) {
138 return;
139 }
140 [protocolList removeAllItems];
141 [protocolList addItemWithTitle:AccountModel::instance().protocolModel()->data(current, Qt::DisplayRole).toString().toNSString()];
142 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400143
144 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
145 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
146 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
147 [self.generalTabItem setView:self.generalVC.view];
148
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500149 self.audioVC = [[AccMediaVC alloc] initWithNibName:@"AccMedia" bundle:nil];
150 [[self.audioVC view] setFrame:[self.mediaTabItem.view frame]];
151 [[self.audioVC view] setBounds:[self.mediaTabItem.view bounds]];
152 [self.mediaTabItem setView:self.audioVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400153
154 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
155 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
156 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
157 [self.advancedTabItem setView:self.advancedVC.view];
158
159 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
160 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
161 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
162 [self.securityTabItem setView:self.securityVC.view];
163
164 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
165 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
166 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
167 [self.ringTabItem setView:self.ringVC.view];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400168
169 self.devicesVC = [[AccDevicesVC alloc] initWithNibName:@"AccDevices" bundle:nil];
170 [[self.devicesVC view] setFrame:[self.ringDevicesTabItem.view frame]];
171 [[self.devicesVC view] setBounds:[self.ringDevicesTabItem.view bounds]];
172 [self.ringDevicesTabItem setView:self.devicesVC.view];
Kateryna Kostiuk77e93902017-05-30 16:38:11 -0400173
174 self.bannedContactsVC = [[AccBannedContactsVC alloc] initWithNibName:@"AccBannedContacts" bundle:nil];
175 [[self.bannedContactsVC view] setFrame:[self.bannedListTabItem.view frame]];
176 [[self.bannedContactsVC view] setBounds:[self.bannedListTabItem.view bounds]];
177 [self.bannedListTabItem setView:self.bannedContactsVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400178}
179
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400180- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400181{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400182 // Start by removing all tabs
183 for(NSTabViewItem* item in configPanels.tabViewItems) {
184 [configPanels removeTabViewItem:item];
185 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500187 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
188 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
189 [configPanels insertTabViewItem:securityTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400190}
191
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400192- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400193{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400194 // Start by removing all tabs
195 for(NSTabViewItem* item in configPanels.tabViewItems) {
196 [configPanels removeTabViewItem:item];
197 }
198
199 [configPanels insertTabViewItem:ringTabItem atIndex:0];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400200 [configPanels insertTabViewItem:ringDevicesTabItem atIndex:1];
201 [configPanels insertTabViewItem:mediaTabItem atIndex:2];
202 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
Kateryna Kostiuk77e93902017-05-30 16:38:11 -0400203 [configPanels insertTabViewItem:bannedListTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400204}
Loïc Siret31d5cc02016-09-08 14:38:24 -0400205
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500206- (IBAction)toggleAccount:(NSButton*)sender {
207 NSInteger row = [accountsListView rowForView:sender];
208 auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
209 accountToToggle->setEnabled(sender.state);
210 accountToToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400211}
212
213#pragma mark - NSOutlineViewDelegate methods
214
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400215- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
216{
217 return YES;
218}
219
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500220- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400221{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500222 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400223}
224
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500225- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400226{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500227 NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400228
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400229 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
230 if(!qIdx.isValid())
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500231 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400232
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500233 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
234 NSTextField* stateLabel = [result viewWithTag:TAG_STATUS];
235 NSButton* checkButton = [result viewWithTag:TAG_CHECK];
236 NSTextField* typeLabel = [result viewWithTag:TAG_TYPE];
Alexandre Lision0ab46082015-08-11 12:10:21 -0400237
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500238 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
239 auto humanState = account->toHumanStateName();
240
241 [nameLabel setStringValue:account->alias().toNSString()];
242 [stateLabel setStringValue:humanState.toNSString()];
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500243
244 switch (account->protocol()) {
245 case Account::Protocol::SIP:
246 [typeLabel setStringValue:@"SIP"];
247 break;
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500248 case Account::Protocol::RING:
249 [typeLabel setStringValue:@"RING"];
250 break;
251 default:
252 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400253 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500254
255 switch (account->registrationState()) {
256 case Account::RegistrationState::READY:
257 [stateLabel setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
258 break;
259 case Account::RegistrationState::TRYING:
260 [stateLabel setTextColor:[NSColor redColor]];
261 break;
262 case Account::RegistrationState::UNREGISTERED:
263 [stateLabel setTextColor:[NSColor blackColor]];
264 break;
265 case Account::RegistrationState::ERROR:
266 [stateLabel setTextColor:[NSColor redColor]];
267 break;
268 default:
269 [stateLabel setTextColor:[NSColor blackColor]];
270 break;
271 }
272
273 [checkButton setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
274
275 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400276}
277
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400278- (void)outlineViewSelectionDidChange:(NSNotification *)notification
279{
280 // ask the tree controller for the current selection
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400281 [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400282 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400283 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400284 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400285 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
286 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400287
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500288 switch (acc->protocol()) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400289 case Account::Protocol::SIP:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400290 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400291 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400292 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400293 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400294 break;
295 default:
296 break;
297 }
298
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400299 [self.accountDetailsView setHidden:NO];
300 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400301 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400302 }
303}
304
Alexandre Lision886cde12016-10-25 17:39:49 -0400305#pragma mark - Delete account
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400306
Alexandre Lision886cde12016-10-25 17:39:49 -0400307- (IBAction)removeAccount:(id)sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400308{
Alexandre Lision886cde12016-10-25 17:39:49 -0400309 AccountModel::instance().remove(AccountModel::instance().selectedAccount());
310 AccountModel::instance().save();
311}
312
313#pragma mark - Advanced menu methods
314
315- (IBAction)advancedActionsClicked:(NSButton *)sender
316{
317 NSMenu* menu = [[NSMenu alloc] init];
318
319 auto backupItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Backup account", @"Contextual menu entry")
320 action:@selector(backupAccount:)
321 keyEquivalent:@""];
322
323 if(treeController.selectedNodes.count == 0) {
324 // Use a fake selector, to grey out the Backup entry in the menu
325 [backupItem setAction:@selector(disable:)];
Alexandre Lision274c22c2016-06-30 11:57:43 -0400326 }
Alexandre Lision886cde12016-10-25 17:39:49 -0400327
328 [menu insertItem:backupItem atIndex:0];
329
330 [menu insertItemWithTitle:NSLocalizedString(@"Restore account", @"Contextual menu entry")
331 action:@selector(restoreAccount:)
332 keyEquivalent:@""
333 atIndex:0];
334
335 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400336}
337
Alexandre Lision886cde12016-10-25 17:39:49 -0400338- (void) backupAccount:(NSMenuItem*) sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400339{
Alexandre Lision886cde12016-10-25 17:39:49 -0400340 auto passwordWC = [[BackupAccountWC alloc] initWithDelegate:self];
341#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
342 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
343#else
344 [NSApp beginSheet: passwordWC.window
345 modalForWindow: self.view.window
346 modalDelegate: self
347 didEndSelector: nil
348 contextInfo: nil];
349#endif
350 [passwordWC setAllowFileSelection:NO];
351 if(treeController.selectedNodes.count > 0) {
352 QStringList accounts;
353 for (id item : [treeController selectedNodes]) {
354 QModelIndex accIdx = [treeController toQIdx:item];
355 accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
356 }
357 [passwordWC setAccounts:accounts];
358 }
359 [passwordWC showWindow:self];
360 accountModal = passwordWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400361}
362
Alexandre Lision886cde12016-10-25 17:39:49 -0400363- (void) restoreAccount:(NSMenuItem*) sender
364{
365 auto passwordWC = [[RestoreAccountWC alloc] initWithDelegate:self];
366#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
367 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
368#else
369 [NSApp beginSheet: passwordWC.window
370 modalForWindow: self.view.window
371 modalDelegate: self
372 didEndSelector: nil
373 contextInfo: nil];
374#endif
375 [passwordWC setAllowFileSelection:YES];
376 [passwordWC showWindow:self];
377 accountModal = passwordWC;
378}
379
380- (NSEvent*) forgedEventForButton:(NSButton*) button
381{
382 NSRect frame = [button frame];
383 NSPoint menuOrigin = [[button superview]
384 convertPoint:NSMakePoint(frame.origin.x, frame.origin.y)
385 toView:nil];
386 return [NSEvent mouseEventWithType:NSLeftMouseDown
387 location:menuOrigin
388 modifierFlags:NSLeftMouseDownMask // 0x100
389 timestamp:0
390 windowNumber:[[button window] windowNumber]
391 context:[[button window] graphicsContext]
392 eventNumber:0
393 clickCount:1
394 pressure:1];
395}
396
397
398#pragma mark - Account creation methods
399
400- (IBAction)addAccountClicked:(NSButton *)sender
401{
402 NSMenu* menu = [[NSMenu alloc] init];
403 [menu insertItemWithTitle:NSLocalizedString(@"Create RING Account", @"Contextual menu entry")
404 action:@selector(createRingAccount:)
405 keyEquivalent:@""
406 atIndex:0];
407 [menu insertItemWithTitle:NSLocalizedString(@"Create SIP Account", @"Contextual menu entry")
408 action:@selector(createSIPAccount:)
409 keyEquivalent:@""
410 atIndex:0];
411
412 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
413}
414
415- (void)createSIPAccount:(NSMenuItem*) sender
416{
417 auto acc = AccountModel::instance().add([NSLocalizedString(@"New SIP account", @"User label") UTF8String]);
418 acc->setDisplayName(acc->alias());
419 acc->setProtocol(Account::Protocol::SIP);
420 acc->setDTMFType(DtmfType::OverSip);
421 AccountModel::instance().save();
422}
423
424- (void)createRingAccount:(NSMenuItem*) sender
425{
426 wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
427 [wizard showChooseWithCancelButton: YES];
428 // [wizard.window makeKeyAndOrderFront:self];
429#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
430 [self.view.window beginSheet:wizard.window completionHandler:nil];
431#else
432 [NSApp beginSheet: wizard.window
433 modalForWindow: self.view.window
434 modalDelegate: self
435 didEndSelector: nil
436 contextInfo: nil];
437#endif
438 [wizard showWindow:self];
439}
440
441#pragma mark - BackupAccountDelegate methods
442
Loïc Siret31d5cc02016-09-08 14:38:24 -0400443-(void) didCompleteExportWithPath:(NSURL*) fileUrl
444{
445 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
446}
447
Alexandre Lision886cde12016-10-25 17:39:49 -0400448#pragma mark - RestoreAccountDelegate methods
449
450-(void) didCompleteImport
451{
452 // Nothing to do here
453}
454
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400455@end