blob: 7559041d048b5b74ab0738da5139b7bbebf1751d [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -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 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050030#import "RingWindowController.h"
31
Alexandre Lision5855b6a2015-02-03 11:31:05 -050032#import <accountmodel.h>
33#import <callmodel.h>
34#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040035#import <call.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050036
Alexandre Lision745e4d62015-03-22 20:03:10 -040037#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040038#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040039#import "CurrentCallVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040040
Alexandre Lision5855b6a2015-02-03 11:31:05 -050041@interface RingWindowController ()
42
Alexandre Lisionc5148052015-03-04 15:10:35 -050043@property NSSearchField* callField;
Alexandre Lision58cab672015-06-09 15:25:40 -040044@property CurrentCallVC* currentVC;
45@property (unsafe_unretained) IBOutlet NSView *callView;
46
Alexandre Lision5855b6a2015-02-03 11:31:05 -050047
48@end
49
50@implementation RingWindowController
Alexandre Lisionc5148052015-03-04 15:10:35 -050051@synthesize callField;
Alexandre Lision58cab672015-06-09 15:25:40 -040052@synthesize currentVC;
53@synthesize callView;
54
Alexandre Lisionc5148052015-03-04 15:10:35 -050055static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier";
56static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
57static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier";
58
Alexandre Lision5855b6a2015-02-03 11:31:05 -050059- (void)windowDidLoad {
60 [super windowDidLoad];
Alexandre Lision745e4d62015-03-22 20:03:10 -040061 [self.window setReleasedWhenClosed:FALSE];
Alexandre Lisionc5148052015-03-04 15:10:35 -050062 [self displayMainToolBar];
Alexandre Lision58cab672015-06-09 15:25:40 -040063
64 currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
65 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
66 [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
67
68 [callView addSubview:[self.currentVC view]];
69 [currentVC initFrame];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050070}
71
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050072- (IBAction)openPreferences:(id)sender
73{
Alexandre Lision1154eb32015-03-20 18:31:48 -040074 if(self.preferencesViewController != nil) {
75 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050076 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -040077 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050078 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
79
Alexandre Lision4ba18022015-04-23 12:17:40 -040080 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050081 self.myCurrentViewController = self.preferencesViewController;
82
83 NSLayoutConstraint* test = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
84 attribute:NSLayoutAttributeWidth
85 relatedBy:NSLayoutRelationEqual
86 toItem:currentView
87 attribute:NSLayoutAttributeWidth
88 multiplier:1.0f
89 constant:0.0f];
90
91 NSLayoutConstraint* test2 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
92 attribute:NSLayoutAttributeHeight
93 relatedBy:NSLayoutRelationEqual
94 toItem:currentView
95 attribute:NSLayoutAttributeHeight
96 multiplier:1.0f
97 constant:0.0f];
98
99 NSLayoutConstraint* test3 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
100 attribute:NSLayoutAttributeCenterX
101 relatedBy:NSLayoutRelationEqual
102 toItem:currentView
103 attribute:NSLayoutAttributeCenterX
104 multiplier:1.0f
105 constant:0.0f];
106
107
108 [currentView addSubview:[self.preferencesViewController view]];
109
110 [tb setDelegate: self.preferencesViewController];
111 [self.window setToolbar: tb];
112
113 [self.window.toolbar setSelectedItemIdentifier:@"GeneralPrefsIdentifier"];
114
115 [currentView addConstraint:test];
116 [currentView addConstraint:test2];
117 [currentView addConstraint:test3];
118 // make sure we automatically resize the controller's view to the current window size
119 [[self.myCurrentViewController view] setFrame:[currentView bounds]];
120
121 // set the view controller's represented object to the number of subviews in that controller
122 // (our NSTextField's value binding will reflect this value)
123 [self.myCurrentViewController setRepresentedObject:[NSNumber numberWithUnsignedInteger:[[[self.myCurrentViewController view] subviews] count]]];
124
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500125}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500126
127- (IBAction) closePreferences:(NSToolbarItem *)sender {
128 if(self.myCurrentViewController != nil)
129 {
130 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500131 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500132 self.preferencesViewController = nil;
133 }
134}
135
Alexandre Lisionc5148052015-03-04 15:10:35 -0500136-(void) displayMainToolBar
137{
138 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"MainToolbar"];
139 [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel];
140 [tb setDelegate: self];
141 [self.window setToolbar: tb];
142}
143
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500144// FIXME: This is sick, NSWindowController is catching my selectors
145- (void)displayGeneral:(NSToolbarItem *)sender {
146 [self.preferencesViewController displayGeneral:sender];
147}
148
149- (void)displayAudio:(NSToolbarItem *)sender {
150 [self.preferencesViewController displayAudio:sender];
151}
152
153- (void)displayAncrage:(NSToolbarItem *)sender {
154 [self.preferencesViewController displayAncrage:sender];
155}
156
157- (void)displayVideo:(NSToolbarItem *)sender {
158 [self.preferencesViewController displayVideo:sender];
159}
160
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400161- (void)displayAccounts:(NSToolbarItem *)sender {
162 [self.preferencesViewController displayAccounts:sender];
163}
164
Alexandre Lision1154eb32015-03-20 18:31:48 -0400165- (void)togglePowerSettings:(id)sender
166{
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400167 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced];
168 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400169 [[NSUserDefaults standardUserDefaults] synchronize];
170
171 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
172 [tb setDelegate: self.preferencesViewController];
173 [self.preferencesViewController displayGeneral:nil];
174 [self.window setToolbar:tb];
175}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400176
Alexandre Lisionc5148052015-03-04 15:10:35 -0500177#pragma NSToolbar Delegate
178
179-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
180{
181 NSToolbarItem* item = nil;
182
183 if ([itemIdentifier isEqualToString: kSearchViewIdentifier]) {
184 item = [[NSToolbarItem alloc] initWithItemIdentifier: kSearchViewIdentifier];
185 callField = [[NSSearchField alloc] initWithFrame:NSMakeRect(0,0,400,21)];
186 [[callField cell] setSearchButtonCell:nil];
187 [callField setToolTip:@"Call"];
Alexandre Lision25bcd482015-03-22 22:49:12 -0400188 [callField setAlignment:NSCenterTextAlignment];
189 [callField setDelegate:self];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500190 [item setView:callField];
191 }
192
193 if ([itemIdentifier isEqualToString: kCallButtonIdentifer]) {
194 item = [[NSToolbarItem alloc] initWithItemIdentifier: kCallButtonIdentifer];
195
196 NSButton *callButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,80,30)];
197
198 [callButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
199 [callButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want]
200 [callButton setBordered:YES];
201 [callButton setTitle:@"Call"];
202 [item setView:callButton];
203 [item setAction:@selector(placeCall:)];
204 }
205
206 if ([itemIdentifier isEqualToString: kPreferencesIdentifier]) {
207 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPreferencesIdentifier];
208 [item setImage: [NSImage imageNamed: @"NSAdvanced"]];
209 [item setLabel: @"Settings"];
210 [item setAction:@selector(openPreferences:)];
211 }
212
213 return item;
Alexandre Lision25bcd482015-03-22 22:49:12 -0400214}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500215
Alexandre Lision25bcd482015-03-22 22:49:12 -0400216- (IBAction)placeCall:(id)sender
217{
218 Call* c = CallModel::instance()->dialingCall();
Alexandre Lision0a22c8f2015-03-29 21:40:06 -0400219 // check for a valid ring hash
220 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
221 BOOL valid = [[[callField stringValue] stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
222
223 if(valid && callField.stringValue.length == 40) {
224 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",[callField stringValue]]));
225 } else {
226 c->setDialNumber(QString::fromNSString([callField stringValue]));
227 }
228
Alexandre Lision25bcd482015-03-22 22:49:12 -0400229 c << Call::Action::ACCEPT;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500230}
231
Alexandre Lisionc5148052015-03-04 15:10:35 -0500232-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
233{
234 return [NSArray arrayWithObjects:
235 NSToolbarSpaceItemIdentifier,
236 NSToolbarFlexibleSpaceItemIdentifier,
237 NSToolbarSpaceItemIdentifier,
238 NSToolbarSpaceItemIdentifier,
239 kSearchViewIdentifier,
240 kCallButtonIdentifer,
241 NSToolbarFlexibleSpaceItemIdentifier,
242 kPreferencesIdentifier,
243 nil];
244}
245
246-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
247{
248 return [NSArray arrayWithObjects:
249 kSearchViewIdentifier,
250 kCallButtonIdentifer,
251 kPreferencesIdentifier,
252 nil];
253}
254
255-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
256{
257 return nil;
258}
259
Alexandre Lision25bcd482015-03-22 22:49:12 -0400260#pragma NSTextField Delegate
261
262- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
263{
Alexandre Lision25bcd482015-03-22 22:49:12 -0400264 if (commandSelector == @selector(insertNewline:)) {
265 if([[callField stringValue] isNotEqualTo:@""]) {
266 [self placeCall:nil];
267 return YES;
268 }
269 }
270
271 return NO;
272}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500273
274
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500275@end