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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/**
* @file
* Implementation of class HBPreferencesController.
*/
#import "HBPreferencesController.h"
#import "HBLanguagesSelection.h"
#define TOOLBAR_GENERAL @"TOOLBAR_GENERAL"
#define TOOLBAR_AUDIO @"TOOLBAR_AUDIO"
#define TOOLBAR_ADVANCED @"TOOLBAR_ADVANCED"
/**
* This class controls the preferences window of HandBrake. Default values for
* all preferences and user defaults are specified in class method
* @c registerUserDefaults. The preferences window is loaded from
* Preferences.nib file when HBPreferencesController is initialized.
*
* All preferences are bound to user defaults in Interface Builder, therefore
* no getter/setter code is needed in this file (unless more complicated
* preference settings are added that cannot be handled with Cocoa bindings).
*/
@interface HBPreferencesController () <NSTokenFieldDelegate>
{
IBOutlet NSView * fGeneralView, * fAudioView, * fAdvancedView;
IBOutlet NSTextField * fSendEncodeToAppField;
}
/* Manage the send encode to xxx.app windows and field */
- (IBAction) browseSendToApp: (id) sender;
- (void) setPrefView: (id) sender;
- (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
label: (NSString *)label
image: (NSImage *)image;
@property (unsafe_unretained) IBOutlet NSTokenField *formatTokenField;
@property (unsafe_unretained) IBOutlet NSTokenField *builtInTokenField;
@property (nonatomic, readonly, strong) NSArray *buildInFormatTokens;
@property (nonatomic, strong) NSArray *matches;
@property (nonatomic, strong) HBLanguagesSelection *languages;
@end
@implementation HBPreferencesController
/**
* +[HBPreferencesController registerUserDefaults]
*
* Registers default values to user defaults. This is called immediately
* when HandBrake starts, from [HBController init].
*/
+ (void)registerUserDefaults
{
NSString *desktopDirectory = [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject];
NSURL *desktopURL = [NSURL fileURLWithPath:desktopDirectory isDirectory:YES];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"HBShowOpenPanelAtLaunch": @YES,
@"DefaultLanguage": @"English",
@"DefaultMpegExtension": @"Auto",
@"UseDvdNav": @"YES",
@"HBDefaultPresetsDrawerShow": @YES,
// Archive the URL because they aren't supported in plist.
@"HBLastDestinationDirectory": [NSKeyedArchiver archivedDataWithRootObject:desktopURL],
@"HBLastSourceDirectory": [NSKeyedArchiver archivedDataWithRootObject:desktopURL],
@"DefaultAutoNaming": @NO,
@"HBAlertWhenDone": @(HBDoneActionNotification),
@"HBAlertWhenDoneSound": @YES,
@"LoggingLevel": @"1",
@"HBClearOldLogs": @YES,
@"EncodeLogLocation": @"NO",
@"MinTitleScanSeconds": @"10",
@"PreviewsNumber": @"10",
@"x264CqSliderFractional": @"0.50",
@"HBShowAdvancedTab": @NO,
@"HBAutoNamingFormat": @[@"{Source}", @" ", @"{Title}"],
@"HBDrawerSize": NSStringFromSize(NSMakeSize(184, 591))
}];
// Overwrite the update check interval because previous versions
// could be set to a dayly check.
NSUInteger week = 60 * 60 * 24 * 7;
[[NSUserDefaults standardUserDefaults] setObject:@(week) forKey:@"SUScheduledCheckInterval"];
}
/**
* -[HBPreferencesController init]
*
* Initializes the preferences controller by loading Preferences.nib file.
*
*/
- (instancetype)init
{
if (self = [super initWithWindowNibName:@"Preferences"])
{
_languages = [[HBLanguagesSelection alloc] init];
}
return self;
}
- (void)showWindow:(id)sender
{
if (!self.window.isVisible)
{
[self.window center];
}
[super showWindow:sender];
}
/**
* -[HBPreferencesController awakeFromNib]
*
* Called after all the outlets in the nib file have been attached. Sets up the
* toolbar and shows the "General" pane.
*
*/
- (void) awakeFromNib
{
NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"Preferences Toolbar"];
[toolbar setDelegate: self];
[toolbar setAllowsUserCustomization: NO];
[toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
[toolbar setSizeMode: NSToolbarSizeModeRegular];
[[self window] setToolbar: toolbar];
// Format token field initialization
[self.formatTokenField setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"%%"]];
[self.formatTokenField setCompletionDelay:0.2];
_buildInFormatTokens = @[@"{Source}", @"{Title}", @"{Date}", @"{Time}", @"{Chapters}", @"{Quality/Bitrate}"];
[self.builtInTokenField setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"%%"]];
[self.builtInTokenField setStringValue:[self.buildInFormatTokens componentsJoinedByString:@"%%"]];
[toolbar setSelectedItemIdentifier: TOOLBAR_GENERAL];
[self setPrefView:nil];
}
- (NSToolbarItem *)toolbar: (NSToolbar *)toolbar
itemForItemIdentifier: (NSString *)ident
willBeInsertedIntoToolbar: (BOOL)flag
{
if ( [ident isEqualToString:TOOLBAR_GENERAL] )
{
return [self toolbarItemWithIdentifier:ident
label:NSLocalizedString(@"General", @"Preferences General Toolbar Item")
image:[NSImage imageNamed:@"settings"]];
}
else if ( [ident isEqualToString:TOOLBAR_AUDIO] )
{
return [self toolbarItemWithIdentifier:ident
label:NSLocalizedString(@"Audio", @"Preferences Audio Toolbar Item")
image:[NSImage imageNamed:@"audio"]];
}
else if ( [ident isEqualToString:TOOLBAR_ADVANCED] )
{
return [self toolbarItemWithIdentifier:ident
label:NSLocalizedString(@"Advanced", @"Preferences Advanced Toolbar Item")
image:[NSImage imageNamed:@"advanced"]];
}
return nil;
}
- (NSArray *) toolbarSelectableItemIdentifiers: (NSToolbar *) toolbar
{
return [self toolbarDefaultItemIdentifiers: toolbar];
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
return [self toolbarAllowedItemIdentifiers: toolbar];
}
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
{
return @[TOOLBAR_GENERAL, TOOLBAR_AUDIO, TOOLBAR_ADVANCED];
}
/* Manage the send encode to xxx.app windows and field */
/*Opens the app browse window*/
- (IBAction) browseSendToApp: (id) sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
[panel setAllowedFileTypes:@[@"app"]];
[panel setMessage:NSLocalizedString(@"Select the desired external application", nil)];
NSString *sendToAppDirectory;
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"])
{
sendToAppDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSendToAppDirectory"];
}
else
{
sendToAppDirectory = @"/Applications";
}
[panel setDirectoryURL:[NSURL fileURLWithPath:sendToAppDirectory]];
[panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
if (result == NSOKButton)
{
NSURL *sendToAppURL = [panel URL];
NSURL *sendToAppDirectoryURL = [sendToAppURL URLByDeletingLastPathComponent];
[[NSUserDefaults standardUserDefaults] setObject:[sendToAppDirectoryURL path] forKey:@"LastSendToAppDirectory"];
// We set the name of the app to send to in the display field
NSString *sendToAppName = [[sendToAppURL lastPathComponent] stringByDeletingPathExtension];
[fSendEncodeToAppField setStringValue:sendToAppName];
[[NSUserDefaults standardUserDefaults] setObject:[fSendEncodeToAppField stringValue] forKey:@"HBSendToApp"];
}
}];
}
#pragma mark - Format Token Field Delegate
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject
{
if ([representedObject rangeOfString: @"{"].location == 0 && [representedObject length] > 1)
{
return [(NSString *)representedObject substringWithRange:NSMakeRange(1, [(NSString*)representedObject length]-2)];
}
return representedObject;
}
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject
{
if ([representedObject rangeOfString: @"{"].location == 0)
{
return NSRoundedTokenStyle;
}
else
{
return NSPlainTextTokenStyle;
}
}
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{
return editingString;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex
indexOfSelectedItem:(NSInteger *)selectedIndex
{
self.matches = [self.buildInFormatTokens filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", substring]];
return self.matches;
}
- (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject
{
if ([representedObject rangeOfString: @"{"].location == 0)
{
return [NSString stringWithFormat:@"%%%@%%", representedObject];
}
else
{
return representedObject;
}
}
- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index
{
return tokens;
}
- (BOOL)tokenField:(NSTokenField *)tokenField writeRepresentedObjects:(NSArray *)objects toPasteboard:(NSPasteboard *)pboard
{
NSString *format = [objects componentsJoinedByString:@"%%"];
[pboard setString:format forType:NSPasteboardTypeString];
return YES;
}
#pragma mark - Private methods
- (void) setPrefView: (id) sender
{
NSView *view = fGeneralView;
if (sender)
{
NSString *identifier = [sender itemIdentifier];
if ([identifier isEqualToString:TOOLBAR_AUDIO])
{
view = fAudioView;
}
else if([identifier isEqualToString:TOOLBAR_ADVANCED])
{
view = fAdvancedView;
}
}
NSWindow *window = self.window;
if (window.contentView == view)
{
return;
}
window.contentView = view;
if (window.isVisible)
{
view.hidden = YES;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
if ([context respondsToSelector:@selector(setAllowsImplicitAnimation:)])
{
context.allowsImplicitAnimation = YES;
}
[window layoutIfNeeded];
} completionHandler:^{
view.hidden = NO;
}];
}
// set title label
if (sender)
{
window.title = [sender label];
}
else
{
NSToolbar *toolbar = window.toolbar;
NSString *itemIdentifier = toolbar.selectedItemIdentifier;
for (NSToolbarItem *item in toolbar.items)
{
if ([item.itemIdentifier isEqualToString:itemIdentifier])
{
window.title = item.label;
break;
}
}
}
}
/**
* -[HBPreferencesController(Private) toolbarItemWithIdentifier:label:image:]
*
* Shared code for creating the NSToolbarItems for the Preferences toolbar.
*
*/
- (NSToolbarItem *)toolbarItemWithIdentifier: (NSString *)identifier
label: (NSString *)label
image: (NSImage *)image
{
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:identifier];
[item setLabel:label];
[item setImage:image];
[item setAction:@selector(setPrefView:)];
[item setAutovalidates:NO];
return item;
}
@end
|