PreferencesVC.mm 9.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */
30
#import "PreferencesVC.h"
31

Alexandre Lision's avatar
Alexandre Lision committed
32 33
#import <QuartzCore/QuartzCore.h>

34
#import <accountmodel.h>
35
#import <audio/codecmodel.h>
36 37

#import "AccountsVC.h"
Alexandre Lision's avatar
Alexandre Lision committed
38 39 40
#import "GeneralPrefsVC.h"
#import "AudioPrefsVC.h"
#import "VideoPrefsVC.h"
41
#import "Constants.h"
Alexandre Lision's avatar
Alexandre Lision committed
42

43
@interface PreferencesVC ()
Alexandre Lision's avatar
Alexandre Lision committed
44

45 46
@property NSButton* toggleAdvancedSettings;

Alexandre Lision's avatar
Alexandre Lision committed
47 48
@end

49
@implementation PreferencesVC
50
@synthesize toggleAdvancedSettings;
Alexandre Lision's avatar
Alexandre Lision committed
51

52
static NSString* const kProfilePrefsIdentifier = @"ProfilesPrefsIdentifier";
Alexandre Lision's avatar
Alexandre Lision committed
53 54 55 56 57
static NSString* const kGeneralPrefsIdentifier = @"GeneralPrefsIdentifier";
static NSString* const kAudioPrefsIdentifer = @"AudioPrefsIdentifer";
static NSString* const kAncragePrefsIdentifer = @"AncragePrefsIdentifer";
static NSString* const kVideoPrefsIdentifer = @"VideoPrefsIdentifer";
static NSString* const kDonePrefsIdentifer = @"DonePrefsIdentifer";
58
static NSString* const kPowerSettingsIdentifer = @"PowerSettingsIdentifer";
Alexandre Lision's avatar
Alexandre Lision committed
59 60 61 62 63 64 65 66 67 68 69 70

-(void)loadView
{
    [super loadView];

    [self displayGeneral:nil];

    [self.view setWantsLayer:YES];
    self.view.layer.backgroundColor = [NSColor windowBackgroundColor].CGColor;

    // Set the layer redraw policy. This would be better done in
    // the initialization method of a NSView subclass instead of here.
71
    self.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
Alexandre Lision's avatar
Alexandre Lision committed
72

73 74
    [self.view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

Alexandre Lision's avatar
Alexandre Lision committed
75 76
    CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);

77 78 79 80
    [CATransaction begin];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    animation.fromValue = @(frame.origin.y);
    animation.toValue = @(self.view.frame.origin.y);
Alexandre Lision's avatar
Alexandre Lision committed
81
    animation.duration = 0.3f;
82

Alexandre Lision's avatar
Alexandre Lision committed
83 84
    [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];
    [self.view.layer addAnimation:animation forKey:animation.keyPath];
85
    [CATransaction commit];
Alexandre Lision's avatar
Alexandre Lision committed
86 87 88 89
}

- (void) close
{
90 91 92 93 94
    // first save codecs for each account
    for (int i = 0 ; i < AccountModel::instance()->rowCount(); ++i) {
        QModelIndex qIdx = AccountModel::instance()->index(i);
        AccountModel::instance()->getAccountByModelIndex(qIdx)->codecModel()->save();
    }
95

96
    // then save accounts
97 98
    AccountModel::instance()->save();

Alexandre Lision's avatar
Alexandre Lision committed
99 100 101
    CGRect frame = CGRectOffset(self.view.frame, 0, -self.view.frame.size.height);

    [CATransaction begin];
102 103 104
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    animation.fromValue = @(self.view.frame.origin.y);
    animation.toValue = @(frame.origin.y);
Alexandre Lision's avatar
Alexandre Lision committed
105 106 107 108 109 110 111 112
    animation.duration = 0.3f;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.7 :0.9 :1 :1]];

    [CATransaction setCompletionBlock:^{
        [self.view removeFromSuperview];
    }];

    [self.view.layer addAnimation:animation forKey:animation.keyPath];
113 114 115

    // set final layer position to prevent glitching back to original one
    [self.view.layer setPosition:frame.origin];;
Alexandre Lision's avatar
Alexandre Lision committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    [CATransaction commit];
}

- (void)displayGeneral:(NSToolbarItem *)sender {
    if (self.currentVC != nil) {
        [self.currentVC.view removeFromSuperview];
    }
    self.generalPrefsVC = [[GeneralPrefsVC alloc] initWithNibName:@"GeneralPrefs" bundle:nil];
    [self.view addSubview:self.generalPrefsVC.view];
    [self.generalPrefsVC.view setFrame:[self.view bounds]];
    self.currentVC = self.generalPrefsVC;
}

- (void)displayAudio:(NSToolbarItem *)sender {
    if (self.currentVC != nil) {
        [self.currentVC.view removeFromSuperview];
    }
    self.audioPrefsVC = [[AudioPrefsVC alloc] initWithNibName:@"AudioPrefs" bundle:nil];
    [self.view addSubview:self.audioPrefsVC.view];
    [self.audioPrefsVC.view setFrame:[self.view bounds]];
    self.currentVC = self.audioPrefsVC;
}

