blob: 5d4610e70dc373967ab2bac3acf7755c3fc35947 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lisionbb5c2462015-07-30 12:55:37 -04002 * Copyright (C) 2015 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * 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 Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050019#import "RingWindowController.h"
20
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040021//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050022#import <accountmodel.h>
23#import <callmodel.h>
24#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040025#import <call.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050026
Alexandre Lision745e4d62015-03-22 20:03:10 -040027#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040028#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040029#import "CurrentCallVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040030
Alexandre Lision2db8f472015-07-22 15:05:46 -040031
Alexandre Lision5855b6a2015-02-03 11:31:05 -050032@interface RingWindowController ()
33
Alexandre Lision58cab672015-06-09 15:25:40 -040034@property CurrentCallVC* currentVC;
35@property (unsafe_unretained) IBOutlet NSView *callView;
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040036@property (unsafe_unretained) IBOutlet NSTextField *ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040037
Alexandre Lision5855b6a2015-02-03 11:31:05 -050038
39@end
40
41@implementation RingWindowController
Alexandre Lision58cab672015-06-09 15:25:40 -040042@synthesize currentVC;
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040043@synthesize callView, ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040044
Alexandre Lisionc5148052015-03-04 15:10:35 -050045static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050046
Alexandre Lision5855b6a2015-02-03 11:31:05 -050047- (void)windowDidLoad {
48 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040049 [self.window setMovableByWindowBackground:YES];
Alexandre Lisionc5148052015-03-04 15:10:35 -050050 [self displayMainToolBar];
Alexandre Lision58cab672015-06-09 15:25:40 -040051
52 currentVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
53 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
54 [[currentVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
55
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040056 [callView addSubview:[self.currentVC view] positioned:NSWindowAbove relativeTo:nil];
57
Alexandre Lision58cab672015-06-09 15:25:40 -040058 [currentVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040059
60 // Update Ring ID label based on account model changes
61 QObject::connect(AccountModel::instance(),
62 &AccountModel::dataChanged,
63 [=] {
64 [self updateRingID];
65 });
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040066
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040067}
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 Lision7f8351b2015-08-20 11:43:37 -040095 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050096}
97
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050098- (IBAction)openPreferences:(id)sender
99{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400100 if(self.preferencesViewController != nil) {
101 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500102 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -0400103 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500104 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
105
Alexandre Lision4ba18022015-04-23 12:17:40 -0400106 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500107 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 Lision5855b6a2015-02-03 11:31:05 -0500151}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500152
153- (IBAction) closePreferences:(NSToolbarItem *)sender {
154 if(self.myCurrentViewController != nil)
155 {
156 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500157 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500158 self.preferencesViewController = nil;
159 }
160}
161
Alexandre Lisionc5148052015-03-04 15:10:35 -0500162-(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 Lision4a7b95e2015-02-20 10:06:43 -0500170// 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 Lisionf5fc4792015-03-17 09:15:43 -0400187- (void)displayAccounts:(NSToolbarItem *)sender {
188 [self.preferencesViewController displayAccounts:sender];
189}
190
Alexandre Lision1154eb32015-03-20 18:31:48 -0400191- (void)togglePowerSettings:(id)sender
192{
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400193 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced];
194 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400195 [[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 Lisionf5fc4792015-03-17 09:15:43 -0400202
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400203#pragma mark - NSToolbar Delegate
Alexandre Lisionc5148052015-03-04 15:10:35 -0500204
205-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
206{
207 NSToolbarItem* item = nil;
208
Alexandre Lisionc5148052015-03-04 15:10:35 -0500209 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 Lision25bcd482015-03-22 22:49:12 -0400217}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500218
Alexandre Lisionc5148052015-03-04 15:10:35 -0500219
Alexandre Lisionc5148052015-03-04 15:10:35 -0500220-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
221{
222 return [NSArray arrayWithObjects:
223 NSToolbarSpaceItemIdentifier,
224 NSToolbarFlexibleSpaceItemIdentifier,
225 NSToolbarSpaceItemIdentifier,
226 NSToolbarSpaceItemIdentifier,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500227 NSToolbarFlexibleSpaceItemIdentifier,
228 kPreferencesIdentifier,
229 nil];
230}
231
232-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
233{
234 return [NSArray arrayWithObjects:
Alexandre Lisionc5148052015-03-04 15:10:35 -0500235 kPreferencesIdentifier,
236 nil];
237}
238
239-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
240{
241 return nil;
242}
243
Alexandre Lisionc5148052015-03-04 15:10:35 -0500244
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500245@end