-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSBPresetManager.m
More file actions
183 lines (140 loc) · 4.08 KB
/
SBPresetManager.m
File metadata and controls
183 lines (140 loc) · 4.08 KB
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
//
// SBPresetManager.m
// Subler
//
// Created by Damiano Galassi on 02/01/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SBPresetManager.h"
#import "MP42File.h"
/// Notification sent to update presets lists.
NSString *SBPresetManagerUpdatedNotification = @"SBPresetManagerUpdatedNotification";
static SBPresetManager *sharedPresetManager = nil;
@interface SBPresetManager (Private)
- (BOOL) loadPresets;
- (BOOL) savePresets;
- (NSString *) appSupportPath;
- (BOOL) removePresetWithName:(NSString*)name;
@end
@implementation SBPresetManager
+ (SBPresetManager*)sharedManager
{
if (sharedPresetManager == nil) {
sharedPresetManager = [[super allocWithZone:NULL] init];
}
return sharedPresetManager;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedManager] retain];
}
- (id)init {
if ((self = [super init])) {
presets = [[NSMutableArray alloc] init];
[self loadPresets];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax; //denotes an object that cannot be released
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
- (void)updateNotification
{
[[NSNotificationCenter defaultCenter] postNotificationName:SBPresetManagerUpdatedNotification object:self];
}
- (void) newSetFromExistingMetadata:(MP42Metadata*)set
{
id newSet = [set copy];
[presets addObject:newSet];
[newSet release];
[self savePresets];
[self updateNotification];
}
- (NSString *) appSupportPath
{
NSString *appSupportPath = nil;
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask,
YES);
if ([allPaths count])
appSupportPath = [[allPaths objectAtIndex:0] stringByAppendingPathComponent:@"Subler"];
return appSupportPath;
}
- (BOOL) loadPresets
{
NSString *file;
NSFileManager *fileManager = [NSFileManager defaultManager];
MP42Metadata *newPreset;
NSString *appSupportPath = [self appSupportPath];
if (!appSupportPath)
return NO;
NSDirectoryEnumerator *dirEnum = [fileManager enumeratorAtPath:appSupportPath];
while ((file = [dirEnum nextObject]))
{
if ([[file pathExtension] isEqualToString: @"sbpreset"])
{
newPreset = [NSKeyedUnarchiver unarchiveObjectWithFile:[appSupportPath stringByAppendingPathComponent:file]];
[presets addObject:newPreset];
}
}
if ( ![presets count] )
return NO;
else
return YES;
}
- (BOOL) savePresets
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL noErr = YES;
NSString *appSupportPath = [self appSupportPath];
if (!appSupportPath)
return NO;
if( ![fileManager fileExistsAtPath:appSupportPath] )
[fileManager createDirectoryAtPath:appSupportPath attributes:nil];
MP42Metadata *object;
for( object in presets ) {
if ([object isEdited]) {
NSString * saveLocation = [NSString stringWithFormat:@"%@/%@.sbpreset", appSupportPath, [object presetName]];
noErr = [NSKeyedArchiver archiveRootObject:object
toFile:saveLocation];
}
}
return noErr;
}
- (BOOL) removePresetAtIndex:(NSUInteger)index
{
NSString *name = [[presets objectAtIndex:index] presetName];
[presets removeObjectAtIndex:index];
[self updateNotification];
return [self removePresetWithName:name];
}
- (BOOL) removePresetWithName:(NSString*)name
{
BOOL err = NO;
NSString *appSupportPath = [self appSupportPath];
if (!appSupportPath)
return NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
err = [fileManager removeItemAtPath: [NSString stringWithFormat:@"%@/%@.sbpreset", appSupportPath, name]
error: NULL];
return err;
}
@synthesize presets;
@end