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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* HBAppDelegate.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBAppDelegate.h"
#import "HBUtilities.h"
#import "HBPresetsManager.h"
#import "HBPreset.h"
#import "HBPresetsMenuBuilder.h"
#import "HBPreferencesController.h"
#import "HBQueueController.h"
#import "HBOutputPanelController.h"
#import "HBController.h"
@import HandBrakeKit;
#define PRESET_FILE @"UserPresets.json"
#define QUEUE_FILE @"Queue.hbqueue"
@interface HBAppDelegate ()
@property (nonatomic, strong) HBPresetsManager *presetsManager;
@property (nonatomic, strong) HBPresetsMenuBuilder *presetsMenuBuilder;
@property (unsafe_unretained) IBOutlet NSMenu *presetsMenu;
@property (nonatomic, strong) HBPreferencesController *preferencesController;
@property (nonatomic, strong) HBQueueController *queueController;
@property (nonatomic, strong) HBOutputPanelController *outputPanel;
@property (nonatomic, strong) HBController *mainController;
@end
@implementation HBAppDelegate
- (instancetype)init
{
self = [super init];
if (self)
{
// Register the default preferences
[HBPreferencesController registerUserDefaults];
[HBCore initGlobal];
[HBCore registerErrorHandler:^(NSString *error) {
fprintf(stderr, "error: %s\n", error.UTF8String);
}];
[HBCore setDVDNav:[[NSUserDefaults standardUserDefaults] boolForKey:@"UseDvdNav"]];
_outputPanel = [[HBOutputPanelController alloc] init];
// we init the HBPresetsManager
NSURL *appSupportURL = [HBUtilities appSupportURL];
_presetsManager = [[HBPresetsManager alloc] initWithURL:[appSupportURL URLByAppendingPathComponent:PRESET_FILE]];
// Queue
_queueController = [[HBQueueController alloc] initWithURL:[appSupportURL URLByAppendingPathComponent:QUEUE_FILE]];
_queueController.delegate = self;
_mainController = [[HBController alloc] initWithQueue:_queueController presetsManager:_presetsManager];
}
return self;
}
#pragma mark - NSApplicationDelegate
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
if (!flag)
{
[self.mainController showWindow:nil];
}
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
if (@available (macOS 10.12.2, *))
{
NSApplication.sharedApplication.automaticCustomizeTouchBarMenuItemEnabled = YES;
}
self.presetsMenuBuilder = [[HBPresetsMenuBuilder alloc] initWithMenu:self.presetsMenu
action:@selector(selectPresetFromMenu:)
size:[NSFont systemFontSize]
presetsManager:self.presetsManager];
[self.presetsMenuBuilder build];
// Get the number of HandBrake instances currently running
NSUInteger instances = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]].count;
// Open debug output window now if it was visible when HB was closed
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OutputPanelIsOpen"])
[self showOutputPanel:nil];
// On Screen Notification
// We check to see if there is already another instance of hb running.
if (instances > 1)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:NSLocalizedString(@"There is already an instance of HandBrake running.", @"Queue -> Multiple instances alert message")];
[alert setInformativeText:NSLocalizedString(@"The queue will be shared between the instances.", @"Queue -> Multiple instances alert informative text")];
[alert runModal];
}
else
{
[self.queueController setEncodingJobsAsPending];
[self.queueController removeCompletedJobs];
}
// Now we re-check the queue array to see if there are
// any remaining encodes to be done
if (self.queueController.count)
{
[self showMainWindow:self];
[self showQueueWindow:self];
}
else
{
// Open queue window now if it was visible when HB was closed
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QueueWindowIsOpen"])
[self showQueueWindow:nil];
[self showMainWindow:self];
[self.mainController launchAction];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Remove encodes logs older than a month
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HBClearOldLogs"])
{
[self cleanEncodeLogs];
}
// If we are a single instance it is safe to clean up the previews if there are any
// left over. This is a bit of a kludge but will prevent a build up of old instance
// live preview cruft. No danger of removing an active preview directory since they
// are created later in HBPreviewController if they don't exist at the moment a live
// preview encode is initiated.
if (instances == 1)
{
[self cleanPreviews];
}
});
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
{
if (self.queueController.core.state != HBStateIdle)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:NSLocalizedString(@"Are you sure you want to quit HandBrake?", @"Quit Alert -> message")];
[alert setInformativeText:NSLocalizedString(@"If you quit HandBrake your current encode will be reloaded into your queue at next launch. Do you want to quit anyway?", @"Quit Alert -> informative text")];
[alert addButtonWithTitle:NSLocalizedString(@"Quit", @"Quit Alert -> first button")];
[alert addButtonWithTitle:NSLocalizedString(@"Don't Quit", @"Quit Alert -> second button")];
[alert.buttons[1] setKeyEquivalent:@"\E"];
[alert setAlertStyle:NSAlertStyleCritical];
NSInteger result = [alert runModal];
if (result == NSAlertFirstButtonReturn)
{
return NSTerminateNow;
}
else
{
return NSTerminateCancel;
}
}
return NSTerminateNow;
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
[self.presetsManager savePresets];
[[NSUserDefaults standardUserDefaults] setBool:_queueController.window.isVisible forKey:@"QueueWindowIsOpen"];
[[NSUserDefaults standardUserDefaults] setBool:_outputPanel.window.isVisible forKey:@"OutputPanelIsOpen"];
_mainController = nil;
_queueController = nil;
[HBCore closeGlobal];
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
[self.mainController openURL:[NSURL fileURLWithPath:filenames.firstObject]];
[NSApp replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
SEL action = menuItem.action;
if (action == @selector(rip:) || action == @selector(pause:))
{
// Delegate the validation to the queue controller
return [self.queueController validateMenuItem:menuItem];
}
else if (action == @selector(showAddPresetPanel:) ||
action == @selector(showPreviewWindow:) ||
action == @selector(browseSources:))
{
// Delegate the validation to the main controller
return [self.mainController validateMenuItem:menuItem];
}
return YES;
}
#pragma mark - Clean ups
/**
* Clears the EncodeLogs folder, removes the logs
* older than a month.
*/
- (void)cleanEncodeLogs
{
NSURL *directoryUrl = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"EncodeLogs"];
if (directoryUrl)
{
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:directoryUrl
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
NSDirectoryEnumerationSkipsHiddenFiles |
NSDirectoryEnumerationSkipsPackageDescendants
error:NULL];
NSDate *limit = [NSDate dateWithTimeIntervalSinceNow: -(60 * 60 * 24 * 30)];
NSFileManager *manager = [[NSFileManager alloc] init];
for (NSURL *fileURL in contents)
{
NSDate *creationDate = nil;
[fileURL getResourceValue:&creationDate forKey:NSURLCreationDateKey error:NULL];
if ([creationDate isLessThan:limit])
{
[manager removeItemAtURL:fileURL error:NULL];
}
}
}
}
- (void)cleanPreviews
{
NSURL *previewDirectory = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"Previews"];
if (previewDirectory)
{
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:previewDirectory
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
NSDirectoryEnumerationSkipsPackageDescendants
error:NULL];
NSFileManager *manager = [[NSFileManager alloc] init];
for (NSURL *url in contents)
{
NSError *error = nil;
BOOL result = [manager removeItemAtURL:url error:&error];
if (result == NO && error)
{
[HBUtilities writeToActivityLog: "Could not remove existing preview at : %s", url.lastPathComponent.UTF8String];
}
}
}
}
#pragma mark - Menu actions
- (IBAction)rip:(id)sender
{
[self.queueController rip:self];
}
- (IBAction)pause:(id)sender
{
[self.queueController togglePauseResume:self];
}
- (IBAction)browseSources:(id)sender
{
[self.mainController browseSources:self];
}
#pragma mark - Presets Menu actions
/**
* We use this method to recreate new, updated factory presets
*/
- (IBAction)addFactoryPresets:(id)sender
{
[self.presetsManager generateBuiltInPresets];
}
- (IBAction)reloadPreset:(id)sender
{
[self.mainController reloadPreset:self];
}
#pragma mark - Show Window Menu Items
/**
* Shows preferences window.
*/
- (IBAction)showPreferencesWindow:(id)sender
{
if (_preferencesController == nil)
{
_preferencesController = [[HBPreferencesController alloc] init];
}
[self.preferencesController showWindow:self];
}
/**
* Shows queue window.
*/
- (IBAction)showQueueWindow:(id)sender
{
[self.queueController showWindow:sender];
}
/**
* Shows debug output window.
*/
- (IBAction)showOutputPanel:(id)sender
{
[self.outputPanel showWindow:sender];
}
- (IBAction)showPreviewWindow:(id)sender
{
[self.mainController showPreviewWindow:self];
}
/**
* Shows main window.
*/
- (IBAction)showMainWindow:(id)sender
{
[self.mainController showWindow:sender];
}
- (IBAction)openHomepage:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL
URLWithString:@"https://handbrake.fr/"]];
}
- (IBAction)openForums:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL
URLWithString:@"https://forum.handbrake.fr/"]];
}
- (IBAction)openUserGuide:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:HBUtilities.documentationURL];
}
@end
@interface NSApplication (TouchBar) <NSTouchBarDelegate>
@end
@implementation NSApplication (TouchBar)
static NSTouchBarItemIdentifier HBTouchBarMain = @"fr.handbrake.appDelegateTouchBar";
static NSTouchBarItemIdentifier HBTouchBarOpen = @"fr.handbrake.openSource";
- (NSTouchBar *)makeTouchBar
{
NSTouchBar *bar = [[NSTouchBar alloc] init];
bar.delegate = self;
bar.defaultItemIdentifiers = @[HBTouchBarOpen];
return bar;
}
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([identifier isEqualTo:HBTouchBarOpen])
{
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
item.customizationLabel = NSLocalizedString(@"Open Source", @"Touch bar");
NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Open Source", @"Touch bar") target:nil action:@selector(browseSources:)];
item.view = button;
return item;
}
return nil;
}
@end
|