blob: 8b4036dc65f309d827cfaae0e670552b7b6cd212 [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
Alexandre Lision08abfac2015-09-22 12:20:51 -040060 // Fresh run, we need to make sure RingID appears
61 [self updateRingID];
62
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040063 // 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 Lision7f8351b2015-08-20 11:43:37 -040097 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -050098}
99
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500100- (IBAction)openPreferences:(id)sender
101{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400102 if(self.preferencesViewController != nil) {
103 [self closePreferences:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500104 return;
Alexandre Lision1154eb32015-03-20 18:31:48 -0400105 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500106 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier: @"PreferencesToolbar"];
107
Alexandre Lision4ba18022015-04-23 12:17:40 -0400108 self.preferencesViewController = [[PreferencesVC alloc] initWithNibName:@"PreferencesScreen" bundle:nil];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500109 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 Lision5855b6a2015-02-03 11:31:05 -0500153}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500154
155- (IBAction) closePreferences:(NSToolbarItem *)sender {
156 if(self.myCurrentViewController != nil)
157 {
158 [self.preferencesViewController close];
Alexandre Lisionc5148052015-03-04 15:10:35 -0500159 [self displayMainToolBar];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500160 self.preferencesViewController = nil;
161 }
162}
163
Alexandre Lisionc5148052015-03-04 15:10:35 -0500164-(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 Lision4a7b95e2015-02-20 10:06:43 -0500172// 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 Lisionf5fc4792015-03-17 09:15:43 -0400189- (void)displayAccounts:(NSToolbarItem *)sender {
190 [self.preferencesViewController displayAccounts:sender];
191}
192
Alexandre Lision1154eb32015-03-20 18:31:48 -0400193- (void)togglePowerSettings:(id)sender
194{
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400195 BOOL advanced = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced];
196 [[NSUserDefaults standardUserDefaults] setBool:!advanced forKey:Preferences::ShowAdvanced];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400197 [[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 Lisionf5fc4792015-03-17 09:15:43 -0400204
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400205#pragma mark - NSToolbar Delegate
Alexandre Lisionc5148052015-03-04 15:10:35 -0500206
207-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
208{
209 NSToolbarItem* item = nil;
210
Alexandre Lisionc5148052015-03-04 15:10:35 -0500211 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 Lision25bcd482015-03-22 22:49:12 -0400219}
Alexandre Lisionc5148052015-03-04 15:10:35 -0500220
Alexandre Lisionc5148052015-03-04 15:10:35 -0500221
Alexandre Lisionc5148052015-03-04 15:10:35 -0500222-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
223{
224 return [NSArray arrayWithObjects:
225 NSToolbarSpaceItemIdentifier,
226 NSToolbarFlexibleSpaceItemIdentifier,
227 NSToolbarSpaceItemIdentifier,
228 NSToolbarSpaceItemIdentifier,
Alexandre Lisionc5148052015-03-04 15:10:35 -0500229 NSToolbarFlexibleSpaceItemIdentifier,
230 kPreferencesIdentifier,
231 nil];
232}
233
234-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
235{
236 return [NSArray arrayWithObjects:
Alexandre Lisionc5148052015-03-04 15:10:35 -0500237 kPreferencesIdentifier,
238 nil];
239}
240
241-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
242{
243 return nil;
244}
245
Alexandre Lisionc5148052015-03-04 15:10:35 -0500246
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500247@end