blob: a5a37f369c1c3c022ac3725474ab91c1cd4d768c [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"
39
Alexandre Lision53428a52015-03-20 17:59:00 -040040// We disabled IAX protocol for now, so don't show it to the user
41class ActiveProtocolModel : public QSortFilterProxyModel
42{
43public:
44 ActiveProtocolModel(QAbstractItemModel* parent) : QSortFilterProxyModel(parent)
45 {
46 setSourceModel(parent);
47 }
48 virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
49 {
50 return sourceModel()->index(source_row,0,source_parent).flags() & Qt::ItemIsEnabled;
51 }
52};
53
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040054@interface AccountsVC ()
55@property (assign) IBOutlet NSPopUpButton *protocolList;
56
57@property (assign) IBOutlet NSTabView *configPanels;
58@property (retain) IBOutlet NSTabViewItem *generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050059@property (retain) IBOutlet NSTabViewItem *mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040060@property (retain) IBOutlet NSTabViewItem *advancedTabItem;
61@property (retain) IBOutlet NSTabViewItem *securityTabItem;
62@property (retain) IBOutlet NSTabViewItem *ringTabItem;
63
64@property QNSTreeController *treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -040065@property ActiveProtocolModel* proxyProtocolModel;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040066@property (assign) IBOutlet NSOutlineView *accountsListView;
67@property (assign) IBOutlet NSTabView *accountDetailsView;
68
69@property AccRingVC* ringVC;
70@property AccGeneralVC* generalVC;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050071@property AccMediaVC* audioVC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072@property AccAdvancedVC* advancedVC;
73@property AccSecurityVC* securityVC;
74
75@end
76
77@implementation AccountsVC
78@synthesize protocolList;
79@synthesize configPanels;
80@synthesize generalTabItem;
Alexandre Lisione4d61cb2016-02-10 09:26:24 -050081@synthesize mediaTabItem;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040082@synthesize advancedTabItem;
83@synthesize securityTabItem;
84@synthesize ringTabItem;
85@synthesize accountsListView;
86@synthesize accountDetailsView;
87@synthesize treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -040088@synthesize proxyProtocolModel;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040089
Alexandre Lision0c56cb62016-02-12 18:04:08 -050090NSInteger const TAG_CHECK = 100;
91NSInteger const TAG_NAME = 200;
92NSInteger const TAG_STATUS = 300;
93NSInteger const TAG_TYPE = 400;
94
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040095- (void)awakeFromNib
96{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040097 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040098 [treeController setAvoidsEmptySelection:NO];
99 [treeController setAlwaysUsesMultipleValuesMarker:YES];
100 [treeController setChildrenKeyPath:@"children"];
101
102 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
103 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
104 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
105
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400106 QObject::connect(&AccountModel::instance(),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107 &QAbstractItemModel::dataChanged,
108 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400109 [accountsListView reloadDataForRowIndexes:
110 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
111 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400112 });
113
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500114 QObject::connect(AccountModel::instance().selectionModel(),
115 &QItemSelectionModel::currentChanged,
116 [=](const QModelIndex &current, const QModelIndex &previous) {
117 [accountDetailsView setHidden:!current.isValid()];
118 if(!current.isValid()) {
119 [accountsListView deselectAll:nil];
120 return;
121 }
122
123 [treeController setSelectionQModelIndex:current];
124 });
125
126
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400127 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400128
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400129 proxyProtocolModel = new ActiveProtocolModel(AccountModel::instance().protocolModel());
130 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400131 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400132 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400133
134 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
135 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
136 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
137 [self.generalTabItem setView:self.generalVC.view];
138
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500139 self.audioVC = [[AccMediaVC alloc] initWithNibName:@"AccMedia" bundle:nil];
140 [[self.audioVC view] setFrame:[self.mediaTabItem.view frame]];
141 [[self.audioVC view] setBounds:[self.mediaTabItem.view bounds]];
142 [self.mediaTabItem setView:self.audioVC.view];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400143
144 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
145 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
146 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
147 [self.advancedTabItem setView:self.advancedVC.view];
148
149 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
150 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
151 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
152 [self.securityTabItem setView:self.securityVC.view];
153
154 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
155 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
156 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
157 [self.ringTabItem setView:self.ringVC.view];
158}
159
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400160- (void) dealloc
161{
162 delete proxyProtocolModel;
163}
164
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400165- (IBAction)moveUp:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400166 AccountModel::instance().moveUp();
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400167}
168
169- (IBAction)moveDown:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400170 AccountModel::instance().moveDown();
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400171}
172
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400173- (IBAction)removeAccount:(id)sender {
174
175 if(treeController.selectedNodes.count > 0) {
176 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400177 AccountModel::instance().remove(qIdx);
178 AccountModel::instance().save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400179 }
180}
181- (IBAction)addAccount:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400182 QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183
Alexandre Lision81c97212015-06-17 15:51:53 -0400184 auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400185 AccountModel::instance().protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
186 auto acc = AccountModel::instance().add([newAccName UTF8String], qIdx);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400187 acc->setDisplayName(acc->alias());
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400188 AccountModel::instance().save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400189}
190
191- (IBAction)protocolSelectedChanged:(id)sender {
192
193 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400194 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400195 AccountModel::instance().protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400196 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400197
198}
199
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400200- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400201{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400202 // Start by removing all tabs
203 for(NSTabViewItem* item in configPanels.tabViewItems) {
204 [configPanels removeTabViewItem:item];
205 }
206
207 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500208 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
209 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
210 [configPanels insertTabViewItem:securityTabItem atIndex:3];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400211}
212
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400213- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400214{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400215 // Start by removing all tabs
216 for(NSTabViewItem* item in configPanels.tabViewItems) {
217 [configPanels removeTabViewItem:item];
218 }
219
220 [configPanels insertTabViewItem:generalTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500221 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400222}
223
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400224- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400225{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400226 // Start by removing all tabs
227 for(NSTabViewItem* item in configPanels.tabViewItems) {
228 [configPanels removeTabViewItem:item];
229 }
230
231 [configPanels insertTabViewItem:ringTabItem atIndex:0];
Alexandre Lisione4d61cb2016-02-10 09:26:24 -0500232 [configPanels insertTabViewItem:mediaTabItem atIndex:1];
233 [configPanels insertTabViewItem:advancedTabItem atIndex:2];
Alexandre Lision9ddfd8d2015-08-10 15:11:16 -0400234
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400235}
236
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500237- (IBAction)toggleAccount:(NSButton*)sender {
238 NSInteger row = [accountsListView rowForView:sender];
239 auto accountToToggle = AccountModel::instance().getAccountByModelIndex(AccountModel::instance().index(row));
240 accountToToggle->setEnabled(sender.state);
241 accountToToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400242}
243
244#pragma mark - NSOutlineViewDelegate methods
245
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400246- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
247{
248 return YES;
249}
250
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500251- (NSTableRowView *)outlineView:(NSOutlineView *)outlineView rowViewForItem:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400252{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500253 return [outlineView makeViewWithIdentifier:@"HoverRowView" owner:nil];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400254}
255
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500256- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400257{
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500258 NSTableView* result = [outlineView makeViewWithIdentifier:@"AccountView" owner:self];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400259
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400260 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
261 if(!qIdx.isValid())
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500262 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400263
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500264 NSTextField* nameLabel = [result viewWithTag:TAG_NAME];
265 NSTextField* stateLabel = [result viewWithTag:TAG_STATUS];
266 NSButton* checkButton = [result viewWithTag:TAG_CHECK];
267 NSTextField* typeLabel = [result viewWithTag:TAG_TYPE];
Alexandre Lision0ab46082015-08-11 12:10:21 -0400268
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500269 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
270 auto humanState = account->toHumanStateName();
271
272 [nameLabel setStringValue:account->alias().toNSString()];
273 [stateLabel setStringValue:humanState.toNSString()];
274 [checkButton setHidden:AccountModel::instance().ip2ip()->index() == qIdx];
275
276 switch (account->protocol()) {
277 case Account::Protocol::SIP:
278 [typeLabel setStringValue:@"SIP"];
279 break;
280 case Account::Protocol::IAX:
281 [typeLabel setStringValue:@"IAX"];
282 break;
283 case Account::Protocol::RING:
284 [typeLabel setStringValue:@"RING"];
285 break;
286 default:
287 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400288 }
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500289
290 switch (account->registrationState()) {
291 case Account::RegistrationState::READY:
292 [stateLabel setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
293 break;
294 case Account::RegistrationState::TRYING:
295 [stateLabel setTextColor:[NSColor redColor]];
296 break;
297 case Account::RegistrationState::UNREGISTERED:
298 [stateLabel setTextColor:[NSColor blackColor]];
299 break;
300 case Account::RegistrationState::ERROR:
301 [stateLabel setTextColor:[NSColor redColor]];
302 break;
303 default:
304 [stateLabel setTextColor:[NSColor blackColor]];
305 break;
306 }
307
308 [checkButton setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
309
310 return result;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400311}
312
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400313- (void)outlineViewSelectionDidChange:(NSNotification *)notification
314{
315 // ask the tree controller for the current selection
316 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400317 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400318 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400319 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
320 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400321
Alexandre Lision0c56cb62016-02-12 18:04:08 -0500322 switch (acc->protocol()) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400323 case Account::Protocol::SIP:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400324 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400325 break;
326 case Account::Protocol::IAX:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400327 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400328 break;
329 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400330 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400331 break;
332 default:
333 break;
334 }
335
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400336 [self.accountDetailsView setHidden:NO];
337 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400338 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400339 }
340}
341
342#pragma mark - NSMenuDelegate methods
343
344- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
345{
Alexandre Lision53428a52015-03-20 17:59:00 -0400346 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400347 QModelIndex qIdx = AccountModel::instance().protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400348 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400349
350 return YES;
351}
352
353- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
354{
Alexandre Lision53428a52015-03-20 17:59:00 -0400355 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400356}
357
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400358@end