blob: f708f7c7540c64fb8052bc6670ed84835b83907f [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 Lision745e4d62015-03-22 20:03:10 -040039
Alexandre Lision5855b6a2015-02-03 11:31:05 -050040@interface RingWindowController ()
41
Alexandre Lisionc5148052015-03-04 15:10:35 -050042@property NSSearchField* callField;
Alexandre Lision5855b6a2015-02-03 11:31:05 -050043
44@end
45
46@implementation RingWindowController
Alexandre Lisionc5148052015-03-04 15:10:35 -050047@synthesize callField;
48static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier";
49static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
50static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier";
51
Alexandre Lision5855b6a2015-02-03 11:31:05 -050052- (void)windowDidLoad {
53 [super windowDidLoad];
Alexandre Lision745e4d62015-03-22 20:03:10 -040054 [self.window setReleasedWhenClosed:FALSE];
Alexandre Lisionc5148052015-03-04 15:10:35 -050055 [self displayMainToolBar];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050056}
57
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050058- (IBAction)openPreferences:(id)sender
59{
Alexandre Lision1154eb32015-03-20 18:31:48 -040060 if(self.preferencesViewController != nil) {
61 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050062 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -040063 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050064 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
65
Alexandre Lision4ba18022015-04-23 12:17:40 -040066 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050067 self.myCurrentViewController = self.preferencesViewController;
68
69 NSLayoutConstraint* test = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
70 attribute:NSLayoutAttributeWidth
71 relatedBy:NSLayoutRelationEqual
72 toItem:currentView
73 attribute:NSLayoutAttributeWidth
74 multiplier:1.0f
75 constant:0.0f];
76
77 NSLayoutConstraint* test2 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
78 attribute:NSLayoutAttributeHeight
79 relatedBy:NSLayoutRelationEqual
80 toItem:currentView
81 attribute:NSLayoutAttributeHeight
82 multiplier:1.0f
83 constant:0.0f];
84
85 NSLayoutConstraint* test3 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view
86 attribute:NSLayoutAttributeCenterX
87 relatedBy:NSLayoutRelationEqual
88 toItem:currentView
89 attribute:NSLayoutAttributeCenterX
90 multiplier:1.0f
91 constant:0.0f];
92
93
94 [currentView addSubview:[self.preferencesViewController view]];
95
96 [tb setDelegate: self.preferencesViewController];
97 [self.window setToolbar: tb];
98
99 [self.window.toolbar setSelectedItemIdentifier:@"GeneralPrefsIdentifier"];
100
101 [currentView addConstraint:test];
102 [currentView addConstraint:test2];
103 [currentView addConstraint:test3];
104 // make sure we automatically resize the controller's view to the current window size
105 [[self.myCurrentViewController view] setFrame:[currentView bounds]];
106
107 // set the view controller's represented object to the number of subviews in that controller
108 // (our NSTextField's value binding will reflect this value)
109 [self.myCurrentViewController setRepresentedObject:[NSNumber numberWithUnsignedInteger:[[[self.myCurrentViewController view] subviews] count]]];
110
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500111}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500112
113- (IBAction) closePreferences:(NSToolbarItem *)sender {
114 if(self.myCurrentViewController != nil)
115 {
116 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500117 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500118 self.preferencesViewController = nil;
119 }
120}
121
Alexandre Lisionc5148052015-03-04 15:10:35 -0500122-(void) displayMainToolBar
123{
124 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"MainToolbar"];
125 [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel];
126 [tb setDelegate: self];
127 [self.window setToolbar: tb];
128}
129
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500130// FIXME: This is sick, NSWindowController is catching my selectors
131- (void)displayGeneral:(NSToolbarItem *)sender {
132 [self.preferencesViewController displayGeneral:sender];
133}
134
135- (void)displayAudio:(NSToolbarItem *)sender {
136 [self.preferencesViewController displayAudio:sender];
137}
138
139- (void)displayAncrage:(NSToolbarItem *)sender {
140 [self.preferencesViewController displayAncrage:sender];
141}
142
143- (void)displayVideo:(NSToolbarItem *)sender {
144 [self.preferencesViewController displayVideo:sender];
145}
146
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400147- (void)displayAccounts:(NSToolbarItem *)sender {
148 [self.preferencesViewController displayAccounts:sender];
149}
150
Alexandre Lision1154eb32015-03-20 18:31:48 -0400151- (void)togglePowerSettings:(id)sender
152{
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400153 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced];
154 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400155 [[NSUserDefaults standardUserDefaults] synchronize];
156
157 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
158 [tb setDelegate: self.preferencesViewController];
159 [self.preferencesViewController displayGeneral:nil];
160 [self.window setToolbar:tb];
161}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400162
Alexandre Lisionc5148052015-03-04 15:10:35 -0500163#pragma NSToolbar Delegate
164
165-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
166{
167 NSToolbarItem* item = nil;
168
169 if ([itemIdentifier isEqualToString: kSearchViewIdentifier]) {
170 item = [[NSToolbarItem alloc] initWithItemIdentifier: kSearchViewIdentifier];
171 callField = [[NSSearchField alloc] initWithFrame:NSMakeRect(0,0,400,21)];
172 [[callField cell] setSearchButtonCell:nil];
173 [callField setToolTip:@"Call"];
Alexandre Lision25bcd482015-03-22 22:49:12 -0400174 [callField setAlignment:NSCenterTextAlignment];
175 [callField setDelegate:self];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500176 [item setView:callField];
177 }
178
179 if ([itemIdentifier isEqualToString: kCallButtonIdentifer]) {
180 item = [[NSToolbarItem alloc] initWithItemIdentifier: kCallButtonIdentifer];
181
182 NSButton *callButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,80,30)];
183
184 [callButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
185 [callButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want]
186 [callButton setBordered:YES];
187 [callButton setTitle:@"Call"];
188 [item setView:callButton];
189 [item setAction:@selector(placeCall:)];
190 }
191
192 if ([itemIdentifier isEqualToString: kPreferencesIdentifier]) {
193 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPreferencesIdentifier];
194 [item setImage: [NSImage imageNamed: @"NSAdvanced"]];
195 [item setLabel: @"Settings"];
196 [item setAction:@selector(openPreferences:)];
197 }
198
199 return item;
Alexandre Lision25bcd482015-03-22 22:49:12 -0400200}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500201
Alexandre Lision25bcd482015-03-22 22:49:12 -0400202- (IBAction)placeCall:(id)sender
203{
204 Call* c = CallModel::instance()->dialingCall();
Alexandre Lision0a22c8f2015-03-29 21:40:06 -0400205 // 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