- (void)displayAncrage:(NSToolbarItem *)sender {

}

- (void)displayVideo:(NSToolbarItem *)sender {
    if (self.currentVC != nil) {
        [self.currentVC.view removeFromSuperview];
    }
    self.videoPrefsVC = [[VideoPrefsVC alloc] initWithNibName:@"VideoPrefs" bundle:nil];
    [self.view addSubview:self.videoPrefsVC.view];
    [self.videoPrefsVC.view setFrame:[self.view bounds]];
    self.currentVC = self.videoPrefsVC;
}

153 154 155 156 157 158 159 160 161 162
- (void) displayAccounts:(NSToolbarItem *) sender {
    if (self.currentVC != nil) {
        [self.currentVC.view removeFromSuperview];
    }
    self.accountsPrefsVC = [[AccountsVC alloc] initWithNibName:@"Accounts" bundle:nil];
    [self.view addSubview:self.accountsPrefsVC.view];
    [self.accountsPrefsVC.view setFrame:[self.view bounds]];
    self.currentVC = self.accountsPrefsVC;
}

Alexandre Lision's avatar
Alexandre Lision committed
163 164 165 166 167 168 169

#pragma NSToolbar Delegate

-(NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
    NSToolbarItem* item = nil;

170 171 172 173 174 175 176 177
    if ([itemIdentifier isEqualToString: kProfilePrefsIdentifier]) {

        item = [[NSToolbarItem alloc] initWithItemIdentifier: kProfilePrefsIdentifier];
        [item setImage: [NSImage imageNamed: @"NSUserAccounts"]];
        [item setLabel: @"Accounts"];
        [item setAction:@selector(displayAccounts:)];
    }

Alexandre Lision's avatar
Alexandre Lision committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
    if ([itemIdentifier isEqualToString: kGeneralPrefsIdentifier]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: kGeneralPrefsIdentifier];
        [item setImage: [NSImage imageNamed: @"general"]];
        [item setLabel: @"General"];
        [item setAction:@selector(displayGeneral:)];
    }

    if ([itemIdentifier isEqualToString: kAudioPrefsIdentifer]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: kAudioPrefsIdentifer];
        [item setImage: [NSImage imageNamed: @"audio"]];
        [item setLabel: @"Audio"];
        [item setAction:@selector(displayAudio:)];
    }

192 193 194 195 196
    if ([itemIdentifier isEqualToString: kPowerSettingsIdentifer]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: kPowerSettingsIdentifer];
        toggleAdvancedSettings = [[NSButton alloc] initWithFrame:NSMakeRect(0,0,20,20)];
        [toggleAdvancedSettings setButtonType:NSSwitchButton];
        [toggleAdvancedSettings setTitle:@""];
197
        [toggleAdvancedSettings setState:[[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
198 199 200 201
        [item setLabel:@"Show Advanced"];
        [item setView:toggleAdvancedSettings];
        [item setAction:@selector(togglePowerSettings:)];
    }
Alexandre Lision's avatar
Alexandre Lision committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    if ([itemIdentifier isEqualToString: kDonePrefsIdentifer]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: kDonePrefsIdentifer];
        [item setImage: [NSImage imageNamed: @"ic_action_cancel"]];
        [item setLabel: @"Done"];
        [item setAction:@selector(closePreferences:)];
    }

    if ([itemIdentifier isEqualToString: kVideoPrefsIdentifer]) {
        item = [[NSToolbarItem alloc] initWithItemIdentifier: kVideoPrefsIdentifer];
        [item setImage: [NSImage imageNamed: @"video"]];
        [item setLabel: @"Video"];
        [item setAction:@selector(displayVideo:)];
    }

    return item;
}

-(NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
222 223 224 225 226 227 228 229 230 231 232 233

    NSMutableArray* items = [NSMutableArray arrayWithObjects:
                      kPowerSettingsIdentifer,
                      NSToolbarFlexibleSpaceItemIdentifier,
                      kGeneralPrefsIdentifier,
                      kAudioPrefsIdentifer,
                      kVideoPrefsIdentifer,
                      //           kAncragePrefsIdentifer,
                      NSToolbarFlexibleSpaceItemIdentifier,
                      kDonePrefsIdentifer,
                      nil];

234
    if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]) {
235 236 237 238 239 240
        [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];
        [items insertObject:kProfilePrefsIdentifier atIndex:2];
    } else
        [items insertObject:NSToolbarSpaceItemIdentifier atIndex:5];

    return items;
Alexandre Lision's avatar
Alexandre Lision committed
241 242 243 244
}

-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
245 246 247 248 249 250 251
    NSMutableArray* items = [NSMutableArray arrayWithObjects:
                             kPowerSettingsIdentifer,
                             kGeneralPrefsIdentifier,
                             kAudioPrefsIdentifer,
                             kVideoPrefsIdentifer,
                             nil];

252
    if([[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced])
253 254 255 256
        [items insertObject:kProfilePrefsIdentifier atIndex:1];


    return items;
Alexandre Lision's avatar
Alexandre Lision committed
257 258 259 260 261 262 263 264 265 266 267 268
}

-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
    return nil;
}





@end