blob: fff3e5df4def0b019e8b31e07e5f0119f705ea38 [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
Alexandre Lision9ddfd8d2015-08-10 15:11:16 -04002 * Copyright (C) 2015 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30
31#define COLUMNID_ENABLE @"EnableColumn"
32#define COLUMNID_NAME @"NameColumn"
33#define COLUMNID_STATE @"StateColumn"
34
35#import "AccountsVC.h"
36
37// LibRingClient
Alexandre Lision91d11e52015-03-20 17:42:05 -040038#import <QSortFilterProxyModel>
39#import <accountmodel.h>
40#import <protocolmodel.h>
41#import <QItemSelectionModel>
42#import <account.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040043
44#import "QNSTreeController.h"
45#import "AccGeneralVC.h"
46#import "AccAudioVC.h"
47#import "AccVideoVC.h"
48#import "AccAdvancedVC.h"
49#import "AccSecurityVC.h"
50#import "AccRingVC.h"
51
Alexandre Lision53428a52015-03-20 17:59:00 -040052// We disabled IAX protocol for now, so don't show it to the user
53class ActiveProtocolModel : public QSortFilterProxyModel
54{
55public:
56 ActiveProtocolModel(QAbstractItemModel* parent) : QSortFilterProxyModel(parent)
57 {
58 setSourceModel(parent);
59 }
60 virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
61 {
62 return sourceModel()->index(source_row,0,source_parent).flags() & Qt::ItemIsEnabled;
63 }
64};
65
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040066@interface AccountsVC ()
67@property (assign) IBOutlet NSPopUpButton *protocolList;
68
69@property (assign) IBOutlet NSTabView *configPanels;
70@property (retain) IBOutlet NSTabViewItem *generalTabItem;
71@property (retain) IBOutlet NSTabViewItem *audioTabItem;
72@property (retain) IBOutlet NSTabViewItem *videoTabItem;
73@property (retain) IBOutlet NSTabViewItem *advancedTabItem;
74@property (retain) IBOutlet NSTabViewItem *securityTabItem;
75@property (retain) IBOutlet NSTabViewItem *ringTabItem;
76
77@property QNSTreeController *treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -040078@property ActiveProtocolModel* proxyProtocolModel;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040079@property (assign) IBOutlet NSOutlineView *accountsListView;
80@property (assign) IBOutlet NSTabView *accountDetailsView;
81
82@property AccRingVC* ringVC;
83@property AccGeneralVC* generalVC;
84@property AccAudioVC* audioVC;
85@property AccVideoVC* videoVC;
86@property AccAdvancedVC* advancedVC;
87@property AccSecurityVC* securityVC;
88
89@end
90
91@implementation AccountsVC
92@synthesize protocolList;
93@synthesize configPanels;
94@synthesize generalTabItem;
95@synthesize audioTabItem;
96@synthesize videoTabItem;
97@synthesize advancedTabItem;
98@synthesize securityTabItem;
99@synthesize ringTabItem;
100@synthesize accountsListView;
101@synthesize accountDetailsView;
102@synthesize treeController;
Alexandre Lision53428a52015-03-20 17:59:00 -0400103@synthesize proxyProtocolModel;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400104
105- (void)awakeFromNib
106{
107 treeController = [[QNSTreeController alloc] initWithQModel:AccountModel::instance()];
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 Lisionf5fc4792015-03-17 09:15:43 -0400116 QObject::connect(AccountModel::instance(),
117 &QAbstractItemModel::dataChanged,
118 [=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400119 [accountsListView reloadDataForRowIndexes:
120 [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(topLeft.row(), bottomRight.row() + 1)]
121 columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, accountsListView.tableColumns.count)]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400122 });
123
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400124 AccountModel::instance()->selectionModel()->clearCurrentIndex();
125
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400126 proxyProtocolModel = new ActiveProtocolModel(AccountModel::instance()->protocolModel());
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400127 QModelIndex qProtocolIdx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
128 [self.protocolList addItemWithTitle:
129 AccountModel::instance()->protocolModel()->data(qProtocolIdx, Qt::DisplayRole).toString().toNSString()];
130
131 self.generalVC = [[AccGeneralVC alloc] initWithNibName:@"AccGeneral" bundle:nil];
132 [[self.generalVC view] setFrame:[self.generalTabItem.view frame]];
133 [[self.generalVC view] setBounds:[self.generalTabItem.view bounds]];
134 [self.generalTabItem setView:self.generalVC.view];
135
136 self.audioVC = [[AccAudioVC alloc] initWithNibName:@"AccAudio" bundle:nil];
137 [[self.audioVC view] setFrame:[self.audioTabItem.view frame]];
138 [[self.audioVC view] setBounds:[self.audioTabItem.view bounds]];
139 [self.audioTabItem setView:self.audioVC.view];
140
141 self.videoVC = [[AccVideoVC alloc] initWithNibName:@"AccVideo" bundle:nil];
142 [[self.videoVC view] setFrame:[self.videoTabItem.view frame]];
143 [[self.videoVC view] setBounds:[self.videoTabItem.view bounds]];
144 [self.videoTabItem setView:self.videoVC.view];
145
146 self.advancedVC = [[AccAdvancedVC alloc] initWithNibName:@"AccAdvanced" bundle:nil];
147 [[self.advancedVC view] setFrame:[self.advancedTabItem.view frame]];
148 [[self.advancedVC view] setBounds:[self.advancedTabItem.view bounds]];
149 [self.advancedTabItem setView:self.advancedVC.view];
150
151 self.securityVC = [[AccSecurityVC alloc] initWithNibName:@"AccSecurity" bundle:nil];
152 [[self.securityVC view] setFrame:[self.securityTabItem.view frame]];
153 [[self.securityVC view] setBounds:[self.securityTabItem.view bounds]];
154 [self.securityTabItem setView:self.securityVC.view];
155
156 self.ringVC = [[AccRingVC alloc] initWithNibName:@"AccRing" bundle:nil];
157 [[self.ringVC view] setFrame:[self.ringTabItem.view frame]];
158 [[self.ringVC view] setBounds:[self.ringTabItem.view bounds]];
159 [self.ringTabItem setView:self.ringVC.view];
160}
161
Alexandre Lision54b0fae2015-08-04 15:19:01 -0400162- (void) dealloc
163{
164 delete proxyProtocolModel;
165}
166
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400167- (IBAction)moveUp:(id)sender {
168 AccountModel::instance()->moveUp();
169}
170
171- (IBAction)moveDown:(id)sender {
172 AccountModel::instance()->moveDown();
173}
174
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400175- (IBAction)removeAccount:(id)sender {
176
177 if(treeController.selectedNodes.count > 0) {
178 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
179 AccountModel::instance()->remove(qIdx);
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400180 AccountModel::instance()->save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400181 }
182}
183- (IBAction)addAccount:(id)sender {
184 QModelIndex qIdx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
185
Alexandre Lision81c97212015-06-17 15:51:53 -0400186 auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400187 AccountModel::instance()->protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400188 auto acc = AccountModel::instance()->add([newAccName UTF8String], qIdx);
189 acc->setDisplayName(acc->alias());
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400190 AccountModel::instance()->save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400191}
192
193- (IBAction)protocolSelectedChanged:(id)sender {
194
195 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400196 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400197 AccountModel::instance()->protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400198 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400199
200}
201
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400202- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400203{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400204 // Start by removing all tabs
205 for(NSTabViewItem* item in configPanels.tabViewItems) {
206 [configPanels removeTabViewItem:item];
207 }
208
209 [configPanels insertTabViewItem:generalTabItem atIndex:0];
210 [configPanels insertTabViewItem:audioTabItem atIndex:1];
211 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400212 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
213 [configPanels insertTabViewItem:securityTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400214}
215
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400216- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400217{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400218 // Start by removing all tabs
219 for(NSTabViewItem* item in configPanels.tabViewItems) {
220 [configPanels removeTabViewItem:item];
221 }
222
223 [configPanels insertTabViewItem:generalTabItem atIndex:0];
224 [configPanels insertTabViewItem:audioTabItem atIndex:1];
225 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400226}
227
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400228- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400229{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400230 // Start by removing all tabs
231 for(NSTabViewItem* item in configPanels.tabViewItems) {
232 [configPanels removeTabViewItem:item];
233 }
234
235 [configPanels insertTabViewItem:ringTabItem atIndex:0];
236 [configPanels insertTabViewItem:audioTabItem atIndex:1];
237 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision9ddfd8d2015-08-10 15:11:16 -0400238 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
239
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400240}
241
242- (IBAction)toggleAccount:(NSOutlineView*)sender {
243
244 if([sender clickedColumn] < 0)
245 return;
246
247 NSTableColumn* col = [sender.tableColumns objectAtIndex:[sender clickedColumn]];
248 if([col.identifier isEqualToString:COLUMNID_ENABLE]) {
249 NSInteger row = [sender clickedRow];
250 QModelIndex accIdx = AccountModel::instance()->index(row);
251 Account* toToggle = AccountModel::instance()->getAccountByModelIndex(accIdx);
252 NSButtonCell *cell = [col dataCellForRow:row];
253 toToggle->setEnabled(cell.state == NSOnState ? NO : YES);
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400254 toToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400255 }
256}
257
258#pragma mark - NSOutlineViewDelegate methods
259
260// -------------------------------------------------------------------------------
261// shouldSelectItem:item
262// -------------------------------------------------------------------------------
263- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
264{
265 return YES;
266}
267
268// -------------------------------------------------------------------------------
269// dataCellForTableColumn:tableColumn:item
270// -------------------------------------------------------------------------------
271- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
272{
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400273 NSCell *returnCell;
274
275 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
276 // Prevent user from enabling/disabling IP2IP account
277 if ([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE] &&
278 AccountModel::instance()->ip2ip()->index() == qIdx) {
279
Alexandre Lision81c97212015-06-17 15:51:53 -0400280 return [[NSCell alloc] init];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400281 } else {
Alexandre Lision81c97212015-06-17 15:51:53 -0400282 return [tableColumn dataCell];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400283 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400284}
285
286// -------------------------------------------------------------------------------
287// textShouldEndEditing:fieldEditor
288// -------------------------------------------------------------------------------
289- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
290{
291 if ([[fieldEditor string] length] == 0)
292 {
293 // don't allow empty node names
294 return NO;
295 }
296 else
297 {
298 return YES;
299 }
300}
301
302// -------------------------------------------------------------------------------
303// shouldEditTableColumn:tableColumn:item
304//
305// Decide to allow the edit of the given outline view "item".
306// -------------------------------------------------------------------------------
307- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
308{
309 return NO;
310}
311
312// -------------------------------------------------------------------------------
313// outlineView:willDisplayCell:forTableColumn:item
314// -------------------------------------------------------------------------------
315- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
316{
317 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
318 if(!qIdx.isValid())
319 return;
320
321 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
322 {
323 cell.title = AccountModel::instance()->data(qIdx, Qt::DisplayRole).toString().toNSString();
324 } else if([[tableColumn identifier] isEqualToString:COLUMNID_STATE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400325 NSTextFieldCell* stateCell = cell;
326 Account::RegistrationState state = qvariant_cast<Account::RegistrationState>(qIdx.data((int)Account::Role::RegistrationState));
Alexandre Lision2528d802015-03-20 18:05:06 -0400327 switch (state) {
328 case Account::RegistrationState::READY:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400329 [stateCell setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
330 [stateCell setTitle:@"Ready"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400331 break;
332 case Account::RegistrationState::TRYING:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400333 [stateCell setTextColor:[NSColor redColor]];
334 [stateCell setTitle:@"Trying..."];
Alexandre Lision2528d802015-03-20 18:05:06 -0400335 break;
336 case Account::RegistrationState::UNREGISTERED:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400337 [stateCell setTextColor:[NSColor blackColor]];
338 [stateCell setTitle:@"Unregistered"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400339 break;
340 case Account::RegistrationState::ERROR:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400341 [stateCell setTextColor:[NSColor redColor]];
342 [stateCell setTitle:@"Error"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400343 break;
344 default:
345 break;
346 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400347 } else if([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400348 [cell setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400349 }
350}
351
352// -------------------------------------------------------------------------------
353// outlineViewSelectionDidChange:notification
354// -------------------------------------------------------------------------------
355- (void)outlineViewSelectionDidChange:(NSNotification *)notification
356{
357 // ask the tree controller for the current selection
358 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400359 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400360 //Update details view
Alexandre Lision81c97212015-06-17 15:51:53 -0400361 auto acc = AccountModel::instance()->getAccountByModelIndex(qIdx);
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400362 AccountModel::instance()->selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400363
364 switch (acc->protocol()) {
365 case Account::Protocol::SIP:
366 NSLog(@"SIP");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400367 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400368 break;
369 case Account::Protocol::IAX:
370 NSLog(@"IAX");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400371 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400372 break;
373 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400374 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400375 NSLog(@"DRING");
376 break;
377 default:
378 break;
379 }
380
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400381 [self.accountDetailsView setHidden:NO];
382 } else {
383 [self.accountDetailsView setHidden:YES];
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400384 AccountModel::instance()->selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400385 }
386}
387
388#pragma mark - NSMenuDelegate methods
389
390- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
391{
Alexandre Lision53428a52015-03-20 17:59:00 -0400392 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
393 QModelIndex qIdx = AccountModel::instance()->protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400394 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400395
396 return YES;
397}
398
399- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
400{
Alexandre Lision53428a52015-03-20 17:59:00 -0400401 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400402}
403
404
405
406@end