blob: 29abd5f1bc739c58a8004f811fe553acdf9b23be [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 Lisionc1f96662016-03-23 17:24:19 -040039#import "PathPasswordWC.h"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040040
Alexandre Lision53428a52015-03-20 17:59:00 -040041// We disabled IAX protocol for now, so don't show it to the user
42class ActiveProtocolModel : public QSortFilterProxyModel
43{
44public:
45 ActiveProtocolModel(QAbstractItemModel* parent) : QSortFilterProxyModel(parent)
46 {
47 setSourceModel(parent);
48 }
49 virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
50 {
51 return sourceModel()->index(source_row,0,source_parent).flags() & Qt::ItemIsEnabled;
52 }
53};
54
Alexandre Lisionc1f96662016-03-23 17:24:19 -040055@interface AccountsVC () <PathPasswordDelegate>
56
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040057@property (assign) IBOutlet NSPopUpButton *protocolList;
58
59@property (assign) IBOutlet NSTabView *configPanels;
60@property (retain) IBOutlet NSTabViewItem *generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050061@property (retain) IBOutlet NSTabViewItem *mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040062@property (retain) IBOutlet NSTabViewItem *advancedTabItem;
63@property (retain) IBOutlet NSTabViewItem *securityTabItem;
64@property (retain) IBOutlet NSTabViewItem *ringTabItem;
65
66@property QNSTreeController *treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -040067@property ActiveProtocolModel* proxyProtocolModel;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040068@property (assign) IBOutlet NSOutlineView *accountsListView;
69@property (assign) IBOutlet NSTabView *accountDetailsView;
Alexandre Lisionc1f96662016-03-23 17:24:19 -040070@property (unsafe_unretained) IBOutlet NSButton* exportAccountButton;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040071
72@property AccRingVC* ringVC;
73@property AccGeneralVC* generalVC;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050074@property AccMediaVC* audioVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040075@property AccAdvancedVC* advancedVC;
76@property AccSecurityVC* securityVC;
Alexandre Lisionc1f96662016-03-23 17:24:19 -040077@property PathPasswordWC* passwordWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040078
79@end
80
81@implementation AccountsVC
82@synthesize protocolList;
83@synthesize configPanels;
84@synthesize generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050085@synthesize mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086@synthesize advancedTabItem;
87@synthesize securityTabItem;
88@synthesize ringTabItem;
89@synthesize accountsListView;
90@synthesize accountDetailsView;
91@synthesize treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -040092@synthesize proxyProtocolModel;
Alexandre Lisionc1f96662016-03-23 17:24:19 -040093@synthesize passwordWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040094
Alexandre Lision0c56cb62016-02-12 18:04:08 -050095NSInteger const TAG_CHECK = 100;
96NSInteger const TAG_NAME = 200;
97NSInteger const TAG_STATUS = 300;
98NSInteger const TAG_TYPE = 400;
99
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400100typedef NS_ENUM(NSUInteger, Action) {
101 ACTION_EXPORT = 0,
102 ACTION_IMPORT = 1,
103};
104
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400105- (void)awakeFromNib
106{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400107 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400108 [treeController setAvoidsEmptySelection:NO];
109 [treeController setAlwaysUsesMultipleValuesMarker:YES];
110 [treeController setChildrenKeyPath:@"children"];
111
112 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
113 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
114 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
115
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400116 QObject::connect(&AccountModel::instance(),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400117 &QAbstractItemModel::dataChanged,
118 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400119 [accountsListView reloadDataForRowIndexes:
120 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
121 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400122 });
123
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500124 QObject::connect(AccountModel::instance().selectionModel(),
125 &QItemSelectionModel::currentChanged,
126 [=](const QModelIndex &current, const QModelIndex &previous) {
127 [accountDetailsView setHidden:!current.isValid()];
128 if(!current.isValid()) {
129 [accountsListView deselectAll:nil];
130 return;
131 }
132
133 [treeController setSelectionQModelIndex:current];
134 });
135
136
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400137 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400138
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400139 proxyProtocolModel = new ActiveProtocolModel(AccountModel::instance().protocolModel());
140 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400141 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400142 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
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];
168}
169
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400170- (void) dealloc
171{
172 delete proxyProtocolModel;
173}
174
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400175- (IBAction)addAccount:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400176 QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400177
Alexandre Lision81c97212015-06-17 15:51:53 -0400178 auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400179 AccountModel::instance().protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
180 auto acc = AccountModel::instance().add([newAccName UTF8String], qIdx);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400181 acc->setDisplayName(acc->alias());
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400182 AccountModel::instance().save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183}
184
185- (IBAction)protocolSelectedChanged:(id)sender {
186
187 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400188 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400189 AccountModel::instance().protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400190 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400191
192}
193
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400194- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400195{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400196 // Start by removing all tabs
197 for(NSTabViewItem* item in configPanels.tabViewItems) {
198 [configPanels removeTabViewItem:item];
199 }
200
201 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500202 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
203 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
204 [configPanels insertTabViewItem:securityTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400205}
206
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400207- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400208{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400209 // Start by removing all tabs
210 for(NSTabViewItem* item in configPanels.tabViewItems) {
211 [configPanels removeTabViewItem:item];
212 }
213
214 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500215 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400216}
217
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400218- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400219{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400220 // Start by removing all tabs
221 for(NSTabViewItem* item in configPanels.tabViewItems) {
222 [configPanels removeTabViewItem:item];
223 }
224
225 [configPanels insertTabViewItem:ringTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500226 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
227 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
Alexandre Lision9ddfd8d2015-08-10 15:11:16 -0400228
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400229}
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400230- (IBAction)exportAccount:(id)sender
231{
232 passwordWC = [[PathPasswordWC alloc] initWithDelegate:self actionCode:Action::ACTION_EXPORT];
233#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
234 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
235#else
236 [NSApp beginSheet: passwordWC.window
237 modalForWindow: self.view.window
238 modalDelegate: self
239 didEndSelector: nil
240 contextInfo: nil];
241#endif
242 [passwordWC setAllowFileSelection:NO];
243}
244
245- (IBAction)importAccount:(id)sender
246{
247 passwordWC = [[PathPasswordWC alloc] initWithDelegate:self actionCode:Action::ACTION_IMPORT];
248#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
249 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
250#else
251 [NSApp beginSheet: passwordWC.window
252 modalForWindow: self.view.window
253 modalDelegate: self
254 didEndSelector: nil
255 contextInfo: nil];
256#endif
257}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400258
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500259- (IBAction)toggleAccount:(NSButton*)sender {
260 NSInteger row = [accountsListView rowForView:sender];
261 auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
262 accountToToggle->setEnabled(sender.state);
263 accountToToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400264}
265
266#pragma mark - NSOutlineViewDelegate methods
267
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400268- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
269{
270 return YES;
271}
272
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500273- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400274{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500275 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400276}
277
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500278- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400279{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500280 NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400281
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400282 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
283 if(!qIdx.isValid())
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500284 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400285
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500286 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
287 NSTextField* stateLabel = [result viewWithTag:TAG_STATUS];
288 NSButton* checkButton = [result viewWithTag:TAG_CHECK];
289 NSTextField* typeLabel = [result viewWithTag:TAG_TYPE];
Alexandre Lision0ab46082015-08-11 12:10:21 -0400290
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500291 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
292 auto humanState = account->toHumanStateName();
293
294 [nameLabel setStringValue:account->alias().toNSString()];
295 [stateLabel setStringValue:humanState.toNSString()];
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500296
297 switch (account->protocol()) {
298 case Account::Protocol::SIP:
299 [typeLabel setStringValue:@"SIP"];
300 break;
301 case Account::Protocol::IAX:
302 [typeLabel setStringValue:@"IAX"];
303 break;
304 case Account::Protocol::RING:
305 [typeLabel setStringValue:@"RING"];
306 break;
307 default:
308 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400309 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500310
311 switch (account->registrationState()) {
312 case Account::RegistrationState::READY:
313 [stateLabel setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
314 break;
315 case Account::RegistrationState::TRYING:
316 [stateLabel setTextColor:[NSColor redColor]];
317 break;
318 case Account::RegistrationState::UNREGISTERED:
319 [stateLabel setTextColor:[NSColor blackColor]];
320 break;
321 case Account::RegistrationState::ERROR:
322 [stateLabel setTextColor:[NSColor redColor]];
323 break;
324 default:
325 [stateLabel setTextColor:[NSColor blackColor]];
326 break;
327 }
328
329 [checkButton setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
330
331 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400332}
333
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400334- (void)outlineViewSelectionDidChange:(NSNotification *)notification
335{
336 // ask the tree controller for the current selection
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400337 [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400338 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400339 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400340 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400341 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
342 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400343
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500344 switch (acc->protocol()) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400345 case Account::Protocol::SIP:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400346 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400347 break;
348 case Account::Protocol::IAX:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400349 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400350 break;
351 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400352 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400353 break;
354 default:
355 break;
356 }
357
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400358 [self.accountDetailsView setHidden:NO];
359 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400360 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400361 }
362}
363
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400364-(void) didCompleteWithPath:(NSURL*) path Password:(NSString*) password ActionCode:(NSInteger)requestCode
365{
366 switch (requestCode) {
367 case Action::ACTION_EXPORT:
368 if(treeController.selectedNodes.count > 0) {
369 QStringList accounts;
370 for (id item : [treeController selectedNodes]) {
371 QModelIndex accIdx = [treeController toQIdx:item];
372 accounts << AccountModel::instance().getAccountByModelIndex(accIdx)->id();
373
374 }
375 auto finalURL = [path URLByAppendingPathComponent:@"accounts.ring"];
376 [passwordWC showLoading];
377 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
378 int result = AccountModel::instance().exportAccounts(accounts, finalURL.path.UTF8String, password.UTF8String);
379 switch (result) {
380 case 0:
381 [[NSWorkspace sharedWorkspace] selectFile:finalURL.path inFileViewerRootedAtPath:@""];
382 [passwordWC close];
383 break;
384 default:
385 [passwordWC showError:NSLocalizedString(@"An error occured during the export", @"Error shown to the user" )];
386 break;
387 }
388 });
389 }
390 break;
391 case Action::ACTION_IMPORT: {
392 [passwordWC showLoading];
393 SEL sel = @selector(importAccountsWithPath:andPassword:);
394 NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:sel]];
395 [inv setSelector:sel];
396 [inv setTarget:self];
397 //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
398 [inv setArgument:&path atIndex:2];
399 [inv setArgument:&password atIndex:3];
400
401 // Schedule import for next iteration of event loop in order to let us start the loading anim
402 [inv performSelector:@selector(invoke) withObject:nil afterDelay:0];
403
404 }
405 break;
406 default:
407 NSLog(@"Unrecognized action %d", requestCode);
408 break;
409 }
410}
411
412- (void) importAccountsWithPath:(NSURL*) path andPassword:(NSString*) password
413{
414 int result = AccountModel::instance().importAccounts(path.path.UTF8String, password.UTF8String);
415 switch (result) {
416 case 0:
417 [passwordWC close];
418 break;
419 default:
420 [passwordWC showError:NSLocalizedString(@"An error occured during the import", @"Error shown to the user" )];
421 break;
422 }
423}
424
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400425#pragma mark - NSMenuDelegate methods
426
427- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
428{
Alexandre Lision53428a52015-03-20 17:59:00 -0400429 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400430 QModelIndex qIdx = AccountModel::instance().protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400431 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400432
433 return YES;
434}
435
436- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
437{
Alexandre Lision53428a52015-03-20 17:59:00 -0400438 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400439}
440
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400441@end