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 | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 39 | #import "PathPasswordWC.h" |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 40 | |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 41 | // We disabled IAX protocol for now, so don't show it to the user |
| 42 | class ActiveProtocolModel : public QSortFilterProxyModel |
| 43 | { |
| 44 | public: |
| 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 Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 55 | @interface AccountsVC () <PathPasswordDelegate> |
| 56 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 57 | @property (assign) IBOutlet NSPopUpButton *protocolList; |
| 58 | |
| 59 | @property (assign) IBOutlet NSTabView *configPanels; |
| 60 | @property (retain) IBOutlet NSTabViewItem *generalTabItem; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 61 | @property (retain) IBOutlet NSTabViewItem *mediaTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 62 | @property (retain) IBOutlet NSTabViewItem *advancedTabItem; |
| 63 | @property (retain) IBOutlet NSTabViewItem *securityTabItem; |
| 64 | @property (retain) IBOutlet NSTabViewItem *ringTabItem; |
| 65 | |
| 66 | @property QNSTreeController *treeController; |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 67 | @property ActiveProtocolModel* proxyProtocolModel; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 68 | @property (assign) IBOutlet NSOutlineView *accountsListView; |
| 69 | @property (assign) IBOutlet NSTabView *accountDetailsView; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 70 | @property (unsafe_unretained) IBOutlet NSButton* exportAccountButton; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 71 | |
| 72 | @property AccRingVC* ringVC; |
| 73 | @property AccGeneralVC* generalVC; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 74 | @property AccMediaVC* audioVC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 75 | @property AccAdvancedVC* advancedVC; |
| 76 | @property AccSecurityVC* securityVC; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 77 | @property PathPasswordWC* passwordWC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 78 | |
| 79 | @end |
| 80 | |
| 81 | @implementation AccountsVC |
| 82 | @synthesize protocolList; |
| 83 | @synthesize configPanels; |
| 84 | @synthesize generalTabItem; |
Alexandre Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 85 | @synthesize mediaTabItem; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 86 | @synthesize advancedTabItem; |
| 87 | @synthesize securityTabItem; |
| 88 | @synthesize ringTabItem; |
| 89 | @synthesize accountsListView; |
| 90 | @synthesize accountDetailsView; |
| 91 | @synthesize treeController; |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 92 | @synthesize proxyProtocolModel; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 93 | @synthesize passwordWC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 94 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 95 | NSInteger const TAG_CHECK = 100; |
| 96 | NSInteger const TAG_NAME = 200; |
| 97 | NSInteger const TAG_STATUS = 300; |
| 98 | NSInteger const TAG_TYPE = 400; |
| 99 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 100 | typedef NS_ENUM(NSUInteger, Action) { |
| 101 | ACTION_EXPORT = 0, |
| 102 | ACTION_IMPORT = 1, |
| 103 | }; |
| 104 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 105 | - (void)awakeFromNib |
| 106 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 107 | treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 108 | [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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 116 | QObject::connect(&AccountModel::instance(), |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 117 | &QAbstractItemModel::dataChanged, |
| 118 | [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 119 | [accountsListView reloadDataForRowIndexes: |
| 120 | [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)] |
| 121 | columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 122 | }); |
| 123 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 124 | QObject::connect(AccountModel::instance().selectionModel(), |
| 125 | &QItemSelectionModel::currentChanged, |
| 126 | [=](const QModelIndex ¤t, 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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 137 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 138 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 139 | proxyProtocolModel = new ActiveProtocolModel(AccountModel::instance().protocolModel()); |
| 140 | QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 141 | [self.protocolList addItemWithTitle: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 142 | AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 143 | |
| 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 Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 149 | 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 153 | |
| 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 Lision | 54b0fae | 2015-08-04 15:19:01 -0400 | [diff] [blame] | 170 | - (void) dealloc |
| 171 | { |
| 172 | delete proxyProtocolModel; |
| 173 | } |
| 174 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 175 | - (IBAction)addAccount:(id)sender { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 176 | QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 177 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 178 | auto newAccName = [[NSString alloc] initWithFormat:@"%@ account", |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 179 | AccountModel::instance().protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil]; |
| 180 | auto acc = AccountModel::instance().add([newAccName UTF8String], qIdx); |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 181 | acc->setDisplayName(acc->alias()); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 182 | AccountModel::instance().save(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | - (IBAction)protocolSelectedChanged:(id)sender { |
| 186 | |
| 187 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 188 | QModelIndex proxyIdx = proxyProtocolModel->index(index, 0); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 189 | AccountModel::instance().protocolModel()->selectionModel()->setCurrentIndex( |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 190 | proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 191 | |
| 192 | } |
| 193 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 194 | - (void) setupSIPPanels |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 195 | { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 196 | // 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 Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 202 | [configPanels insertTabViewItem:mediaTabItem atIndex:1]; |
| 203 | [configPanels insertTabViewItem:advancedTabItem atIndex:2]; |
| 204 | [configPanels insertTabViewItem:securityTabItem atIndex:3]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 207 | - (void) setupIAXPanels |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 208 | { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 209 | // 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 Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 215 | [configPanels insertTabViewItem:mediaTabItem atIndex:1]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 218 | - (void) setupRINGPanels |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 219 | { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 220 | // 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 Lision | e4d61cb | 2016-02-10 09:26:24 -0500 | [diff] [blame] | 226 | [configPanels insertTabViewItem:mediaTabItem atIndex:1]; |
| 227 | [configPanels insertTabViewItem:advancedTabItem atIndex:2]; |
Alexandre Lision | 9ddfd8d | 2015-08-10 15:11:16 -0400 | [diff] [blame] | 228 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 229 | } |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 230 | - (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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 258 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 259 | - (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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | #pragma mark - NSOutlineViewDelegate methods |
| 267 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 268 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item; |
| 269 | { |
| 270 | return YES; |
| 271 | } |
| 272 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 273 | - (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 274 | { |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 275 | return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 278 | - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 279 | { |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 280 | NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 281 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 282 | QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
| 283 | if(!qIdx.isValid()) |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 284 | return result; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 285 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 286 | 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 Lision | 0ab4608 | 2015-08-11 12:10:21 -0400 | [diff] [blame] | 290 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 291 | auto account = AccountModel::instance().getAccountByModelIndex(qIdx); |
| 292 | auto humanState = account->toHumanStateName(); |
| 293 | |
| 294 | [nameLabel setStringValue:account->alias().toNSString()]; |
| 295 | [stateLabel setStringValue:humanState.toNSString()]; |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 296 | |
| 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 309 | } |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 310 | |
| 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 332 | } |
| 333 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 334 | - (void)outlineViewSelectionDidChange:(NSNotification *)notification |
| 335 | { |
| 336 | // ask the tree controller for the current selection |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 337 | [self.exportAccountButton setEnabled:[[treeController selectedNodes] count] > 0]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 338 | if([[treeController selectedNodes] count] > 0) { |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 339 | auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 340 | //Update details view |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 341 | auto acc = AccountModel::instance().getAccountByModelIndex(qIdx); |
| 342 | AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 343 | |
Alexandre Lision | 0c56cb6 | 2016-02-12 18:04:08 -0500 | [diff] [blame] | 344 | switch (acc->protocol()) { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 345 | case Account::Protocol::SIP: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 346 | [self setupSIPPanels]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 347 | break; |
| 348 | case Account::Protocol::IAX: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 349 | [self setupIAXPanels]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 350 | break; |
| 351 | case Account::Protocol::RING: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 352 | [self setupRINGPanels]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 353 | break; |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 358 | [self.accountDetailsView setHidden:NO]; |
| 359 | } else { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 360 | AccountModel::instance().selectionModel()->clearCurrentIndex(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 364 | -(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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 425 | #pragma mark - NSMenuDelegate methods |
| 426 | |
| 427 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 428 | { |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 429 | QModelIndex proxyIdx = proxyProtocolModel->index(index, 0); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 430 | QModelIndex qIdx = AccountModel::instance().protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row()); |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 431 | [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 432 | |
| 433 | return YES; |
| 434 | } |
| 435 | |
| 436 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 437 | { |
Alexandre Lision | 53428a5 | 2015-03-20 17:59:00 -0400 | [diff] [blame] | 438 | return proxyProtocolModel->rowCount(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 441 | @end |