Licensing ITMac under the GNU Lesser General Public License version 2.1.
[ITMac.git] / ITOSAComponent.m
1 #import "ITOSAComponent.h"
2
3 // Need to add a constant data store containing all available component instances... could be lazy and build it on class +load.
4
5 @implementation ITOSAComponent
6
7 + (ITOSAComponent *)appleScriptComponent
8 {
9         return [[[ITOSAComponent alloc] initWithSubtype:kAppleScriptSubtype] autorelease];
10 }
11
12 + (ITOSAComponent *)componentWithCarbonComponent:(Component)component
13 {
14         return [[[ITOSAComponent alloc] initWithComponent:component] autorelease];
15 }
16
17 + (NSArray *)availableComponents
18 {
19         Component currentComponent = 0;
20         ComponentDescription cd;
21         NSMutableArray *components = [[NSMutableArray alloc] init];
22         
23         cd.componentType = kOSAComponentType;
24         cd.componentSubType = 0;
25         cd.componentManufacturer = 0;
26         cd.componentFlags = 0;
27         cd.componentFlagsMask = 0;
28         while ((currentComponent = FindNextComponent(currentComponent, &cd)) != 0) {
29                 [components addObject:[ITOSAComponent componentWithCarbonComponent:currentComponent]];
30         }
31         return [NSArray arrayWithArray:[components autorelease]];
32 }
33
34 - (id)initWithSubtype:(unsigned long)subtype
35 {
36         ComponentDescription cd;
37         cd.componentType = kOSAComponentType;
38         cd.componentSubType = subtype;
39         cd.componentManufacturer = 0;
40         cd.componentFlags = 0;
41         cd.componentFlagsMask = 0;
42         Component temp = FindNextComponent(0, &cd);
43         if ( (self = [self initWithComponent:temp]) ) {
44         }
45         return self;
46 }
47
48 - (id)initWithComponent:(Component)component;
49 {
50         if ( (self = [super init]) ) {
51                 Handle componentName = NewHandle(0);
52                 Handle componentInfo = NewHandle(0);
53                 ComponentDescription description;
54                 NSMutableDictionary *information;
55                 
56                 _component = component;
57                 _componentInstance = OpenComponent(component);
58                 
59                 if (GetComponentInfo(component, &description, componentName, componentInfo, nil) != 0) {
60                         NSLog(@"FATAL ERROR!");
61                         return nil;
62                 }
63                 
64                 information = [[NSMutableDictionary alloc] init];
65                 
66                 AEDesc name;
67                 Ptr buffer;
68                 Size length;
69                 OSAScriptingComponentName(_componentInstance, &name);
70                 length = AEGetDescDataSize(&name);
71                 buffer = malloc(length);
72
73                 AEGetDescData(&name, buffer, length);
74                 AEDisposeDesc(&name);
75                 [information setObject:[NSString stringWithCString:buffer length:length] forKey:@"ITOSAComponentName"];
76                 free(buffer);
77                 buffer = NULL;
78                 
79                 //[information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentName"];
80                 [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentInfo"];
81                 [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITOSAComponentSubtype"];
82                 [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITOSAComponentManufacturer"];
83                 _information = [information copy];
84         }
85         return self;
86 }
87
88 - (void)dealloc
89 {
90         [_information release];
91 }
92
93 - (NSString *)description
94 {
95         return [_information objectForKey:@"ITOSAComponentName"];
96 }
97
98 - (Component)component
99 {
100         return _component;
101 }
102
103 - (ComponentInstance)componentInstance
104 {
105         return _componentInstance;
106 }
107
108 - (NSDictionary *)information
109 {
110         return _information;
111 }
112
113 @end