blob: 4feaa011209fe39b0c1ef5407eed38c9b91ae27c [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 Lision4a7b95e2015-02-20 10:06:43 -050030#import "PreferencesViewController.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 Lisionf5fc4792015-03-17 09:15:43 -040035
36#import "AccountsVC.h"
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050037#import "GeneralPrefsVC.h"
38#import "AudioPrefsVC.h"
39#import "VideoPrefsVC.h"
40
41@interface PreferencesViewController ()
42
Alexandre Lision1154eb32015-03-20 18:31:48 -040043@property NSButton* toggleAdvancedSettings;
44
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050045@end
46
47@implementation PreferencesViewController
Alexandre Lision1154eb32015-03-20 18:31:48 -040048@synthesize toggleAdvancedSettings;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050049
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040050static NSString* const kProfilePrefsIdentifier = @"ProfilesPrefsIdentifier";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050051static NSString* const kGeneralPrefsIdentifier = @"GeneralPrefsIdentifier";
52static NSString* const kAudioPrefsIdentifer = @"AudioPrefsIdentifer";
53static NSString* const kAncragePrefsIdentifer = @"AncragePrefsIdentifer";
54static NSString* const kVideoPrefsIdentifer = @"VideoPrefsIdentifer";
55static NSString* const kDonePrefsIdentifer = @"DonePrefsIdentifer";
Alexandre Lision1154eb32015-03-20 18:31:48 -040056static NSString* const kPowerSettingsIdentifer = @"PowerSettingsIdentifer";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050057
58-(void)loadView
59{
60 [super loadView];
61
62 [self displayGeneral:nil];
63
64 [self.view setWantsLayer:YES];
65 self.view.layer.backgroundColor = [NSColor windowBackgroundColor].CGColor;
66
67 // Set the layer redraw policy. This would be better done in
68 // the initialization method of a NSView subclass instead of here.
69 self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
70
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040071 [self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
72
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050073 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
74
75 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
76 animation.fromValue = [NSValue valueWithPoint:frame.origin];
77 animation.toValue = [NSValue valueWithPoint:self.view.frame.origin];
78 animation.duration = 0.3f;
79 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050080 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050081}
82
83- (void) close
84{
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040085
86 AccountModel::instance()->save();
87
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050088 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
89
90 [CATransaction begin];
91 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
92 animation.fromValue = [NSValue valueWithPoint:self.view.frame.origin];
93 animation.toValue = [NSValue valueWithPoint:frame.origin];
94 animation.duration = 0.3f;
95 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
96
97 [CATransaction setCompletionBlock:^{
98 [self.view removeFromSuperview];
99 }];
100
101
102 [self.view.layer addAnimation:animation forKey:animation.keyPath];
103 [CATransaction commit];
104}
105
106- (void)displayGeneral:(NSToolbarItem *)sender {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500107 if (self.currentVC != nil) {
108 [self.currentVC.view removeFromSuperview];
109 }
110 self.generalPrefsVC = [[GeneralPrefsVC alloc] initWithNibName:@"GeneralPrefs" bundle:nil];
111 [self.view addSubview:self.generalPrefsVC.view];
112 [self.generalPrefsVC.view setFrame:[self.view bounds]];
113 self.currentVC = self.generalPrefsVC;
114}
115
116- (void)displayAudio:(NSToolbarItem *)sender {
117 if (self.currentVC != nil) {
118 [self.currentVC.view removeFromSuperview];
119 }
120 self.audioPrefsVC = [[AudioPrefsVC alloc] initWithNibName:@"AudioPrefs" bundle:nil];
121 [self.view addSubview:self.audioPrefsVC.view];
122 [self.audioPrefsVC.view setFrame:[self.view bounds]];
123 self.currentVC = self.audioPrefsVC;
124}
125
126- (void)displayAncrage:(NSToolbarItem *)sender {
127
128}
129
130- (void)displayVideo:(NSToolbarItem *)sender {
131 if (self.currentVC != nil) {
132 [self.currentVC.view removeFromSuperview];
133 }
134 self.videoPrefsVC = [[VideoPrefsVC alloc] initWithNibName:@"VideoPrefs" bundle:nil];
135 [self.view addSubview:self.videoPrefsVC.view];
136 [self.videoPrefsVC.view setFrame:[self.view bounds]];
137 self.currentVC = self.videoPrefsVC;
138}
139
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400140- (void) displayAccounts:(NSToolbarItem *) sender {
141 if (self.currentVC != nil) {
142 [self.currentVC.view removeFromSuperview];
143 }
144 self.accountsPrefsVC = [[AccountsVC alloc] initWithNibName:@"Accounts" bundle:nil];
145 [self.view addSubview:self.accountsPrefsVC.view];
146 [self.accountsPrefsVC.view setFrame:[self.view bounds]];
147 self.currentVC = self.accountsPrefsVC;
148}
149
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500150
151#pragma NSToolbar Delegate
152
153-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
154{
155 NSToolbarItem* item = nil;
156
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400157 if ([itemIdentifier isEqualToString: kProfilePrefsIdentifier]) {
158
159 item = [[NSToolbarItem alloc] initWithItemIdentifier: kProfilePrefsIdentifier];
160 [item setImage: [NSImage imageNamed: @"NSUserAccounts"]];
161 [item setLabel: @"Accounts"];
162 [item setAction:@selector(displayAccounts:)];
163 }
164
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500165 if ([itemIdentifier isEqualToString: kGeneralPrefsIdentifier]) {
166 item = [[NSToolbarItem alloc] initWithItemIdentifier: kGeneralPrefsIdentifier];
167 [item setImage: [NSImage imageNamed: @"general"]];
168 [item setLabel: @"General"];
169 [item setAction:@selector(displayGeneral:)];
170 }
171
172 if ([itemIdentifier isEqualToString: kAudioPrefsIdentifer]) {
173 item = [[NSToolbarItem alloc] initWithItemIdentifier: kAudioPrefsIdentifer];
174 [item setImage: [NSImage imageNamed: @"audio"]];
175 [item setLabel: @"Audio"];
176 [item setAction:@selector(displayAudio:)];
177 }
178
Alexandre Lision1154eb32015-03-20 18:31:48 -0400179 if ([itemIdentifier isEqualToString: kPowerSettingsIdentifer]) {
180 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPowerSettingsIdentifer];
181 toggleAdvancedSettings = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,20,20)];
182 [toggleAdvancedSettings setButtonType:NSSwitchButton];
183 [toggleAdvancedSettings setTitle:@""];
184 [toggleAdvancedSettings setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"]];
185 [item setLabel:@"Show Advanced"];
186 [item setView:toggleAdvancedSettings];
187 [item setAction:@selector(togglePowerSettings:)];
188 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500189
190 if ([itemIdentifier isEqualToString: kDonePrefsIdentifer]) {
191 item = [[NSToolbarItem alloc] initWithItemIdentifier: kDonePrefsIdentifer];
192 [item setImage: [NSImage imageNamed: @"ic_action_cancel"]];
193 [item setLabel: @"Done"];
194 [item setAction:@selector(closePreferences:)];
195 }
196
197 if ([itemIdentifier isEqualToString: kVideoPrefsIdentifer]) {
198 item = [[NSToolbarItem alloc] initWithItemIdentifier: kVideoPrefsIdentifer];
199 [item setImage: [NSImage imageNamed: @"video"]];
200 [item setLabel: @"Video"];
201 [item setAction:@selector(displayVideo:)];
202 }
203
204 return item;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500205}
206
207-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
208{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400209
210 NSMutableArray* items = [NSMutableArray arrayWithObjects:
211 kPowerSettingsIdentifer,
212 NSToolbarFlexibleSpaceItemIdentifier,
213 kGeneralPrefsIdentifier,
214 kAudioPrefsIdentifer,
215 kVideoPrefsIdentifer,
216 // kAncragePrefsIdentifer,
217 NSToolbarFlexibleSpaceItemIdentifier,
218 kDonePrefsIdentifer,
219 nil];
220
221 if([[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"]) {
222 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
223 [items insertObject:kProfilePrefsIdentifier atIndex:2];
224 } else
225 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
226
227 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500228}
229
230-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
231{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400232 NSMutableArray* items = [NSMutableArray arrayWithObjects:
233 kPowerSettingsIdentifer,
234 kGeneralPrefsIdentifier,
235 kAudioPrefsIdentifer,
236 kVideoPrefsIdentifer,
237 nil];
238
239 if([[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"])
240 [items insertObject:kProfilePrefsIdentifier atIndex:1];
241
242
243 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500244}
245
246-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
247{
248 return nil;
249}
250
251
252
253
254
255@end