-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSaverLabModuleList.m
187 lines (158 loc) · 7.64 KB
/
SaverLabModuleList.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
/* Copyright 2001-2007 by Brian Nenninger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "SaverLabModuleList.h"
static SaverLabModuleList *_sharedInstance = nil;
static NSString *slideShowModulePath = nil;
static NSArray *screenSaverSuffixes = nil;
static NSString *SCREEN_SAVER_DIR = @"Screen Savers";
static NSString *HIDE_PREFIX = @"."; // hide saver bundles starting with "."
static NSString *SS_FRAMEWORK_MODULE_PATH = @"/System/Library/Frameworks/ScreenSaver.framework/Resources";
static NSString *MODULE_EXTENSION = @"saver";
static NSString *SLIDESHOW_MODULE_EXTENSION = @"slideSaver";
static NSString *QUARTZ_COMPOSER_MODULE_EXTENSION = @"qtz";
// returns all locations to search for .saver bundles
static NSArray* screenSaverSearchPaths() {
NSMutableArray *array = [NSMutableArray array];
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES);
NSString *path;
NSEnumerator *pathenum = [libPaths objectEnumerator];
[array addObject:SS_FRAMEWORK_MODULE_PATH];
while (path=[pathenum nextObject]) {
[array addObject:[path stringByAppendingPathComponent:SCREEN_SAVER_DIR]];
}
//NSLog(@"screenSaverSearchPaths() returning:%@", array);
return array;
}
static NSArray *gModulesIgnoringHiddenPrefix = nil;
static NSArray *modulesIgnoringHiddenPrefix() {
if (!gModulesIgnoringHiddenPrefix) {
// we would allow .Mac here, but it doesn't work anyway
gModulesIgnoringHiddenPrefix = [[NSArray alloc] initWithObjects:/*@".Mac.slideSaver",*/ nil];
}
return gModulesIgnoringHiddenPrefix;
}
@implementation SaverLabModuleList
+(NSArray *)screenSaverSuffixes {
if (!screenSaverSuffixes) {
screenSaverSuffixes = [[NSArray arrayWithObjects:MODULE_EXTENSION, SLIDESHOW_MODULE_EXTENSION, QUARTZ_COMPOSER_MODULE_EXTENSION, nil] retain];
}
return screenSaverSuffixes;
}
// Jaguar changes the slide show module to "Pictures Folder", it was "Slide Show" in 10.1.x.
// check to see which of them exists inside the ScreenSaver framework
+(NSString *)slideShowModulePath {
if (!slideShowModulePath) {
NSArray *ssModuleNames = [NSArray arrayWithObjects:@"Pictures Folder", @"Slide Show", nil];
NSEnumerator *ne = [ssModuleNames objectEnumerator];
NSString *name;
while ((!slideShowModulePath) && (name=[ne nextObject])) {
NSString *path = [SS_FRAMEWORK_MODULE_PATH stringByAppendingPathComponent:[name stringByAppendingPathExtension:MODULE_EXTENSION]];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
slideShowModulePath = [path retain];
}
}
}
return slideShowModulePath;
}
+(NSString *)quartzComposerModulePath {
return @"/System/Library/Frameworks/ScreenSaver.framework/Resources/.Quartz Composer.saver";
}
+(SaverLabModuleList *)sharedInstance {
if (!_sharedInstance) {
_sharedInstance = [[SaverLabModuleList alloc] init];
[_sharedInstance updateList];
}
return _sharedInstance;
}
// never actually called
-(void)dealloc {
[modulePathDictionary release];
[super dealloc];
}
// updates module names and paths, returns true if anything changed
-(BOOL)updateList {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *libraryDirs = screenSaverSearchPaths();
NSEnumerator *saverDirEnum = [libraryDirs objectEnumerator];
NSString *saverDir = nil;
NSMutableDictionary *newPathDictionary = [NSMutableDictionary dictionary];
// append "Screen Savers" to each library path and search for .saver bundles
while (saverDir = [saverDirEnum nextObject]) {
NSEnumerator *saverBundleEnum = [fileManager enumeratorAtPath:saverDir];
NSString *saverBundle = nil;
while (saverBundle=[saverBundleEnum nextObject]) {
if ((![saverBundle hasPrefix:HIDE_PREFIX] || [modulesIgnoringHiddenPrefix() containsObject:saverBundle]) &&
[[[self class] screenSaverSuffixes] containsObject:[saverBundle pathExtension]])
{
NSString *path = [saverDir stringByAppendingPathComponent:saverBundle];
// this assumes all filenames are unique
[newPathDictionary setObject:path
forKey:[[path lastPathComponent] stringByDeletingPathExtension]];
}
}
}
if ([newPathDictionary isEqualToDictionary:modulePathDictionary]) {
return NO;
}
else {
[modulePathDictionary release];
modulePathDictionary = [newPathDictionary retain];
[sortedModuleNames release];
sortedModuleNames = [[[modulePathDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
return YES;
}
}
-(NSArray *)sortedModuleNames {
return sortedModuleNames;
}
-(NSString *)pathForModuleName:(NSString *)name {
return [modulePathDictionary objectForKey:name];
}
-(NSBundle *)bundleForModulePath:(NSString *)path {
// special case for slideshow and Quartz Composer modules
if ([path hasSuffix:SLIDESHOW_MODULE_EXTENSION]) {
path = [[self class] slideShowModulePath];
}
else if ([path hasSuffix:QUARTZ_COMPOSER_MODULE_EXTENSION]) {
path = [[self class] quartzComposerModulePath];
}
return [NSBundle bundleWithPath:path];
}
-(NSBundle *)bundleForModuleName:(NSString *)name {
NSString *path = [self pathForModuleName:name];
return [self bundleForModulePath:path];
}
-(Class)classForModulePath:(NSString *)path {
return [[self bundleForModulePath:path] principalClass];
}
-(Class)classForModuleName:(NSString *)name {
return [[self bundleForModuleName:name] principalClass];
}
-(id)createScreenSaverViewForModulePath:(NSString *)path frame:(NSRect)frame isPreview:(BOOL)preview {
Class screenSaverClass = [self classForModulePath:path];
id screenSaverView = nil;
// Quartz Composer support
if ([path hasSuffix:QUARTZ_COMPOSER_MODULE_EXTENSION]) {
screenSaverView = [[screenSaverClass alloc] _initWithComposition:path frame:frame isPreview:preview];
[NSClassFromString(@"SaverLabQCPlayerViewWrapper") swizzleMethodForClass:screenSaverClass];
return screenSaverView;
}
else {
// XScreenSaver support
[NSClassFromString(@"SaverLabXScreenSaverWrapper") swizzle];
screenSaverView = [[screenSaverClass alloc] initWithFrame:frame isPreview:preview];
// slideshow support
if ([path hasSuffix:SLIDESHOW_MODULE_EXTENSION] && [screenSaverView respondsToSelector:@selector(setImageDirectory:)]) {
[screenSaverView setImageDirectory:[[path stringByAppendingPathComponent:@"Contents"]
stringByAppendingPathComponent:@"Resources"]];
}
}
return screenSaverView;
}
-(id)createScreenSaverViewForName:(NSString *)name frame:(NSRect)frame isPreview:(BOOL)preview {
return [self createScreenSaverViewForModulePath:[self pathForModuleName:name] frame:frame isPreview:preview];
}
@end