forked from eglaysher/vitaminsee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentManager.m
322 lines (277 loc) · 10.9 KB
/
ComponentManager.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
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
/////////////////////////////////////////////////////////////////////////
// File: $URL$
// Module: Loads Components at runtime.
// Part of: VitaminSEE
//
// ID: $Id: ApplicationController.m 123 2005-04-18 00:21:02Z elliot $
// Revision: $Revision$
// Last edited: $Date$
// Author: $Author$
// Copyright: (c) 2005 Elliot Glaysher
// Created: 9/4/05
//
/////////////////////////////////////////////////////////////////////////
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
////////////////////////////////////////////////////////////////////////
#import "ComponentManager.h"
#import "NSString+FileTasks.h"
#import "CurrentFilePlugin.h"
//
static int initialized = 0;
// Dictionaries of names to File List Plugins
static NSMutableDictionary* fileListPlugins = 0;
// Dictionaries of names to Current File Plugins
static NSMutableDictionary* currentFilePlugins = 0;
// Dictionaries of names to Internal Components
static NSMutableDictionary* internalComponents = 0;
// Dictionaries of View menu entries to names of FileListPlugins
static NSMutableArray* viewMenuNamesToBundleName = 0;
// Dictionaries of simple dictionaries of menu contexts.
static NSMutableArray* viewMenuCurrentFilePluginsToBundleName = 0;
// Array of CurrentFile plugins that have already been loaded (and therefore
// have to have their state updated whenever the user changes images/image
// viewer windows)
static NSMutableArray* loadedCurrentFilePlugins = 0;
// PUBLIC INTERFACE
@implementation ComponentManager
//<key>VSPluginType</key>
//<string>FileList</string>
//<key>VSPluginName</key>
//<string>ViewAsIcons</string>
//<key>VSFLMenuName</key>
//<string>as Icons</string>
/** Builds the static components of this class. Guarenteed to be called before
* any other method is called.
*/
+(void)initialize
{
fileListPlugins = [[NSMutableDictionary alloc] init];
currentFilePlugins = [[NSMutableDictionary alloc] init];
internalComponents = [[NSMutableDictionary alloc] init];
viewMenuNamesToBundleName = [[NSMutableArray alloc] init];
viewMenuCurrentFilePluginsToBundleName = [[NSMutableArray alloc] init];
loadedCurrentFilePlugins = [[NSMutableArray alloc] init];
}
//-----------------------------------------------------------------------------
/** Scans through the directory path, looking for ".bundle"s that are components
* of VitaminSEE. We check the Info.plist of each Bundle, checking certain keys
* so we have information on all the possible plugins that could be loaded at
* runtime.
*
*/
+(void)scanDirectoryForPlugins:(NSString*)path
{
// Look for all paths in the directory "path" that have the suffix ".bundle"
NSArray* itemsInPath = [[NSFileManager defaultManager]
directoryContentsAtPath:path];
int i, count = [itemsInPath count];
for(i = 0; i < count; ++i)
{
NSString* fileName = [itemsInPath objectAtIndex:i];
NSString* current = [path stringByAppendingPathComponent:fileName];
if([[current pathExtension] isEqual:@"bundle"] && [current isDir]) {
NSBundle* currentBundle = [NSBundle bundleWithPath:current];
NSDictionary* info = [currentBundle infoDictionary];
NSDictionary* localized = [currentBundle localizedInfoDictionary];
NSString* pluginName = [info objectForKey:@"VSPluginName"];
NSString* pluginType = [info objectForKey:@"VSPluginType"];
if([pluginType isEqual:@"FileList"]) {
NSString* menuName = [localized objectForKey:@"VSFLMenuName"];
if(!menuName)
menuName = [info objectForKey:@"VSFLMenuName"];
NSMutableDictionary* pluginInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
pluginName, @"PluginName",
currentBundle, @"Bundle",
@"FileList", @"PluginType",
menuName, @"MenuName",
nil];
// Add this FileList to the list of array of FileLists that will
// be displayed in the View menu.
[viewMenuNamesToBundleName addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
menuName, @"MenuName",
pluginName, @"PluginName", nil]];
[fileListPlugins setObject:pluginInfo forKey:pluginName];
}
else if([pluginType isEqual:@"CurrentFile"]) {
NSString* menuName = [localized objectForKey:@"VSFLMenuName"];
if(!menuName)
menuName = [info objectForKey:@"VSCFMenuName"];
NSString* menuLocation = [info objectForKey:@"VSCFMenuItemLocation"];
NSMutableDictionary* pluginInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
pluginName, @"PluginName", currentBundle, @"Bundle",
@"CurrentFile", @"PluginType",
menuName, @"MenuItemName", menuLocation, @"MenuLocation",
nil];
// Add the plugin's menu entries to one of the various names
//
[viewMenuCurrentFilePluginsToBundleName addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
menuName, @"Menu Name",
pluginName, @"Plugin Name", nil]];
[currentFilePlugins setObject:pluginInfo forKey:pluginName];
}
else if([pluginType isEqual:@"InternalUse"]) {
// The plugin equivalent of cheating.
NSMutableDictionary* pluginInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
pluginName, @"PluginName",
@"Internal Use", @"PluginType",
currentBundle, @"Bundle", nil];
[internalComponents setObject:pluginInfo forKey:pluginName];
}
else {
NSLog(@"WARNING! Ignoring bundle %@ since it has the invalid Bundle Type %@",
current, pluginType);
}
}
}
initialized = 1;
}
//-----------------------------------------------------------------------------
/** Gets a FileList, loading the bundle that contains it from disk if
* neccessary.
*
* @param name The name of the FileList
* @return An instantiation of the FileList, which is owned by the
* ComponentManager, and should not be released.
*/
+(id <FileListFactory>)getFileListPluginNamed:(NSString*)name
{
BOOL firstTime;
return [self returnPluginNamed:name
fromDictionary:fileListPlugins
protocol:@protocol(FileListFactory)
firstTime:&firstTime];
}
//-----------------------------------------------------------------------------
/** Gets a CurrentFilePlugin, loading the bundle that contains it from disk if
* neccessary.
*
* @param name The name of the CurrentFilePlugin
* @return An instantiation of the CurrentFilePlugin, which is owned by the
* ComponentManager, and should not be released.
*/
+(id<CurrentFilePlugin>)getCurrentFilePluginNamed:(NSString*)name
{
BOOL firstTime = NO;
id<CurrentFilePlugin> component = [self returnPluginNamed:name
fromDictionary:currentFilePlugins
protocol:nil
firstTime:&firstTime];
// Make sure that the component gets thrown in a list of loaded components
// so we can make sure their state is updated whenever necessary.
if(firstTime)
{
[loadedCurrentFilePlugins addObject:component];
}
return component;
}
//-----------------------------------------------------------------------------
/** Gets a internal component (A piece of code that we cheated and stuck
* in a plugin without defining some sort of formal interface).
*
* @param name The name of the component
* @return An instantiation of the component, which is owned by the
* ComponentManager, and should not be released.
*/
+(id)getInteranlComponentNamed:(NSString*)name
{
BOOL firstTime;
return [self returnPluginNamed:name
fromDictionary:internalComponents
protocol:nil
firstTime:&firstTime];
}
//-----------------------------------------------------------------------------
/** Returns the array of all FileLists loaded and unloaded for display in the
* view menu.
*/
+(NSArray*)getFileListsToDisplayInMenu
{
return viewMenuNamesToBundleName;
}
//-----------------------------------------------------------------------------
/** Returns the array of all CurrentFilePlugins that should be displayed in the
* view menu.
*/
+(NSArray*)getCurrentFilePluginsInViewMenu
{
return viewMenuCurrentFilePluginsToBundleName;
}
//-----------------------------------------------------------------------------
+(NSArray *)getLoadedCurrentFilePlugins
{
return loadedCurrentFilePlugins;
}
//-----------------------------------------------------------------------------
/** Private internal function used to lookup/load plugins. When the plugin has
* already been loaded into memory, the plugin will be pulled from a dictionary,
* and NO will be assigned to firstTime. When the requested plugin hasn't been
* loaded yet, it will be loaded from disk and firstTime will be set to YES.
*
* @param name The key to lookup in the dictionary
* @param dictionary A dictionary of string keys to plugin objects.
* @param protocol A protocol to check the object against.
* @param firstTime A pointer to a variable. It will set based on whether the
* plugin had to be loaded from disk.
* @return The plugin instance object, which is owned by the ComponentManager
* and should not be released.
*/
+(id)returnPluginNamed:(NSString*)name
fromDictionary:(NSDictionary*)dictionary
protocol:(Protocol*)protocol
firstTime:(BOOL*)firstTime
{
if(!initialized)
return nil;
NSMutableDictionary* pluginInfo = [dictionary objectForKey:name];
if(pluginInfo == nil) {
NSLog(@"WARNING! Could not find component named %@", name);
return nil;
}
// Check to see if there is already a root instance
id instance = [pluginInfo objectForKey:@"Instance"];
*firstTime = NO;
if(instance == nil)
{
NSBundle* bundle = [pluginInfo objectForKey:@"Bundle"];
BOOL loaded = [bundle load];
if(!loaded)
{
NSLog(@"WARNING! Couldn't load the bundle %@", bundle);
return nil;
}
Class principle = [bundle principalClass];
// Check to make sure this class conforms to whatever protocol we want
// to check if we were asked to verify that it conforms to a protocol.
if(protocol && ![principle conformsToProtocol:protocol]) {
NSLog(@"WARNING! Component named '%@' claims to conform to '%@' but doesn't!",
[pluginInfo objectForKey:@"PluginName"],
[pluginInfo objectForKey:@"PluginType"]);
NSLog(@"This component's principle class is %@",
NSStringFromClass(principle));
return nil;
}
// NSLog(@"Class: %@", principle);
// Create the instance
instance = [[principle alloc] init];
[pluginInfo setObject:instance forKey:@"Instance"];
*firstTime = YES;
}
return instance;
}
@end