Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 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 | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 32 | //LRC |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 33 | #import <accountmodel.h> |
| 34 | #import <callmodel.h> |
| 35 | #import <account.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 36 | #import <call.h> |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 37 | #import <personmodel.h> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 38 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 39 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 40 | #import "Constants.h" |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 41 | #import "CurrentCallVC.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 42 | |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 43 | #import "backends/AddressBookBackend.h" |
| 44 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 45 | @interface RingWindowController () |
| 46 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 47 | @property NSSearchField* callField; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 48 | @property CurrentCallVC* currentVC; |
| 49 | @property (unsafe_unretained) IBOutlet NSView *callView; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 50 | @property (unsafe_unretained) IBOutlet NSTextField *ringIDLabel; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 51 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 52 | |
| 53 | @end |
| 54 | |
| 55 | @implementation RingWindowController |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 56 | @synthesize callField; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 57 | @synthesize currentVC; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 58 | @synthesize callView, ringIDLabel; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 59 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 60 | static NSString* const kSearchViewIdentifier = @"SearchViewIdentifier"; |
| 61 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
| 62 | static NSString* const kCallButtonIdentifer = @"CallButtonIdentifier"; |
| 63 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 64 | - (void)windowDidLoad { |
| 65 | [super windowDidLoad]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 66 | [self.window setReleasedWhenClosed:FALSE]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 67 | [self displayMainToolBar]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 68 | |
| 69 | currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil]; |
| 70 | [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 71 | [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 72 | |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 73 | |
| 74 | PersonModel::instance()->addCollection<AddressBookBackend>(LoadOptions::FORCE_ENABLED); |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 75 | [callView addSubview:[self.currentVC view] positioned:NSWindowAbove relativeTo:nil]; |
| 76 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 77 | [currentVC initFrame]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 78 | |
| 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 Lision | 7f8351b | 2015-08-20 11:43:37 -0400 | [diff] [blame] | 113 | [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 116 | - (IBAction)openPreferences:(id)sender |
| 117 | { |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 118 | if(self.preferencesViewController != nil) { |
| 119 | [self closePreferences:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 120 | return; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 121 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 122 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"]; |
| 123 | |
Alexandre Lision | 4ba1802 | 2015-04-23 12:17:40 -0400 | [diff] [blame] | 124 | self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 125 | 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 Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 169 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 170 | |
| 171 | - (IBAction) closePreferences:(NSToolbarItem *)sender { |
| 172 | if(self.myCurrentViewController != nil) |
| 173 | { |
| 174 | [self.preferencesViewController close]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 175 | [self displayMainToolBar]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 176 | self.preferencesViewController = nil; |
| 177 | } |
| 178 | } |
| 179 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 180 | -(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 Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 188 | // 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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 205 | - (void)displayAccounts:(NSToolbarItem *)sender { |
| 206 | [self.preferencesViewController displayAccounts:sender]; |
| 207 | } |
| 208 | |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 209 | - (void)togglePowerSettings:(id)sender |
| 210 | { |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 211 | BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]; |
| 212 | [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced]; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 213 | [[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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 220 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 221 | #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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 232 | [callField setAlignment:NSCenterTextAlignment]; |
| 233 | [callField setDelegate:self]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 234 | [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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 258 | } |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 259 | |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 260 | - (IBAction)placeCall:(id)sender |
| 261 | { |
| 262 | Call* c = CallModel::instance()->dialingCall(); |
Alexandre Lision | 0a22c8f | 2015-03-29 21:40:06 -0400 | [diff] [blame] | 263 | // 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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 273 | c << Call::Action::ACCEPT; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 276 | -(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 Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 304 | #pragma NSTextField Delegate |
| 305 | |
| 306 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 307 | { |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 308 | if (commandSelector == @selector(insertNewline:)) { |
| 309 | if([[callField stringValue] isNotEqualTo:@""]) { |
| 310 | [self placeCall:nil]; |
| 311 | return YES; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return NO; |
| 316 | } |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 317 | |
| 318 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 319 | @end |