blob: 84b4ca6237c58ebc96a398925162084a9fdba77e [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
20#define COLUMNID_ENABLE @"EnableColumn"
21#define COLUMNID_NAME @"NameColumn"
22#define COLUMNID_STATE @"StateColumn"
23
24#import "AccountsVC.h"
25
26// LibRingClient
Alexandre Lision91d11e52015-03-20 17:42:05 -040027#import <QSortFilterProxyModel>
28#import <accountmodel.h>
29#import <protocolmodel.h>
30#import <QItemSelectionModel>
31#import <account.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040032
33#import "QNSTreeController.h"
34#import "AccGeneralVC.h"
35#import "AccAudioVC.h"
36#import "AccVideoVC.h"
37#import "AccAdvancedVC.h"
38#import "AccSecurityVC.h"
39#import "AccRingVC.h"
40
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 Lisionf5fc4792015-03-17 09:15:43 -040055@interface AccountsVC ()
56@property (assign) IBOutlet NSPopUpButton *protocolList;
57
58@property (assign) IBOutlet NSTabView *configPanels;
59@property (retain) IBOutlet NSTabViewItem *generalTabItem;
60@property (retain) IBOutlet NSTabViewItem *audioTabItem;
61@property (retain) IBOutlet NSTabViewItem *videoTabItem;
62@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;
70
71@property AccRingVC* ringVC;
72@property AccGeneralVC* generalVC;
73@property AccAudioVC* audioVC;
74@property AccVideoVC* videoVC;
75@property AccAdvancedVC* advancedVC;
76@property AccSecurityVC* securityVC;
77
78@end
79
80@implementation AccountsVC
81@synthesize protocolList;
82@synthesize configPanels;
83@synthesize generalTabItem;
84@synthesize audioTabItem;
85@synthesize videoTabItem;
86@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 Lisionf5fc4792015-03-17 09:15:43 -040093
94- (void)awakeFromNib
95{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040096 treeController = [[QNSTreeController alloc] initWithQModel:&AccountModel::instance()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040097 [treeController setAvoidsEmptySelection:NO];
98 [treeController setAlwaysUsesMultipleValuesMarker:YES];
99 [treeController setChildrenKeyPath:@"children"];
100
101 [accountsListView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
102 [accountsListView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
103 [accountsListView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
104
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400105 QObject::connect(&AccountModel::instance(),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400106 &QAbstractItemModel::dataChanged,
107 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400108 [accountsListView reloadDataForRowIndexes:
109 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
110 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111 });
112
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400113 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400114
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400115 proxyProtocolModel = new ActiveProtocolModel(AccountModel::instance().protocolModel());
116 QModelIndex qProtocolIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400117 [self.protocolList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400118 AccountModel::instance().protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400119
120 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
121 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
122 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
123 [self.generalTabItem setView:self.generalVC.view];
124
125 self.audioVC = [[AccAudioVC alloc] initWithNibName:@"AccAudio" bundle:nil];
126 [[self.audioVC view] setFrame:[self.audioTabItem.view frame]];
127 [[self.audioVC view] setBounds:[self.audioTabItem.view bounds]];
128 [self.audioTabItem setView:self.audioVC.view];
129
130 self.videoVC = [[AccVideoVC alloc] initWithNibName:@"AccVideo" bundle:nil];
131 [[self.videoVC view] setFrame:[self.videoTabItem.view frame]];
132 [[self.videoVC view] setBounds:[self.videoTabItem.view bounds]];
133 [self.videoTabItem setView:self.videoVC.view];
134
135 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
136 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
137 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
138 [self.advancedTabItem setView:self.advancedVC.view];
139
140 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
141 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
142 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
143 [self.securityTabItem setView:self.securityVC.view];
144
145 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
146 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
147 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
148 [self.ringTabItem setView:self.ringVC.view];
149}
150
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400151- (void) dealloc
152{
153 delete proxyProtocolModel;
154}
155
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400156- (IBAction)moveUp:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400157 AccountModel::instance().moveUp();
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400158}
159
160- (IBAction)moveDown:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400161 AccountModel::instance().moveDown();
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400162}
163
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400164- (IBAction)removeAccount:(id)sender {
165
166 if(treeController.selectedNodes.count > 0) {
167 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400168 AccountModel::instance().remove(qIdx);
169 AccountModel::instance().save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400170 }
171}
172- (IBAction)addAccount:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400173 QModelIndex qIdx = AccountModel::instance().protocolModel()->selectionModel()->currentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400174
Alexandre Lision81c97212015-06-17 15:51:53 -0400175 auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400176 AccountModel::instance().protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
177 auto acc = AccountModel::instance().add([newAccName UTF8String], qIdx);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400178 acc->setDisplayName(acc->alias());
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400179 AccountModel::instance().save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400180}
181
182- (IBAction)protocolSelectedChanged:(id)sender {
183
184 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400185 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400186 AccountModel::instance().protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400187 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400188
189}
190
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400191- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400192{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400193 // Start by removing all tabs
194 for(NSTabViewItem* item in configPanels.tabViewItems) {
195 [configPanels removeTabViewItem:item];
196 }
197
198 [configPanels insertTabViewItem:generalTabItem atIndex:0];
199 [configPanels insertTabViewItem:audioTabItem atIndex:1];
200 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400201 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
202 [configPanels insertTabViewItem:securityTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400203}
204
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400205- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400206{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400207 // Start by removing all tabs
208 for(NSTabViewItem* item in configPanels.tabViewItems) {
209 [configPanels removeTabViewItem:item];
210 }
211
212 [configPanels insertTabViewItem:generalTabItem atIndex:0];
213 [configPanels insertTabViewItem:audioTabItem atIndex:1];
214 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400215}
216
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400217- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400218{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400219 // Start by removing all tabs
220 for(NSTabViewItem* item in configPanels.tabViewItems) {
221 [configPanels removeTabViewItem:item];
222 }
223
224 [configPanels insertTabViewItem:ringTabItem atIndex:0];
225 [configPanels insertTabViewItem:audioTabItem atIndex:1];
226 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision9ddfd8d2015-08-10 15:11:16 -0400227 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
228
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400229}
230
231- (IBAction)toggleAccount:(NSOutlineView*)sender {
232
233 if([sender clickedColumn] < 0)
234 return;
235
236 NSTableColumn* col = [sender.tableColumns objectAtIndex:[sender clickedColumn]];
237 if([col.identifier isEqualToString:COLUMNID_ENABLE]) {
238 NSInteger row = [sender clickedRow];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400239 QModelIndex accIdx = AccountModel::instance().index(row);
240 Account* toToggle = AccountModel::instance().getAccountByModelIndex(accIdx);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400241 NSButtonCell *cell = [col dataCellForRow:row];
242 toToggle->setEnabled(cell.state == NSOnState ? NO : YES);
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400243 toToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400244 }
245}
246
247#pragma mark - NSOutlineViewDelegate methods
248
249// -------------------------------------------------------------------------------
250// shouldSelectItem:item
251// -------------------------------------------------------------------------------
252- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
253{
254 return YES;
255}
256
257// -------------------------------------------------------------------------------
258// dataCellForTableColumn:tableColumn:item
259// -------------------------------------------------------------------------------
260- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
261{
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400262 NSCell *returnCell;
263
264 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
265 // Prevent user from enabling/disabling IP2IP account
266 if ([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE] &&
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400267 AccountModel::instance().ip2ip()->index() == qIdx) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400268
Alexandre Lision81c97212015-06-17 15:51:53 -0400269 return [[NSCell alloc] init];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400270 } else {
Alexandre Lision81c97212015-06-17 15:51:53 -0400271 return [tableColumn dataCell];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400272 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400273}
274
275// -------------------------------------------------------------------------------
276// textShouldEndEditing:fieldEditor
277// -------------------------------------------------------------------------------
278- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
279{
280 if ([[fieldEditor string] length] == 0)
281 {
282 // don't allow empty node names
283 return NO;
284 }
285 else
286 {
287 return YES;
288 }
289}
290
291// -------------------------------------------------------------------------------
292// shouldEditTableColumn:tableColumn:item
293//
294// Decide to allow the edit of the given outline view "item".
295// -------------------------------------------------------------------------------
296- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
297{
298 return NO;
299}
300
301// -------------------------------------------------------------------------------
302// outlineView:willDisplayCell:forTableColumn:item
303// -------------------------------------------------------------------------------
304- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
305{
306 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
307 if(!qIdx.isValid())
308 return;
309
310 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
311 {
Alexandre Lision0ab46082015-08-11 12:10:21 -0400312 cell.title = qIdx.data(Qt::DisplayRole).toString().toNSString();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400313 } else if([[tableColumn identifier] isEqualToString:COLUMNID_STATE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400314 NSTextFieldCell* stateCell = cell;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400315 auto account = AccountModel::instance().getAccountByModelIndex(qIdx);
Alexandre Lision0ab46082015-08-11 12:10:21 -0400316 auto humanState = account->toHumanStateName();
317 [stateCell setTitle:humanState.toNSString()];
318
319 switch (account->registrationState()) {
Alexandre Lision2528d802015-03-20 18:05:06 -0400320 case Account::RegistrationState::READY:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400321 [stateCell setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
Alexandre Lision2528d802015-03-20 18:05:06 -0400322 break;
323 case Account::RegistrationState::TRYING:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400324 [stateCell setTextColor:[NSColor redColor]];
Alexandre Lision2528d802015-03-20 18:05:06 -0400325 break;
326 case Account::RegistrationState::UNREGISTERED:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400327 [stateCell setTextColor:[NSColor blackColor]];
Alexandre Lision2528d802015-03-20 18:05:06 -0400328 break;
329 case Account::RegistrationState::ERROR:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400330 [stateCell setTextColor:[NSColor redColor]];
Alexandre Lision2528d802015-03-20 18:05:06 -0400331 break;
332 default:
Alexandre Lision0ab46082015-08-11 12:10:21 -0400333 [stateCell setTextColor:[NSColor blackColor]];
Alexandre Lision2528d802015-03-20 18:05:06 -0400334 break;
335 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400336 } else if([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400337 [cell setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400338 }
339}
340
341// -------------------------------------------------------------------------------
342// outlineViewSelectionDidChange:notification
343// -------------------------------------------------------------------------------
344- (void)outlineViewSelectionDidChange:(NSNotification *)notification
345{
346 // ask the tree controller for the current selection
347 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400348 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400349 //Update details view
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400350 auto acc = AccountModel::instance().getAccountByModelIndex(qIdx);
351 AccountModel::instance().selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400352
353 switch (acc->protocol()) {
354 case Account::Protocol::SIP:
355 NSLog(@"SIP");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400356 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400357 break;
358 case Account::Protocol::IAX:
359 NSLog(@"IAX");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400360 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400361 break;
362 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400363 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400364 NSLog(@"DRING");
365 break;
366 default:
367 break;
368 }
369
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400370 [self.accountDetailsView setHidden:NO];
371 } else {
372 [self.accountDetailsView setHidden:YES];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400373 AccountModel::instance().selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400374 }
375}
376
377#pragma mark - NSMenuDelegate methods
378
379- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
380{
Alexandre Lision53428a52015-03-20 17:59:00 -0400381 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400382 QModelIndex qIdx = AccountModel::instance().protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400383 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400384
385 return YES;
386}
387
388- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
389{
Alexandre Lision53428a52015-03-20 17:59:00 -0400390 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400391}
392
393
394
395@end