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