Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 3 | * 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 20 | #import "AccountsVC.h" |
| 21 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 22 | // Qt |
| 23 | #import <QItemSelectionModel> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 24 | #import <QSortFilterProxyModel> |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 25 | #import <QtCore/qdir.h> |
| 26 | #import <QtCore/qstandardpaths.h> |
| 27 | |
| 28 | // LRC |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 29 | #import <accountmodel.h> |
| 30 | #import <protocolmodel.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 31 | #import <account.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 32 | |
| 33 | #import "QNSTreeController.h" |
| 34 | #import "AccGeneralVC.h" |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 35 | #import "AccMediaVC.h" |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 36 | #import "AccAdvancedVC.h" |
| 37 | #import "AccSecurityVC.h" |
| 38 | #import "AccRingVC.h" |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 39 | #import "AccDevicesVC.h" |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 40 | #import "BackupAccountWC.h" |
| 41 | #import "RestoreAccountWC.h" |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 42 | #import "RingWizardWC.h" |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 44 | @interface AccountsVC () <BackupAccountDelegate, RestoreAccountDelegate> |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 45 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 46 | @property (assign) IBOutlet NSPopUpButton *protocolList; |
| 47 | |
| 48 | @property (assign) IBOutlet NSTabView *configPanels; |
| 49 | @property (retain) IBOutlet NSTabViewItem *generalTabItem; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 50 | @property (retain) IBOutlet NSTabViewItem *mediaTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 51 | @property (retain) IBOutlet NSTabViewItem *advancedTabItem; |
| 52 | @property (retain) IBOutlet NSTabViewItem *securityTabItem; |
| 53 | @property (retain) IBOutlet NSTabViewItem *ringTabItem; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 54 | @property (retain) IBOutlet NSTabViewItem *ringDevicesTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 55 | |
| 56 | @property QNSTreeController *treeController; |
| 57 | @property (assign) IBOutlet NSOutlineView *accountsListView; |
| 58 | @property (assign) IBOutlet NSTabView *accountDetailsView; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 59 | @property (unsafe_unretained) IBOutlet NSButton* exportAccountButton; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 60 | |
| 61 | @property AccRingVC* ringVC; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 62 | @property AccDevicesVC* devicesVC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 63 | @property AccGeneralVC* generalVC; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 64 | @property AccMediaVC* audioVC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 65 | @property AccAdvancedVC* advancedVC; |
| 66 | @property AccSecurityVC* securityVC; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 67 | @property AbstractLoadingWC* accountModal; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 68 | @property RingWizardWC* wizard; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 69 | |
| 70 | @end |
| 71 | |
| 72 | @implementation AccountsVC |
| 73 | @synthesize protocolList; |
| 74 | @synthesize configPanels; |
| 75 | @synthesize generalTabItem; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 76 | @synthesize mediaTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 77 | @synthesize advancedTabItem; |
| 78 | @synthesize securityTabItem; |
| 79 | @synthesize ringTabItem; |
Alexandre Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 80 | @synthesize ringDevicesTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 81 | @synthesize accountsListView; |
| 82 | @synthesize accountDetailsView; |
| 83 | @synthesize treeController; |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 84 | @synthesize accountModal; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 85 | @synthesize wizard; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 86 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 87 | NSInteger const TAG_CHECK = 100; |
| 88 | NSInteger const TAG_NAME = 200; |
| 89 | NSInteger const TAG_STATUS = 300; |
| 90 | NSInteger const TAG_TYPE = 400; |
| 91 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 92 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 93 | - (void)awakeFromNib |
| 94 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 95 | treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 96 | [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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 104 | QObject::connect(&AccountModel::instance(), |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 105 | &QAbstractItemModel::dataChanged, |
| 106 | [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 107 | [accountsListView reloadDataForRowIndexes: |
| 108 | [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)] |
| 109 | columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 110 | }); |
| 111 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 112 | QObject::connect(AccountModel::instance().selectionModel(), |
| 113 | &QItemSelectionModel::currentChanged, |
| 114 | [=](const QModelIndex ¤t, 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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 125 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 126 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 127 | QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 128 | [self.protocolList addItemWithTitle: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 129 | AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 274c22c | 2016-06-30 11:57:43 -0400 | [diff] [blame] | 130 | QObject::connect(AccountModel::instance().protocolModel()->selectionModel(), |
| 131 | &QItemSelectionModel::currentChanged, |
| 132 | [=](const QModelIndex ¤t, 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 139 | |
| 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 Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 145 | 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 149 | |
| 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 Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 164 | |
| 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 171 | - (void) setupSIPPanels |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 172 | { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 173 | // Start by removing all tabs |
| 174 | for(NSTabViewItem* item in configPanels.tabViewItems) { |
| 175 | [configPanels removeTabViewItem:item]; |
| 176 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 177 | [configPanels insertTabViewItem:generalTabItem atIndex:0]; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 178 | [configPanels insertTabViewItem:mediaTabItem atIndex:1]; |
| 179 | [configPanels insertTabViewItem:advancedTabItem atIndex:2]; |
| 180 | [configPanels insertTabViewItem:securityTabItem atIndex:3]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 183 | - (void) setupRINGPanels |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 184 | { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 185 | // 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 Lision | 45f1f54 | 2016-08-25 15:16:17 -0400 | [diff] [blame] | 191 | [configPanels insertTabViewItem:ringDevicesTabItem atIndex:1]; |
| 192 | [configPanels insertTabViewItem:mediaTabItem atIndex:2]; |
| 193 | [configPanels insertTabViewItem:advancedTabItem atIndex:3]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 194 | } |
Loïc Siret | 31d5cc0 | 2016-09-08 14:38:24 -0400 | [diff] [blame] | 195 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 196 | - (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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | #pragma mark - NSOutlineViewDelegate methods |
| 204 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 205 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item; |
| 206 | { |
| 207 | return YES; |
| 208 | } |
| 209 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 210 | - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 211 | { |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 212 | return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 215 | - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 216 | { |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 217 | NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 218 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 219 | QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
| 220 | if(!qIdx.isValid()) |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 221 | return result; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 222 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 223 | 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 Lision | 0ab4608 | 2015-08-11 12:10:21 -0400 | [diff] [blame] | 227 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 228 | auto account = AccountModel::instance().getAccountByModelIndex(qIdx); |
| 229 | auto humanState = account->toHumanStateName(); |
| 230 | |
| 231 | [nameLabel setStringValue:account->alias().toNSString()]; |
| 232 | [stateLabel setStringValue:humanState.toNSString()]; |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 233 | |
| 234 | switch (account->protocol()) { |
| 235 | case Account::Protocol::SIP: |
| 236 | [typeLabel setStringValue:@"SIP"]; |
| 237 | break; |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 238 | case Account::Protocol::RING: |
| 239 | [typeLabel setStringValue:@"RING"]; |
| 240 | break; |
| 241 | default: |
| 242 | break; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 243 | } |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 244 | |
| 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 268 | - (void)outlineViewSelectionDidChange:(NSNotification *)notification |
| 269 | { |
| 270 | // ask the tree controller for the current selection |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 271 | [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 272 | if([[treeController selectedNodes] count] > 0) { |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 273 | auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 274 | //Update details view |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 275 | auto acc = AccountModel::instance().getAccountByModelIndex(qIdx); |
| 276 | AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 277 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 278 | switch (acc->protocol()) { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 279 | case Account::Protocol::SIP: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 280 | [self setupSIPPanels]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 281 | break; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 282 | case Account::Protocol::RING: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 283 | [self setupRINGPanels]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 284 | break; |
| 285 | default: |
| 286 | break; |
| 287 | } |
| 288 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 289 | [self.accountDetailsView setHidden:NO]; |
| 290 | } else { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 291 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 295 | #pragma mark - Delete account |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 296 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 297 | - (IBAction)removeAccount:(id)sender |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 298 | { |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 299 | 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 Lision | 274c22c | 2016-06-30 11:57:43 -0400 | [diff] [blame] | 316 | } |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 317 | |
| 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 328 | - (void) backupAccount:(NSMenuItem*) sender |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 329 | { |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 330 | 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 353 | - (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 Siret | 31d5cc0 | 2016-09-08 14:38:24 -0400 | [diff] [blame] | 433 | -(void) didCompleteExportWithPath:(NSURL*) fileUrl |
| 434 | { |
| 435 | [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""]; |
| 436 | } |
| 437 | |
Alexandre Lision | 886cde1 | 2016-10-25 17:39:49 -0400 | [diff] [blame] | 438 | #pragma mark - RestoreAccountDelegate methods |
| 439 | |
| 440 | -(void) didCompleteImport |
| 441 | { |
| 442 | // Nothing to do here |
| 443 | } |
| 444 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 445 | @end |