]>
Commit | Line | Data |
---|---|---|
7dc3cc31 | 1 | ///////////////////////////////////////////////////////////////////////////// |
03603400 | 2 | // Name: src/mac/carbon/mimetype.cpp |
172da31f | 3 | // Purpose: Mac Carbon implementation for wx MIME-related classes |
c9e227f4 | 4 | // Author: Ryan Norton |
7dc3cc31 | 5 | // Modified by: |
c9e227f4 | 6 | // Created: 04/16/2005 |
7dc3cc31 | 7 | // RCS-ID: $Id$ |
c9e227f4 RN |
8 | // Copyright: (c) 2005 Ryan Norton (<wxprojects@comcast.net>) |
9 | // Licence: wxWindows licence | |
7dc3cc31 VS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
5af840f1 | 12 | // |
172da31f | 13 | // TODO: Search Info[-macos](classic).plist dictionary in addition |
5af840f1 RN |
14 | // to Internet Config database. |
15 | // | |
16 | // Maybe try a brainstorm a way to change the wxMimeTypesManager API | |
17 | // to get info from a file instead/addition to current get all stuff | |
172da31f | 18 | // API so that we can use Launch Services to get MIME type info. |
5af840f1 | 19 | // |
172da31f DS |
20 | // Implement GetIcon from one of the FinderInfo functions - or |
21 | // use Launch Services and search that app's plist for the icon. | |
5af840f1 | 22 | // |
cb9c71ec | 23 | // Put some special juice in for the print command. |
5af840f1 | 24 | // |
5af840f1 | 25 | |
7dc3cc31 VS |
26 | // for compilers that support precompilation, includes "wx.h". |
27 | #include "wx/wxprec.h" | |
28 | ||
29 | #ifdef __BORLANDC__ | |
ad9835c9 | 30 | #pragma hdrstop |
7dc3cc31 VS |
31 | #endif |
32 | ||
03603400 WS |
33 | #if wxUSE_MIMETYPE |
34 | ||
88a7a4e1 WS |
35 | #include "wx/mac/mimetype.h" |
36 | ||
7dc3cc31 | 37 | #ifndef WX_PRECOMP |
ad9835c9 WS |
38 | #include "wx/dynarray.h" |
39 | #include "wx/string.h" | |
88a7a4e1 | 40 | #include "wx/intl.h" |
e4db172a | 41 | #include "wx/log.h" |
172da31f | 42 | |
ad9835c9 WS |
43 | #if wxUSE_GUI |
44 | #include "wx/icon.h" | |
45 | #endif | |
172da31f | 46 | #endif |
7dc3cc31 | 47 | |
7dc3cc31 | 48 | #include "wx/file.h" |
7dc3cc31 VS |
49 | #include "wx/confbase.h" |
50 | ||
172da31f | 51 | #include "wx/mac/private.h" |
7dc3cc31 VS |
52 | |
53 | // other standard headers | |
54 | #include <ctype.h> | |
c9e227f4 | 55 | |
768c6e8b | 56 | #ifndef __DARWIN__ |
172da31f | 57 | #include <InternetConfig.h> |
749da442 | 58 | #include <CoreServices.h> |
768c6e8b | 59 | #endif |
7dc3cc31 | 60 | |
c9e227f4 | 61 | |
172da31f DS |
62 | // START CODE SAMPLE FROM TECHNOTE 1002 (http://developer.apple.com/technotes/tn/tn1002.html) |
63 | ||
64 | // IsRemoteVolume can be used to find out if the | |
65 | // volume referred to by vRefNum is a remote volume | |
66 | // located somewhere on a network. the volume's attribute | |
67 | // flags (copied from the GetVolParmsInfoBuffer structure) | |
68 | // are returned in the longword pointed to by vMAttrib. | |
69 | OSErr IsRemoteVolume(short vRefNum, Boolean *isRemote, long *vMAttrib) | |
70 | { | |
c9e227f4 RN |
71 | HParamBlockRec volPB; |
72 | GetVolParmsInfoBuffer volinfo; | |
73 | OSErr err; | |
172da31f | 74 | |
c9e227f4 RN |
75 | volPB.ioParam.ioVRefNum = vRefNum; |
76 | volPB.ioParam.ioNamePtr = NULL; | |
172da31f | 77 | volPB.ioParam.ioBuffer = (Ptr)&volinfo; |
c9e227f4 | 78 | volPB.ioParam.ioReqCount = sizeof(volinfo); |
172da31f DS |
79 | err = PBHGetVolParmsSync( &volPB ); |
80 | if (err == noErr) | |
81 | { | |
c9e227f4 RN |
82 | *isRemote = (volinfo.vMServerAdr != 0); |
83 | *vMAttrib = volinfo.vMAttrib; | |
84 | } | |
172da31f | 85 | |
c9e227f4 RN |
86 | return err; |
87 | } | |
88 | ||
172da31f DS |
89 | // BuildVolumeList fills the array pointed to by vols with |
90 | // a list of the currently mounted volumes. If includeRemote | |
91 | // is true, then remote server volumes will be included in | |
92 | // the list. When remote server volumes are included in the | |
93 | // list, they will be added to the end of the list. On entry, | |
94 | // *count should contain the size of the array pointed to by | |
95 | // vols. On exit, *count will be set to the number of id numbers | |
96 | // placed in the array. If vMAttribMask is non-zero, then | |
97 | // only volumes with matching attributes are added to the | |
98 | // list of volumes. bits in the vMAttribMask should use the | |
99 | // same encoding as bits in the vMAttrib field of | |
100 | // the GetVolParmsInfoBuffer structure. | |
c9e227f4 | 101 | OSErr BuildVolumeList(Boolean includeRemote, short *vols, |
172da31f DS |
102 | long *count, long vMAttribMask) |
103 | { | |
c9e227f4 RN |
104 | HParamBlockRec volPB; |
105 | Boolean isRemote; | |
cb9c71ec | 106 | OSErr err = noErr; |
c9e227f4 RN |
107 | long nlocal, nremote; |
108 | long vMAttrib; | |
109 | ||
172da31f | 110 | // set up and check parameters |
c9e227f4 RN |
111 | volPB.volumeParam.ioNamePtr = NULL; |
112 | nlocal = nremote = 0; | |
172da31f DS |
113 | if (*count == 0) |
114 | return noErr; | |
c9e227f4 | 115 | |
172da31f | 116 | // iterate through volumes |
c9e227f4 RN |
117 | for (volPB.volumeParam.ioVolIndex = 1; |
118 | PBHGetVInfoSync(&volPB) == noErr; | |
172da31f DS |
119 | volPB.volumeParam.ioVolIndex++) |
120 | { | |
121 | // skip remote volumes, if necessary | |
c9e227f4 | 122 | err = IsRemoteVolume(volPB.volumeParam.ioVRefNum, &isRemote, &vMAttrib); |
172da31f DS |
123 | if (err != noErr) |
124 | goto bail; | |
c9e227f4 | 125 | |
172da31f DS |
126 | if ((includeRemote || !isRemote) && ((vMAttrib & vMAttribMask) == vMAttribMask)) |
127 | { | |
128 | // add local volumes at the front; remote volumes at the end | |
c9e227f4 RN |
129 | if (isRemote) |
130 | vols[nlocal + nremote++] = volPB.volumeParam.ioVRefNum; | |
172da31f DS |
131 | else |
132 | { | |
c9e227f4 | 133 | if (nremote > 0) |
172da31f DS |
134 | BlockMoveData( |
135 | vols + nlocal, | |
136 | vols + nlocal + 1, | |
137 | nremote * sizeof(short) ); | |
c9e227f4 RN |
138 | vols[nlocal++] = volPB.volumeParam.ioVRefNum; |
139 | } | |
140 | ||
172da31f DS |
141 | // list full? |
142 | if ((nlocal + nremote) >= *count) | |
143 | break; | |
c9e227f4 RN |
144 | } |
145 | } | |
172da31f | 146 | |
c9e227f4 RN |
147 | bail: |
148 | *count = (nlocal + nremote); | |
172da31f | 149 | |
c9e227f4 RN |
150 | return err; |
151 | } | |
152 | ||
153 | ||
172da31f DS |
154 | // FindApplication iterates through mounted volumes |
155 | // searching for an application with the given creator | |
156 | // type. If includeRemote is true, then remote volumes | |
157 | // will be searched (after local ones) for an application | |
158 | // with the creator type. | |
159 | // | |
160 | // Hacked to output to appName | |
161 | // | |
c9e227f4 RN |
162 | #define kMaxVols 20 |
163 | ||
172da31f DS |
164 | OSErr FindApplication(OSType appCreator, Boolean includeRemote, Str255 appName, FSSpec* appSpec) |
165 | { | |
c9e227f4 RN |
166 | short rRefNums[kMaxVols]; |
167 | long i, volCount; | |
168 | DTPBRec desktopPB; | |
169 | OSErr err; | |
cb9c71ec | 170 | |
172da31f | 171 | // get a list of volumes - with desktop files |
c9e227f4 | 172 | volCount = kMaxVols; |
172da31f DS |
173 | err = BuildVolumeList(includeRemote, rRefNums, &volCount, (1 << bHasDesktopMgr) ); |
174 | if (err != noErr) | |
175 | return err; | |
c9e227f4 | 176 | |
172da31f DS |
177 | // iterate through the list |
178 | for (i=0; i<volCount; i++) | |
179 | { | |
180 | // has a desktop file? | |
c9e227f4 RN |
181 | desktopPB.ioCompletion = NULL; |
182 | desktopPB.ioVRefNum = rRefNums[i]; | |
183 | desktopPB.ioNamePtr = NULL; | |
184 | desktopPB.ioIndex = 0; | |
172da31f DS |
185 | err = PBDTGetPath( &desktopPB ); |
186 | if (err != noErr) | |
187 | continue; | |
c9e227f4 | 188 | |
172da31f | 189 | // has the correct app?? |
c9e227f4 RN |
190 | desktopPB.ioFileCreator = appCreator; |
191 | desktopPB.ioNamePtr = appName; | |
172da31f DS |
192 | err = PBDTGetAPPLSync( &desktopPB ); |
193 | if (err != noErr) | |
194 | continue; | |
c9e227f4 | 195 | |
172da31f DS |
196 | // make a file spec referring to it |
197 | err = FSMakeFSSpec( rRefNums[i], desktopPB.ioAPPLParID, appName, appSpec ); | |
198 | if (err != noErr) | |
199 | continue; | |
c9e227f4 | 200 | |
172da31f | 201 | // found it! |
c9e227f4 | 202 | return noErr; |
c9e227f4 | 203 | } |
172da31f | 204 | |
c9e227f4 RN |
205 | return fnfErr; |
206 | } | |
207 | ||
172da31f | 208 | // END CODE SAMPLE FROM TECHNOTE 1002 (http://developer.apple.com/technotes/tn/tn1002.html) |
c9e227f4 | 209 | |
172da31f DS |
210 | // yeah, duplicated code |
211 | pascal OSErr FSpGetFullPath( const FSSpec *spec, | |
212 | short *fullPathLength, | |
213 | Handle *fullPath ) | |
7baa887c | 214 | { |
172da31f DS |
215 | OSErr result, realResult; |
216 | FSSpec tempSpec; | |
217 | CInfoPBRec pb; | |
cb9c71ec | 218 | |
172da31f DS |
219 | *fullPathLength = 0; |
220 | *fullPath = NULL; | |
cb9c71ec | 221 | |
172da31f DS |
222 | // default to noErr |
223 | realResult = result = noErr; | |
cb9c71ec | 224 | |
172da31f DS |
225 | // work around Nav Services "bug" (it returns invalid FSSpecs with empty names) |
226 | #if 0 | |
227 | if ( spec->name[0] == 0 ) | |
7baa887c | 228 | { |
172da31f | 229 | result = FSMakeFSSpecCompat(spec->vRefNum, spec->parID, spec->name, &tempSpec); |
7baa887c RN |
230 | } |
231 | else | |
232 | { | |
172da31f DS |
233 | #endif |
234 | ||
235 | // Make a copy of the input FSSpec that can be modified | |
236 | BlockMoveData( spec, &tempSpec, sizeof(FSSpec) ); | |
237 | ||
238 | if ( result == noErr ) | |
239 | { | |
240 | if ( tempSpec.parID == fsRtParID ) | |
7baa887c | 241 | { |
172da31f DS |
242 | // object is a volume |
243 | // Add a colon to make it a full pathname | |
244 | ++tempSpec.name[0]; | |
245 | tempSpec.name[tempSpec.name[0]] = ':'; | |
cb9c71ec | 246 | |
172da31f DS |
247 | // We're done |
248 | result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]); | |
249 | } | |
250 | else | |
7baa887c | 251 | { |
172da31f DS |
252 | // object isn't a volume |
253 | ||
254 | // Is the object a file or a directory? | |
255 | pb.dirInfo.ioNamePtr = tempSpec.name; | |
256 | pb.dirInfo.ioVRefNum = tempSpec.vRefNum; | |
257 | pb.dirInfo.ioDrDirID = tempSpec.parID; | |
258 | pb.dirInfo.ioFDirIndex = 0; | |
259 | result = PBGetCatInfoSync( &pb ); | |
260 | ||
261 | // Allow file/directory name at end of path to not exist. | |
262 | realResult = result; | |
263 | if ((result == noErr) || (result == fnfErr)) | |
7baa887c | 264 | { |
172da31f DS |
265 | // if the object is a directory, append a colon so full pathname ends with colon |
266 | if ((result == noErr) && (pb.hFileInfo.ioFlAttrib & kioFlAttribDirMask) != 0) | |
267 | { | |
268 | ++tempSpec.name[0]; | |
269 | tempSpec.name[tempSpec.name[0]] = ':'; | |
270 | } | |
271 | ||
272 | // Put the object name in first | |
273 | result = PtrToHand( &tempSpec.name[1], fullPath, tempSpec.name[0] ); | |
274 | if ( result == noErr ) | |
275 | { | |
276 | // Get the ancestor directory names | |
277 | pb.dirInfo.ioNamePtr = tempSpec.name; | |
278 | pb.dirInfo.ioVRefNum = tempSpec.vRefNum; | |
279 | pb.dirInfo.ioDrParID = tempSpec.parID; | |
280 | ||
281 | // loop until we have an error or find the root directory | |
282 | do | |
283 | { | |
284 | pb.dirInfo.ioFDirIndex = -1; | |
285 | pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID; | |
286 | result = PBGetCatInfoSync(&pb); | |
287 | if ( result == noErr ) | |
288 | { | |
289 | // Append colon to directory name | |
290 | ++tempSpec.name[0]; | |
291 | tempSpec.name[tempSpec.name[0]] = ':'; | |
cb9c71ec | 292 | |
172da31f DS |
293 | // Add directory name to beginning of fullPath |
294 | (void)Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1], tempSpec.name[0]); | |
295 | result = MemError(); | |
296 | } | |
297 | } | |
298 | while ( (result == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) ); | |
299 | } | |
7baa887c | 300 | } |
7baa887c | 301 | } |
7baa887c | 302 | } |
cb9c71ec | 303 | |
172da31f | 304 | if ( result == noErr ) |
7baa887c | 305 | { |
172da31f DS |
306 | // Return the length |
307 | *fullPathLength = GetHandleSize( *fullPath ); | |
308 | result = realResult; // return realResult in case it was fnfErr | |
309 | } | |
310 | else | |
311 | { | |
312 | // Dispose of the handle and return NULL and zero length | |
313 | if ( *fullPath != NULL ) | |
314 | { | |
315 | DisposeHandle( *fullPath ); | |
316 | *fullPath = NULL; | |
317 | } | |
318 | *fullPathLength = 0; | |
7baa887c | 319 | } |
cb9c71ec | 320 | |
172da31f | 321 | return result; |
cb9c71ec WS |
322 | } |
323 | ||
7baa887c RN |
324 | // |
325 | // On the mac there are two ways to open a file - one is through apple events and the | |
326 | // finder, another is through mime types. | |
327 | // | |
328 | // So, really there are two ways to implement wxFileType... | |
329 | // | |
330 | // Mime types are only available on OS 8.1+ through the InternetConfig API | |
cb9c71ec | 331 | // |
7baa887c RN |
332 | // Much like the old-style file manager, it has 3 levels of flexibility for its methods - |
333 | // Low - which means you have to iterate yourself through the mime database | |
334 | // Medium - which lets you sort of cache the database if you want to use lowlevel functions | |
335 | // High - which requires access to the database every time | |
336 | // | |
337 | // We want to be efficient (i.e. professional :) ) about it, so we use a combo of low | |
338 | // and mid-level functions | |
339 | // | |
340 | // TODO: Should we call ICBegin/ICEnd? Then where? | |
341 | // | |
342 | ||
343 | // debug helper | |
344 | inline void wxLogMimeDebug(const wxChar* szMsg, OSStatus status) | |
345 | { | |
346 | wxLogDebug(wxString::Format(wxT("%s LINE:%i OSERROR:%i"), szMsg, __LINE__, (int)status)); | |
347 | } | |
348 | ||
349 | // in case we're compiling in non-GUI mode | |
350 | class WXDLLEXPORT wxIcon; | |
351 | ||
352 | bool wxFileTypeImpl::SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt) | |
353 | { | |
03271fa7 RN |
354 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
355 | ||
cb9c71ec | 356 | return false; |
7baa887c RN |
357 | } |
358 | ||
359 | bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int index) | |
360 | { | |
03271fa7 RN |
361 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
362 | ||
cb9c71ec | 363 | return false; |
7baa887c RN |
364 | } |
365 | ||
366 | bool wxFileTypeImpl::GetOpenCommand(wxString *openCmd, | |
367 | const wxFileType::MessageParameters& params) const | |
368 | { | |
369 | wxString cmd = GetCommand(wxT("open")); | |
370 | ||
371 | *openCmd = wxFileType::ExpandCommand(cmd, params); | |
372 | ||
373 | return !openCmd->empty(); | |
374 | } | |
375 | ||
376 | bool | |
172da31f DS |
377 | wxFileTypeImpl::GetPrintCommand( |
378 | wxString *printCmd, | |
379 | const wxFileType::MessageParameters& params) const | |
7baa887c RN |
380 | { |
381 | wxString cmd = GetCommand(wxT("print")); | |
382 | ||
383 | *printCmd = wxFileType::ExpandCommand(cmd, params); | |
384 | ||
385 | return !printCmd->empty(); | |
386 | } | |
387 | ||
388 | // | |
389 | // Internet Config vs. Launch Services | |
390 | // | |
391 | // From OS 8 on there was internet config... | |
cb9c71ec | 392 | // However, OSX and its finder does not use info |
7baa887c RN |
393 | // from Internet Config at all - the Internet Config |
394 | // database ONLY CONTAINS APPS THAT ARE CLASSIC APPS | |
395 | // OR REGISTERED THROUGH INTERNET CONFIG | |
396 | // | |
397 | // Therefore on OSX in order for the open command to be useful | |
398 | // we need to go straight to launch services | |
399 | // | |
400 | ||
401 | #if defined(__DARWIN__) | |
402 | ||
403 | //on darwin, use launch services | |
768c6e8b | 404 | #include <ApplicationServices/ApplicationServices.h> |
7baa887c | 405 | |
c9e227f4 | 406 | wxString wxFileTypeImpl::GetCommand(const wxString& verb) const |
7dc3cc31 | 407 | { |
03271fa7 | 408 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
cb9c71ec | 409 | |
172da31f | 410 | if (verb == wxT("open")) |
7baa887c RN |
411 | { |
412 | ICMapEntry entry; | |
cb9c71ec WS |
413 | ICGetMapEntry( (ICInstance) m_manager->m_hIC, |
414 | (Handle) m_manager->m_hDatabase, | |
7baa887c | 415 | m_lIndex, &entry); |
cb9c71ec | 416 | |
7baa887c | 417 | wxString sCurrentExtension = wxMacMakeStringFromPascal(entry.extension); |
03603400 | 418 | sCurrentExtension = sCurrentExtension.Right(sCurrentExtension.length()-1 ); |
7baa887c RN |
419 | |
420 | //type, creator, ext, roles, outapp (FSRef), outappurl | |
421 | CFURLRef cfurlAppPath; | |
172da31f | 422 | OSStatus status = LSGetApplicationForInfo( kLSUnknownType, |
cb9c71ec WS |
423 | kLSUnknownCreator, |
424 | wxMacCFStringHolder(sCurrentExtension, wxLocale::GetSystemEncoding()), | |
425 | kLSRolesAll, | |
7baa887c | 426 | NULL, |
172da31f | 427 | &cfurlAppPath ); |
cb9c71ec | 428 | |
172da31f | 429 | if (status == noErr) |
7baa887c RN |
430 | { |
431 | CFStringRef cfsUnixPath = CFURLCopyFileSystemPath(cfurlAppPath, kCFURLPOSIXPathStyle); | |
432 | CFRelease(cfurlAppPath); | |
cb9c71ec | 433 | |
172da31f DS |
434 | // PHEW! Success! |
435 | // Since a filename might have spaces in it, so surround it with quotes | |
436 | if (cfsUnixPath) | |
437 | { | |
438 | wxString resultStr; | |
439 | ||
440 | resultStr = | |
441 | wxString(wxT("'")) | |
442 | + wxMacCFStringHolder(cfsUnixPath).AsString(wxLocale::GetSystemEncoding()) | |
443 | + wxString(wxT("'")); | |
444 | ||
445 | return resultStr; | |
446 | } | |
7baa887c RN |
447 | } |
448 | else | |
449 | { | |
cb9c71ec WS |
450 | wxLogDebug(wxString::Format(wxT("%i - %s - %i"), |
451 | __LINE__, | |
7baa887c | 452 | wxT("LSGetApplicationForInfo failed."), |
cb9c71ec | 453 | (int)status)); |
7baa887c RN |
454 | } |
455 | } | |
cb9c71ec | 456 | |
7baa887c RN |
457 | return wxEmptyString; |
458 | } | |
459 | ||
460 | #else //carbon/classic implementation | |
461 | ||
462 | wxString wxFileTypeImpl::GetCommand(const wxString& verb) const | |
463 | { | |
03271fa7 | 464 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
cb9c71ec | 465 | |
172da31f | 466 | if (verb == wxT("open")) |
7dc3cc31 | 467 | { |
c9e227f4 | 468 | ICMapEntry entry; |
cb9c71ec WS |
469 | ICGetMapEntry( (ICInstance) m_manager->m_hIC, |
470 | (Handle) m_manager->m_hDatabase, | |
c02af653 | 471 | m_lIndex, &entry); |
cb9c71ec | 472 | |
c9e227f4 RN |
473 | //The entry in the mimetype database only contains the app |
474 | //that's registered - it may not exist... we need to remap the creator | |
475 | //type and find the right application | |
cb9c71ec | 476 | |
172da31f DS |
477 | // THIS IS REALLY COMPLICATED :\. |
478 | // There are a lot of conversions going on here. | |
c9e227f4 | 479 | Str255 outName; |
7baa887c | 480 | FSSpec outSpec; |
172da31f DS |
481 | OSErr err = FindApplication( entry.fileCreator, false, outName, &outSpec ); |
482 | if (err != noErr) | |
c9e227f4 | 483 | return wxEmptyString; |
cb9c71ec | 484 | |
7baa887c RN |
485 | Handle outPathHandle; |
486 | short outPathSize; | |
172da31f DS |
487 | err = FSpGetFullPath( &outSpec, &outPathSize, &outPathHandle ); |
488 | if (err == noErr) | |
7baa887c RN |
489 | { |
490 | char* szPath = *outPathHandle; | |
491 | wxString sClassicPath(szPath, wxConvLocal, outPathSize); | |
172da31f | 492 | |
7baa887c | 493 | #if defined(__DARWIN__) |
172da31f DS |
494 | // Classic Path --> Unix (OSX) Path |
495 | CFURLRef finalURL = CFURLCreateWithFileSystemPath( | |
496 | kCFAllocatorDefault, | |
497 | wxMacCFStringHolder(sClassicPath, wxLocale::GetSystemEncoding()), | |
498 | kCFURLHFSPathStyle, | |
499 | false ); //false == not a directory | |
7baa887c RN |
500 | |
501 | //clean up memory from the classic path handle | |
172da31f | 502 | DisposeHandle( outPathHandle ); |
cb9c71ec | 503 | |
172da31f | 504 | if (finalURL) |
7baa887c RN |
505 | { |
506 | CFStringRef cfsUnixPath = CFURLCopyFileSystemPath(finalURL, kCFURLPOSIXPathStyle); | |
507 | CFRelease(finalURL); | |
cb9c71ec | 508 | |
172da31f DS |
509 | // PHEW! Success! |
510 | if (cfsUnixPath) | |
7baa887c RN |
511 | return wxMacCFStringHolder(cfsUnixPath).AsString(wxLocale::GetSystemEncoding()); |
512 | } | |
513 | #else //classic HFS path acceptable | |
514 | return sClassicPath; | |
515 | #endif | |
516 | } | |
517 | else | |
518 | { | |
519 | wxLogMimeDebug(wxT("FSpGetFullPath failed."), (OSStatus)err); | |
520 | } | |
7dc3cc31 | 521 | } |
172da31f | 522 | |
c9e227f4 RN |
523 | return wxEmptyString; |
524 | } | |
7baa887c | 525 | #endif //!DARWIN |
c9e227f4 | 526 | |
0be2eb2f RN |
527 | bool wxFileTypeImpl::GetDescription(wxString *desc) const |
528 | { | |
529 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); | |
cb9c71ec | 530 | |
0be2eb2f | 531 | ICMapEntry entry; |
cb9c71ec | 532 | ICGetMapEntry( (ICInstance) m_manager->m_hIC, |
172da31f DS |
533 | (Handle) m_manager->m_hDatabase, m_lIndex, &entry ); |
534 | ||
535 | *desc = wxMacMakeStringFromPascal( entry.entryName ); | |
cb9c71ec | 536 | |
0be2eb2f RN |
537 | return true; |
538 | } | |
539 | ||
c9e227f4 RN |
540 | bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) |
541 | { | |
03271fa7 | 542 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
cb9c71ec | 543 | |
c9e227f4 | 544 | ICMapEntry entry; |
cb9c71ec | 545 | ICGetMapEntry( (ICInstance) m_manager->m_hIC, |
172da31f | 546 | (Handle) m_manager->m_hDatabase, m_lIndex, &entry ); |
cb9c71ec | 547 | |
c9e227f4 | 548 | //entry has period in it |
172da31f | 549 | wxString sCurrentExtension = wxMacMakeStringFromPascal( entry.extension ); |
03603400 | 550 | extensions.Add( sCurrentExtension.Right( sCurrentExtension.length() - 1 ) ); |
172da31f | 551 | |
c9e227f4 RN |
552 | return true; |
553 | } | |
554 | ||
555 | bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const | |
556 | { | |
03271fa7 | 557 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
cb9c71ec | 558 | |
c9e227f4 | 559 | ICMapEntry entry; |
cb9c71ec | 560 | ICGetMapEntry( (ICInstance) m_manager->m_hIC, |
172da31f | 561 | (Handle) m_manager->m_hDatabase, m_lIndex, &entry ); |
cb9c71ec | 562 | |
c9e227f4 | 563 | *mimeType = wxMacMakeStringFromPascal(entry.MIMEType); |
172da31f | 564 | |
c9e227f4 | 565 | return true; |
7dc3cc31 VS |
566 | } |
567 | ||
4d2976ad VS |
568 | bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const |
569 | { | |
570 | wxString s; | |
cb9c71ec | 571 | |
4d2976ad VS |
572 | if (GetMimeType(&s)) |
573 | { | |
574 | mimeTypes.Clear(); | |
575 | mimeTypes.Add(s); | |
172da31f | 576 | |
cb9c71ec | 577 | return true; |
4d2976ad | 578 | } |
172da31f DS |
579 | |
580 | return false; | |
4d2976ad VS |
581 | } |
582 | ||
da0766ab | 583 | bool wxFileTypeImpl::GetIcon(wxIconLocation *WXUNUSED(icon)) const |
7dc3cc31 | 584 | { |
03271fa7 RN |
585 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
586 | ||
7dc3cc31 | 587 | // no such file type or no value or incorrect icon entry |
cb9c71ec | 588 | return false; |
7dc3cc31 VS |
589 | } |
590 | ||
172da31f DS |
591 | size_t wxFileTypeImpl::GetAllCommands(wxArrayString * verbs, |
592 | wxArrayString * commands, | |
593 | const wxFileType::MessageParameters& params) const | |
007ae8d0 | 594 | { |
03271fa7 RN |
595 | wxASSERT_MSG( m_manager != NULL , wxT("Bad wxFileType") ); |
596 | ||
597 | wxString sCommand; | |
598 | size_t ulCount = 0; | |
cb9c71ec | 599 | |
172da31f | 600 | if (GetOpenCommand(&sCommand, params)) |
03271fa7 RN |
601 | { |
602 | verbs->Add(wxString(wxT("open"))); | |
603 | commands->Add(sCommand); | |
604 | ++ulCount; | |
605 | } | |
cb9c71ec | 606 | |
03271fa7 | 607 | return ulCount; |
f040060e GD |
608 | } |
609 | ||
c9e227f4 | 610 | void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, const wxString& extraDir) |
f040060e | 611 | { |
c9e227f4 | 612 | wxASSERT_MSG(m_hIC == NULL, wxT("Already initialized wxMimeTypesManager!")); |
007ae8d0 | 613 | |
172da31f DS |
614 | // some apps (non-wx) use the 'plst' resource instead |
615 | #if 0 | |
5af840f1 RN |
616 | CFBundleRef cfbMain = CFBundleGetMainBundle(); |
617 | wxCFDictionary cfdInfo( CFBundleGetInfoDictionary(cfbMain), wxCF_RETAIN ); | |
618 | wxString sLog; | |
619 | cfdInfo.PrintOut(sLog); | |
620 | wxLogDebug(sLog); | |
172da31f | 621 | #endif |
5af840f1 | 622 | |
172da31f DS |
623 | // start Internet Config - log if there's an error |
624 | // the second param is the signature of the application, also known | |
625 | // as resource ID 0. However, as per some recent discussions, we may not | |
626 | // have a signature for this app, so a generic 'APPL' which is the executable | |
627 | // type will work for now. | |
628 | OSStatus status = ICStart( (ICInstance*)&m_hIC, 'APPL' ); | |
cb9c71ec | 629 | |
172da31f | 630 | if (status != noErr) |
7dc3cc31 | 631 | { |
7baa887c | 632 | wxLogDebug(wxT("Could not initialize wxMimeTypesManager!")); |
4362c705 | 633 | wxFAIL; |
03271fa7 | 634 | m_hIC = NULL; |
172da31f | 635 | |
c9e227f4 | 636 | return; |
7dc3cc31 | 637 | } |
cb9c71ec | 638 | |
c9e227f4 RN |
639 | ICAttr attr; |
640 | m_hDatabase = (void**) NewHandle(0); | |
641 | status = ICFindPrefHandle( (ICInstance) m_hIC, kICMapping, &attr, (Handle) m_hDatabase ); | |
642 | ||
643 | //the database file can be corrupt (on OSX its | |
644 | //~/Library/Preferences/com.apple.internetconfig.plist) | |
645 | //- bail if it is | |
172da31f | 646 | if (status != noErr) |
7dc3cc31 | 647 | { |
c9e227f4 | 648 | ClearData(); |
172da31f | 649 | wxLogDebug(wxT("Corrupt MIME database!")); |
c9e227f4 | 650 | return; |
7dc3cc31 | 651 | } |
c9e227f4 RN |
652 | |
653 | //obtain the number of entries in the map | |
654 | status = ICCountMapEntries( (ICInstance) m_hIC, (Handle) m_hDatabase, &m_lCount ); | |
655 | wxASSERT( status == noErr ); | |
172da31f DS |
656 | |
657 | #if 0 | |
7baa887c RN |
658 | //debug stuff |
659 | ICMapEntry entry; | |
660 | long pos; | |
cb9c71ec | 661 | |
172da31f | 662 | for (long i = 1; i <= m_lCount; ++i) |
7baa887c | 663 | { |
172da31f | 664 | OSStatus status = ICGetIndMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, i, &pos, &entry ); |
cb9c71ec | 665 | |
172da31f | 666 | if (status == noErr) |
cb9c71ec | 667 | { |
7baa887c RN |
668 | wxString sCreator = wxMacMakeStringFromPascal(entry.creatorAppName); |
669 | wxString sCurrentExtension = wxMacMakeStringFromPascal(entry.extension); | |
670 | wxString sMIMEType = wxMacMakeStringFromPascal(entry.MIMEType); | |
cb9c71ec | 671 | |
7baa887c RN |
672 | wxFileTypeImpl impl; |
673 | impl.Init(this, pos); | |
cb9c71ec | 674 | |
172da31f | 675 | if (sMIMEType == wxT("text/html") && sCurrentExtension == wxT(".html")) |
7baa887c RN |
676 | { |
677 | wxString cmd; | |
7baa887c | 678 | |
172da31f | 679 | impl.GetOpenCommand( &cmd, wxFileType::MessageParameters (wxT("http://www.google.com"))); |
7baa887c RN |
680 | wxPrintf(wxT("APP: [%s]\n"), cmd.c_str()); |
681 | } | |
682 | } | |
683 | } | |
172da31f | 684 | #endif |
c9e227f4 RN |
685 | } |
686 | ||
687 | void wxMimeTypesManagerImpl::ClearData() | |
688 | { | |
172da31f | 689 | if (m_hIC != NULL) |
7dc3cc31 | 690 | { |
172da31f DS |
691 | DisposeHandle( (Handle)m_hDatabase ); |
692 | ||
693 | // this can return an error, but we don't really care that much about it | |
694 | ICStop( (ICInstance)m_hIC ); | |
c9e227f4 | 695 | m_hIC = NULL; |
7dc3cc31 | 696 | } |
c9e227f4 RN |
697 | } |
698 | ||
03271fa7 RN |
699 | // |
700 | // Q) Iterating through the map - why does it use if (err == noErr) instead of just asserting? | |
701 | // A) Some intermediate indexes are bad while subsequent ones may be good. Its wierd, I know. | |
702 | // | |
703 | ||
c9e227f4 RN |
704 | // extension -> file type |
705 | wxFileType* wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& e) | |
706 | { | |
707 | wxASSERT_MSG( m_hIC != NULL, wxT("wxMimeTypesManager not Initialized!") ); | |
cb9c71ec WS |
708 | |
709 | //low level functions - iterate through the database | |
c9e227f4 | 710 | ICMapEntry entry; |
c9e227f4 | 711 | long pos; |
cb9c71ec | 712 | |
172da31f | 713 | for (long i = 1; i <= m_lCount; ++i) |
7dc3cc31 | 714 | { |
172da31f | 715 | OSStatus status = ICGetIndMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, i, &pos, &entry ); |
cb9c71ec | 716 | |
172da31f | 717 | if (status == noErr) |
cb9c71ec | 718 | { |
c9e227f4 | 719 | wxString sCurrentExtension = wxMacMakeStringFromPascal(entry.extension); |
03603400 | 720 | if ( sCurrentExtension.Right(sCurrentExtension.length() - 1) == e ) // entry has period in it |
c9e227f4 | 721 | { |
03271fa7 | 722 | wxFileType* pFileType = new wxFileType(); |
c9e227f4 | 723 | pFileType->m_impl->Init((wxMimeTypesManagerImpl*)this, pos); |
172da31f | 724 | |
03271fa7 | 725 | return pFileType; |
c9e227f4 RN |
726 | } |
727 | } | |
7dc3cc31 | 728 | } |
cb9c71ec WS |
729 | |
730 | return NULL; | |
7dc3cc31 VS |
731 | } |
732 | ||
733 | // MIME type -> extension -> file type | |
c9e227f4 | 734 | wxFileType* wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) |
7dc3cc31 | 735 | { |
c9e227f4 RN |
736 | wxASSERT_MSG( m_hIC != NULL, wxT("wxMimeTypesManager not Initialized!") ); |
737 | ||
c9e227f4 | 738 | ICMapEntry entry; |
c9e227f4 | 739 | long pos; |
cb9c71ec | 740 | |
172da31f DS |
741 | // low level functions - iterate through the database |
742 | for (long i = 1; i <= m_lCount; ++i) | |
c9e227f4 | 743 | { |
172da31f | 744 | OSStatus status = ICGetIndMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, i, &pos, &entry ); |
c9e227f4 | 745 | wxASSERT_MSG( status == noErr, wxString::Format(wxT("Error: %d"), (int)status) ); |
cb9c71ec | 746 | |
172da31f | 747 | if (status == noErr) |
cb9c71ec | 748 | { |
172da31f | 749 | if ( wxMacMakeStringFromPascal(entry.MIMEType) == mimeType) |
c9e227f4 | 750 | { |
03271fa7 | 751 | wxFileType* pFileType = new wxFileType(); |
c9e227f4 | 752 | pFileType->m_impl->Init((wxMimeTypesManagerImpl*)this, pos); |
172da31f | 753 | |
03271fa7 | 754 | return pFileType; |
c9e227f4 RN |
755 | } |
756 | } | |
757 | } | |
cb9c71ec | 758 | |
03271fa7 | 759 | return NULL; |
7dc3cc31 VS |
760 | } |
761 | ||
762 | size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) | |
763 | { | |
c9e227f4 | 764 | wxASSERT_MSG( m_hIC != NULL, wxT("wxMimeTypesManager not Initialized!") ); |
7dc3cc31 | 765 | |
c9e227f4 | 766 | ICMapEntry entry; |
172da31f | 767 | long pos, lStartCount; |
cb9c71ec | 768 | |
172da31f DS |
769 | // low level functions - iterate through the database |
770 | lStartCount = (long) mimetypes.GetCount(); | |
771 | for (long i = 1; i <= m_lCount; ++i) | |
c9e227f4 | 772 | { |
172da31f DS |
773 | OSStatus status = ICGetIndMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, i, &pos, &entry ); |
774 | if ( status == noErr ) | |
c9e227f4 RN |
775 | mimetypes.Add( wxMacMakeStringFromPascal(entry.MIMEType) ); |
776 | } | |
cb9c71ec | 777 | |
7baa887c | 778 | return mimetypes.GetCount() - lStartCount; |
7dc3cc31 VS |
779 | } |
780 | ||
7baa887c | 781 | pascal OSStatus MoreProcGetProcessTypeSignature( |
172da31f DS |
782 | const ProcessSerialNumberPtr pPSN, |
783 | OSType *pProcessType, | |
784 | OSType *pCreator) | |
7baa887c | 785 | { |
172da31f DS |
786 | OSStatus anErr = noErr; |
787 | ProcessInfoRec infoRec; | |
788 | ProcessSerialNumber localPSN; | |
789 | ||
790 | infoRec.processInfoLength = sizeof(ProcessInfoRec); | |
791 | infoRec.processName = NULL; | |
792 | infoRec.processAppSpec = NULL; | |
793 | ||
794 | if ( pPSN == NULL ) | |
795 | { | |
796 | localPSN.highLongOfPSN = 0; | |
797 | localPSN.lowLongOfPSN = kCurrentProcess; | |
798 | } | |
799 | else | |
800 | { | |
801 | localPSN = *pPSN; | |
802 | } | |
803 | ||
804 | anErr = GetProcessInformation(&localPSN, &infoRec); | |
805 | if (anErr == noErr) | |
806 | { | |
807 | *pProcessType = infoRec.processType; | |
808 | *pCreator = infoRec.processSignature; | |
809 | } | |
810 | ||
811 | return anErr; | |
812 | } | |
7baa887c | 813 | |
5af840f1 RN |
814 | // |
815 | // | |
816 | // TODO: clean this up, its messy | |
817 | // | |
818 | // | |
819 | // | |
820 | ||
821 | #include "wx/mac/corefoundation/cfstring.h" | |
5af840f1 RN |
822 | |
823 | #define wxCF_RELEASE true | |
824 | #define wxCF_RETAIN false | |
825 | ||
826 | // ---------------------------------------------------------------------------- | |
827 | // wxCFDictionary | |
828 | // ---------------------------------------------------------------------------- | |
829 | ||
830 | class wxCFDictionary | |
dd9b4b7f | 831 | { |
5af840f1 RN |
832 | public: |
833 | wxCFDictionary(CFTypeRef ref, bool bRetain = wxCF_RELEASE) | |
834 | { | |
835 | m_cfmdRef = (CFMutableDictionaryRef) ref; | |
172da31f | 836 | if (bRetain == wxCF_RETAIN && ref) |
5af840f1 RN |
837 | CFRetain(ref); |
838 | } | |
cb9c71ec | 839 | |
5af840f1 RN |
840 | wxCFDictionary(CFIndex cfiSize = 0) |
841 | { | |
842 | CFDictionaryKeyCallBacks kcbs; | |
843 | CFDictionaryValueCallBacks vcbs; | |
844 | BuildKeyCallbacks(&kcbs); | |
845 | BuildValueCallbacks(&vcbs); | |
846 | ||
847 | m_cfmdRef = CFDictionaryCreateMutable( | |
172da31f | 848 | kCFAllocatorDefault, cfiSize, &kcbs, &vcbs ); |
5af840f1 | 849 | } |
cb9c71ec WS |
850 | |
851 | ~wxCFDictionary() | |
852 | { Clear(); } | |
853 | ||
5af840f1 | 854 | void Clear() |
172da31f DS |
855 | { |
856 | if (m_cfmdRef) | |
857 | CFRelease(m_cfmdRef); | |
858 | } | |
cb9c71ec | 859 | |
5af840f1 | 860 | static const void* RetainProc(CFAllocatorRef, const void* v) |
cb9c71ec WS |
861 | { return (const void*) CFRetain(v); } |
862 | ||
5af840f1 | 863 | static void ReleaseProc(CFAllocatorRef, const void* v) |
cb9c71ec | 864 | { CFRelease(v); } |
5af840f1 RN |
865 | |
866 | void MakeMutable(CFIndex cfiSize = 0) | |
867 | { | |
868 | CFDictionaryRef oldref = (CFDictionaryRef) m_cfmdRef; | |
cb9c71ec | 869 | |
5af840f1 | 870 | m_cfmdRef = CFDictionaryCreateMutableCopy( |
172da31f | 871 | kCFAllocatorDefault, cfiSize, oldref ); |
cb9c71ec | 872 | |
172da31f | 873 | CFRelease( oldref ); |
5af840f1 | 874 | } |
cb9c71ec | 875 | |
5af840f1 RN |
876 | void BuildKeyCallbacks(CFDictionaryKeyCallBacks* pCbs) |
877 | { | |
878 | pCbs->version = 0; | |
879 | pCbs->retain = RetainProc; | |
880 | pCbs->release = ReleaseProc; | |
881 | pCbs->copyDescription = NULL; | |
882 | pCbs->equal = NULL; | |
883 | pCbs->hash = NULL; | |
884 | } | |
885 | ||
886 | void BuildValueCallbacks(CFDictionaryValueCallBacks* pCbs) | |
887 | { | |
888 | pCbs->version = 0; | |
889 | pCbs->retain = RetainProc; | |
890 | pCbs->release = ReleaseProc; | |
891 | pCbs->copyDescription = NULL; | |
892 | pCbs->equal = NULL; | |
893 | } | |
894 | ||
895 | operator CFTypeRef () const | |
cb9c71ec WS |
896 | { return (CFTypeRef)m_cfmdRef; } |
897 | ||
5af840f1 | 898 | CFDictionaryRef GetCFDictionary() const |
cb9c71ec | 899 | { return (CFDictionaryRef)m_cfmdRef; } |
5af840f1 RN |
900 | |
901 | CFMutableDictionaryRef GetCFMutableDictionary() | |
cb9c71ec | 902 | { return (CFMutableDictionaryRef) m_cfmdRef; } |
5af840f1 RN |
903 | |
904 | CFTypeRef operator [] (CFTypeRef cftEntry) const | |
cb9c71ec | 905 | { |
5af840f1 | 906 | wxASSERT(IsValid()); |
cb9c71ec WS |
907 | return (CFTypeRef) CFDictionaryGetValue((CFDictionaryRef)m_cfmdRef, cftEntry); |
908 | } | |
909 | ||
5af840f1 | 910 | CFIndex GetCount() const |
cb9c71ec | 911 | { |
5af840f1 | 912 | wxASSERT(IsValid()); |
cb9c71ec | 913 | return CFDictionaryGetCount((CFDictionaryRef)m_cfmdRef); |
5af840f1 | 914 | } |
cb9c71ec | 915 | |
5af840f1 RN |
916 | void Add(CFTypeRef cftKey, CFTypeRef cftValue) |
917 | { | |
918 | wxASSERT(IsValid()); | |
919 | wxASSERT(Exists(cftKey) == false); | |
cb9c71ec | 920 | CFDictionaryAddValue(m_cfmdRef, cftKey, cftValue); |
5af840f1 | 921 | } |
cb9c71ec | 922 | |
5af840f1 | 923 | void Remove(CFTypeRef cftKey) |
cb9c71ec | 924 | { |
5af840f1 RN |
925 | wxASSERT(IsValid()); |
926 | wxASSERT(Exists(cftKey)); | |
cb9c71ec | 927 | CFDictionaryRemoveValue(m_cfmdRef, cftKey); |
5af840f1 | 928 | } |
cb9c71ec | 929 | |
5af840f1 | 930 | void Set(CFTypeRef cftKey, CFTypeRef cftValue) |
cb9c71ec | 931 | { |
5af840f1 RN |
932 | wxASSERT(IsValid()); |
933 | wxASSERT(Exists(cftKey)); | |
cb9c71ec | 934 | CFDictionarySetValue(m_cfmdRef, cftKey, cftValue); |
5af840f1 | 935 | } |
cb9c71ec | 936 | |
5af840f1 RN |
937 | bool Exists(CFTypeRef cftKey) const |
938 | { | |
939 | wxASSERT(IsValid()); | |
d0ee33f5 | 940 | return CFDictionaryContainsKey((CFDictionaryRef)m_cfmdRef, cftKey); |
5af840f1 | 941 | } |
cb9c71ec | 942 | |
172da31f DS |
943 | bool IsOk() const |
944 | { return m_cfmdRef != NULL; } | |
5af840f1 RN |
945 | |
946 | bool IsValid() const | |
172da31f | 947 | { return IsOk() && CFGetTypeID((CFTypeRef)m_cfmdRef) == CFDictionaryGetTypeID(); } |
cb9c71ec | 948 | |
5af840f1 RN |
949 | void PrintOut(wxString& sMessage) |
950 | { | |
951 | PrintOutDictionary(sMessage, m_cfmdRef); | |
952 | } | |
953 | ||
954 | static void PrintOutDictionary(wxString& sMessage, CFDictionaryRef cfdRef) | |
955 | { | |
956 | CFIndex cfiCount = CFDictionaryGetCount(cfdRef); | |
957 | CFTypeRef* pKeys = new CFTypeRef[cfiCount]; | |
958 | CFTypeRef* pValues = new CFTypeRef[cfiCount]; | |
959 | ||
960 | CFDictionaryGetKeysAndValues(cfdRef, pKeys, pValues); | |
cb9c71ec | 961 | |
172da31f | 962 | for (CFIndex i = 0; i < cfiCount; ++i) |
5af840f1 RN |
963 | { |
964 | wxString sKey = wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pKeys[i]))).AsString(); | |
965 | wxString sValue = wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pValues[i]))).AsString(); | |
cb9c71ec WS |
966 | |
967 | sMessage << | |
5af840f1 RN |
968 | wxString::Format(wxT("[{#%d} Key : %s]"), (int) i, |
969 | sKey.c_str()); | |
cb9c71ec | 970 | |
5af840f1 | 971 | PrintOutType(sMessage, sKey, pKeys[i]); |
cb9c71ec WS |
972 | |
973 | sMessage << | |
974 | wxString::Format(wxT("\n\t[Value : %s]"), | |
5af840f1 | 975 | sValue.c_str()); |
cb9c71ec | 976 | |
5af840f1 | 977 | PrintOutType(sMessage, sValue, pValues[i]); |
cb9c71ec | 978 | |
5af840f1 RN |
979 | sMessage << wxT("\n"); |
980 | } | |
cb9c71ec | 981 | |
172da31f DS |
982 | delete [] pKeys; |
983 | delete [] pValues; | |
5af840f1 | 984 | } |
cb9c71ec | 985 | |
5af840f1 RN |
986 | static void PrintOutArray(wxString& sMessage, CFArrayRef cfaRef) |
987 | { | |
172da31f | 988 | for (CFIndex i = 0; i < CFArrayGetCount(cfaRef); ++i) |
5af840f1 RN |
989 | { |
990 | wxString sValue = wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID( | |
991 | CFArrayGetValueAtIndex(cfaRef, i) | |
992 | ))).AsString(); | |
cb9c71ec WS |
993 | |
994 | sMessage << | |
5af840f1 RN |
995 | wxString::Format(wxT("\t\t[{#%d} ArrayValue : %s]\n"), (int) i, |
996 | sValue.c_str()); | |
cb9c71ec | 997 | |
5af840f1 RN |
998 | PrintOutType(sMessage, sValue, CFArrayGetValueAtIndex(cfaRef, i)); |
999 | } | |
1000 | } | |
cb9c71ec | 1001 | |
fbfb8bcc | 1002 | static void PrintOutType(wxString& sMessage, const wxString& sValue, CFTypeRef cfRef) |
5af840f1 RN |
1003 | { |
1004 | sMessage << wxT(" {"); | |
cb9c71ec | 1005 | |
172da31f | 1006 | if (sValue == wxT("CFString")) |
5af840f1 RN |
1007 | { |
1008 | sMessage << wxMacCFStringHolder((CFStringRef)cfRef, false).AsString(); | |
1009 | } | |
172da31f | 1010 | else if (sValue == wxT("CFNumber")) |
5af840f1 RN |
1011 | { |
1012 | int nOut; | |
1013 | CFNumberGetValue((CFNumberRef)cfRef, kCFNumberIntType, &nOut); | |
1014 | sMessage << nOut; | |
1015 | } | |
172da31f | 1016 | else if (sValue == wxT("CFDictionary")) |
5af840f1 RN |
1017 | { |
1018 | PrintOutDictionary(sMessage, (CFDictionaryRef)cfRef); | |
1019 | } | |
172da31f | 1020 | else if (sValue == wxT("CFArray")) |
5af840f1 RN |
1021 | { |
1022 | PrintOutArray(sMessage, (CFArrayRef)cfRef); | |
1023 | } | |
172da31f | 1024 | else if (sValue == wxT("CFBoolean")) |
5af840f1 RN |
1025 | { |
1026 | sMessage << (cfRef == kCFBooleanTrue ? wxT("true") : wxT("false")); | |
1027 | } | |
172da31f | 1028 | else if (sValue == wxT("CFURL")) |
5af840f1 RN |
1029 | { |
1030 | sMessage << wxMacCFStringHolder(CFURLCopyPath((CFURLRef) cfRef)).AsString(); | |
1031 | } | |
1032 | else | |
1033 | { | |
1034 | sMessage << wxT("*****UNKNOWN TYPE******"); | |
1035 | } | |
cb9c71ec | 1036 | |
5af840f1 RN |
1037 | sMessage << wxT("} "); |
1038 | } | |
cb9c71ec | 1039 | |
5af840f1 RN |
1040 | #if wxUSE_MIMETYPE |
1041 | void MakeValidXML(); | |
1042 | #endif | |
cb9c71ec | 1043 | |
5af840f1 RN |
1044 | CFTypeRef WriteAsXML() |
1045 | { | |
1046 | return CFPropertyListCreateXMLData(kCFAllocatorDefault, m_cfmdRef); | |
1047 | } | |
cb9c71ec | 1048 | |
5af840f1 RN |
1049 | bool ReadAsXML(CFTypeRef cfData, wxString* pErrorMsg = NULL) |
1050 | { | |
1051 | Clear(); | |
1052 | CFStringRef cfsError=NULL; | |
1053 | m_cfmdRef = (CFMutableDictionaryRef) CFPropertyListCreateFromXMLData( | |
1054 | kCFAllocatorDefault, | |
1055 | (CFDataRef)cfData, | |
1056 | kCFPropertyListMutableContainersAndLeaves, | |
cb9c71ec | 1057 | &cfsError ); |
172da31f | 1058 | if (cfsError) |
5af840f1 | 1059 | { |
172da31f | 1060 | if (pErrorMsg) |
5af840f1 RN |
1061 | *pErrorMsg = wxMacCFStringHolder(cfsError).AsString(); |
1062 | else | |
1063 | CFRelease(cfsError); | |
cb9c71ec | 1064 | } |
5af840f1 RN |
1065 | |
1066 | return m_cfmdRef != NULL; | |
1067 | } | |
172da31f DS |
1068 | |
1069 | private: | |
5af840f1 RN |
1070 | CFMutableDictionaryRef m_cfmdRef; |
1071 | }; | |
1072 | ||
1073 | // ---------------------------------------------------------------------------- | |
1074 | // wxCFArray | |
1075 | // ---------------------------------------------------------------------------- | |
1076 | ||
1077 | class wxCFArray | |
1078 | { | |
1079 | public: | |
1080 | wxCFArray(CFTypeRef ref, bool bRetain = wxCF_RELEASE) | |
1081 | { | |
1082 | m_cfmaRef = (CFMutableArrayRef)ref; | |
172da31f | 1083 | if (bRetain == wxCF_RETAIN && ref) |
5af840f1 RN |
1084 | CFRetain(ref); |
1085 | } | |
cb9c71ec | 1086 | |
5af840f1 | 1087 | wxCFArray(CFIndex cfiSize = 0) : m_cfmaRef(NULL) |
cb9c71ec WS |
1088 | { Create(cfiSize); } |
1089 | ||
1090 | ~wxCFArray() | |
1091 | { Clear(); } | |
1092 | ||
5af840f1 RN |
1093 | void MakeMutable(CFIndex cfiSize = 0) |
1094 | { | |
1095 | wxASSERT(IsValid()); | |
cb9c71ec | 1096 | |
5af840f1 RN |
1097 | CFMutableArrayRef oldref = m_cfmaRef; |
1098 | m_cfmaRef = CFArrayCreateMutableCopy( | |
1099 | kCFAllocatorDefault, | |
cb9c71ec WS |
1100 | cfiSize, |
1101 | (CFArrayRef)oldref); | |
5af840f1 RN |
1102 | CFRelease(oldref); |
1103 | } | |
cb9c71ec | 1104 | |
5af840f1 RN |
1105 | void BuildCallbacks(CFArrayCallBacks* pCbs) |
1106 | { | |
1107 | pCbs->version = 0; | |
1108 | pCbs->retain = RetainProc; | |
1109 | pCbs->release = ReleaseProc; | |
1110 | pCbs->copyDescription = NULL; | |
1111 | pCbs->equal = NULL; | |
1112 | } | |
1113 | ||
1114 | void Create(CFIndex cfiSize = 0) | |
1115 | { | |
1116 | Clear(); | |
1117 | CFArrayCallBacks cb; | |
1118 | BuildCallbacks(&cb); | |
cb9c71ec | 1119 | |
5af840f1 RN |
1120 | m_cfmaRef = CFArrayCreateMutable(kCFAllocatorDefault, cfiSize, &cb); |
1121 | } | |
cb9c71ec | 1122 | |
5af840f1 | 1123 | void Clear() |
172da31f | 1124 | { if (m_cfmaRef) CFRelease(m_cfmaRef); } |
cb9c71ec | 1125 | |
5af840f1 | 1126 | static const void* RetainProc(CFAllocatorRef, const void* v) |
cb9c71ec WS |
1127 | { return (const void*) CFRetain(v); } |
1128 | ||
5af840f1 | 1129 | static void ReleaseProc(CFAllocatorRef, const void* v) |
cb9c71ec WS |
1130 | { CFRelease(v); } |
1131 | ||
5af840f1 | 1132 | operator CFTypeRef () const |
cb9c71ec WS |
1133 | { return (CFTypeRef)m_cfmaRef; } |
1134 | ||
5af840f1 | 1135 | CFArrayRef GetCFArray() const |
cb9c71ec | 1136 | { return (CFArrayRef)m_cfmaRef; } |
5af840f1 RN |
1137 | |
1138 | CFMutableArrayRef GetCFMutableArray() | |
cb9c71ec | 1139 | { return (CFMutableArrayRef) m_cfmaRef; } |
5af840f1 RN |
1140 | |
1141 | CFTypeRef operator [] (CFIndex cfiIndex) const | |
cb9c71ec | 1142 | { |
5af840f1 | 1143 | wxASSERT(IsValid()); |
cb9c71ec WS |
1144 | return (CFTypeRef) CFArrayGetValueAtIndex((CFArrayRef)m_cfmaRef, cfiIndex); |
1145 | } | |
1146 | ||
5af840f1 | 1147 | CFIndex GetCount() |
cb9c71ec | 1148 | { |
5af840f1 | 1149 | wxASSERT(IsValid()); |
cb9c71ec | 1150 | return CFArrayGetCount((CFArrayRef)m_cfmaRef); |
5af840f1 | 1151 | } |
cb9c71ec | 1152 | |
5af840f1 | 1153 | void Add(CFTypeRef cftValue) |
cb9c71ec | 1154 | { |
5af840f1 | 1155 | wxASSERT(IsValid()); |
cb9c71ec | 1156 | CFArrayAppendValue(m_cfmaRef, cftValue); |
5af840f1 | 1157 | } |
cb9c71ec | 1158 | |
5af840f1 | 1159 | void Remove(CFIndex cfiIndex) |
cb9c71ec | 1160 | { |
5af840f1 RN |
1161 | wxASSERT(IsValid()); |
1162 | wxASSERT(cfiIndex < GetCount()); | |
cb9c71ec | 1163 | CFArrayRemoveValueAtIndex(m_cfmaRef, cfiIndex); |
5af840f1 | 1164 | } |
cb9c71ec | 1165 | |
5af840f1 | 1166 | void Set(CFIndex cfiIndex, CFTypeRef cftValue) |
cb9c71ec | 1167 | { |
5af840f1 RN |
1168 | wxASSERT(IsValid()); |
1169 | wxASSERT(cfiIndex < GetCount()); | |
cb9c71ec | 1170 | CFArraySetValueAtIndex(m_cfmaRef, cfiIndex, cftValue); |
5af840f1 RN |
1171 | } |
1172 | ||
172da31f DS |
1173 | bool IsOk() const |
1174 | { return m_cfmaRef != NULL; } | |
5af840f1 RN |
1175 | |
1176 | bool IsValid() const | |
1177 | { | |
1178 | return IsOk() && CFGetTypeID((CFTypeRef)m_cfmaRef) == CFArrayGetTypeID(); | |
1179 | } | |
1180 | ||
1181 | #if wxUSE_MIMETYPE | |
1182 | void MakeValidXML(); | |
cb9c71ec WS |
1183 | #endif |
1184 | ||
5af840f1 RN |
1185 | private: |
1186 | CFMutableArrayRef m_cfmaRef; | |
1187 | }; | |
1188 | ||
1189 | // ---------------------------------------------------------------------------- | |
1190 | // wxCFString | |
1191 | // ---------------------------------------------------------------------------- | |
1192 | ||
cb9c71ec | 1193 | class wxCFString |
5af840f1 RN |
1194 | { |
1195 | public: | |
1196 | wxCFString(CFTypeRef ref, bool bRetain = wxCF_RELEASE) : m_Holder((CFStringRef)ref, bRetain == wxCF_RELEASE) | |
172da31f | 1197 | {} |
cb9c71ec | 1198 | |
5af840f1 | 1199 | wxCFString(const wxChar* szString) : m_Holder(wxString(szString), wxLocale::GetSystemEncoding()) |
172da31f | 1200 | {} |
cb9c71ec | 1201 | |
5af840f1 | 1202 | wxCFString(const wxString& sString) : m_Holder(sString, wxLocale::GetSystemEncoding()) |
172da31f DS |
1203 | {} |
1204 | ||
1205 | virtual ~wxCFString() {} | |
cb9c71ec | 1206 | |
5af840f1 | 1207 | operator CFTypeRef() const |
cb9c71ec WS |
1208 | { return (CFTypeRef) ((CFStringRef) m_Holder); } |
1209 | ||
172da31f DS |
1210 | bool IsOk() |
1211 | { return ((CFTypeRef)(*this)) != NULL; } | |
cb9c71ec | 1212 | |
172da31f DS |
1213 | wxString BuildWXString() |
1214 | { return m_Holder.AsString(); } | |
5af840f1 RN |
1215 | |
1216 | private: | |
1217 | wxMacCFStringHolder m_Holder; | |
1218 | }; | |
1219 | ||
1220 | // ---------------------------------------------------------------------------- | |
1221 | // wxCFNumber | |
1222 | // ---------------------------------------------------------------------------- | |
1223 | ||
1224 | class wxCFNumber | |
1225 | { | |
1226 | public: | |
cb9c71ec | 1227 | wxCFNumber(int nValue) |
5af840f1 | 1228 | { |
cb9c71ec | 1229 | m_cfnRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &nValue); |
5af840f1 | 1230 | } |
cb9c71ec | 1231 | |
5af840f1 RN |
1232 | wxCFNumber(CFTypeRef ref, bool bRetain = wxCF_RELEASE) : m_cfnRef((CFNumberRef)ref) |
1233 | { | |
172da31f | 1234 | if (bRetain == wxCF_RETAIN && ref) |
5af840f1 RN |
1235 | CFRetain(ref); |
1236 | } | |
cb9c71ec | 1237 | |
172da31f | 1238 | virtual ~wxCFNumber() |
cb9c71ec | 1239 | { |
172da31f | 1240 | if (m_cfnRef) |
cb9c71ec WS |
1241 | CFRelease(m_cfnRef); |
1242 | } | |
1243 | ||
5af840f1 | 1244 | operator CFTypeRef() const |
cb9c71ec | 1245 | { return (CFTypeRef) m_cfnRef; } |
5af840f1 RN |
1246 | |
1247 | int GetValue() | |
cb9c71ec | 1248 | { |
5af840f1 | 1249 | int nOut; |
172da31f | 1250 | CFNumberGetValue( m_cfnRef, kCFNumberIntType, &nOut ); |
cb9c71ec | 1251 | |
5af840f1 RN |
1252 | return nOut; |
1253 | } | |
1254 | ||
172da31f DS |
1255 | bool IsOk() |
1256 | { return m_cfnRef != NULL; } | |
5af840f1 RN |
1257 | |
1258 | private: | |
1259 | CFNumberRef m_cfnRef; | |
1260 | }; | |
1261 | ||
1262 | // ---------------------------------------------------------------------------- | |
1263 | // wxCFURL | |
1264 | // ---------------------------------------------------------------------------- | |
1265 | ||
1266 | class wxCFURL | |
1267 | { | |
1268 | public: | |
cb9c71ec | 1269 | wxCFURL(CFTypeRef ref = NULL, bool bRetain = wxCF_RELEASE) : m_cfurlRef((CFURLRef)ref) |
5af840f1 | 1270 | { |
172da31f | 1271 | if (bRetain == wxCF_RETAIN && ref) |
5af840f1 RN |
1272 | CFRetain(ref); |
1273 | } | |
172da31f | 1274 | |
5af840f1 RN |
1275 | wxCFURL(const wxCFString& URLString, CFTypeRef BaseURL = NULL) |
1276 | { | |
1277 | Create(URLString, BaseURL); | |
1278 | } | |
cb9c71ec | 1279 | |
5af840f1 RN |
1280 | void Create(const wxCFString& URLString, CFTypeRef BaseURL = NULL) |
1281 | { | |
1282 | m_cfurlRef = CFURLCreateWithString( | |
1283 | kCFAllocatorDefault, | |
1284 | (CFStringRef)(CFTypeRef)URLString, | |
1285 | (CFURLRef) BaseURL); | |
1286 | } | |
cb9c71ec | 1287 | |
172da31f DS |
1288 | virtual ~wxCFURL() |
1289 | { | |
1290 | if (m_cfurlRef) | |
1291 | CFRelease(m_cfurlRef); | |
1292 | } | |
5af840f1 RN |
1293 | |
1294 | wxString BuildWXString() | |
1295 | { | |
1296 | return wxCFString(CFURLCopyPath(m_cfurlRef)).BuildWXString(); | |
1297 | } | |
cb9c71ec | 1298 | |
5af840f1 | 1299 | operator CFTypeRef() const |
cb9c71ec WS |
1300 | { return (CFTypeRef)m_cfurlRef; } |
1301 | ||
172da31f DS |
1302 | bool IsOk() |
1303 | { return m_cfurlRef != NULL; } | |
1304 | ||
5af840f1 RN |
1305 | private: |
1306 | CFURLRef m_cfurlRef; | |
1307 | }; | |
1308 | ||
1309 | // ---------------------------------------------------------------------------- | |
1310 | // wxCFData | |
1311 | // ---------------------------------------------------------------------------- | |
1312 | ||
1313 | #define wxCFDATA_RELEASEBUFFER 1 | |
5af840f1 RN |
1314 | |
1315 | class wxCFData | |
1316 | { | |
1317 | public: | |
cb9c71ec | 1318 | wxCFData(CFTypeRef ref, bool bRetain = wxCF_RELEASE) : m_cfdaRef((CFDataRef)ref) |
5af840f1 | 1319 | { |
172da31f | 1320 | if (bRetain == wxCF_RETAIN && ref) |
5af840f1 RN |
1321 | CFRetain(ref); |
1322 | } | |
172da31f | 1323 | |
5af840f1 RN |
1324 | wxCFData(const UInt8* pBytes, CFIndex len, bool bKeep = wxCFDATA_RELEASEBUFFER) |
1325 | { | |
172da31f | 1326 | if (bKeep == wxCFDATA_RELEASEBUFFER) |
5af840f1 RN |
1327 | { |
1328 | m_cfdaRef = CFDataCreateWithBytesNoCopy | |
1329 | (kCFAllocatorDefault, pBytes, len, kCFAllocatorDefault); | |
1330 | } | |
1331 | else | |
1332 | { | |
1333 | m_cfdaRef = CFDataCreate(kCFAllocatorDefault, pBytes, len); | |
1334 | } | |
1335 | } | |
5af840f1 | 1336 | |
172da31f | 1337 | virtual ~wxCFData() |
5af840f1 | 1338 | { |
172da31f DS |
1339 | if (m_cfdaRef) |
1340 | CFRelease(m_cfdaRef); | |
5af840f1 | 1341 | } |
cb9c71ec | 1342 | |
172da31f DS |
1343 | const UInt8* GetValue() |
1344 | { return CFDataGetBytePtr(m_cfdaRef); } | |
1345 | ||
5af840f1 | 1346 | CFIndex GetCount() |
172da31f | 1347 | { return CFDataGetLength(m_cfdaRef); } |
cb9c71ec | 1348 | |
5af840f1 | 1349 | operator CFTypeRef() const |
cb9c71ec WS |
1350 | { return (CFTypeRef)m_cfdaRef; } |
1351 | ||
172da31f DS |
1352 | bool IsOk() |
1353 | { return m_cfdaRef != NULL; } | |
1354 | ||
5af840f1 RN |
1355 | private: |
1356 | CFDataRef m_cfdaRef; | |
1357 | }; | |
1358 | ||
1359 | void wxCFDictionary::MakeValidXML() | |
1360 | { | |
1361 | CFIndex cfiCount = GetCount(); | |
1362 | CFTypeRef* pKeys = new CFTypeRef[cfiCount]; | |
1363 | CFTypeRef* pValues = new CFTypeRef[cfiCount]; | |
1364 | ||
1365 | CFDictionaryGetKeysAndValues(m_cfmdRef, pKeys, pValues); | |
1366 | ||
172da31f DS |
1367 | // for plist xml format, all dictionary keys must be cfstrings and |
1368 | // no values in the dictionary or subkeys/values can be NULL; | |
1369 | // additionally, CFURLs are not allowed | |
1370 | for (CFIndex i = 0; i < cfiCount; ++i) | |
5af840f1 | 1371 | { |
172da31f DS |
1372 | // must be an array, dictionary, string, bool, or int and cannot be null |
1373 | // and dictionaries can only contain cfstring keys | |
5af840f1 | 1374 | CFTypeRef cfRef = pValues[i]; |
172da31f | 1375 | if (!pKeys[i] || |
cb9c71ec | 1376 | CFGetTypeID(pKeys[i]) != CFStringGetTypeID() || |
5af840f1 RN |
1377 | !cfRef) |
1378 | { | |
1379 | Remove(pKeys[i]); | |
1380 | --i; | |
1381 | --cfiCount; | |
172da31f DS |
1382 | delete [] pKeys; |
1383 | delete [] pValues; | |
5af840f1 RN |
1384 | pKeys = new CFTypeRef[cfiCount]; |
1385 | pValues = new CFTypeRef[cfiCount]; | |
1386 | CFDictionaryGetKeysAndValues(m_cfmdRef, pKeys, pValues); | |
1387 | } | |
1388 | else if (CFGetTypeID(cfRef) == CFArrayGetTypeID()) | |
1389 | { | |
1390 | CFRetain(cfRef); | |
1391 | wxCFArray cfaCurrent(cfRef); | |
1392 | cfaCurrent.MakeMutable(); | |
1393 | cfaCurrent.MakeValidXML(); | |
1394 | Set(pKeys[i], cfaCurrent); | |
1395 | } | |
1396 | else if (CFGetTypeID(cfRef) == CFDictionaryGetTypeID()) | |
1397 | { | |
1398 | CFRetain(cfRef); | |
1399 | wxCFDictionary cfdCurrent(cfRef); | |
1400 | cfdCurrent.MakeMutable(); | |
1401 | cfdCurrent.MakeValidXML(); | |
1402 | Set(pKeys[i], cfdCurrent); | |
1403 | } | |
d0ee33f5 WS |
1404 | else if ( CFGetTypeID(cfRef) != CFStringGetTypeID() && |
1405 | CFGetTypeID(cfRef) != CFNumberGetTypeID() && | |
1406 | CFGetTypeID(cfRef) != CFBooleanGetTypeID() ) | |
5af840f1 RN |
1407 | { |
1408 | Remove(pKeys[i]); | |
1409 | --i; | |
1410 | --cfiCount; | |
172da31f DS |
1411 | delete [] pKeys; |
1412 | delete [] pValues; | |
5af840f1 RN |
1413 | pKeys = new CFTypeRef[cfiCount]; |
1414 | pValues = new CFTypeRef[cfiCount]; | |
1415 | CFDictionaryGetKeysAndValues(m_cfmdRef, pKeys, pValues); | |
cb9c71ec | 1416 | } |
5af840f1 RN |
1417 | } |
1418 | ||
172da31f DS |
1419 | delete [] pValues; |
1420 | delete [] pKeys; | |
5af840f1 RN |
1421 | } |
1422 | ||
1423 | void wxCFArray::MakeValidXML() | |
1424 | { | |
172da31f | 1425 | for (CFIndex i = 0; i < GetCount(); ++i) |
5af840f1 RN |
1426 | { |
1427 | //must be an array, dictionary, string, bool, or int and cannot be null | |
1428 | //and dictionaries can only contain cfstring keys | |
1429 | CFTypeRef cfRef = (*this)[i]; | |
172da31f | 1430 | if (!cfRef) |
5af840f1 RN |
1431 | { |
1432 | Remove(i); | |
1433 | --i; | |
1434 | } | |
1435 | else if (CFGetTypeID(cfRef) == CFArrayGetTypeID()) | |
1436 | { | |
1437 | CFRetain(cfRef); | |
1438 | wxCFArray cfaCurrent(cfRef); | |
1439 | cfaCurrent.MakeMutable(); | |
1440 | cfaCurrent.MakeValidXML(); | |
1441 | Set(i, cfaCurrent); | |
1442 | } | |
1443 | else if (CFGetTypeID(cfRef) == CFDictionaryGetTypeID()) | |
1444 | { | |
1445 | CFRetain(cfRef); | |
1446 | wxCFDictionary cfdCurrent(cfRef); | |
1447 | cfdCurrent.MakeMutable(); | |
1448 | cfdCurrent.MakeValidXML(); | |
1449 | Set(i, cfdCurrent); | |
1450 | } | |
d0ee33f5 WS |
1451 | else if ( CFGetTypeID(cfRef) != CFStringGetTypeID() && |
1452 | CFGetTypeID(cfRef) != CFNumberGetTypeID() && | |
1453 | CFGetTypeID(cfRef) != CFBooleanGetTypeID() ) | |
5af840f1 RN |
1454 | { |
1455 | Remove(i); | |
1456 | --i; | |
1457 | } | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | // | |
1462 | // | |
1463 | // | |
1464 | // END TODO | |
1465 | // | |
1466 | // | |
1467 | // | |
1468 | ||
1469 | wxFileType* wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo) | |
1470 | { | |
1471 | bool bInfoSuccess = false; | |
cb9c71ec WS |
1472 | |
1473 | const wxArrayString& asExtensions = ftInfo.GetExtensions(); | |
5af840f1 | 1474 | size_t dwFoundIndex = 0; |
172da31f | 1475 | if (!asExtensions.GetCount()) |
5af840f1 RN |
1476 | { |
1477 | wxLogDebug(wxT("Must have extension to associate with")); | |
1478 | } | |
cb9c71ec | 1479 | |
172da31f DS |
1480 | // Find and write to Info.plist in main bundle (note that some other |
1481 | // apps have theirs named differently, i.e. IE's is named Info-macos.plist | |
1482 | // some apps (non-wx) use the 'plst' resource instead | |
5af840f1 | 1483 | CFBundleRef cfbMain = CFBundleGetMainBundle(); |
172da31f | 1484 | if (cfbMain) |
cb9c71ec | 1485 | { |
5af840f1 RN |
1486 | UInt32 dwBundleType, dwBundleCreator; |
1487 | CFBundleGetPackageInfo(cfbMain, &dwBundleType, &dwBundleCreator); | |
cb9c71ec | 1488 | |
172da31f DS |
1489 | // if launching terminal non-app, version will be 'BNDL' (generic bundle, maybe in other cases too), |
1490 | // which will give us the incorrect info.plist path | |
1491 | // otherwise it will be 'APPL', or in the case of a framework, 'FMWK' | |
1492 | if (dwBundleType == 'APPL') | |
5af840f1 | 1493 | { |
5af840f1 RN |
1494 | wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyBundleURL(cfbMain)); |
1495 | // wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyExecutableURL(cfbMain)); | |
1496 | wxString sInfoPath; | |
1497 | // sInfoPath << wxT("file://"); | |
1498 | sInfoPath << cfurlBundleLoc.BuildWXString(); | |
1499 | sInfoPath << wxT("Contents/Info.plist"); | |
cb9c71ec | 1500 | |
5af840f1 RN |
1501 | // wxCFDictionary cfdInfo( CFBundleGetInfoDictionary(cfbMain), wxCF_RETAIN ); |
1502 | wxCFDictionary cfdInfo; | |
1503 | bool bInfoOpenSuccess = false; | |
1504 | wxFile indictfile; | |
172da31f | 1505 | if (indictfile.Open(sInfoPath, wxFile::read)) |
5af840f1 RN |
1506 | { |
1507 | CFIndex cfiBufLen = (CFIndex) indictfile.Length(); | |
1508 | const UInt8* pBuffer = new UInt8[cfiBufLen]; | |
1509 | indictfile.Read((void*)pBuffer, cfiBufLen); | |
1510 | wxCFData cfdaInDict(pBuffer, cfiBufLen); | |
1511 | wxString sError; | |
cb9c71ec | 1512 | bInfoOpenSuccess = cfdInfo.ReadAsXML(cfdaInDict, &sError); |
172da31f | 1513 | if (!bInfoOpenSuccess) |
5af840f1 RN |
1514 | wxLogDebug(sError); |
1515 | indictfile.Close(); | |
1516 | } | |
172da31f DS |
1517 | |
1518 | if (bInfoOpenSuccess) | |
5af840f1 RN |
1519 | { |
1520 | cfdInfo.MakeMutable( cfdInfo.GetCount() + 1 ); | |
1521 | ||
1522 | wxCFArray cfaDocTypes( cfdInfo[ wxCFString(wxT("CFBundleDocumentTypes")) ], wxCF_RETAIN ); | |
cb9c71ec | 1523 | |
172da31f DS |
1524 | bool bAddDocTypesArrayToDictionary = !cfaDocTypes.IsOk(); |
1525 | if (bAddDocTypesArrayToDictionary) | |
5af840f1 RN |
1526 | cfaDocTypes.Create(); |
1527 | else | |
1528 | cfaDocTypes.MakeMutable( cfaDocTypes.GetCount() + 1 ); | |
cb9c71ec | 1529 | |
5af840f1 | 1530 | bool bEntryFound = false; |
cb9c71ec | 1531 | |
172da31f | 1532 | // search for duplicates |
5af840f1 | 1533 | CFIndex i; |
172da31f | 1534 | for (i = 0; i < cfaDocTypes.GetCount(); ++i) |
5af840f1 RN |
1535 | { |
1536 | wxCFDictionary cfdDocTypeEntry( cfaDocTypes[i], wxCF_RETAIN ); | |
cb9c71ec | 1537 | |
172da31f DS |
1538 | // A lot of apps don't support MIME types for some reason |
1539 | // so we go by extensions only | |
cb9c71ec | 1540 | wxCFArray cfaExtensions( cfdDocTypeEntry[ wxCFString(wxT("CFBundleTypeExtensions")) ], |
5af840f1 | 1541 | wxCF_RETAIN ); |
cb9c71ec | 1542 | |
172da31f | 1543 | if (!cfaExtensions.IsOk()) |
5af840f1 | 1544 | continue; |
cb9c71ec | 1545 | |
172da31f | 1546 | for (CFIndex iExt = 0; iExt < cfaExtensions.GetCount(); ++iExt) |
cb9c71ec | 1547 | { |
5af840f1 RN |
1548 | for (size_t iWXExt = 0; iWXExt < asExtensions.GetCount(); ++iWXExt) |
1549 | { | |
172da31f | 1550 | if (asExtensions[iWXExt] == |
5af840f1 RN |
1551 | wxCFString(cfaExtensions[iExt], wxCF_RETAIN).BuildWXString()) |
1552 | { | |
1553 | bEntryFound = true; | |
1554 | dwFoundIndex = iWXExt; | |
172da31f | 1555 | |
5af840f1 RN |
1556 | break; |
1557 | } | |
cb9c71ec WS |
1558 | } //end of wxstring array |
1559 | ||
172da31f | 1560 | if (bEntryFound) |
5af840f1 | 1561 | break; |
cb9c71ec WS |
1562 | } //end for cf array |
1563 | ||
172da31f | 1564 | if (bEntryFound) |
5af840f1 | 1565 | break; |
172da31f | 1566 | } //end for doctypes |
cb9c71ec | 1567 | |
5af840f1 RN |
1568 | wxCFDictionary cfdNewEntry; |
1569 | ||
172da31f | 1570 | if (!ftInfo.GetDescription().empty()) |
5af840f1 | 1571 | { |
cb9c71ec | 1572 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeName")), |
5af840f1 RN |
1573 | wxCFString(ftInfo.GetDescription()) ); |
1574 | } | |
cb9c71ec | 1575 | |
172da31f | 1576 | if (!ftInfo.GetIconFile().empty()) |
5af840f1 | 1577 | { |
cb9c71ec | 1578 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeIconFile")), |
5af840f1 RN |
1579 | wxCFString(ftInfo.GetIconFile()) ); |
1580 | } | |
cb9c71ec | 1581 | |
5af840f1 RN |
1582 | wxCFArray cfaOSTypes; |
1583 | wxCFArray cfaExtensions; | |
1584 | wxCFArray cfaMimeTypes; | |
cb9c71ec | 1585 | |
5af840f1 RN |
1586 | //OSTypes is a cfarray of four-char-codes - '****' for unrestricted |
1587 | cfaOSTypes.Add( wxCFString(wxT("****")) ); | |
1588 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeOSTypes")), cfaOSTypes ); | |
1589 | ||
172da31f DS |
1590 | //'*' for unrestricted |
1591 | if (ftInfo.GetExtensionsCount() != 0) | |
5af840f1 | 1592 | { |
03603400 | 1593 | for (size_t iExtension = 0; iExtension < ftInfo.GetExtensionsCount(); ++iExtension) |
5af840f1 RN |
1594 | { |
1595 | cfaExtensions.Add( wxCFString( asExtensions[iExtension] ) ); | |
1596 | } | |
cb9c71ec | 1597 | |
5af840f1 RN |
1598 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeExtensions")), cfaExtensions ); |
1599 | } | |
cb9c71ec | 1600 | |
172da31f | 1601 | if (!ftInfo.GetMimeType().empty()) |
5af840f1 RN |
1602 | { |
1603 | cfaMimeTypes.Add( wxCFString(ftInfo.GetMimeType()) ); | |
1604 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeMIMETypes")), cfaMimeTypes ); | |
1605 | } | |
cb9c71ec | 1606 | |
5af840f1 RN |
1607 | // Editor - can perform all actions |
1608 | // Viewer - all actions except manipulation/saving | |
1609 | // None - can perform no actions | |
1610 | cfdNewEntry.Add( wxCFString(wxT("CFBundleTypeRole")), wxCFString(wxT("Editor")) ); | |
cb9c71ec | 1611 | |
5af840f1 RN |
1612 | // Is application bundled? |
1613 | cfdNewEntry.Add( wxCFString(wxT("LSTypeIsPackage")), kCFBooleanTrue ); | |
1614 | ||
172da31f | 1615 | if (bEntryFound) |
5af840f1 RN |
1616 | cfaDocTypes.Set(i, cfdNewEntry); |
1617 | else | |
1618 | cfaDocTypes.Add(cfdNewEntry); | |
cb9c71ec | 1619 | |
5af840f1 | 1620 | // set the doc types array in the muted dictionary |
172da31f | 1621 | if (bAddDocTypesArrayToDictionary) |
5af840f1 RN |
1622 | cfdInfo.Add(wxCFString(wxT("CFBundleDocumentTypes")), cfaDocTypes); |
1623 | else | |
1624 | cfdInfo.Set(wxCFString(wxT("CFBundleDocumentTypes")), cfaDocTypes); | |
cb9c71ec | 1625 | |
5af840f1 RN |
1626 | cfdInfo.MakeValidXML(); |
1627 | ||
1628 | wxFile outdictfile; | |
172da31f | 1629 | if (outdictfile.Open(sInfoPath, wxFile::write)) |
5af840f1 RN |
1630 | { |
1631 | wxCFData cfdaInfo(cfdInfo.WriteAsXML()); | |
172da31f | 1632 | if (cfdaInfo.IsOk()) |
5af840f1 | 1633 | { |
172da31f | 1634 | if (outdictfile.Write(cfdaInfo.GetValue(), cfdaInfo.GetCount()) != |
5af840f1 RN |
1635 | (wxFileOffset)cfdaInfo.GetCount()) |
1636 | { | |
1637 | wxLogDebug(wxT("error in writing to file")); | |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | bInfoSuccess = true; | |
172da31f | 1642 | |
5af840f1 RN |
1643 | //#if defined(__DARWIN__) |
1644 | // //force launch services to update its database for the finder | |
1645 | // OSStatus status = LSRegisterURL((CFURLRef)(CFTypeRef)cfurlBundleLoc, true); | |
172da31f | 1646 | // if (status != noErr) |
5af840f1 RN |
1647 | // { |
1648 | // wxLogDebug(wxT("LSRegisterURL Failed.")); | |
1649 | // } | |
1650 | //#endif | |
1651 | } | |
1652 | outdictfile.Close(); | |
1653 | } | |
1654 | else | |
1655 | { | |
1656 | outdictfile.Close(); | |
1657 | wxLogDebug(wxT("Could not read in new dictionary")); | |
1658 | } | |
1659 | } | |
1660 | else | |
1661 | { | |
cb9c71ec | 1662 | wxLogDebug(wxString(wxT("Could not open [")) + |
5af840f1 RN |
1663 | sInfoPath + wxT("] for writing.")); |
1664 | } | |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | wxLogDebug(wxT("No info dictionary in main bundle")); | |
1669 | } | |
1670 | } | |
cb9c71ec | 1671 | else |
5af840f1 RN |
1672 | { |
1673 | wxLogDebug(wxT("Can only call associate from bundled app within XXX.app")); | |
1674 | } | |
1675 | } | |
1676 | else | |
1677 | { | |
1678 | wxLogDebug(wxT("No main bundle")); | |
1679 | } | |
1680 | ||
1681 | #if defined(__DARWIN__) | |
172da31f | 1682 | if (!bInfoSuccess) |
5af840f1 RN |
1683 | return NULL; |
1684 | #endif | |
007ae8d0 | 1685 | |
172da31f DS |
1686 | // on mac you have to embed it into the mac's file reference resource ('FREF' I believe) |
1687 | // or, alternately, you could just add an entry to m_hDatabase, but you'd need to get | |
1688 | // the app's signature somehow... | |
cb9c71ec | 1689 | |
172da31f DS |
1690 | OSType processType, creator; |
1691 | OSStatus status = MoreProcGetProcessTypeSignature(NULL, &processType, &creator); | |
1692 | ||
1693 | if (status == noErr) | |
7baa887c RN |
1694 | { |
1695 | Str255 psCreatorName; | |
1696 | FSSpec dummySpec; | |
1697 | status = FindApplication(creator, false, psCreatorName, &dummySpec); | |
cb9c71ec | 1698 | |
172da31f | 1699 | if (status == noErr) |
7baa887c | 1700 | { |
cb9c71ec | 1701 | //get the file type if it exists - |
7baa887c RN |
1702 | //if it really does then modify the database then save it, |
1703 | //otherwise we need to create a whole new entry | |
5af840f1 | 1704 | wxFileType* pFileType = GetFileTypeFromExtension(asExtensions[dwFoundIndex]); |
172da31f | 1705 | if (pFileType) |
7baa887c RN |
1706 | { |
1707 | ICMapEntry entry; | |
cb9c71ec | 1708 | ICGetMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, |
172da31f | 1709 | pFileType->m_impl->m_lIndex, &entry ); |
cb9c71ec WS |
1710 | |
1711 | memcpy(entry.creatorAppName, psCreatorName, sizeof(Str255)); | |
7baa887c | 1712 | entry.fileCreator = creator; |
cb9c71ec WS |
1713 | |
1714 | status = ICSetMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, | |
172da31f | 1715 | pFileType->m_impl->m_lIndex, &entry ); |
7baa887c RN |
1716 | |
1717 | //success | |
172da31f | 1718 | if (status == noErr) |
5af840f1 | 1719 | { |
172da31f DS |
1720 | return pFileType; |
1721 | ||
5af840f1 RN |
1722 | //kICAttrNoChange means we don't care about attributes such as |
1723 | //locking in the database | |
cb9c71ec | 1724 | // status = ICSetPrefHandle((ICInstance) m_hIC, kICMapping, |
5af840f1 | 1725 | // kICAttrNoChange, (Handle) m_hDatabase); |
172da31f DS |
1726 | // if (status == noErr) |
1727 | // return pFileType; | |
5af840f1 RN |
1728 | // else |
1729 | // { | |
cb9c71ec | 1730 | // wxLogDebug(wxString::Format(wxT("%i - %s"), (int)status, wxT("ICSetPrefHandle failed."))); |
172da31f | 1731 | // } |
5af840f1 | 1732 | } |
cb9c71ec WS |
1733 | else |
1734 | { | |
1735 | wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICSetMapEntry failed."))); | |
7baa887c | 1736 | } |
cb9c71ec | 1737 | |
172da31f | 1738 | // failure - cleanup |
7baa887c RN |
1739 | delete pFileType; |
1740 | } | |
1741 | else | |
1742 | { | |
172da31f DS |
1743 | // TODO: Maybe force all 3 of these to be non-empty? |
1744 | Str255 psExtension, psMimeType, psDescription; | |
cb9c71ec WS |
1745 | |
1746 | wxMacStringToPascal(wxString(wxT(".")) + ftInfo.GetExtensions()[0], psExtension); | |
1747 | wxMacStringToPascal(ftInfo.GetMimeType(), psMimeType); | |
1748 | wxMacStringToPascal(ftInfo.GetDescription(), psDescription); | |
7baa887c RN |
1749 | |
1750 | Str255 psPostCreatorName; | |
03603400 | 1751 | wxMacStringToPascal(wxEmptyString, psPostCreatorName); |
7baa887c | 1752 | |
7baa887c | 1753 | //add the entry to the database |
7baa887c RN |
1754 | ICMapEntry entry; |
1755 | entry.totalLength = sizeof(ICMapEntry); | |
1756 | entry.fixedLength = kICMapFixedLength; | |
1757 | entry.version = 0; | |
1758 | entry.fileType = 0; //TODO: File type? | |
1759 | entry.fileCreator = creator; | |
1760 | entry.postCreator = 0; | |
1761 | entry.flags = kICMapDataForkBit; //TODO: Maybe resource is valid by default too? | |
b94fa9f5 | 1762 | PLstrcpy( entry.extension , psExtension ) ; |
fcc7c995 RN |
1763 | memcpy(entry.creatorAppName, psCreatorName, sizeof(Str255)); |
1764 | memcpy(entry.postAppName, psPostCreatorName, sizeof(Str255)); | |
1765 | memcpy(entry.MIMEType, psMimeType, sizeof(Str255)); | |
1766 | memcpy(entry.entryName, psDescription, sizeof(Str255)); | |
cb9c71ec | 1767 | |
7baa887c | 1768 | status = ICAddMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, &entry); |
172da31f | 1769 | if (status == noErr) |
7baa887c | 1770 | { |
172da31f DS |
1771 | return GetFileTypeFromExtension(ftInfo.GetMimeType()); |
1772 | ||
1773 | // kICAttrNoChange means we don't care about attributes such as | |
1774 | // locking in the database | |
1775 | // status = ICSetPrefHandle((ICInstance) m_hIC, kICMapping, | |
1776 | // kICAttrNoChange, (Handle) m_hDatabase); | |
1777 | ||
1778 | // return the entry in the database if successful | |
1779 | // if (status == noErr) | |
1780 | // return GetFileTypeFromExtension(ftInfo.GetMimeType()); | |
1781 | // else | |
1782 | // { | |
1783 | // wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICSetPrefHandle failed."))); | |
1784 | // } | |
7baa887c | 1785 | } |
cb9c71ec WS |
1786 | else |
1787 | { | |
1788 | wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICAppMapEntry failed."))); | |
7baa887c RN |
1789 | } |
1790 | } | |
172da31f | 1791 | } // end if FindApplcation was successful |
cb9c71ec WS |
1792 | else |
1793 | { | |
1794 | wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("FindApplication failed."))); | |
7baa887c | 1795 | } |
172da31f | 1796 | } // end if it could obtain app's signature |
cb9c71ec WS |
1797 | else |
1798 | { | |
1799 | wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("GetProcessSignature failed."))); | |
7baa887c | 1800 | } |
172da31f | 1801 | |
007ae8d0 GD |
1802 | return NULL; |
1803 | } | |
f040060e GD |
1804 | |
1805 | bool | |
7baa887c | 1806 | wxMimeTypesManagerImpl::Unassociate(wxFileType *pFileType) |
f040060e | 1807 | { |
5af840f1 RN |
1808 | wxASSERT(pFileType); |
1809 | bool bInfoSuccess = false; | |
cb9c71ec | 1810 | |
5af840f1 | 1811 | wxArrayString asExtensions; |
cb9c71ec WS |
1812 | pFileType->GetExtensions(asExtensions); |
1813 | ||
172da31f | 1814 | if (!asExtensions.GetCount()) |
5af840f1 RN |
1815 | { |
1816 | wxLogDebug(wxT("Must have extension to disassociate")); | |
cb9c71ec | 1817 | return false; |
5af840f1 RN |
1818 | } |
1819 | ||
172da31f DS |
1820 | // Find and write to Info.plist in main bundle (note that some other |
1821 | // apps have theirs named differently, i.e. IE's is named Info-macos.plist | |
1822 | // some apps (non-wx) use the 'plst' resource instead | |
5af840f1 | 1823 | CFBundleRef cfbMain = CFBundleGetMainBundle(); |
172da31f | 1824 | if (cfbMain) |
cb9c71ec | 1825 | { |
5af840f1 RN |
1826 | UInt32 dwBundleType, dwBundleCreator; |
1827 | CFBundleGetPackageInfo(cfbMain, &dwBundleType, &dwBundleCreator); | |
cb9c71ec | 1828 | |
172da31f DS |
1829 | // if launching terminal non-app, version will be 'BNDL' (generic bundle, maybe in other cases too), |
1830 | // which will give us the incorrect info.plist path | |
1831 | // otherwise it will be 'APPL', or in the case of a framework, 'FMWK' | |
1832 | if (dwBundleType == 'APPL') | |
5af840f1 RN |
1833 | { |
1834 | ||
1835 | wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyBundleURL(cfbMain)); | |
1836 | // wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyExecutableURL(cfbMain)); | |
1837 | wxString sInfoPath; | |
1838 | // sInfoPath << wxT("file://"); | |
1839 | sInfoPath << cfurlBundleLoc.BuildWXString(); | |
1840 | sInfoPath << wxT("Contents/Info.plist"); | |
cb9c71ec | 1841 | |
5af840f1 RN |
1842 | // wxCFDictionary cfdInfo( (CFTypeRef) CFBundleGetInfoDictionary(cfbMain), wxCF_RETAIN ); |
1843 | wxCFDictionary cfdInfo; | |
1844 | bool bInfoOpenSuccess = false; | |
1845 | wxFile indictfile; | |
172da31f | 1846 | if (indictfile.Open(sInfoPath, wxFile::read)) |
5af840f1 RN |
1847 | { |
1848 | CFIndex cfiBufLen = (CFIndex) indictfile.Length(); | |
1849 | const UInt8* pBuffer = new UInt8[cfiBufLen]; | |
1850 | indictfile.Read((void*)pBuffer, cfiBufLen); | |
1851 | wxCFData cfdaInDict(pBuffer, cfiBufLen); | |
1852 | wxString sError; | |
cb9c71ec | 1853 | bInfoOpenSuccess = cfdInfo.ReadAsXML(cfdaInDict, &sError); |
172da31f | 1854 | if (!bInfoOpenSuccess) |
5af840f1 RN |
1855 | wxLogDebug(sError); |
1856 | indictfile.Close(); | |
1857 | } | |
172da31f DS |
1858 | |
1859 | if (bInfoOpenSuccess) | |
5af840f1 RN |
1860 | { |
1861 | cfdInfo.MakeMutable( cfdInfo.GetCount() + 1 ); | |
1862 | ||
1863 | wxCFArray cfaDocTypes( cfdInfo[ wxCFString(wxT("CFBundleDocumentTypes")) ], wxCF_RETAIN ); | |
cb9c71ec | 1864 | |
172da31f | 1865 | if (cfaDocTypes.IsOk()) |
cb9c71ec | 1866 | { |
5af840f1 | 1867 | bool bEntryFound = false; |
cb9c71ec | 1868 | |
5af840f1 RN |
1869 | //search for duplicate |
1870 | CFIndex i; | |
172da31f | 1871 | for (i = 0; i < cfaDocTypes.GetCount(); ++i) |
5af840f1 RN |
1872 | { |
1873 | wxCFDictionary cfdDocTypeEntry( cfaDocTypes[i], wxCF_RETAIN ); | |
cb9c71ec | 1874 | |
5af840f1 RN |
1875 | //A lot of apps dont do to mime types for some reason |
1876 | //so we go by extensions only | |
cb9c71ec | 1877 | wxCFArray cfaExtensions( cfdDocTypeEntry[ wxCFString(wxT("CFBundleTypeExtensions")) ], |
5af840f1 | 1878 | wxCF_RETAIN ); |
cb9c71ec | 1879 | |
172da31f | 1880 | if (!cfaExtensions.IsOk()) |
5af840f1 | 1881 | continue; |
cb9c71ec | 1882 | |
172da31f | 1883 | for (CFIndex iExt = 0; iExt < cfaExtensions.GetCount(); ++iExt) |
cb9c71ec | 1884 | { |
5af840f1 RN |
1885 | for (size_t iWXExt = 0; iWXExt < asExtensions.GetCount(); ++iWXExt) |
1886 | { | |
172da31f | 1887 | if (asExtensions[iWXExt] == |
5af840f1 RN |
1888 | wxCFString(cfaExtensions[iExt], wxCF_RETAIN).BuildWXString()) |
1889 | { | |
1890 | bEntryFound = true; | |
1891 | cfaDocTypes.Remove(i); | |
1892 | cfdInfo.Set( wxCFString(wxT("CFBundleDocumentTypes")) , cfaDocTypes ); | |
1893 | break; | |
1894 | } | |
cb9c71ec WS |
1895 | } //end of wxstring array |
1896 | ||
172da31f | 1897 | if (bEntryFound) |
5af840f1 | 1898 | break; |
cb9c71ec WS |
1899 | } //end for cf array |
1900 | ||
172da31f | 1901 | if (bEntryFound) |
5af840f1 RN |
1902 | break; |
1903 | }//end for doctypes | |
cb9c71ec | 1904 | |
172da31f | 1905 | if (bEntryFound) |
5af840f1 RN |
1906 | { |
1907 | cfdInfo.MakeValidXML(); | |
1908 | ||
1909 | wxFile outdictfile; | |
172da31f | 1910 | if (outdictfile.Open(sInfoPath, wxFile::write)) |
5af840f1 RN |
1911 | { |
1912 | wxCFData cfdaInfo(cfdInfo.WriteAsXML()); | |
172da31f | 1913 | if (cfdaInfo.IsOk()) |
5af840f1 | 1914 | { |
172da31f | 1915 | if (outdictfile.Write(cfdaInfo.GetValue(), cfdaInfo.GetCount()) != |
5af840f1 RN |
1916 | (wxFileOffset)cfdaInfo.GetCount()) |
1917 | { | |
1918 | wxLogDebug(wxT("error in writing to file")); | |
1919 | } | |
1920 | else | |
1921 | { | |
1922 | bInfoSuccess = true; | |
172da31f | 1923 | |
5af840f1 RN |
1924 | //#if defined(__DARWIN__) |
1925 | // //force launch services to update its database for the finder | |
1926 | // OSStatus status = LSRegisterURL((CFURLRef)(CFTypeRef)cfurlBundleLoc, true); | |
172da31f | 1927 | // if (status != noErr) |
5af840f1 RN |
1928 | // { |
1929 | // wxLogDebug(wxT("LSRegisterURL Failed.")); | |
1930 | // } | |
1931 | //#endif | |
1932 | } | |
1933 | outdictfile.Close(); | |
1934 | } | |
1935 | else | |
1936 | { | |
1937 | outdictfile.Close(); | |
1938 | wxLogDebug(wxT("Could not read in new dictionary")); | |
1939 | } | |
1940 | } | |
1941 | else | |
1942 | { | |
172da31f DS |
1943 | wxLogDebug( |
1944 | wxString(wxT("Could not open [")) + | |
1945 | sInfoPath + wxT("] for writing.")); | |
5af840f1 RN |
1946 | } |
1947 | } | |
1948 | else | |
1949 | { | |
1950 | wxLogDebug(wxT("Entry not found to remove")); | |
172da31f | 1951 | |
cb9c71ec WS |
1952 | wxString sPrintOut; |
1953 | wxCFDictionary::PrintOutArray(sPrintOut, (CFArrayRef)(CFTypeRef)cfaDocTypes); | |
5af840f1 | 1954 | wxLogDebug(sPrintOut); |
172da31f DS |
1955 | |
1956 | for (size_t i = 0; i < asExtensions.GetCount(); ++i) | |
5af840f1 RN |
1957 | wxLogDebug(asExtensions[i]); |
1958 | } | |
1959 | } | |
1960 | else | |
1961 | { | |
1962 | wxLogDebug(wxT("No doc types array found")); | |
1963 | wxString sPrintOut; cfdInfo.PrintOut(sPrintOut); wxLogDebug(sPrintOut); | |
1964 | } | |
1965 | } | |
1966 | else | |
1967 | { | |
1968 | wxLogDebug(wxT("No info dictionary in main bundle")); | |
1969 | } | |
1970 | } | |
cb9c71ec | 1971 | else |
5af840f1 RN |
1972 | { |
1973 | wxLogDebug(wxT("Can only call associate from bundled app within XXX.app")); | |
1974 | } | |
1975 | } | |
1976 | else | |
1977 | { | |
1978 | wxLogDebug(wxT("No main bundle")); | |
1979 | } | |
1980 | ||
1981 | #if defined(__DARWIN__) | |
172da31f | 1982 | if (!bInfoSuccess) |
cb9c71ec | 1983 | return false; |
5af840f1 RN |
1984 | #endif |
1985 | ||
172da31f DS |
1986 | // this should be as easy as removing the entry from the database |
1987 | // and then saving the database | |
cb9c71ec | 1988 | OSStatus status = ICDeleteMapEntry( (ICInstance) m_hIC, (Handle) m_hDatabase, |
7baa887c | 1989 | pFileType->m_impl->m_lIndex); |
cb9c71ec | 1990 | |
172da31f | 1991 | if (status == noErr) |
7baa887c | 1992 | { |
172da31f DS |
1993 | return true; |
1994 | ||
7baa887c RN |
1995 | //kICAttrNoChange means we don't care about attributes such as |
1996 | //locking in the database | |
cb9c71ec | 1997 | // status = ICSetPrefHandle((ICInstance) m_hIC, kICMapping, |
5af840f1 | 1998 | // kICAttrNoChange, (Handle) m_hDatabase); |
cb9c71ec | 1999 | |
172da31f DS |
2000 | // if (status == noErr) |
2001 | // { | |
2002 | // return true; | |
2003 | // } | |
5af840f1 | 2004 | // else |
cb9c71ec WS |
2005 | // { |
2006 | // wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICSetPrefHandle failed."))); | |
5af840f1 | 2007 | // } |
7baa887c RN |
2008 | } |
2009 | else | |
cb9c71ec WS |
2010 | { |
2011 | wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICDeleteMapEntry failed."))); | |
7baa887c | 2012 | } |
cb9c71ec WS |
2013 | |
2014 | return false; | |
f040060e | 2015 | } |
cb9c71ec | 2016 | |
172da31f DS |
2017 | #if 0 |
2018 | CFWriteStreamRef cfwsInfo = CFWriteStreamCreateWithFile( | |
2019 | kCFAllocatorDefault, | |
2020 | (CFURLRef) (CFTypeRef)cfurlInfoLoc ); | |
2021 | ||
2022 | // CFShow(cfdInfo); | |
2023 | if (cfwsInfo) | |
2024 | { | |
2025 | Boolean bOpened = CFWriteStreamOpen(cfwsInfo); | |
2026 | if (bOpened) | |
2027 | { | |
2028 | CFStringRef cfsError; | |
2029 | CFIndex cfiWritten = CFPropertyListWriteToStream((CFPropertyListRef)(CFTypeRef)cfdInfo, | |
2030 | cfwsInfo, | |
2031 | kCFPropertyListXMLFormat_v1_0, //100 | |
2032 | &cfsError); | |
2033 | if (cfsError && cfiWritten == 0) | |
2034 | { | |
2035 | wxLogDebug(wxCFString(cfsError).BuildWXString()); | |
2036 | wxString sMessage; | |
2037 | cfdInfo.PrintOut(sMessage); | |
2038 | wxLogDebug(sMessage); | |
2039 | } | |
2040 | else | |
2041 | { | |
2042 | bInfoSuccess = true; | |
5af840f1 RN |
2043 | //#if defined(__DARWIN__) |
2044 | // //force launch services to update its database for the finder | |
2045 | // OSStatus status = LSRegisterURL((CFURLRef)(CFTypeRef)cfurlBundleLoc, true); | |
172da31f | 2046 | // if (status != noErr) |
5af840f1 RN |
2047 | // { |
2048 | // wxLogDebug(wxT("LSRegisterURL Failed.")); | |
2049 | // } | |
2050 | //#endif | |
172da31f | 2051 | } |
cb9c71ec | 2052 | |
172da31f DS |
2053 | CFWriteStreamClose(cfwsInfo); |
2054 | #endif | |
f040060e | 2055 | |
d0ee33f5 | 2056 | #endif //wxUSE_MIMETYPE |