blob: 26f11d8030ccf1ebea27154e9cd30e116468771b [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.
71 self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
72
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
77 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
78 animation.fromValue = [NSValue valueWithPoint:frame.origin];
79 animation.toValue = [NSValue valueWithPoint:self.view.frame.origin];
80 animation.duration = 0.3f;
81 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050082 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050083}
84
85- (void) close
86{
Alexandre Lisione4041492015-03-20 18:20:43 -040087 // first save codecs for each account
88 for (int i = 0 ; i < AccountModel::instance()->rowCount(); ++i) {
89 QModelIndex qIdx = AccountModel::instance()->index(i);
90 AccountModel::instance()->getAccountByModelIndex(qIdx)->codecModel()->save();
91 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040092
Alexandre Lisione4041492015-03-20 18:20:43 -040093 // then save accounts
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040094 AccountModel::instance()->save();
95
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050096 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
97
98 [CATransaction begin];
99 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
100 animation.fromValue = [NSValue valueWithPoint:self.view.frame.origin];
101 animation.toValue = [NSValue valueWithPoint:frame.origin];
102 animation.duration = 0.3f;
103 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
104
105 [CATransaction setCompletionBlock:^{
106 [self.view removeFromSuperview];
107 }];
108
109
110 [self.view.layer addAnimation:animation forKey:animation.keyPath];
111 [CATransaction commit];
112}
113
114- (void)displayGeneral:(NSToolbarItem *)sender {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500115 if (self.currentVC != nil) {
116 [self.currentVC.view removeFromSuperview];
117 }
118 self.generalPrefsVC = [[GeneralPrefsVC alloc] initWithNibName:@"GeneralPrefs" bundle:nil];
119 [self.view addSubview:self.generalPrefsVC.view];
120 [self.generalPrefsVC.view setFrame:[self.view bounds]];
121 self.currentVC = self.generalPrefsVC;
122}
123
124- (void)displayAudio:(NSToolbarItem *)sender {
125 if (self.currentVC != nil) {
126 [self.currentVC.view removeFromSuperview];
127 }
128 self.audioPrefsVC = [[AudioPrefsVC alloc] initWithNibName:@"AudioPrefs" bundle:nil];
129 [self.view addSubview:self.audioPrefsVC.view];
130 [self.audioPrefsVC.view setFrame:[self.view bounds]];
131 self.currentVC = self.audioPrefsVC;
132}
133
134- (void)displayAncrage:(NSToolbarItem *)sender {
135
136}
137
138- (void)displayVideo:(NSToolbarItem *)sender {
139 if (self.currentVC != nil) {
140 [self.currentVC.view removeFromSuperview];
141 }
142 self.videoPrefsVC = [[VideoPrefsVC alloc] initWithNibName:@"VideoPrefs" bundle:nil];
143 [self.view addSubview:self.videoPrefsVC.view];
144 [self.videoPrefsVC.view setFrame:[self.view bounds]];
145 self.currentVC = self.videoPrefsVC;
146}
147
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400148- (void) displayAccounts:(NSToolbarItem *) sender {
149 if (self.currentVC != nil) {
150 [self.currentVC.view removeFromSuperview];
151 }
152 self.accountsPrefsVC = [[AccountsVC alloc] initWithNibName:@"Accounts" bundle:nil];
153 [self.view addSubview:self.accountsPrefsVC.view];
154 [self.accountsPrefsVC.view setFrame:[self.view bounds]];
155 self.currentVC = self.accountsPrefsVC;
156}
157
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500158
159#pragma NSToolbar Delegate
160
161-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
162{
163 NSToolbarItem* item = nil;
164
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400165 if ([itemIdentifier isEqualToString: kProfilePrefsIdentifier]) {
166
167 item = [[NSToolbarItem alloc] initWithItemIdentifier: kProfilePrefsIdentifier];
168 [item setImage: [NSImage imageNamed: @"NSUserAccounts"]];
169 [item setLabel: @"Accounts"];
170 [item setAction:@selector(displayAccounts:)];
171 }
172
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500173 if ([itemIdentifier isEqualToString: kGeneralPrefsIdentifier]) {
174 item = [[NSToolbarItem alloc] initWithItemIdentifier: kGeneralPrefsIdentifier];
175 [item setImage: [NSImage imageNamed: @"general"]];
176 [item setLabel: @"General"];
177 [item setAction:@selector(displayGeneral:)];
178 }
179
180 if ([itemIdentifier isEqualToString: kAudioPrefsIdentifer]) {
181 item = [[NSToolbarItem alloc] initWithItemIdentifier: kAudioPrefsIdentifer];
182 [item setImage: [NSImage imageNamed: @"audio"]];
183 [item setLabel: @"Audio"];
184 [item setAction:@selector(displayAudio:)];
185 }
186
Alexandre Lision1154eb32015-03-20 18:31:48 -0400187 if ([itemIdentifier isEqualToString: kPowerSettingsIdentifer]) {
188 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPowerSettingsIdentifer];
189 toggleAdvancedSettings = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,20,20)];
190 [toggleAdvancedSettings setButtonType:NSSwitchButton];
191 [toggleAdvancedSettings setTitle:@""];
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400192 [toggleAdvancedSettings setState:[[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
Alexandre Lision1154eb32015-03-20 18:31:48 -0400193 [item setLabel:@"Show Advanced"];
194 [item setView:toggleAdvancedSettings];
195 [item setAction:@selector(togglePowerSettings:)];
196 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500197
198 if ([itemIdentifier isEqualToString: kDonePrefsIdentifer]) {
199 item = [[NSToolbarItem alloc] initWithItemIdentifier: kDonePrefsIdentifer];
200 [item setImage: [NSImage imageNamed: @"ic_action_cancel"]];
201 [item setLabel: @"Done"];
202 [item setAction:@selector(closePreferences:)];
203 }
204
205 if ([itemIdentifier isEqualToString: kVideoPrefsIdentifer]) {
206 item = [[NSToolbarItem alloc] initWithItemIdentifier: kVideoPrefsIdentifer];
207 [item setImage: [NSImage imageNamed: @"video"]];
208 [item setLabel: @"Video"];
209 [item setAction:@selector(displayVideo:)];
210 }
211
212 return item;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500213}
214
215-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
216{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400217
218 NSMutableArray* items = [NSMutableArray arrayWithObjects:
219 kPowerSettingsIdentifer,
220 NSToolbarFlexibleSpaceItemIdentifier,
221 kGeneralPrefsIdentifier,
222 kAudioPrefsIdentifer,
223 kVideoPrefsIdentifer,
224 // kAncragePrefsIdentifer,
225 NSToolbarFlexibleSpaceItemIdentifier,
226 kDonePrefsIdentifer,
227 nil];
228
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400229 if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]) {
Alexandre Lision1154eb32015-03-20 18:31:48 -0400230 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
231 [items insertObject:kProfilePrefsIdentifier atIndex:2];
232 } else
233 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
234
235 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500236}
237
238-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
239{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400240 NSMutableArray* items = [NSMutableArray arrayWithObjects:
241 kPowerSettingsIdentifer,
242 kGeneralPrefsIdentifier,
243 kAudioPrefsIdentifer,
244 kVideoPrefsIdentifer,
245 nil];
246
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400247 if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced])
Alexandre Lision1154eb32015-03-20 18:31:48 -0400248 [items insertObject:kProfilePrefsIdentifier atIndex:1];
249
250
251 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500252}
253
254-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
255{
256 return nil;
257}
258
259
260
261
262
263@end