blob: 5ccde7a043827bf39ef2252a7f683e42ea333852 [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 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"
41
42@interface PreferencesViewController ()
43
Alexandre Lision1154eb32015-03-20 18:31:48 -040044@property NSButton* toggleAdvancedSettings;
45
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050046@end
47
48@implementation PreferencesViewController
Alexandre Lision1154eb32015-03-20 18:31:48 -040049@synthesize toggleAdvancedSettings;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050050
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040051static NSString* const kProfilePrefsIdentifier = @"ProfilesPrefsIdentifier";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050052static NSString* const kGeneralPrefsIdentifier = @"GeneralPrefsIdentifier";
53static NSString* const kAudioPrefsIdentifer = @"AudioPrefsIdentifer";
54static NSString* const kAncragePrefsIdentifer = @"AncragePrefsIdentifer";
55static NSString* const kVideoPrefsIdentifer = @"VideoPrefsIdentifer";
56static NSString* const kDonePrefsIdentifer = @"DonePrefsIdentifer";
Alexandre Lision1154eb32015-03-20 18:31:48 -040057static NSString* const kPowerSettingsIdentifer = @"PowerSettingsIdentifer";
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050058
59-(void)loadView
60{
61 [super loadView];
62
63 [self displayGeneral:nil];
64
65 [self.view setWantsLayer:YES];
66 self.view.layer.backgroundColor = [NSColor windowBackgroundColor].CGColor;
67
68 // Set the layer redraw policy. This would be better done in
69 // the initialization method of a NSView subclass instead of here.
70 self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
71
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072 [self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
73
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050074 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
75
76 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
77 animation.fromValue = [NSValue valueWithPoint:frame.origin];
78 animation.toValue = [NSValue valueWithPoint:self.view.frame.origin];
79 animation.duration = 0.3f;
80 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050081 [self.view.layer addAnimation:animation forKey:animation.keyPath];
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050082}
83
84- (void) close
85{
Alexandre Lisione4041492015-03-20 18:20:43 -040086 // first save codecs for each account
87 for (int i = 0 ; i < AccountModel::instance()->rowCount(); ++i) {
88 QModelIndex qIdx = AccountModel::instance()->index(i);
89 AccountModel::instance()->getAccountByModelIndex(qIdx)->codecModel()->save();
90 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040091
Alexandre Lisione4041492015-03-20 18:20:43 -040092 // then save accounts
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040093 AccountModel::instance()->save();
94
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050095 CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);
96
97 [CATransaction begin];
98 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
99 animation.fromValue = [NSValue valueWithPoint:self.view.frame.origin];
100 animation.toValue = [NSValue valueWithPoint:frame.origin];
101 animation.duration = 0.3f;
102 [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
103
104 [CATransaction setCompletionBlock:^{
105 [self.view removeFromSuperview];
106 }];
107
108
109 [self.view.layer addAnimation:animation forKey:animation.keyPath];
110 [CATransaction commit];
111}
112
113- (void)displayGeneral:(NSToolbarItem *)sender {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500114 if (self.currentVC != nil) {
115 [self.currentVC.view removeFromSuperview];
116 }
117 self.generalPrefsVC = [[GeneralPrefsVC alloc] initWithNibName:@"GeneralPrefs" bundle:nil];
118 [self.view addSubview:self.generalPrefsVC.view];
119 [self.generalPrefsVC.view setFrame:[self.view bounds]];
120 self.currentVC = self.generalPrefsVC;
121}
122
123- (void)displayAudio:(NSToolbarItem *)sender {
124 if (self.currentVC != nil) {
125 [self.currentVC.view removeFromSuperview];
126 }
127 self.audioPrefsVC = [[AudioPrefsVC alloc] initWithNibName:@"AudioPrefs" bundle:nil];
128 [self.view addSubview:self.audioPrefsVC.view];
129 [self.audioPrefsVC.view setFrame:[self.view bounds]];
130 self.currentVC = self.audioPrefsVC;
131}
132
133- (void)displayAncrage:(NSToolbarItem *)sender {
134
135}
136
137- (void)displayVideo:(NSToolbarItem *)sender {
138 if (self.currentVC != nil) {
139 [self.currentVC.view removeFromSuperview];
140 }
141 self.videoPrefsVC = [[VideoPrefsVC alloc] initWithNibName:@"VideoPrefs" bundle:nil];
142 [self.view addSubview:self.videoPrefsVC.view];
143 [self.videoPrefsVC.view setFrame:[self.view bounds]];
144 self.currentVC = self.videoPrefsVC;
145}
146
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400147- (void) displayAccounts:(NSToolbarItem *) sender {
148 if (self.currentVC != nil) {
149 [self.currentVC.view removeFromSuperview];
150 }
151 self.accountsPrefsVC = [[AccountsVC alloc] initWithNibName:@"Accounts" bundle:nil];
152 [self.view addSubview:self.accountsPrefsVC.view];
153 [self.accountsPrefsVC.view setFrame:[self.view bounds]];
154 self.currentVC = self.accountsPrefsVC;
155}
156
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500157
158#pragma NSToolbar Delegate
159
160-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
161{
162 NSToolbarItem* item = nil;
163
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400164 if ([itemIdentifier isEqualToString: kProfilePrefsIdentifier]) {
165
166 item = [[NSToolbarItem alloc] initWithItemIdentifier: kProfilePrefsIdentifier];
167 [item setImage: [NSImage imageNamed: @"NSUserAccounts"]];
168 [item setLabel: @"Accounts"];
169 [item setAction:@selector(displayAccounts:)];
170 }
171
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500172 if ([itemIdentifier isEqualToString: kGeneralPrefsIdentifier]) {
173 item = [[NSToolbarItem alloc] initWithItemIdentifier: kGeneralPrefsIdentifier];
174 [item setImage: [NSImage imageNamed: @"general"]];
175 [item setLabel: @"General"];
176 [item setAction:@selector(displayGeneral:)];
177 }
178
179 if ([itemIdentifier isEqualToString: kAudioPrefsIdentifer]) {
180 item = [[NSToolbarItem alloc] initWithItemIdentifier: kAudioPrefsIdentifer];
181 [item setImage: [NSImage imageNamed: @"audio"]];
182 [item setLabel: @"Audio"];
183 [item setAction:@selector(displayAudio:)];
184 }
185
Alexandre Lision1154eb32015-03-20 18:31:48 -0400186 if ([itemIdentifier isEqualToString: kPowerSettingsIdentifer]) {
187 item = [[NSToolbarItem alloc] initWithItemIdentifier: kPowerSettingsIdentifer];
188 toggleAdvancedSettings = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,20,20)];
189 [toggleAdvancedSettings setButtonType:NSSwitchButton];
190 [toggleAdvancedSettings setTitle:@""];
191 [toggleAdvancedSettings setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"]];
192 [item setLabel:@"Show Advanced"];
193 [item setView:toggleAdvancedSettings];
194 [item setAction:@selector(togglePowerSettings:)];
195 }
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500196
197 if ([itemIdentifier isEqualToString: kDonePrefsIdentifer]) {
198 item = [[NSToolbarItem alloc] initWithItemIdentifier: kDonePrefsIdentifer];
199 [item setImage: [NSImage imageNamed: @"ic_action_cancel"]];
200 [item setLabel: @"Done"];
201 [item setAction:@selector(closePreferences:)];
202 }
203
204 if ([itemIdentifier isEqualToString: kVideoPrefsIdentifer]) {
205 item = [[NSToolbarItem alloc] initWithItemIdentifier: kVideoPrefsIdentifer];
206 [item setImage: [NSImage imageNamed: @"video"]];
207 [item setLabel: @"Video"];
208 [item setAction:@selector(displayVideo:)];
209 }
210
211 return item;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500212}
213
214-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
215{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400216
217 NSMutableArray* items = [NSMutableArray arrayWithObjects:
218 kPowerSettingsIdentifer,
219 NSToolbarFlexibleSpaceItemIdentifier,
220 kGeneralPrefsIdentifier,
221 kAudioPrefsIdentifer,
222 kVideoPrefsIdentifer,
223 // kAncragePrefsIdentifer,
224 NSToolbarFlexibleSpaceItemIdentifier,
225 kDonePrefsIdentifer,
226 nil];
227
228 if([[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"]) {
229 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
230 [items insertObject:kProfilePrefsIdentifier atIndex:2];
231 } else
232 [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
233
234 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500235}
236
237-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
238{
Alexandre Lision1154eb32015-03-20 18:31:48 -0400239 NSMutableArray* items = [NSMutableArray arrayWithObjects:
240 kPowerSettingsIdentifer,
241 kGeneralPrefsIdentifier,
242 kAudioPrefsIdentifer,
243 kVideoPrefsIdentifer,
244 nil];
245
246 if([[NSUserDefaults standardUserDefaults] boolForKey:@"show_advanced"])
247 [items insertObject:kProfilePrefsIdentifier atIndex:1];
248
249
250 return items;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500251}
252
253-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
254{
255 return nil;
256}
257
258
259
260
261
262@end