-
Notifications
You must be signed in to change notification settings - Fork 48
/
AudioBookBinderAppDelegate.m
219 lines (175 loc) · 8.01 KB
/
AudioBookBinderAppDelegate.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
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
//
// Copyright (c) 2010-2016 Oleksandr Tymoshenko <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice unmodified, this list of conditions, and the following
// disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
#import "AudioBookBinderAppDelegate.h"
#import "ExpandedPathToPathTransformer.h"
#import "ExpandedPathToIconTransformer.h"
#import "AudioBinderWindowController.h"
#import "QueueController.h"
#import "ConfigNames.h"
#define TEXT_ACTION_REQUIRED NSLocalizedString(@"User action required", nil)
#define TEXT_UPGRADE_HACK \
NSLocalizedString(@"It seems you are upgrading from previous version of Audiobook Binder. This upgrade introduces change in configuration format that requires your action: please confirm destination folder for audiobook files. This is one-time operation.", nil)
BOOL requiresUpdateHack = NO;
static BOOL hackChecked = NO;
@interface AudioBookBinderAppDelegate() {
IBOutlet NSMenu *applicationMenu;
NSMutableArray *windowControllers;
}
@end
@implementation AudioBookBinderAppDelegate
+ (void) initialize
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// make sure it's ran only once
if (!hackChecked) {
NSString *dir = [defaults stringForKey:kConfigDestinationFolder];
NSURL *url = [NSURL URLByResolvingBookmarkData:[defaults objectForKey:kConfigDestinationFolderBookmark] options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
if ((dir != nil) && (url == nil)) {
requiresUpdateHack = YES;
}
hackChecked = YES;
}
NSMutableDictionary *appDefaults = [NSMutableDictionary
dictionaryWithObject:[NSNumber numberWithInt:2] forKey:kConfigChannels];
// for checkbox "add to itunes"
[appDefaults setObject:[NSNumber numberWithBool:YES] forKey:kConfigAddToITunes];
[appDefaults setObject:@"44100" forKey:kConfigSampleRate];
[appDefaults setObject:@"128000" forKey:kConfigBitrate];
[appDefaults setObject:[NSNumber numberWithInt:12] forKey:kConfigMaxVolumeSize];
[appDefaults setObject:[NSNumber numberWithBool:YES] forKey:kConfigSortAudioFiles];
[appDefaults setObject:[NSNumber numberWithBool:YES] forKey:kConfigChaptersEnabled];
// for pop-up button Destination Folder
NSArray* paths = NSSearchPathForDirectoriesInDomains(
NSMusicDirectory,
NSUserDomainMask,
YES);
NSString *musicPath;
if ([paths count])
musicPath = [paths objectAtIndex:0];
else // just use something
musicPath = NSHomeDirectory();
[appDefaults setObject:musicPath forKey:kConfigDestinationFolder];
[defaults registerDefaults:appDefaults];
//set custom value transformers
ExpandedPathToPathTransformer * pathTransformer = [[ExpandedPathToPathTransformer alloc] init];
[NSValueTransformer setValueTransformer: pathTransformer forName: @"ExpandedPathToPathTransformer"];
ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
[NSValueTransformer setValueTransformer: iconTransformer forName: @"ExpandedPathToIconTransformer"];
}
- (id) init
{
self = [super init];
if (self) {
windowControllers = [NSMutableArray new];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL *url;
NSMenu *firstSubmenu = [[applicationMenu itemAtIndex:0] submenu];
[firstSubmenu removeItemAtIndex:1];
if (requiresUpdateHack) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currentDest = [defaults stringForKey:kConfigDestinationFolder];
while ((url = [NSURL URLByResolvingBookmarkData:[defaults objectForKey:kConfigDestinationFolderBookmark] options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil]) == nil) {
NSAlert *a = [[NSAlert alloc] init];
[a setMessageText:TEXT_ACTION_REQUIRED];
[a setInformativeText:TEXT_UPGRADE_HACK];
[a setAlertStyle:NSWarningAlertStyle];
[a runModal];
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setPrompt: NSLocalizedString(@"Select", nil)];
[panel setAllowsMultipleSelection: NO];
[panel setCanChooseFiles: NO];
[panel setCanChooseDirectories: YES];
[panel setCanCreateDirectories: YES];
[panel setDirectoryURL:[NSURL fileURLWithPath:currentDest]];
NSInteger result = [panel runModal];
if (result == NSOKButton)
{
NSURL *folderURL = [panel URL];
NSData* data = [folderURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope includingResourceValuesForKeys:nil relativeToURL:nil error:nil];
[defaults setObject:data forKey:kConfigDestinationFolderBookmark];
[defaults synchronize];
break;
}
}
}
self.queueController = [[QueueController alloc] initWithWindowNibName:@"QueueWindow"];
[self newAudiobookWindow:nil];
}
- (void)openChaptersHowTo:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://bluezbox.com/audiobookbinder/chapters.html"]];
}
- (void) checkForUpdates:(id)sender
{
}
- (IBAction) newAudiobookWindow: (id)sender
{
AudioBinderWindowController *controller = [[AudioBinderWindowController alloc] initWithWindowNibName:@"AudioBinderWindow"];
[controller showWindow:self];
[[controller window] makeMainWindow];
[windowControllers addObject:controller];
}
- (BOOL) applicationShouldHandleReopen:(NSApplication *)theApplication
hasVisibleWindows:(BOOL)flag
{
if (!flag)
[self newAudiobookWindow:nil];
return YES;
}
// Window delegate part
- (void)windowWillClose:(NSNotification *)notification
{
AudioBinderWindowController *controller = nil;
for (AudioBinderWindowController *c in windowControllers) {
if (c.window == notification.object) {
controller = c;
break;
}
}
if (controller)
[windowControllers removeObject:controller];
}
- (void)windowDidResignKey:(NSNotification *)notification
{
AudioBinderWindowController *controller = nil;
for (AudioBinderWindowController *c in windowControllers) {
if (c.window == notification.object) {
controller = c;
break;
}
}
if (controller)
[controller updateWindowTitle];
}
- (IBAction)showQueueWindow: (id)sender
{
[self.queueController showWindow:nil];
}
@end