Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
| 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 Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 30 | #import "RingWindowController.h" |
| 31 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 32 | #import <accountmodel.h> |
| 33 | #import <callmodel.h> |
| 34 | #import <account.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 35 | #import <call.h> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 36 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 37 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame^] | 38 | #import "Constants.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 39 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 40 | @interface RingWindowController () |
| 41 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 42 | @property NSSearchField* callField; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 43 | |
| 44 | @end |
| 45 | |
| 46 | @implementation RingWindowController |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 47 | @synthesize callField; |
| 48 | static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier"; |
| 49 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
| 50 | static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier"; |
| 51 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 52 | - (void)windowDidLoad { |
| 53 | [super windowDidLoad]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 54 | [self.window setReleasedWhenClosed:FALSE]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 55 | [self displayMainToolBar]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 58 | - (IBAction)openPreferences:(id)sender |
| 59 | { |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 60 | if(self.preferencesViewController != nil) { |
| 61 | [self closePreferences:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 62 | return; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 63 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 64 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"]; |
| 65 | |
Alexandre Lision | 4ba1802 | 2015-04-23 12:17:40 -0400 | [diff] [blame] | 66 | self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 67 | 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 Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 111 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 112 | |
| 113 | - (IBAction) closePreferences:(NSToolbarItem *)sender { |
| 114 | if(self.myCurrentViewController != nil) |
| 115 | { |
| 116 | [self.preferencesViewController close]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 117 | [self displayMainToolBar]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 118 | self.preferencesViewController = nil; |
| 119 | } |
| 120 | } |
| 121 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 122 | -(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 Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 130 | // 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 147 | - (void)displayAccounts:(NSToolbarItem *)sender { |
| 148 | [self.preferencesViewController displayAccounts:sender]; |
| 149 | } |
| 150 | |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 151 | - (void)togglePowerSettings:(id)sender |
| 152 | { |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame^] | 153 | BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]; |
| 154 | [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced]; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 155 | [[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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 162 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 163 | #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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 174 | [callField setAlignment:NSCenterTextAlignment]; |
| 175 | [callField setDelegate:self]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 176 | [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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 200 | } |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 201 | |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 202 | - (IBAction)placeCall:(id)sender |
| 203 | { |
| 204 | Call* c = CallModel::instance()->dialingCall(); |
Alexandre Lision | 0a22c8f | 2015-03-29 21:40:06 -0400 | [diff] [blame] | 205 | |
| 206 | // check for a valid ring hash |
| 207 | NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"]; |
| 208 | BOOL valid = [[[callField stringValue] stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""]; |
| 209 | |
| 210 | if(valid && callField.stringValue.length == 40) { |
| 211 | c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",[callField stringValue]])); |
| 212 | } else { |
| 213 | c->setDialNumber(QString::fromNSString([callField stringValue])); |
| 214 | } |
| 215 | |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 216 | c << Call::Action::ACCEPT; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 219 | -(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar |
| 220 | { |
| 221 | return [NSArray arrayWithObjects: |
| 222 | NSToolbarSpaceItemIdentifier, |
| 223 | NSToolbarFlexibleSpaceItemIdentifier, |
| 224 | NSToolbarSpaceItemIdentifier, |
| 225 | NSToolbarSpaceItemIdentifier, |
| 226 | kSearchViewIdentifier, |
| 227 | kCallButtonIdentifer, |
| 228 | NSToolbarFlexibleSpaceItemIdentifier, |
| 229 | kPreferencesIdentifier, |
| 230 | nil]; |
| 231 | } |
| 232 | |
| 233 | -(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar |
| 234 | { |
| 235 | return [NSArray arrayWithObjects: |
| 236 | kSearchViewIdentifier, |
| 237 | kCallButtonIdentifer, |
| 238 | kPreferencesIdentifier, |
| 239 | nil]; |
| 240 | } |
| 241 | |
| 242 | -(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar |
| 243 | { |
| 244 | return nil; |
| 245 | } |
| 246 | |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 247 | #pragma NSTextField Delegate |
| 248 | |
| 249 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 250 | { |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 251 | if (commandSelector == @selector(insertNewline:)) { |
| 252 | if([[callField stringValue] isNotEqualTo:@""]) { |
| 253 | [self placeCall:nil]; |
| 254 | return YES; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return NO; |
| 259 | } |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 260 | |
| 261 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 262 | @end |