blob: 93fddec1d66a0159ec13cc4c97a6e2b264fbec21 [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"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040043
Alexandre Lision886cde12016-10-25 17:39:49 -040044@interface AccountsVC () <BackupAccountDelegate, RestoreAccountDelegate>
Alexandre Lisionc1f96662016-03-23 17:24:19 -040045
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040046@property (assign) IBOutlet NSPopUpButton *protocolList;
47
48@property (assign) IBOutlet NSTabView *configPanels;
49@property (retain) IBOutlet NSTabViewItem *generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050050@property (retain) IBOutlet NSTabViewItem *mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040051@property (retain) IBOutlet NSTabViewItem *advancedTabItem;
52@property (retain) IBOutlet NSTabViewItem *securityTabItem;
53@property (retain) IBOutlet NSTabViewItem *ringTabItem;
Alexandre Lision45f1f542016-08-25 15:16:17 -040054@property (retain) IBOutlet NSTabViewItem *ringDevicesTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040055
56@property QNSTreeController *treeController;
57@property (assign) IBOutlet NSOutlineView *accountsListView;
58@property (assign) IBOutlet NSTabView *accountDetailsView;
Alexandre Lisionc1f96662016-03-23 17:24:19 -040059@property (unsafe_unretained) IBOutlet NSButton* exportAccountButton;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040060
61@property AccRingVC* ringVC;
Alexandre Lision45f1f542016-08-25 15:16:17 -040062@property AccDevicesVC* devicesVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040063@property AccGeneralVC* generalVC;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050064@property AccMediaVC* audioVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040065@property AccAdvancedVC* advancedVC;
66@property AccSecurityVC* securityVC;
Alexandre Lision886cde12016-10-25 17:39:49 -040067@property AbstractLoadingWC* accountModal;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040068@property RingWizardWC* wizard;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040069
70@end
71
72@implementation AccountsVC
73@synthesize protocolList;
74@synthesize configPanels;
75@synthesize generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050076@synthesize mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040077@synthesize advancedTabItem;
78@synthesize securityTabItem;
79@synthesize ringTabItem;
Alexandre Lision45f1f542016-08-25 15:16:17 -040080@synthesize ringDevicesTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040081@synthesize accountsListView;
82@synthesize accountDetailsView;
83@synthesize treeController;
Alexandre Lision886cde12016-10-25 17:39:49 -040084@synthesize accountModal;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040085@synthesize wizard;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086
Alexandre Lision0c56cb62016-02-12 18:04:08 -050087NSInteger const TAG_CHECK = 100;
88NSInteger const TAG_NAME = 200;
89NSInteger const TAG_STATUS = 300;
90NSInteger const TAG_TYPE = 400;
91
Alexandre Lisionc1f96662016-03-23 17:24:19 -040092
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040093- (void)awakeFromNib
94{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040095 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040096 [treeController setAvoidsEmptySelection:NO];
97 [treeController setAlwaysUsesMultipleValuesMarker:YES];
98 [treeController setChildrenKeyPath:@"children"];
99
100 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
101 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
102 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
103
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400104 QObject::connect(&AccountModel::instance(),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400105 &QAbstractItemModel::dataChanged,
106 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107 [accountsListView reloadDataForRowIndexes:
108 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
109 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400110 });
111
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500112 QObject::connect(AccountModel::instance().selectionModel(),
113 &QItemSelectionModel::currentChanged,
114 [=](const QModelIndex &current, const QModelIndex &previous) {
115 [accountDetailsView setHidden:!current.isValid()];
116 if(!current.isValid()) {
117 [accountsListView deselectAll:nil];
118 return;
119 }
120
121 [treeController setSelectionQModelIndex:current];
122 });
123
124
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400125 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400126
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400127 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400128 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400129 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision274c22c2016-06-30 11:57:43 -0400130 QObject::connect(AccountModel::instance().protocolModel()->selectionModel(),
131 &QItemSelectionModel::currentChanged,
132 [=](const QModelIndex &current, const QModelIndex &previous) {
133 if (!current.isValid()) {
134 return;
135 }
136 [protocolList removeAllItems];
137 [protocolList addItemWithTitle:AccountModel::instance().protocolModel()->data(current, Qt::DisplayRole).toString().toNSString()];
138 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400139
140 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
141 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
142 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
143 [self.generalTabItem setView:self.generalVC.view];
144
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500145 self.audioVC = [[AccMediaVC alloc] initWithNibName:@"AccMedia" bundle:nil];
146 [[self.audioVC view] setFrame:[self.mediaTabItem.view frame]];
147 [[self.audioVC view] setBounds:[self.mediaTabItem.view bounds]];
148 [self.mediaTabItem setView:self.audioVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400149
150 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
151 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
152 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
153 [self.advancedTabItem setView:self.advancedVC.view];
154
155 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
156 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
157 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
158 [self.securityTabItem setView:self.securityVC.view];
159
160 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
161 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
162 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
163 [self.ringTabItem setView:self.ringVC.view];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400164
165 self.devicesVC = [[AccDevicesVC alloc] initWithNibName:@"AccDevices" bundle:nil];
166 [[self.devicesVC view] setFrame:[self.ringDevicesTabItem.view frame]];
167 [[self.devicesVC view] setBounds:[self.ringDevicesTabItem.view bounds]];
168 [self.ringDevicesTabItem setView:self.devicesVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400169}
170
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400171- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400172{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400173 // Start by removing all tabs
174 for(NSTabViewItem* item in configPanels.tabViewItems) {
175 [configPanels removeTabViewItem:item];
176 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400177 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500178 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
179 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
180 [configPanels insertTabViewItem:securityTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400181}
182
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400183- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400184{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400185 // Start by removing all tabs
186 for(NSTabViewItem* item in configPanels.tabViewItems) {
187 [configPanels removeTabViewItem:item];
188 }
189
190 [configPanels insertTabViewItem:ringTabItem atIndex:0];
Alexandre Lision45f1f542016-08-25 15:16:17 -0400191 [configPanels insertTabViewItem:ringDevicesTabItem atIndex:1];
192 [configPanels insertTabViewItem:mediaTabItem atIndex:2];
193 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400194}
Loïc Siret31d5cc02016-09-08 14:38:24 -0400195
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500196- (IBAction)toggleAccount:(NSButton*)sender {
197 NSInteger row = [accountsListView rowForView:sender];
198 auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
199 accountToToggle->setEnabled(sender.state);
200 accountToToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400201}
202
203#pragma mark - NSOutlineViewDelegate methods
204
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400205- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
206{
207 return YES;
208}
209
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500210- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400211{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500212 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400213}
214
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500215- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400216{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500217 NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400218
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400219 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
220 if(!qIdx.isValid())
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500221 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400222
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500223 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
224 NSTextField* stateLabel = [result viewWithTag:TAG_STATUS];
225 NSButton* checkButton = [result viewWithTag:TAG_CHECK];
226 NSTextField* typeLabel = [result viewWithTag:TAG_TYPE];
Alexandre Lision0ab46082015-08-11 12:10:21 -0400227
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500228 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
229 auto humanState = account->toHumanStateName();
230
231 [nameLabel setStringValue:account->alias().toNSString()];
232 [stateLabel setStringValue:humanState.toNSString()];
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500233
234 switch (account->protocol()) {
235 case Account::Protocol::SIP:
236 [typeLabel setStringValue:@"SIP"];
237 break;
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500238 case Account::Protocol::RING:
239 [typeLabel setStringValue:@"RING"];
240 break;
241 default:
242 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400243 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500244
245 switch (account->registrationState()) {
246 case Account::RegistrationState::READY:
247 [stateLabel setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
248 break;
249 case Account::RegistrationState::TRYING:
250 [stateLabel setTextColor:[NSColor redColor]];
251 break;
252 case Account::RegistrationState::UNREGISTERED:
253 [stateLabel setTextColor:[NSColor blackColor]];
254 break;
255 case Account::RegistrationState::ERROR:
256 [stateLabel setTextColor:[NSColor redColor]];
257 break;
258 default:
259 [stateLabel setTextColor:[NSColor blackColor]];
260 break;
261 }
262
263 [checkButton setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
264
265 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400266}
267
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400268- (void)outlineViewSelectionDidChange:(NSNotification *)notification
269{
270 // ask the tree controller for the current selection
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400271 [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400272 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400273 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400274 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400275 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
276 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400277
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500278 switch (acc->protocol()) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400279 case Account::Protocol::SIP:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400280 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400281 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400282 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400283 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400284 break;
285 default:
286 break;
287 }
288
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400289 [self.accountDetailsView setHidden:NO];
290 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400291 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400292 }
293}
294
Alexandre Lision886cde12016-10-25 17:39:49 -0400295#pragma mark - Delete account
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400296
Alexandre Lision886cde12016-10-25 17:39:49 -0400297- (IBAction)removeAccount:(id)sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400298{
Alexandre Lision886cde12016-10-25 17:39:49 -0400299 AccountModel::instance().remove(AccountModel::instance().selectedAccount());
300 AccountModel::instance().save();
301}
302
303#pragma mark - Advanced menu methods
304
305- (IBAction)advancedActionsClicked:(NSButton *)sender
306{
307 NSMenu* menu = [[NSMenu alloc] init];
308
309 auto backupItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Backup account", @"Contextual menu entry")
310 action:@selector(backupAccount:)
311 keyEquivalent:@""];
312
313 if(treeController.selectedNodes.count == 0) {
314 // Use a fake selector, to grey out the Backup entry in the menu
315 [backupItem setAction:@selector(disable:)];
Alexandre Lision274c22c2016-06-30 11:57:43 -0400316 }
Alexandre Lision886cde12016-10-25 17:39:49 -0400317
318 [menu insertItem:backupItem atIndex:0];
319
320 [menu insertItemWithTitle:NSLocalizedString(@"Restore account", @"Contextual menu entry")
321 action:@selector(restoreAccount:)
322 keyEquivalent:@""
323 atIndex:0];
324
325 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400326}
327
Alexandre Lision886cde12016-10-25 17:39:49 -0400328- (void) backupAccount:(NSMenuItem*) sender
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400329{
Alexandre Lision886cde12016-10-25 17:39:49 -0400330 auto passwordWC = [[BackupAccountWC alloc] initWithDelegate:self];
331#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
332 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
333#else
334 [NSApp beginSheet: passwordWC.window
335 modalForWindow: self.view.window
336 modalDelegate: self
337 didEndSelector: nil
338 contextInfo: nil];
339#endif
340 [passwordWC setAllowFileSelection:NO];
341 if(treeController.selectedNodes.count > 0) {
342 QStringList accounts;
343 for (id item : [treeController selectedNodes]) {
344 QModelIndex accIdx = [treeController toQIdx:item];
345 accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
346 }
347 [passwordWC setAccounts:accounts];
348 }
349 [passwordWC showWindow:self];
350 accountModal = passwordWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400351}
352
Alexandre Lision886cde12016-10-25 17:39:49 -0400353- (void) restoreAccount:(NSMenuItem*) sender
354{
355 auto passwordWC = [[RestoreAccountWC alloc] initWithDelegate:self];
356#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
357 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
358#else
359 [NSApp beginSheet: passwordWC.window
360 modalForWindow: self.view.window
361 modalDelegate: self
362 didEndSelector: nil
363 contextInfo: nil];
364#endif
365 [passwordWC setAllowFileSelection:YES];
366 [passwordWC showWindow:self];
367 accountModal = passwordWC;
368}
369
370- (NSEvent*) forgedEventForButton:(NSButton*) button
371{
372 NSRect frame = [button frame];
373 NSPoint menuOrigin = [[button superview]
374 convertPoint:NSMakePoint(frame.origin.x, frame.origin.y)
375 toView:nil];
376 return [NSEvent mouseEventWithType:NSLeftMouseDown
377 location:menuOrigin
378 modifierFlags:NSLeftMouseDownMask // 0x100
379 timestamp:0
380 windowNumber:[[button window] windowNumber]
381 context:[[button window] graphicsContext]
382 eventNumber:0
383 clickCount:1
384 pressure:1];
385}
386
387
388#pragma mark - Account creation methods
389
390- (IBAction)addAccountClicked:(NSButton *)sender
391{
392 NSMenu* menu = [[NSMenu alloc] init];
393 [menu insertItemWithTitle:NSLocalizedString(@"Create RING Account", @"Contextual menu entry")
394 action:@selector(createRingAccount:)
395 keyEquivalent:@""
396 atIndex:0];
397 [menu insertItemWithTitle:NSLocalizedString(@"Create SIP Account", @"Contextual menu entry")
398 action:@selector(createSIPAccount:)
399 keyEquivalent:@""
400 atIndex:0];
401
402 [NSMenu popUpContextMenu:menu withEvent:[self forgedEventForButton:sender] forView:(NSButton *)sender];
403}
404
405- (void)createSIPAccount:(NSMenuItem*) sender
406{
407 auto acc = AccountModel::instance().add([NSLocalizedString(@"New SIP account", @"User label") UTF8String]);
408 acc->setDisplayName(acc->alias());
409 acc->setProtocol(Account::Protocol::SIP);
410 acc->setDTMFType(DtmfType::OverSip);
411 AccountModel::instance().save();
412}
413
414- (void)createRingAccount:(NSMenuItem*) sender
415{
416 wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
417 [wizard showChooseWithCancelButton: YES];
418 // [wizard.window makeKeyAndOrderFront:self];
419#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
420 [self.view.window beginSheet:wizard.window completionHandler:nil];
421#else
422 [NSApp beginSheet: wizard.window
423 modalForWindow: self.view.window
424 modalDelegate: self
425 didEndSelector: nil
426 contextInfo: nil];
427#endif
428 [wizard showWindow:self];
429}
430
431#pragma mark - BackupAccountDelegate methods
432
Loïc Siret31d5cc02016-09-08 14:38:24 -0400433-(void) didCompleteExportWithPath:(NSURL*) fileUrl
434{
435 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
436}
437
Alexandre Lision886cde12016-10-25 17:39:49 -0400438#pragma mark - RestoreAccountDelegate methods
439
440-(void) didCompleteImport
441{
442 // Nothing to do here
443}
444
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400445@end