blob: e317dc970a7ab536f21ed66921a3be8361187732 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lisionbb5c2462015-07-30 12:55:37 -04002 * Copyright (C) 2015 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -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 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050030#import "RingWindowController.h"
31
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040032//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050033#import <accountmodel.h>
34#import <callmodel.h>
35#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040036#import <call.h>
Alexandre Lision2db8f472015-07-22 15:05:46 -040037#import <personmodel.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050038
Alexandre Lision745e4d62015-03-22 20:03:10 -040039#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040040#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040041#import "CurrentCallVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040042
Alexandre Lision2db8f472015-07-22 15:05:46 -040043#import "backends/AddressBookBackend.h"
44
Alexandre Lision5855b6a2015-02-03 11:31:05 -050045@interface RingWindowController ()
46
Alexandre Lisionc5148052015-03-04 15:10:35 -050047@property NSSearchField* callField;
Alexandre Lision58cab672015-06-09 15:25:40 -040048@property CurrentCallVC* currentVC;
49@property (unsafe_unretained) IBOutlet NSView *callView;
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040050@property (unsafe_unretained) IBOutlet NSTextField *ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040051
Alexandre Lision5855b6a2015-02-03 11:31:05 -050052
53@end
54
55@implementation RingWindowController
Alexandre Lisionc5148052015-03-04 15:10:35 -050056@synthesize callField;
Alexandre Lision58cab672015-06-09 15:25:40 -040057@synthesize currentVC;
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040058@synthesize callView, ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040059
Alexandre Lisionc5148052015-03-04 15:10:35 -050060static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier";
61static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
62static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier";
63
Alexandre Lision5855b6a2015-02-03 11:31:05 -050064- (void)windowDidLoad {
65 [super windowDidLoad];
Alexandre Lision745e4d62015-03-22 20:03:10 -040066 [self.window setReleasedWhenClosed:FALSE];
Alexandre Lisionc5148052015-03-04 15:10:35 -050067 [self displayMainToolBar];
Alexandre Lision58cab672015-06-09 15:25:40 -040068
69 currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
70 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
71 [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
72
Alexandre Lision2db8f472015-07-22 15:05:46 -040073
74 PersonModel::instance()->addCollection<AddressBookBackend>(LoadOptions::FORCE_ENABLED);
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040075 [callView addSubview:[self.currentVC view] positioned:NSWindowAbove relativeTo:nil];
76
Alexandre Lision58cab672015-06-09 15:25:40 -040077 [currentVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040078
79 // Update Ring ID label based on account model changes
80 QObject::connect(AccountModel::instance(),
81 &AccountModel::dataChanged,
82 [=] {
83 [self updateRingID];
84 });
85}
86
87/**
88 * Implement the necessary logic to choose which Ring ID to display.
89 * This tries to choose the "best" ID to show
90 */
91- (void) updateRingID
92{
93 Account* registered = nullptr;
94 Account* enabled = nullptr;
95 Account* finalChoice = nullptr;
96
97 [ringIDLabel setStringValue:@""];
98 auto ringList = AccountModel::instance()->getAccountsByProtocol(Account::Protocol::RING);
99 for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
100 Account* acc = ringList.value(i);
101 if (acc->isEnabled()) {
102 if(!enabled)
103 enabled = finalChoice = acc;
104 if (acc->registrationState() == Account::RegistrationState::READY) {
105 registered = enabled = finalChoice = acc;
106 }
107 } else {
108 if (!finalChoice)
109 finalChoice = acc;
110 }
111 }
112
Alexandre Lision7f8351b2015-08-20 11:43:37 -0400113 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500114}
115
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500116- (IBAction)openPreferences:(id)sender
117{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400118 if(self.preferencesViewController != nil) {
119 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500120 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -0400121 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500122 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
123
Alexandre Lision4ba18022015-04-23 12:17:40 -0400124 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500125 self.myCurrentViewController = self.preferencesViewController;
126
127 NSLayoutConstraint* test = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
128 attribute:NSLayoutAttributeWidth
129 relatedBy:NSLayoutRelationEqual
130 toItem:currentView
131 attribute:NSLayoutAttributeWidth
132 multiplier:1.0f
133 constant:0.0f];
134
135 NSLayoutConstraint* test2 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
136 attribute:NSLayoutAttributeHeight
137 relatedBy:NSLayoutRelationEqual
138 toItem:currentView
139 attribute:NSLayoutAttributeHeight
140 multiplier:1.0f
141 constant:0.0f];
142
143 NSLayoutConstraint* test3 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
144 attribute:NSLayoutAttributeCenterX
145 relatedBy:NSLayoutRelationEqual
146 toItem:currentView
147 attribute:NSLayoutAttributeCenterX
148 multiplier:1.0f
149 constant:0.0f];
150
151
152 [currentView addSubview:[self.preferencesViewController view]];
153
154 [tb setDelegate: self.preferencesViewController];
155 [self.window setToolbar: tb];
156
157 [self.window.toolbar setSelectedItemIdentifier:@"GeneralPrefsIdentifier"];
158
159 [currentView addConstraint:test];
160 [currentView addConstraint:test2];
161 [currentView addConstraint:test3];
162 // make sure we automatically resize the controller's view to the current window size
163 [[self.myCurrentViewController view] setFrame:[currentView bounds]];
164
165 // set the view controller's represented object to the number of subviews in that controller
166 // (our NSTextField's value binding will reflect this value)
167 [self.myCurrentViewController setRepresentedObject:[NSNumber numberWithUnsignedInteger:[[[self.myCurrentViewController view] subviews] count]]];
168
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500169}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500170
171- (IBAction) closePreferences:(NSToolbarItem *)sender {
172 if(self.myCurrentViewController != nil)
173 {
174 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500175 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500176 self.preferencesViewController = nil;
177 }
178}
179
Alexandre Lisionc5148052015-03-04 15:10:35 -0500180-(void) displayMainToolBar
181{
182 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"MainToolbar"];
183 [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel];
184 [tb setDelegate: self];
185 [self.window setToolbar: tb];
186}
187
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500188// FIXME: This is sick, NSWindowController is catching my selectors
189- (void)displayGeneral:(NSToolbarItem *)sender {
190 [self.preferencesViewController displayGeneral:sender];
191}
192
193- (void)displayAudio:(NSToolbarItem *)sender {
194 [self.preferencesViewController displayAudio:sender];
195}
196
197- (void)displayAncrage:(NSToolbarItem *)sender {
198 [self.preferencesViewController displayAncrage:sender];
199}
200
201- (void)displayVideo:(NSToolbarItem *)sender {
202 [self.preferencesViewController displayVideo:sender];
203}
204
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400205- (void)displayAccounts:(NSToolbarItem *)sender {
206 [self.preferencesViewController displayAccounts:sender];
207}
208
Alexandre Lision1154eb32015-03-20 18:31:48 -0400209- (void)togglePowerSettings:(id)sender
210{
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400211 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced];
212 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400213 [[NSUserDefaults standardUserDefaults] synchronize];
214
215 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
216 [tb setDelegate: self.preferencesViewController];
217 [self.preferencesViewController displayGeneral:nil];
218 [self.window setToolbar:tb];
219}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400220
Alexandre Lisionc5148052015-03-04 15:10:35 -0500221#pragma NSToolbar Delegate
222
223-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
224{
225 NSToolbarItem* item = nil;
226
227 if ([itemIdentifier isEqualToString: kSearchViewIdentifier]) {
228 item = [[NSToolbarItem alloc] initWithItemIdentifier: kSearchViewIdentifier];
229 callField = [[NSSearchField alloc] initWithFrame:NSMakeRect(0,0,400,21)];
230 [[callField cell] setSearchButtonCell:nil];
231 [callField setToolTip:@"Call"];
Alexandre Lision25bcd482015-03-22 22:49:12 -0400232 [callField setAlignment:NSCenterTextAlignment];
233 [callField setDelegate:self];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500234 [item setView:callField];
235 }
236
237 if ([itemIdentifier isEqualToString: kCallButtonIdentifer]) {
238 item = [[NSToolbarItem alloc] initWithItemIdentifier: kCallButtonIdentifer];
239
240 NSButton *callButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,80,30)];
241
242 [callButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
243 [callButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want]
244 [callButton setBordered:YES];
245 [callButton setTitle:@"Call"];
246 [item setView:callButton];
247 [item setAction:@selector(placeCall:)];
248 }
249
250 if ([itemIdentifier isEqualToString: kPreferencesIdentifier]) {
251 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPreferencesIdentifier];
252 [item setImage: [NSImage imageNamed: @"NSAdvanced"]];
253 [item setLabel: @"Settings"];
254 [item setAction:@selector(openPreferences:)];
255 }
256
257 return item;
Alexandre Lision25bcd482015-03-22 22:49:12 -0400258}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500259
Alexandre Lision25bcd482015-03-22 22:49:12 -0400260- (IBAction)placeCall:(id)sender
261{
262 Call* c = CallModel::instance()->dialingCall();
Alexandre Lision0a22c8f2015-03-29 21:40:06 -0400263 // check for a valid ring hash
264 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
265 BOOL valid = [[[callField stringValue] stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
266
267 if(valid && callField.stringValue.length == 40) {
268 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",[callField stringValue]]));
269 } else {
270 c->setDialNumber(QString::fromNSString([callField stringValue]));
271 }
272
Alexandre Lision25bcd482015-03-22 22:49:12 -0400273 c << Call::Action::ACCEPT;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500274}
275
Alexandre Lisionc5148052015-03-04 15:10:35 -0500276-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
277{
278 return [NSArray arrayWithObjects:
279 NSToolbarSpaceItemIdentifier,
280 NSToolbarFlexibleSpaceItemIdentifier,
281 NSToolbarSpaceItemIdentifier,
282 NSToolbarSpaceItemIdentifier,
283 kSearchViewIdentifier,
284 kCallButtonIdentifer,
285 NSToolbarFlexibleSpaceItemIdentifier,
286 kPreferencesIdentifier,
287 nil];
288}
289
290-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
291{
292 return [NSArray arrayWithObjects:
293 kSearchViewIdentifier,
294 kCallButtonIdentifer,
295 kPreferencesIdentifier,
296 nil];
297}
298
299-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
300{
301 return nil;
302}
303
Alexandre Lision25bcd482015-03-22 22:49:12 -0400304#pragma NSTextField Delegate
305
306- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
307{
Alexandre Lision25bcd482015-03-22 22:49:12 -0400308 if (commandSelector == @selector(insertNewline:)) {
309 if([[callField stringValue] isNotEqualTo:@""]) {
310 [self placeCall:nil];
311 return YES;
312 }
313 }
314
315 return NO;
316}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500317
318
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500319@end