-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefController.m
180 lines (155 loc) · 6.88 KB
/
PrefController.m
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
//
// PrefController.m
// QueueManager
//
// Created by Cory Powers on 12/20/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "PrefController.h"
#import "SystemEvents.h"
#import "AppController.h"
@implementation PrefController
+ (void)registerUserDefaults: (NSString *)appSupportDir {
//TODO: Change to use NSSearchPathForDirectoriesInDomains
NSString *desktopDirectory = [@"~/Desktop/" stringByExpandingTildeInPath];
// Make sure the debug level and log files settings are actually in the pref file
// to ensure the applescript can read the values.
if([[NSUserDefaults standardUserDefaults] objectForKey:@"logFile"] == nil){
[[NSUserDefaults standardUserDefaults] setObject:[appSupportDir stringByAppendingPathComponent:@"tm.log"] forKey:@"logFile"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([[NSUserDefaults standardUserDefaults] objectForKey:@"debugLevel"] == nil){
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"debugLevel"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"folderActionInstalled": @"NO",
@"monitoredFolder": desktopDirectory,
@"outputFolder": desktopDirectory,
@"allowedExtensions": @"avi,m4v,mkv,mp4",
@"ttdApiKey": @"87C0BA77037B5012",
@"tmdApiKey": @"329edec1b30392adcc0a28f351b09336",
@"transcoderPath": @"/Applications/HandBrakeCLI",
@"transcoderArgs": @"-i |INPUT| -o |OUTPUT| -e x264 -q 20.0 -a 1,1 -E faac,ac3 -B 160,160 -6 dpl2,auto -R 48,Auto -D 0.0,0.0 -f mp4 -4 -X 1280 --loose-anamorphic -m -x cabac=0:ref=2:me=umh:b-adapt=2:weightb=0:trellis=0:weightp=0"}];
}
- (id)initWithController:(AppController *)controller {
if (self = [super initWithController: controller withNibName:@"Preferences"]){
NSAssert([self window], @"[PrefController init] window outlet is not connected in Preferences.nib");
[self setMonitorFields];
}
return self;
}
- (void)showWindow{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSWindow *window = [self window];
// Set the fields to their current values
[window center];
[monitoredFolderField setStringValue:[standardDefaults objectForKey:@"monitoredFolder"]];
[outputFolderField setStringValue:[standardDefaults objectForKey:@"outputFolder"]];
[transcoderField setStringValue:[standardDefaults objectForKey:@"transcoderPath"]];
[transcoderArgsField setStringValue:[standardDefaults objectForKey:@"transcoderArgs"]];
[fileExtensionsField setStringValue:[standardDefaults objectForKey:@"allowedExtensions"]];
[self setMonitorFields];
[window makeKeyAndOrderFront: nil];
}
- (IBAction)closeWindow:(id)sender{
NSWindow *window = [self window];
[window orderOut:sender];
}
- (IBAction)savePrefs:(id)sender{
BOOL isDir;
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *oldWatchedFolder = [[NSUserDefaults standardUserDefaults] stringForKey:@"monitoredFolder"];
NSString *newWatchedFolder = [monitoredFolderField stringValue];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Add trailing slash
if (![newWatchedFolder hasSuffix:@"/"]) {
newWatchedFolder = [newWatchedFolder stringByAppendingString:@"/"];
[monitoredFolderField setStringValue:newWatchedFolder];
}
if (![fileManager fileExistsAtPath:newWatchedFolder isDirectory:&isDir] || !isDir) {
NSRunAlertPanel(@"Invalid monitored folder", @"Invalid monitored folder, make sure the directory exists and is readable", @"Ok", nil, nil);
[monitoredFolderField selectText:sender];
return;
}
if (![fileManager fileExistsAtPath:[outputFolderField stringValue] isDirectory:&isDir] || !isDir) {
NSRunAlertPanel(@"Invalid output folder", @"Invalid output folder, make sure the directory exists and is writable", @"Ok", nil, nil);
[outputFolderField selectText:sender];
return;
}
if (![fileManager isExecutableFileAtPath:[transcoderField stringValue]]) {
NSRunAlertPanel(@"Invalid transcoder path", @"Invalid transcoder path, make sure the file exists and is excutable", @"Ok", nil, nil);
[transcoderField selectText:sender];
return;
}
if(![oldWatchedFolder isEqual:newWatchedFolder]){
// Remove folder action
[self.appController disableFolderActionOn:oldWatchedFolder];
// add new folder action
[self.appController enableFolderActionOn:newWatchedFolder];
}
[standardDefaults setObject:[monitoredFolderField stringValue] forKey:@"monitoredFolder"];
[standardDefaults setObject:[outputFolderField stringValue] forKey:@"outputFolder"];
[standardDefaults setObject:[transcoderField stringValue] forKey:@"transcoderPath"];
[standardDefaults setObject:[transcoderArgsField stringValue] forKey:@"transcoderArgs"];
[standardDefaults setObject:[fileExtensionsField stringValue] forKey:@"allowedExtensions"];
[self closeWindow:sender];
}
- (IBAction)toggleMonitoring:(id)sender{
NSString *newWatchedFolder = [monitoredFolderField stringValue];
// Add trailing slash
if (![newWatchedFolder hasSuffix:@"/"]) {
newWatchedFolder = [newWatchedFolder stringByAppendingString:@"/"];
[monitoredFolderField setStringValue:newWatchedFolder];
}
// Validate path
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if ([fileManager fileExistsAtPath:newWatchedFolder isDirectory:&isDir] && isDir){
if ([[monitoringButton title] isEqual:@"Enable"]) {
[self.appController enableFolderActionOn: newWatchedFolder];
}else{
[self.appController disableFolderActionOn:newWatchedFolder];
}
[self setMonitorFields];
}
}
- (void)setMonitorFields{
NSString *watchedFolder = [monitoredFolderField stringValue];
if ([self.appController areFolderActionsEnabledOn:watchedFolder]) {
[enabledLabel setStringValue:@"Monitoring active"];
[monitoringButton setTitle:@"Disable"];
}else{
[enabledLabel setStringValue:@"Monitoring not active"];
[monitoringButton setTitle:@"Enable"];
}
}
- (IBAction) browseDirectory: (id) sender{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:FALSE];
[panel setCanChooseFiles:FALSE];
[panel setCanChooseDirectories:TRUE];
NSString *panelDir = nil;
if (sender == browseMonitoredButton) {
panelDir = [monitoredFolderField stringValue];
}else{
panelDir = [outputFolderField stringValue];
}
/* We get the current file name and path from the destination field here */
[panel beginSheetForDirectory: panelDir
file: nil
modalForWindow: [self window] modalDelegate: self
didEndSelector: @selector( browseDirectoryDone:returnCode:contextInfo: )
contextInfo: (__bridge void *)(sender)];
}
- (void) browseDirectoryDone: (NSSavePanel *) sheet
returnCode: (int) returnCode
contextInfo: (void *) contextInfo{
if( returnCode == NSOKButton ){
if (contextInfo == (__bridge void *)(browseMonitoredButton)) {
[monitoredFolderField setStringValue:[sheet directory]];
}else{
[outputFolderField setStringValue:[sheet directory]];
}
}
}
@end