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