Some API changes... of note, ITOSAScript's execution API returns an
[ITFoundation.git] / ITOSAComponent.m
1 /*
2  *      ITFoundation
3  *      ITOSAComponent
4  *          A Cocoa wrapper for scripting components.
5  *
6  *      Original Author : Kent Sutherland <ksutherland@ithinksw.com>
7  *      Responsibility : Kent Sutherland <ksutherland@ithinksw.com>
8  *      Responsibility : Joseph Spiros <joseph.spiros@ithinksw.com>
9  *
10  *      Copyright (c) 2002 - 2004 iThink Software.
11  *      All Rights Reserved
12  *
13  */
14
15 #import "ITOSAComponent.h"
16
17 #warning Need to add a constant data store containing all available component instances... could be lazy and build it on class +load.
18
19 @implementation ITOSAComponent
20
21 + (ITOSAComponent *)appleScriptComponent
22 {
23     return [[[ITOSAComponent alloc] initWithSubtype:kAppleScriptSubtype] autorelease];
24 }
25
26 + (ITOSAComponent *)componentWithCarbonComponent:(Component)component
27 {
28     return [[[ITOSAComponent alloc] initWithComponent:component] autorelease];
29 }
30
31 + (NSArray *)availableComponents
32 {
33     Component currentComponent = 0;
34     ComponentDescription cd;
35     NSMutableArray *components = [[NSMutableArray alloc] init];
36     
37     cd.componentType = kOSAComponentType;
38     cd.componentSubType = 0;
39     cd.componentManufacturer = 0;
40     cd.componentFlags = 0;
41     cd.componentFlagsMask = 0;
42     while ((currentComponent = FindNextComponent(currentComponent, &cd)) != 0) {
43         [components addObject:[ITOSAComponent componentWithCarbonComponent:currentComponent]];
44     }
45     return [NSArray arrayWithArray:[components autorelease]];
46 }
47
48 - (id)initWithSubtype:(unsigned long)subtype
49 {
50     ComponentDescription cd;
51     cd.componentType = kOSAComponentType;
52     cd.componentSubType = subtype;
53     cd.componentManufacturer = 0;
54     cd.componentFlags = 0;
55     cd.componentFlagsMask = 0;
56     Component temp = FindNextComponent(0, &cd);
57     if ( (self = [self initWithComponent:temp]) ) {
58     }
59     return self;
60 }
61
62 - (id)initWithComponent:(Component)component;
63 {
64     if ( (self = [super init]) ) {
65         Handle componentName = NewHandle(0);
66         Handle componentInfo = NewHandle(0);
67         ComponentDescription description;
68         NSMutableDictionary *information;
69         
70         _component = component;
71         _componentInstance = OpenComponent(component);
72         
73         if (GetComponentInfo(component, &description, componentName, componentInfo, nil) != 0) {
74             NSLog(@"FATAL ERROR!");
75             return nil;
76         }
77         
78         information = [[NSMutableDictionary alloc] init];
79         
80         AEDesc name;
81         Ptr buffer;
82         Size length;
83         OSAScriptingComponentName(_componentInstance, &name);
84         length = AEGetDescDataSize(&name);
85         buffer = malloc(length);
86
87         AEGetDescData(&name, buffer, length);
88         AEDisposeDesc(&name);
89         [information setObject:[NSString stringWithCString:buffer length:length] forKey:@"ITOSAComponentName"];
90         free(buffer);
91         buffer = NULL;
92         
93         //[information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentName"];
94         [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentInfo"];
95         [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITOSAComponentSubtype"];
96         [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITOSAComponentManufacturer"];
97         _information = [information copy];
98     }
99     return self;
100 }
101
102 - (void)dealloc
103 {
104     [_information release];
105 }
106
107 - (NSString *)description
108 {
109     return [_information objectForKey:@"ITOSAComponentName"];
110 }
111
112 - (Component)component
113 {
114     return _component;
115 }
116
117 - (ComponentInstance)componentInstance
118 {
119     return _componentInstance;
120 }
121
122 - (NSDictionary *)information
123 {
124     return _information;
125 }
126
127 @end