blob: 0fc4844c6af7ae15841bc84ceaa433f3ef210eae [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
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.
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 Lision53428a52015-03-20 17:59:00 -0400126 self.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 Lisionb7ee3ed2015-03-20 18:04:35 -0400162- (IBAction)moveUp:(id)sender {
163 AccountModel::instance()->moveUp();
164}
165
166- (IBAction)moveDown:(id)sender {
167 AccountModel::instance()->moveDown();
168}
169
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400170- (IBAction)removeAccount:(id)sender {
171
172 if(treeController.selectedNodes.count > 0) {
173 QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
174 AccountModel::instance()->remove(qIdx);
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400175 AccountModel::instance()->save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400176 }
177}
178- (IBAction)addAccount:(id)sender {
179 QModelIndex qIdx = AccountModel::instance()->protocolModel()->selectionModel()->currentIndex();
180
Alexandre Lision81c97212015-06-17 15:51:53 -0400181 auto newAccName = [[NSString alloc] initWithFormat:@"%@ account",
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400182 AccountModel::instance()->protocolModel()->data(qIdx, Qt::DisplayRole).toString().toNSString(), nil];
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400183 auto acc = AccountModel::instance()->add([newAccName UTF8String], qIdx);
184 acc->setDisplayName(acc->alias());
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400185 AccountModel::instance()->save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186}
187
188- (IBAction)protocolSelectedChanged:(id)sender {
189
190 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400191 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400192 AccountModel::instance()->protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400193 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400194
195}
196
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400197- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400198{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400199 // Start by removing all tabs
200 for(NSTabViewItem* item in configPanels.tabViewItems) {
201 [configPanels removeTabViewItem:item];
202 }
203
204 [configPanels insertTabViewItem:generalTabItem atIndex:0];
205 [configPanels insertTabViewItem:audioTabItem atIndex:1];
206 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400207 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
208 [configPanels insertTabViewItem:securityTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400209}
210
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400211- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400212{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400213 // Start by removing all tabs
214 for(NSTabViewItem* item in configPanels.tabViewItems) {
215 [configPanels removeTabViewItem:item];
216 }
217
218 [configPanels insertTabViewItem:generalTabItem atIndex:0];
219 [configPanels insertTabViewItem:audioTabItem atIndex:1];
220 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400221}
222
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400223- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400224{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400225 // Start by removing all tabs
226 for(NSTabViewItem* item in configPanels.tabViewItems) {
227 [configPanels removeTabViewItem:item];
228 }
229
230 [configPanels insertTabViewItem:ringTabItem atIndex:0];
231 [configPanels insertTabViewItem:audioTabItem atIndex:1];
232 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400233}
234
235- (IBAction)toggleAccount:(NSOutlineView*)sender {
236
237 if([sender clickedColumn] < 0)
238 return;
239
240 NSTableColumn* col = [sender.tableColumns objectAtIndex:[sender clickedColumn]];
241 if([col.identifier isEqualToString:COLUMNID_ENABLE]) {
242 NSInteger row = [sender clickedRow];
243 QModelIndex accIdx = AccountModel::instance()->index(row);
244 Account* toToggle = AccountModel::instance()->getAccountByModelIndex(accIdx);
245 NSButtonCell *cell = [col dataCellForRow:row];
246 toToggle->setEnabled(cell.state == NSOnState ? NO : YES);
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400247 toToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400248 }
249}
250
251#pragma mark - NSOutlineViewDelegate methods
252
253// -------------------------------------------------------------------------------
254// shouldSelectItem:item
255// -------------------------------------------------------------------------------
256- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
257{
258 return YES;
259}
260
261// -------------------------------------------------------------------------------
262// dataCellForTableColumn:tableColumn:item
263// -------------------------------------------------------------------------------
264- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
265{
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400266 NSCell *returnCell;
267
268 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
269 // Prevent user from enabling/disabling IP2IP account
270 if ([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE] &&
271 AccountModel::instance()->ip2ip()->index() == qIdx) {
272
Alexandre Lision81c97212015-06-17 15:51:53 -0400273 return [[NSCell alloc] init];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400274 } else {
Alexandre Lision81c97212015-06-17 15:51:53 -0400275 return [tableColumn dataCell];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400276 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400277}
278
279// -------------------------------------------------------------------------------
280// textShouldEndEditing:fieldEditor
281// -------------------------------------------------------------------------------
282- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
283{
284 if ([[fieldEditor string] length] == 0)
285 {
286 // don't allow empty node names
287 return NO;
288 }
289 else
290 {
291 return YES;
292 }
293}
294
295// -------------------------------------------------------------------------------
296// shouldEditTableColumn:tableColumn:item
297//
298// Decide to allow the edit of the given outline view "item".
299// -------------------------------------------------------------------------------
300- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
301{
302 return NO;
303}
304
305// -------------------------------------------------------------------------------
306// outlineView:willDisplayCell:forTableColumn:item
307// -------------------------------------------------------------------------------
308- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
309{
310 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
311 if(!qIdx.isValid())
312 return;
313
314 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
315 {
316 cell.title = AccountModel::instance()->data(qIdx, Qt::DisplayRole).toString().toNSString();
317 } else if([[tableColumn identifier] isEqualToString:COLUMNID_STATE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400318 NSTextFieldCell* stateCell = cell;
319 Account::RegistrationState state = qvariant_cast<Account::RegistrationState>(qIdx.data((int)Account::Role::RegistrationState));
Alexandre Lision2528d802015-03-20 18:05:06 -0400320 switch (state) {
321 case Account::RegistrationState::READY:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400322 [stateCell setTextColor:[NSColor colorWithCalibratedRed:116/255.0 green:179/255.0 blue:93/255.0 alpha:1.0]];
323 [stateCell setTitle:@"Ready"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400324 break;
325 case Account::RegistrationState::TRYING:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400326 [stateCell setTextColor:[NSColor redColor]];
327 [stateCell setTitle:@"Trying..."];
Alexandre Lision2528d802015-03-20 18:05:06 -0400328 break;
329 case Account::RegistrationState::UNREGISTERED:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400330 [stateCell setTextColor:[NSColor blackColor]];
331 [stateCell setTitle:@"Unregistered"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400332 break;
333 case Account::RegistrationState::ERROR:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400334 [stateCell setTextColor:[NSColor redColor]];
335 [stateCell setTitle:@"Error"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400336 break;
337 default:
338 break;
339 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400340 } else if([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400341 [cell setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400342 }
343}
344
345// -------------------------------------------------------------------------------
346// outlineViewSelectionDidChange:notification
347// -------------------------------------------------------------------------------
348- (void)outlineViewSelectionDidChange:(NSNotification *)notification
349{
350 // ask the tree controller for the current selection
351 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400352 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400353 //Update details view
Alexandre Lision81c97212015-06-17 15:51:53 -0400354 auto acc = AccountModel::instance()->getAccountByModelIndex(qIdx);
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400355 AccountModel::instance()->selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400356
357 switch (acc->protocol()) {
358 case Account::Protocol::SIP:
359 NSLog(@"SIP");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400360 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400361 break;
362 case Account::Protocol::IAX:
363 NSLog(@"IAX");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400364 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400365 break;
366 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400367 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400368 NSLog(@"DRING");
369 break;
370 default:
371 break;
372 }
373
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400374 [self.accountDetailsView setHidden:NO];
375 } else {
376 [self.accountDetailsView setHidden:YES];
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400377 AccountModel::instance()->selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400378 }
379}
380
381#pragma mark - NSMenuDelegate methods
382
383- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
384{
Alexandre Lision53428a52015-03-20 17:59:00 -0400385 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
386 QModelIndex qIdx = AccountModel::instance()->protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400387 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400388
389 return YES;
390}
391
392- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
393{
Alexandre Lision53428a52015-03-20 17:59:00 -0400394 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400395}
396
397
398
399@end