NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / appleutility / CAComponent.cpp
1 /*      Copyright:      � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
3         Disclaimer:     IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
4                         ("Apple") in consideration of your agreement to the following terms, and your
5                         use, installation, modification or redistribution of this Apple software
6                         constitutes acceptance of these terms.  If you do not agree with these terms,
7                         please do not use, install, modify or redistribute this Apple software.
8
9                         In consideration of your agreement to abide by the following terms, and subject
10                         to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11                         copyrights in this original Apple software (the "Apple Software"), to use,
12                         reproduce, modify and redistribute the Apple Software, with or without
13                         modifications, in source and/or binary forms; provided that if you redistribute
14                         the Apple Software in its entirety and without modifications, you must retain
15                         this notice and the following text and disclaimers in all such redistributions of
16                         the Apple Software.  Neither the name, trademarks, service marks or logos of
17                         Apple Computer, Inc. may be used to endorse or promote products derived from the
18                         Apple Software without specific prior written permission from Apple.  Except as
19                         expressly stated in this notice, no other rights or licenses, express or implied,
20                         are granted by Apple herein, including but not limited to any patent rights that
21                         may be infringed by your derivative works or by other works in which the Apple
22                         Software may be incorporated.
23
24                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
25                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28                         COMBINATION WITH YOUR PRODUCTS.
29
30                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*=============================================================================
39         CAComponent.cpp
40
41 =============================================================================*/
42
43 #include "CAComponent.h"
44 #include "CAComponentDescription.h"
45 #include "CACFDictionary.h"
46 #include <stdlib.h>
47
48 CAComponent::CAComponent (const ComponentDescription& inDesc, CAComponent* next)
49         : mManuName(0), mAUName(0), mCompName(0), mCompInfo (0)
50 {
51         mComp = FindNextComponent ((next ? next->Comp() : NULL), const_cast<ComponentDescription*>(&inDesc));
52         if (mComp)
53                 GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
54         else
55                 memcpy (&mDesc, &inDesc, sizeof(ComponentDescription));
56 }
57
58 CAComponent::CAComponent (const Component& comp)
59         : mComp (comp),
60           mManuName(0),
61           mAUName(0),
62           mCompName(0),
63           mCompInfo (0)
64 {
65         GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
66 }
67
68 CAComponent::CAComponent (const ComponentInstance& compInst)
69         : mComp (Component(compInst)),
70           mManuName(0),
71           mAUName(0),
72           mCompName(0),
73           mCompInfo (0)
74 {
75         GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
76 }
77
78 CAComponent::CAComponent (OSType inType, OSType inSubtype, OSType inManu)
79         : mDesc (inType, inSubtype, inManu),
80           mManuName(0), mAUName(0), mCompName(0), mCompInfo (0)
81 {
82         mComp = FindNextComponent (NULL, &mDesc);
83         GetComponentInfo (Comp(), &mDesc, NULL, NULL, NULL);
84 }
85
86 CAComponent::~CAComponent ()
87 {
88         Clear();
89 }
90
91 OSStatus                CAComponent::GetResourceVersion (UInt32 &outVersion) const
92 {
93         bool versionFound = false;
94         ResFileRefNum componentResFileID = kResFileNotOpened;
95         OSStatus result;
96         short thngResourceCount;
97
98         short curRes = CurResFile();
99         require_noerr (result = OpenAComponentResFile( mComp, &componentResFileID), home);
100         require_noerr (result = componentResFileID <= 0, home);
101
102         UseResFile(componentResFileID);
103
104         thngResourceCount = Count1Resources(kComponentResourceType);
105
106         require_noerr (result = ResError(), home);
107                         // only go on if we successfully found at least 1 thng resource
108         require_noerr (thngResourceCount <= 0 ? -1 : 0, home);
109
110         // loop through all of the Component thng resources trying to
111         // find one that matches this Component description
112         for (short i = 0; i < thngResourceCount && (!versionFound); i++)
113         {
114                 // try to get a handle to this code resource
115                 Handle thngResourceHandle = Get1IndResource(kComponentResourceType, i+1);
116                 if (thngResourceHandle != NULL && ((*thngResourceHandle) != NULL))
117                 {
118                         if (UInt32(GetHandleSize(thngResourceHandle)) >= sizeof(ExtComponentResource))
119                         {
120                                 ExtComponentResource * componentThng = (ExtComponentResource*) (*thngResourceHandle);
121
122                                 // check to see if this is the thng resource for the particular Component that we are looking at
123                                 // (there often is more than one Component described in the resource)
124                                 if ((componentThng->cd.componentType == mDesc.Type())
125                                                 && (componentThng->cd.componentSubType == mDesc.SubType())
126                                                 && (componentThng->cd.componentManufacturer == mDesc.Manu()))
127                                 {
128                                         outVersion = componentThng->componentVersion;
129                                         versionFound = true;
130                                 }
131                         }
132                         ReleaseResource(thngResourceHandle);
133                 }
134         }
135
136         if (!versionFound)
137                 result = resNotFound;
138
139         UseResFile(curRes);     // revert
140
141         if ( componentResFileID != kResFileNotOpened )
142                 CloseComponentResFile(componentResFileID);
143
144 home:
145         return result;
146 }
147
148 void                    CAComponent::Clear ()
149 {
150         if (mManuName) { CFRelease (mManuName); mManuName = 0; }
151         if (mAUName) { CFRelease (mAUName);  mAUName = 0; }
152         if (mCompName) { CFRelease (mCompName); mCompName = 0; }
153         if (mCompInfo) { CFRelease (mCompInfo); mCompInfo = 0; }
154 }
155
156 CAComponent&    CAComponent::operator= (const CAComponent& y)
157 {
158         Clear();
159
160         mComp = y.mComp;
161         mDesc = y.mDesc;
162
163         if (y.mManuName) { mManuName = y.mManuName; CFRetain (mManuName); }
164         if (y.mAUName) { mAUName = y.mAUName; CFRetain (mAUName); }
165         if (y.mCompName) { mCompName = y.mCompName; CFRetain (mCompName); }
166         if (y.mCompInfo) { mCompInfo = y.mCompInfo; CFRetain (mCompInfo); }
167
168         return *this;
169 }
170
171 void            CAComponent::SetCompNames () const
172 {
173         if (!mCompName) {
174                 Handle h1 = NewHandle(4);
175                 CAComponentDescription desc;
176                 OSStatus err = GetComponentInfo (Comp(), &desc, h1, 0, 0);
177
178                 if (err) { DisposeHandle(h1); return; }
179
180                 HLock(h1);
181                 char* ptr1 = *h1;
182                 // Get the manufacturer's name... look for the ':' character convention
183                 int len = *ptr1++;
184                 char* displayStr = 0;
185
186                 const_cast<CAComponent*>(this)->mCompName = CFStringCreateWithPascalString(NULL, (const unsigned char*)*h1, kCFStringEncodingMacRoman);
187
188                 for (int i = 0; i < len; ++i) {
189                         if (ptr1[i] == ':') { // found the name
190                                 ptr1[i] = 0;
191                                 displayStr = ptr1;
192                                 break;
193                         }
194                 }
195
196                 if (displayStr)
197                 {
198                         const_cast<CAComponent*>(this)->mManuName = CFStringCreateWithCString(NULL, displayStr, kCFStringEncodingMacRoman);
199
200                         //move displayStr ptr past the manu, to the name
201                         // we move the characters down a index, because the handle doesn't have any room
202                         // at the end for the \0
203                         int i = strlen(displayStr), j = 0;
204                         while (displayStr[++i] == ' ' && i < len)
205                                         ;
206                         while (i < len)
207                                 displayStr[j++] = displayStr[i++];
208                         displayStr[j] = 0;
209
210                         const_cast<CAComponent*>(this)->mAUName = CFStringCreateWithCString(NULL, displayStr, kCFStringEncodingMacRoman);
211                 }
212
213                 DisposeHandle (h1);
214         }
215 }
216
217 void    CAComponent::SetCompInfo () const
218 {
219         if (!mCompInfo) {
220                 Handle h1 = NewHandle(4);
221                 CAComponentDescription desc;
222                 OSStatus err = GetComponentInfo (Comp(), &desc, 0, h1, 0);
223                 if (err) return;
224                 HLock (h1);
225                 const_cast<CAComponent*>(this)->mCompInfo = CFStringCreateWithPascalString(NULL, (const unsigned char*)*h1, kCFStringEncodingMacRoman);
226
227                 DisposeHandle (h1);
228         }
229 }
230
231 void    _ShowCF (FILE* file, CFStringRef str)
232 {
233         if (CFGetTypeID(str) != CFStringGetTypeID()) {
234                 CFShow(str);
235                 return;
236         }
237
238         UInt32 len = CFStringGetLength(str);
239         char* chars = (char*)malloc (len * 2); // give us plenty of room for unichar chars
240         if (CFStringGetCString (str, chars, len * 2, kCFStringEncodingUTF8))
241                 fprintf (file, "%s", chars);
242         else
243                 CFShow (str);
244
245         free (chars);
246 }
247
248 void    CAComponent::Print(FILE* file) const
249 {
250         fprintf (file, "CAComponent: %p", (void*)Comp());
251         if (mManuName) {
252                 fprintf (file, ", Manu:"); _ShowCF (file, mManuName);
253                 if (mAUName) fprintf (file, ", Name:"); _ShowCF (file, mAUName);
254         }
255         fprintf (file, ", ");
256         Desc ().Print(file);
257 }