blob: 1f8e153a5ab057f6e1bdcff1cec76f56a1ee1f0e [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
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 Lision4ba18022015-04-23 12:17:40 -040030#import "PreferencesVC.h"
Alexandre Lision8521baa2015-03-13 11:08:00 -040031
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050032#import <QuartzCore/QuartzCore.h>
33
Alexandre Lision91d11e52015-03-20 17:42:05 -040034#import <accountmodel.h>
Alexandre Lisione4041492015-03-20 18:20:43 -040035#import <audio/codecmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040036
37#import "AccountsVC.h"
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050038#import "GeneralPrefsVC.h"
39#import "AudioPrefsVC.h"
40#import "VideoPrefsVC.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040041#import "Constants.h"
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050042
Alexandre Lision4ba18022015-04-23 12:17:40 -040043@interface PreferencesVC ()
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050044
Alexandre Lision1154eb32015-03-20 18:31:48 -040045@property NSButton* toggleAdvancedSettings;
46
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050047@end
48
Alexandre Lision4ba18022015-04-23 12:17:40 -040049@implementation PreferencesVC
Alexandre Lision1154eb32015-03-20 18:31:48 -040050@synthesize toggleAdvancedSettings;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050051
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040052static NSString* const kProfilePrefsIdentifier = @"ProfilesPrefsIdentifier";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050053static NSString* const kGeneralPrefsIdentifier = @"GeneralPrefsIdentifier";
54static NSString* const kAudioPrefsIdentifer = @"AudioPrefsIdentifer";
55static NSString* const kAncragePrefsIdentifer = @"AncragePrefsIdentifer";
56static NSString* const kVideoPrefsIdentifer = @"VideoPrefsIdentifer";
57static NSString* const kDonePrefsIdentifer = @"DonePrefsIdentifer";
Alexandre Lision1154eb32015-03-20 18:31:48 -040058static NSString* const kPowerSettingsIdentifer = @"PowerSettingsIdentifer";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050059
60-(void)loadView
61{
62 [super loadView];
63
64 [self displayGeneral:nil];
65
66 [self.view setWantsLayer:YES];
67 self.view.layer.backgroundColor = [NSColor windowBackgroundColor].CGColor;
68
69 // Set the layer redraw policy. This would be better done in
70 // the initialization method of a NSView subclass instead of here.
Alexandre Lisiona1c6d752015-06-23 12:27:38 -040071 self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050072
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040073 [self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
74
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050075 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
76
Alexandre Lisiona1c6d752015-06-23 12:27:38 -040077 [CATransaction begin];
78 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
79 animation.fromValue = @(frame.origin.y);
80 animation.toValue = @(self.view.frame.origin.y);
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050081 animation.duration = 0.3f;
Alexandre Lisiona1c6d752015-06-23 12:27:38 -040082
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050083 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050084 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -040085 [CATransaction commit];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050086}
87
88- (void) close
89{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040090 AccountModel::instance()->save();
91
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050092 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
93
94 [CATransaction begin];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -040095 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
96 animation.fromValue = @(self.view.frame.origin.y);
97 animation.toValue = @(frame.origin.y);
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050098 animation.duration = 0.3f;
99 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
100
101 [CATransaction setCompletionBlock:^{
102 [self.view removeFromSuperview];
103 }];
104
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500105 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lisiona1c6d752015-06-23 12:27:38 -0400106
107 // set final layer position to prevent glitching back to original one
108 [self.view.layer setPosition:frame.origin];;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500109 [CATransaction commit];
110}
111
112- (void)displayGeneral:(NSToolbarItem *)sender {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500113 if (self.currentVC != nil) {
114 [self.currentVC.view removeFromSuperview];
115 }
116 self.generalPrefsVC = [[GeneralPrefsVC alloc] initWithNibName:@"GeneralPrefs" bundle:nil];
117 [self.view addSubview:self.generalPrefsVC.view];
118 [self.generalPrefsVC.view setFrame:[self.view bounds]];
119 self.currentVC = self.generalPrefsVC;
120}
121
122- (void)displayAudio:(NSToolbarItem *)sender {
123 if (self.currentVC != nil) {
124 [self.currentVC.view removeFromSuperview];
125 }
126 self.audioPrefsVC = [[AudioPrefsVC alloc] initWithNibName:@"AudioPrefs" bundle:nil];
127 [self.view addSubview:self.audioPrefsVC.view];
128 [self.audioPrefsVC.view setFrame:[self.view bounds]];
129 self.currentVC = self.audioPrefsVC;
130}
131
132- (void)displayAncrage:(NSToolbarItem *)sender {
133
134}
135
136- (void)displayVideo:(NSToolbarItem *)sender {
137 if (self.currentVC != nil) {
138 [self.currentVC.view removeFromSuperview];
139 }
140 self.videoPrefsVC = [[VideoPrefsVC alloc] initWithNibName:@"VideoPrefs" bundle:nil];
141 [self.view addSubview:self.videoPrefsVC.view];
142 [self.videoPrefsVC.view setFrame:[self.view bounds]];
143 self.currentVC = self.videoPrefsVC;
144}
145
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400146- (void) displayAccounts:(NSToolbarItem *) sender {
147 if (self.currentVC != nil) {
148 [self.currentVC.view removeFromSuperview];
149 }
150 self.accountsPrefsVC = [[AccountsVC alloc] initWithNibName:@"Accounts" bundle:nil];
151 [self.view addSubview:self.accountsPrefsVC.view];
152 [self.accountsPrefsVC.view setFrame:[self.view bounds]];
153 self.currentVC = self.accountsPrefsVC;
154}
155
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500156
157#pragma NSToolbar Delegate
158
159-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
160{
161 NSToolbarItem* item = nil;
162
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400163 if ([itemIdentifier isEqualToString: kProfilePrefsIdentifier]) {
164
165 item = [[NSToolbarItem alloc] initWithItemIdentifier: kProfilePrefsIdentifier];
166 [item setImage: [NSImage imageNamed: @"NSUserAccounts"]];
167 [item setLabel: @"Accounts"];
168 [item setAction:@selector(displayAccounts:)];
169 }
170
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500171 if ([itemIdentifier isEqualToString: kGeneralPrefsIdentifier]) {
172 item = [[NSToolbarItem alloc] initWithItemIdentifier: kGeneralPrefsIdentifier];
173 [item setImage: [NSImage imageNamed: @"general"]];
174 [item setLabel: @"General"];
175 [item setAction:@selector(displayGeneral:)];
176 }
177
178 if ([itemIdentifier isEqualToString: kAudioPrefsIdentifer]) {
179 item = [[NSToolbarItem alloc] initWithItemIdentifier: kAudioPrefsIdentifer];
180 [item setImage: [NSImage imageNamed: @"audio"]];
181 [item setLabel: @"Audio"];
182 [item setAction:@selector(displayAudio:)];
183 }
184
Alexandre Lision1154eb32015-03-20 18:31:48 -0400185 if ([itemIdentifier isEqualToString: kPowerSettingsIdentifer]) {
186 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPowerSettingsIdentifer];
187 toggleAdvancedSettings = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,20,20)];
188 [toggleAdvancedSettings setButtonType:NSSwitchButton];
189 [toggleAdvancedSettings setTitle:@""];
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400190 [toggleAdvancedSettings setState:[[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400191 [item setLabel:@"Show Advanced"];
192 [item setView:toggleAdvancedSettings];
193 [item setAction:@selector(togglePowerSettings:)];
194 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500195
196 if ([itemIdentifier isEqualToString: kDonePrefsIdentifer]) {
197 item = [[NSToolbarItem alloc] initWithItemIdentifier: kDonePrefsIdentifer];
198 [item setImage: [NSImage imageNamed: @"ic_action_cancel"]];
199 [item setLabel: @"Done"];
200 [item setAction:@selector(closePreferences:)];
201 }
202
203 if ([itemIdentifier isEqualToString: kVideoPrefsIdentifer]) {
204 item = [[NSToolbarItem alloc] initWithItemIdentifier: kVideoPrefsIdentifer];
205 [item setImage: [NSImage imageNamed: @"video"]];
206 [item setLabel: @"Video"];
207 [item setAction:@selector(displayVideo:)];
208 }
209
210 return item;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500211}
212
213-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
214{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400215
216 NSMutableArray* items = [NSMutableArray arrayWithObjects:
217 kPowerSettingsIdentifer,
218 NSToolbarFlexibleSpaceItemIdentifier,
219 kGeneralPrefsIdentifier,
220 kAudioPrefsIdentifer,
221 kVideoPrefsIdentifer,
222 // kAncragePrefsIdentifer,
223 NSToolbarFlexibleSpaceItemIdentifier,
224 kDonePrefsIdentifer,
225 nil];
226
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400227 if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]) {
Alexandre Lision1154eb32015-03-20 18:31:48 -0400228 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
229 [items insertObject:kProfilePrefsIdentifier atIndex:2];
230 } else
231 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
232
233 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500234}
235
236-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
237{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400238 NSMutableArray* items = [NSMutableArray arrayWithObjects:
239 kPowerSettingsIdentifer,
240 kGeneralPrefsIdentifier,
241 kAudioPrefsIdentifer,
242 kVideoPrefsIdentifer,
243 nil];
244
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400245 if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced])
Alexandre Lision1154eb32015-03-20 18:31:48 -0400246 [items insertObject:kProfilePrefsIdentifier atIndex:1];
247
248
249 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500250}
251
252-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
253{
254 return nil;
255}
256
257
258
259
260
261@end