summaryrefslogtreecommitdiffstats
path: root/Liaison/PluginManager.m
blob: 319dd20e5238a2fed40b1682891d1ad8e4e94e11 (plain)
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
//
//  PluginManager.m
//  Liaison
//
//  Created by Brian Cully on Thu May 15 2003.
//  Copyright (c) 2003 Brian Cully. All rights reserved.
//

#import "PluginManager.h"

@implementation PluginManager
static PluginManager *defaultManager = nil;
+ (PluginManager *)defaultManager
{
    if (defaultManager == nil)
        defaultManager = [[self alloc] init];
    return defaultManager;
}

- (id)init
{
    self = [super init];

    theFileStorePlugins = [[NSMutableArray alloc] init];
    theBrowserPlugins = [[NSMutableArray alloc] init];
    theInspectorPlugins = [[NSMutableArray alloc] init];

    [self scanForPlugins];

    return self;
}

- (void)dealloc
{
    [theFileStorePlugins release];
    [theBrowserPlugins release];
    [theInspectorPlugins release];

    [super dealloc];
}

- (void)activatePluginAtPath: (NSString *)aPath
{
    NSBundle *pluginBundle;

    pluginBundle = [NSBundle bundleWithPath: aPath];
    if (pluginBundle != nil) {
        NSDictionary *pluginDict;
        NSString *classString;

        pluginDict = [pluginBundle infoDictionary];
        classString = [pluginDict objectForKey: @"NSPrincipalClass"];
        if (classString != nil) {
            Class pluginClass;

            pluginClass = NSClassFromString(classString);
            if (pluginClass == nil) {
                id plugin;
                
                pluginClass = [pluginBundle principalClass];
                plugin = [[[pluginClass alloc] init] autorelease];
                if ([pluginClass conformsToProtocol:@protocol(LiFileStorePlugin)]) {
                    [pluginClass setBundle: pluginBundle];
                    [theFileStorePlugins addObject: plugin];
                }
                if ([pluginClass conformsToProtocol:@protocol(LiBrowserPlugin)]) {
                    [pluginClass setBundle: pluginBundle];
                    [theBrowserPlugins addObject: plugin];
                }
                if ([pluginClass conformsToProtocol:@protocol(LiInspectorPlugin)]) {
                    [pluginClass setBundle: pluginBundle];
                    [theInspectorPlugins addObject: plugin];
                }
            }
        }
    }
    return;
}

- (void)scanForPlugins
{
    NSString *appLocation;

    appLocation = [[NSBundle mainBundle] builtInPlugInsPath];
    if (appLocation) {
        NSEnumerator *pluginEnum;
        NSString *pluginPath;

        pluginEnum = [[NSBundle pathsForResourcesOfType: @"liaisonplugin"
                                            inDirectory: appLocation] objectEnumerator];
        while ((pluginPath = [pluginEnum nextObject]) != nil) {
            [self activatePluginAtPath: pluginPath];
        }
    }
}
@synthesize theBrowserPlugins;
@synthesize theFileStorePlugins;
@synthesize theInspectorPlugins;
@end

@implementation PluginManager (Accessors)
- (NSArray *)fileStorePlugins
{
    return theFileStorePlugins;
}

- (NSArray *)browserPlugins
{
    return theBrowserPlugins;
}

- (NSArray *)inspectorPlugins
{
    return theInspectorPlugins;
}
@end