blob: 48a5c0c0eda48bd3de67bb3340931ce5eff1a613 [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"
38
Alexandre Lision5855b6a2015-02-03 11:31:05 -050039@interface RingWindowController ()
40
Alexandre Lisionc5148052015-03-04 15:10:35 -050041@property NSSearchField* callField;
Alexandre Lision5855b6a2015-02-03 11:31:05 -050042
43@end
44
45@implementation RingWindowController
Alexandre Lisionc5148052015-03-04 15:10:35 -050046@synthesize callField;
47static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier";
48static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
49static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier";
50
Alexandre Lision5855b6a2015-02-03 11:31:05 -050051- (void)windowDidLoad {
52 [super windowDidLoad];
Alexandre Lision745e4d62015-03-22 20:03:10 -040053 [self.window setReleasedWhenClosed:FALSE];
Alexandre Lisionc5148052015-03-04 15:10:35 -050054 [self displayMainToolBar];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050055}
56
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050057- (IBAction)openPreferences:(id)sender
58{
Alexandre Lision1154eb32015-03-20 18:31:48 -040059 if(self.preferencesViewController != nil) {
60 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050061 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -040062 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050063 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
64
Alexandre Lision4ba18022015-04-23 12:17:40 -040065 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050066 self.myCurrentViewController = self.preferencesViewController;
67
68 NSLayoutConstraint* test = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
69 attribute:NSLayoutAttributeWidth
70 relatedBy:NSLayoutRelationEqual
71 toItem:currentView
72 attribute:NSLayoutAttributeWidth
73 multiplier:1.0f
74 constant:0.0f];
75
76 NSLayoutConstraint* test2 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
77 attribute:NSLayoutAttributeHeight
78 relatedBy:NSLayoutRelationEqual
79 toItem:currentView
80 attribute:NSLayoutAttributeHeight
81 multiplier:1.0f
82 constant:0.0f];
83
84 NSLayoutConstraint* test3 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
85 attribute:NSLayoutAttributeCenterX
86 relatedBy:NSLayoutRelationEqual
87 toItem:currentView
88 attribute:NSLayoutAttributeCenterX
89 multiplier:1.0f
90 constant:0.0f];
91
92
93 [currentView addSubview:[self.preferencesViewController view]];
94
95 [tb setDelegate: self.preferencesViewController];
96 [self.window setToolbar: tb];
97
98 [self.window.toolbar setSelectedItemIdentifier:@"GeneralPrefsIdentifier"];
99
100 [currentView addConstraint:test];
101 [currentView addConstraint:test2];
102 [currentView addConstraint:test3];
103 // make sure we automatically resize the controller's view to the current window size
104 [[self.myCurrentViewController view] setFrame:[currentView bounds]];
105
106 // set the view controller's represented object to the number of subviews in that controller
107 // (our NSTextField's value binding will reflect this value)
108 [self.myCurrentViewController setRepresentedObject:[NSNumber numberWithUnsignedInteger:[[[self.myCurrentViewController view] subviews] count]]];
109
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500110}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500111
112- (IBAction) closePreferences:(NSToolbarItem *)sender {
113 if(self.myCurrentViewController != nil)
114 {
115 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500116 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500117 self.preferencesViewController = nil;
118 }
119}
120
Alexandre Lisionc5148052015-03-04 15:10:35 -0500121-(void) displayMainToolBar
122{
123 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"MainToolbar"];
124 [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel];
125 [tb setDelegate: self];
126 [self.window setToolbar: tb];
127}
128
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500129// FIXME: This is sick, NSWindowController is catching my selectors
130- (void)displayGeneral:(NSToolbarItem *)sender {
131 [self.preferencesViewController displayGeneral:sender];
132}
133
134- (void)displayAudio:(NSToolbarItem *)sender {
135 [self.preferencesViewController displayAudio:sender];
136}
137
138- (void)displayAncrage:(NSToolbarItem *)sender {
139 [self.preferencesViewController displayAncrage:sender];
140}
141
142- (void)displayVideo:(NSToolbarItem *)sender {
143 [self.preferencesViewController displayVideo:sender];
144}
145
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400146- (void)displayAccounts:(NSToolbarItem *)sender {
147 [self.preferencesViewController displayAccounts:sender];
148}
149
Alexandre Lision1154eb32015-03-20 18:31:48 -0400150- (void)togglePowerSettings:(id)sender
151{
152 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"];
153 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:@"show_advanced"];
154 [[NSUserDefaults standardUserDefaults] synchronize];
155
156 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
157 [tb setDelegate: self.preferencesViewController];
158 [self.preferencesViewController displayGeneral:nil];
159 [self.window setToolbar:tb];
160}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400161
Alexandre Lisionc5148052015-03-04 15:10:35 -0500162#pragma NSToolbar Delegate
163
164-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
165{
166 NSToolbarItem* item = nil;
167
168 if ([itemIdentifier isEqualToString: kSearchViewIdentifier]) {
169 item = [[NSToolbarItem alloc] initWithItemIdentifier: kSearchViewIdentifier];
170 callField = [[NSSearchField alloc] initWithFrame:NSMakeRect(0,0,400,21)];
171 [[callField cell] setSearchButtonCell:nil];
172 [callField setToolTip:@"Call"];
Alexandre Lision25bcd482015-03-22 22:49:12 -0400173 [callField setAlignment:NSCenterTextAlignment];
174 [callField setDelegate:self];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500175 [item setView:callField];
176 }
177
178 if ([itemIdentifier isEqualToString: kCallButtonIdentifer]) {
179 item = [[NSToolbarItem alloc] initWithItemIdentifier: kCallButtonIdentifer];
180
181 NSButton *callButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,80,30)];
182
183 [callButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
184 [callButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want]
185 [callButton setBordered:YES];
186 [callButton setTitle:@"Call"];
187 [item setView:callButton];
188 [item setAction:@selector(placeCall:)];
189 }
190
191 if ([itemIdentifier isEqualToString: kPreferencesIdentifier]) {
192 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPreferencesIdentifier];
193 [item setImage: [NSImage imageNamed: @"NSAdvanced"]];
194 [item setLabel: @"Settings"];
195 [item setAction:@selector(openPreferences:)];
196 }
197
198 return item;
Alexandre Lision25bcd482015-03-22 22:49:12 -0400199}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500200
Alexandre Lision25bcd482015-03-22 22:49:12 -0400201- (IBAction)placeCall:(id)sender
202{
203 Call* c = CallModel::instance()->dialingCall();
Alexandre Lision0a22c8f2015-03-29 21:40:06 -0400204
205 // check for a valid ring hash
206 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
207 BOOL valid = [[[callField stringValue] stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
208
209 if(valid && callField.stringValue.length == 40) {
210 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",[callField stringValue]]));
211 } else {
212 c->setDialNumber(QString::fromNSString([callField stringValue]));
213 }
214
Alexandre Lision25bcd482015-03-22 22:49:12 -0400215 c << Call::Action::ACCEPT;
Alexandre Lisionc5148052015-03-04 15:10:35 -0500216}
217
Alexandre Lisionc5148052015-03-04 15:10:35 -0500218-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
219{
220 return [NSArray arrayWithObjects:
221 NSToolbarSpaceItemIdentifier,
222 NSToolbarFlexibleSpaceItemIdentifier,
223 NSToolbarSpaceItemIdentifier,
224 NSToolbarSpaceItemIdentifier,
225 kSearchViewIdentifier,
226 kCallButtonIdentifer,
227 NSToolbarFlexibleSpaceItemIdentifier,
228 kPreferencesIdentifier,
229 nil];
230}
231
232-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
233{
234 return [NSArray arrayWithObjects:
235 kSearchViewIdentifier,
236 kCallButtonIdentifer,
237 kPreferencesIdentifier,
238 nil];
239}
240
241-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
242{
243 return nil;
244}
245
Alexandre Lision25bcd482015-03-22 22:49:12 -0400246#pragma NSTextField Delegate
247
248- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
249{
Alexandre Lision25bcd482015-03-22 22:49:12 -0400250 if (commandSelector == @selector(insertNewline:)) {
251 if([[callField stringValue] isNotEqualTo:@""]) {
252 [self placeCall:nil];
253 return YES;
254 }
255 }
256
257 return NO;
258}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500259
260
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500261@end