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. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 19 | #import "RingWindowController.h" |
| 20 | |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 21 | //LRC |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 22 | #import <accountmodel.h> |
| 23 | #import <callmodel.h> |
| 24 | #import <account.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 25 | #import <call.h> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 26 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 27 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 28 | #import "Constants.h" |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 29 | #import "CurrentCallVC.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 30 | |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 31 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 32 | @interface RingWindowController () |
| 33 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 34 | @property CurrentCallVC* currentVC; |
| 35 | @property (unsafe_unretained) IBOutlet NSView *callView; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 36 | @property (unsafe_unretained) IBOutlet NSTextField *ringIDLabel; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 38 | |
| 39 | @end |
| 40 | |
| 41 | @implementation RingWindowController |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 42 | @synthesize currentVC; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 43 | @synthesize callView, ringIDLabel; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 44 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 45 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 46 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 47 | - (void)windowDidLoad { |
| 48 | [super windowDidLoad]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame^] | 49 | [self.window setMovableByWindowBackground:YES]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 50 | [self displayMainToolBar]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 51 | |
| 52 | currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil]; |
| 53 | [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 54 | [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 55 | |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 56 | [callView addSubview:[self.currentVC view] positioned:NSWindowAbove relativeTo:nil]; |
| 57 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 58 | [currentVC initFrame]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 59 | |
| 60 | // Update Ring ID label based on account model changes |
| 61 | QObject::connect(AccountModel::instance(), |
| 62 | &AccountModel::dataChanged, |
| 63 | [=] { |
| 64 | [self updateRingID]; |
| 65 | }); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame^] | 66 | |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Implement the necessary logic to choose which Ring ID to display. |
| 71 | * This tries to choose the "best" ID to show |
| 72 | */ |
| 73 | - (void) updateRingID |
| 74 | { |
| 75 | Account* registered = nullptr; |
| 76 | Account* enabled = nullptr; |
| 77 | Account* finalChoice = nullptr; |
| 78 | |
| 79 | [ringIDLabel setStringValue:@""]; |
| 80 | auto ringList = AccountModel::instance()->getAccountsByProtocol(Account::Protocol::RING); |
| 81 | for (int i = 0 ; i < ringList.size() && !registered ; ++i) { |
| 82 | Account* acc = ringList.value(i); |
| 83 | if (acc->isEnabled()) { |
| 84 | if(!enabled) |
| 85 | enabled = finalChoice = acc; |
| 86 | if (acc->registrationState() == Account::RegistrationState::READY) { |
| 87 | registered = enabled = finalChoice = acc; |
| 88 | } |
| 89 | } else { |
| 90 | if (!finalChoice) |
| 91 | finalChoice = acc; |
| 92 | } |
| 93 | } |
| 94 | |
Alexandre Lision | 7f8351b | 2015-08-20 11:43:37 -0400 | [diff] [blame] | 95 | [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 98 | - (IBAction)openPreferences:(id)sender |
| 99 | { |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 100 | if(self.preferencesViewController != nil) { |
| 101 | [self closePreferences:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 102 | return; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 103 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 104 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"]; |
| 105 | |
Alexandre Lision | 4ba1802 | 2015-04-23 12:17:40 -0400 | [diff] [blame] | 106 | self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 107 | self.myCurrentViewController = self.preferencesViewController; |
| 108 | |
| 109 | NSLayoutConstraint* test = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view |
| 110 | attribute:NSLayoutAttributeWidth |
| 111 | relatedBy:NSLayoutRelationEqual |
| 112 | toItem:currentView |
| 113 | attribute:NSLayoutAttributeWidth |
| 114 | multiplier:1.0f |
| 115 | constant:0.0f]; |
| 116 | |
| 117 | NSLayoutConstraint* test2 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view |
| 118 | attribute:NSLayoutAttributeHeight |
| 119 | relatedBy:NSLayoutRelationEqual |
| 120 | toItem:currentView |
| 121 | attribute:NSLayoutAttributeHeight |
| 122 | multiplier:1.0f |
| 123 | constant:0.0f]; |
| 124 | |
| 125 | NSLayoutConstraint* test3 = [NSLayoutConstraint constraintWithItem:self.preferencesViewController.view |
| 126 | attribute:NSLayoutAttributeCenterX |
| 127 | relatedBy:NSLayoutRelationEqual |
| 128 | toItem:currentView |
| 129 | attribute:NSLayoutAttributeCenterX |
| 130 | multiplier:1.0f |
| 131 | constant:0.0f]; |
| 132 | |
| 133 | |
| 134 | [currentView addSubview:[self.preferencesViewController view]]; |
| 135 | |
| 136 | [tb setDelegate: self.preferencesViewController]; |
| 137 | [self.window setToolbar: tb]; |
| 138 | |
| 139 | [self.window.toolbar setSelectedItemIdentifier:@"GeneralPrefsIdentifier"]; |
| 140 | |
| 141 | [currentView addConstraint:test]; |
| 142 | [currentView addConstraint:test2]; |
| 143 | [currentView addConstraint:test3]; |
| 144 | // make sure we automatically resize the controller's view to the current window size |
| 145 | [[self.myCurrentViewController view] setFrame:[currentView bounds]]; |
| 146 | |
| 147 | // set the view controller's represented object to the number of subviews in that controller |
| 148 | // (our NSTextField's value binding will reflect this value) |
| 149 | [self.myCurrentViewController setRepresentedObject:[NSNumber numberWithUnsignedInteger:[[[self.myCurrentViewController view] subviews] count]]]; |
| 150 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 151 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 152 | |
| 153 | - (IBAction) closePreferences:(NSToolbarItem *)sender { |
| 154 | if(self.myCurrentViewController != nil) |
| 155 | { |
| 156 | [self.preferencesViewController close]; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 157 | [self displayMainToolBar]; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 158 | self.preferencesViewController = nil; |
| 159 | } |
| 160 | } |
| 161 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 162 | -(void) displayMainToolBar |
| 163 | { |
| 164 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"MainToolbar"]; |
| 165 | [tb setDisplayMode:NSToolbarDisplayModeIconAndLabel]; |
| 166 | [tb setDelegate: self]; |
| 167 | [self.window setToolbar: tb]; |
| 168 | } |
| 169 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 170 | // FIXME: This is sick, NSWindowController is catching my selectors |
| 171 | - (void)displayGeneral:(NSToolbarItem *)sender { |
| 172 | [self.preferencesViewController displayGeneral:sender]; |
| 173 | } |
| 174 | |
| 175 | - (void)displayAudio:(NSToolbarItem *)sender { |
| 176 | [self.preferencesViewController displayAudio:sender]; |
| 177 | } |
| 178 | |
| 179 | - (void)displayAncrage:(NSToolbarItem *)sender { |
| 180 | [self.preferencesViewController displayAncrage:sender]; |
| 181 | } |
| 182 | |
| 183 | - (void)displayVideo:(NSToolbarItem *)sender { |
| 184 | [self.preferencesViewController displayVideo:sender]; |
| 185 | } |
| 186 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 187 | - (void)displayAccounts:(NSToolbarItem *)sender { |
| 188 | [self.preferencesViewController displayAccounts:sender]; |
| 189 | } |
| 190 | |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 191 | - (void)togglePowerSettings:(id)sender |
| 192 | { |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 193 | BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]; |
| 194 | [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced]; |
Alexandre Lision | 1154eb3 | 2015-03-20 18:31:48 -0400 | [diff] [blame] | 195 | [[NSUserDefaults standardUserDefaults] synchronize]; |
| 196 | |
| 197 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"]; |
| 198 | [tb setDelegate: self.preferencesViewController]; |
| 199 | [self.preferencesViewController displayGeneral:nil]; |
| 200 | [self.window setToolbar:tb]; |
| 201 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 202 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame^] | 203 | #pragma mark - NSToolbar Delegate |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 204 | |
| 205 | -(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag |
| 206 | { |
| 207 | NSToolbarItem* item = nil; |
| 208 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 209 | if ([itemIdentifier isEqualToString: kPreferencesIdentifier]) { |
| 210 | item = [[NSToolbarItem alloc] initWithItemIdentifier: kPreferencesIdentifier]; |
| 211 | [item setImage: [NSImage imageNamed: @"NSAdvanced"]]; |
| 212 | [item setLabel: @"Settings"]; |
| 213 | [item setAction:@selector(openPreferences:)]; |
| 214 | } |
| 215 | |
| 216 | return item; |
Alexandre Lision | 25bcd48 | 2015-03-22 22:49:12 -0400 | [diff] [blame] | 217 | } |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 218 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 219 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 220 | -(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar |
| 221 | { |
| 222 | return [NSArray arrayWithObjects: |
| 223 | NSToolbarSpaceItemIdentifier, |
| 224 | NSToolbarFlexibleSpaceItemIdentifier, |
| 225 | NSToolbarSpaceItemIdentifier, |
| 226 | NSToolbarSpaceItemIdentifier, |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 227 | NSToolbarFlexibleSpaceItemIdentifier, |
| 228 | kPreferencesIdentifier, |
| 229 | nil]; |
| 230 | } |
| 231 | |
| 232 | -(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar |
| 233 | { |
| 234 | return [NSArray arrayWithObjects: |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 235 | kPreferencesIdentifier, |
| 236 | nil]; |
| 237 | } |
| 238 | |
| 239 | -(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar |
| 240 | { |
| 241 | return nil; |
| 242 | } |
| 243 | |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 244 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 245 | @end |