blob: 168e59b79c7bfe7bfc849e9b97c1b184f1c6791c [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 Lision81c97212015-06-17 15:51:53 -0400183 AccountModel::instance()->add([newAccName UTF8String], qIdx);
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400184 AccountModel::instance()->save();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400185}
186
187- (IBAction)protocolSelectedChanged:(id)sender {
188
189 int index = [sender indexOfSelectedItem];
Alexandre Lision53428a52015-03-20 17:59:00 -0400190 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400191 AccountModel::instance()->protocolModel()->selectionModel()->setCurrentIndex(
Alexandre Lision53428a52015-03-20 17:59:00 -0400192 proxyProtocolModel->mapToSource(proxyIdx), QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400193
194}
195
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400196- (void) setupSIPPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400197{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400198 // Start by removing all tabs
199 for(NSTabViewItem* item in configPanels.tabViewItems) {
200 [configPanels removeTabViewItem:item];
201 }
202
203 [configPanels insertTabViewItem:generalTabItem atIndex:0];
204 [configPanels insertTabViewItem:audioTabItem atIndex:1];
205 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400206 [configPanels insertTabViewItem:advancedTabItem atIndex:3];
207 [configPanels insertTabViewItem:securityTabItem atIndex:4];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400208}
209
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400210- (void) setupIAXPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400211{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400212 // Start by removing all tabs
213 for(NSTabViewItem* item in configPanels.tabViewItems) {
214 [configPanels removeTabViewItem:item];
215 }
216
217 [configPanels insertTabViewItem:generalTabItem atIndex:0];
218 [configPanels insertTabViewItem:audioTabItem atIndex:1];
219 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400220}
221
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400222- (void) setupRINGPanels
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400223{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400224 // Start by removing all tabs
225 for(NSTabViewItem* item in configPanels.tabViewItems) {
226 [configPanels removeTabViewItem:item];
227 }
228
229 [configPanels insertTabViewItem:ringTabItem atIndex:0];
230 [configPanels insertTabViewItem:audioTabItem atIndex:1];
231 [configPanels insertTabViewItem:videoTabItem atIndex:2];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400232}
233
234- (IBAction)toggleAccount:(NSOutlineView*)sender {
235
236 if([sender clickedColumn] < 0)
237 return;
238
239 NSTableColumn* col = [sender.tableColumns objectAtIndex:[sender clickedColumn]];
240 if([col.identifier isEqualToString:COLUMNID_ENABLE]) {
241 NSInteger row = [sender clickedRow];
242 QModelIndex accIdx = AccountModel::instance()->index(row);
243 Account* toToggle = AccountModel::instance()->getAccountByModelIndex(accIdx);
244 NSButtonCell *cell = [col dataCellForRow:row];
245 toToggle->setEnabled(cell.state == NSOnState ? NO : YES);
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400246 toToggle << Account::EditAction::SAVE;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400247 }
248}
249
250#pragma mark - NSOutlineViewDelegate methods
251
252// -------------------------------------------------------------------------------
253// shouldSelectItem:item
254// -------------------------------------------------------------------------------
255- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
256{
257 return YES;
258}
259
260// -------------------------------------------------------------------------------
261// dataCellForTableColumn:tableColumn:item
262// -------------------------------------------------------------------------------
263- (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
264{
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400265 NSCell *returnCell;
266
267 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
268 // Prevent user from enabling/disabling IP2IP account
269 if ([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE] &&
270 AccountModel::instance()->ip2ip()->index() == qIdx) {
271
Alexandre Lision81c97212015-06-17 15:51:53 -0400272 return [[NSCell alloc] init];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400273 } else {
Alexandre Lision81c97212015-06-17 15:51:53 -0400274 return [tableColumn dataCell];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400275 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400276}
277
278// -------------------------------------------------------------------------------
279// textShouldEndEditing:fieldEditor
280// -------------------------------------------------------------------------------
281- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
282{
283 if ([[fieldEditor string] length] == 0)
284 {
285 // don't allow empty node names
286 return NO;
287 }
288 else
289 {
290 return YES;
291 }
292}
293
294// -------------------------------------------------------------------------------
295// shouldEditTableColumn:tableColumn:item
296//
297// Decide to allow the edit of the given outline view "item".
298// -------------------------------------------------------------------------------
299- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
300{
301 return NO;
302}
303
304// -------------------------------------------------------------------------------
305// outlineView:willDisplayCell:forTableColumn:item
306// -------------------------------------------------------------------------------
307- (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
308{
309 QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
310 if(!qIdx.isValid())
311 return;
312
313 if ([[tableColumn identifier] isEqualToString:COLUMNID_NAME])
314 {
315 cell.title = AccountModel::instance()->data(qIdx, Qt::DisplayRole).toString().toNSString();
316 } else if([[tableColumn identifier] isEqualToString:COLUMNID_STATE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400317 NSTextFieldCell* stateCell = cell;
318 Account::RegistrationState state = qvariant_cast<Account::RegistrationState>(qIdx.data((int)Account::Role::RegistrationState));
Alexandre Lision2528d802015-03-20 18:05:06 -0400319 switch (state) {
320 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]];
322 [stateCell setTitle:@"Ready"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400323 break;
324 case Account::RegistrationState::TRYING:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400325 [stateCell setTextColor:[NSColor redColor]];
326 [stateCell setTitle:@"Trying..."];
Alexandre Lision2528d802015-03-20 18:05:06 -0400327 break;
328 case Account::RegistrationState::UNREGISTERED:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400329 [stateCell setTextColor:[NSColor blackColor]];
330 [stateCell setTitle:@"Unregistered"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400331 break;
332 case Account::RegistrationState::ERROR:
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400333 [stateCell setTextColor:[NSColor redColor]];
334 [stateCell setTitle:@"Error"];
Alexandre Lision2528d802015-03-20 18:05:06 -0400335 break;
336 default:
337 break;
338 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400339 } else if([[tableColumn identifier] isEqualToString:COLUMNID_ENABLE]) {
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400340 [cell setState:qIdx.data(Qt::CheckStateRole).value<BOOL>()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400341 }
342}
343
344// -------------------------------------------------------------------------------
345// outlineViewSelectionDidChange:notification
346// -------------------------------------------------------------------------------
347- (void)outlineViewSelectionDidChange:(NSNotification *)notification
348{
349 // ask the tree controller for the current selection
350 if([[treeController selectedNodes] count] > 0) {
Alexandre Lision81c97212015-06-17 15:51:53 -0400351 auto qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400352 //Update details view
Alexandre Lision81c97212015-06-17 15:51:53 -0400353 auto acc = AccountModel::instance()->getAccountByModelIndex(qIdx);
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400354 AccountModel::instance()->selectionModel()->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400355
356 switch (acc->protocol()) {
357 case Account::Protocol::SIP:
358 NSLog(@"SIP");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400359 [self setupSIPPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400360 break;
361 case Account::Protocol::IAX:
362 NSLog(@"IAX");
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400363 [self setupIAXPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400364 break;
365 case Account::Protocol::RING:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400366 [self setupRINGPanels];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400367 NSLog(@"DRING");
368 break;
369 default:
370 break;
371 }
372
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400373 [self.accountDetailsView setHidden:NO];
374 } else {
375 [self.accountDetailsView setHidden:YES];
Alexandre Lisionb7ee3ed2015-03-20 18:04:35 -0400376 AccountModel::instance()->selectionModel()->clearCurrentIndex();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400377 }
378}
379
380#pragma mark - NSMenuDelegate methods
381
382- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
383{
Alexandre Lision53428a52015-03-20 17:59:00 -0400384 QModelIndex proxyIdx = proxyProtocolModel->index(index, 0);
385 QModelIndex qIdx = AccountModel::instance()->protocolModel()->index(proxyProtocolModel->mapToSource(proxyIdx).row());
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400386 [item setTitle:qIdx.data(Qt::DisplayRole).toString().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400387
388 return YES;
389}
390
391- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
392{
Alexandre Lision53428a52015-03-20 17:59:00 -0400393 return proxyProtocolModel->rowCount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400394}
395
396
397
398@end