1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/mimetype.cpp
3 // Purpose: Mac Carbon implementation for wx MIME-related classes
8 // Copyright: (c) 2005 Ryan Norton (<wxprojects@comcast.net>)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // TODO: Search Info[-macos](classic).plist dictionary in addition
14 // to Internet Config database.
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.
20 // Implement GetIcon from one of the FinderInfo functions - or
21 // use Launch Services and search that app's plist for the icon.
23 // Put some special juice in for the print command.
26 // for compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
36 #include "wx/string.h"
46 #include "wx/dynarray.h"
47 #include "wx/confbase.h"
49 #include "wx/mac/mimetype.h"
50 #include "wx/mac/private.h"
52 // other standard headers
56 #include <InternetConfig.h>
57 #include <CoreServices.h>
61 // START CODE SAMPLE FROM TECHNOTE 1002 (http://developer.apple.com/technotes/tn/tn1002.html)
63 // IsRemoteVolume can be used to find out if the
64 // volume referred to by vRefNum is a remote volume
65 // located somewhere on a network. the volume's attribute
66 // flags (copied from the GetVolParmsInfoBuffer structure)
67 // are returned in the longword pointed to by vMAttrib.
68 OSErr
IsRemoteVolume(short vRefNum
, Boolean
*isRemote
, long *vMAttrib
)
71 GetVolParmsInfoBuffer volinfo
;
74 volPB
.ioParam
.ioVRefNum
= vRefNum
;
75 volPB
.ioParam
.ioNamePtr
= NULL
;
76 volPB
.ioParam
.ioBuffer
= (Ptr
)&volinfo
;
77 volPB
.ioParam
.ioReqCount
= sizeof(volinfo
);
78 err
= PBHGetVolParmsSync( &volPB
);
81 *isRemote
= (volinfo
.vMServerAdr
!= 0);
82 *vMAttrib
= volinfo
.vMAttrib
;
88 // BuildVolumeList fills the array pointed to by vols with
89 // a list of the currently mounted volumes. If includeRemote
90 // is true, then remote server volumes will be included in
91 // the list. When remote server volumes are included in the
92 // list, they will be added to the end of the list. On entry,
93 // *count should contain the size of the array pointed to by
94 // vols. On exit, *count will be set to the number of id numbers
95 // placed in the array. If vMAttribMask is non-zero, then
96 // only volumes with matching attributes are added to the
97 // list of volumes. bits in the vMAttribMask should use the
98 // same encoding as bits in the vMAttrib field of
99 // the GetVolParmsInfoBuffer structure.
100 OSErr
BuildVolumeList(Boolean includeRemote
, short *vols
,
101 long *count
, long vMAttribMask
)
103 HParamBlockRec volPB
;
106 long nlocal
, nremote
;
109 // set up and check parameters
110 volPB
.volumeParam
.ioNamePtr
= NULL
;
111 nlocal
= nremote
= 0;
115 // iterate through volumes
116 for (volPB
.volumeParam
.ioVolIndex
= 1;
117 PBHGetVInfoSync(&volPB
) == noErr
;
118 volPB
.volumeParam
.ioVolIndex
++)
120 // skip remote volumes, if necessary
121 err
= IsRemoteVolume(volPB
.volumeParam
.ioVRefNum
, &isRemote
, &vMAttrib
);
125 if ((includeRemote
|| !isRemote
) && ((vMAttrib
& vMAttribMask
) == vMAttribMask
))
127 // add local volumes at the front; remote volumes at the end
129 vols
[nlocal
+ nremote
++] = volPB
.volumeParam
.ioVRefNum
;
136 nremote
* sizeof(short) );
137 vols
[nlocal
++] = volPB
.volumeParam
.ioVRefNum
;
141 if ((nlocal
+ nremote
) >= *count
)
147 *count
= (nlocal
+ nremote
);
153 // FindApplication iterates through mounted volumes
154 // searching for an application with the given creator
155 // type. If includeRemote is true, then remote volumes
156 // will be searched (after local ones) for an application
157 // with the creator type.
159 // Hacked to output to appName
163 OSErr
FindApplication(OSType appCreator
, Boolean includeRemote
, Str255 appName
, FSSpec
* appSpec
)
165 short rRefNums
[kMaxVols
];
170 // get a list of volumes - with desktop files
172 err
= BuildVolumeList(includeRemote
, rRefNums
, &volCount
, (1 << bHasDesktopMgr
) );
176 // iterate through the list
177 for (i
=0; i
<volCount
; i
++)
179 // has a desktop file?
180 desktopPB
.ioCompletion
= NULL
;
181 desktopPB
.ioVRefNum
= rRefNums
[i
];
182 desktopPB
.ioNamePtr
= NULL
;
183 desktopPB
.ioIndex
= 0;
184 err
= PBDTGetPath( &desktopPB
);
188 // has the correct app??
189 desktopPB
.ioFileCreator
= appCreator
;
190 desktopPB
.ioNamePtr
= appName
;
191 err
= PBDTGetAPPLSync( &desktopPB
);
195 // make a file spec referring to it
196 err
= FSMakeFSSpec( rRefNums
[i
], desktopPB
.ioAPPLParID
, appName
, appSpec
);
207 // END CODE SAMPLE FROM TECHNOTE 1002 (http://developer.apple.com/technotes/tn/tn1002.html)
209 // yeah, duplicated code
210 pascal OSErr
FSpGetFullPath( const FSSpec
*spec
,
211 short *fullPathLength
,
214 OSErr result
, realResult
;
222 realResult
= result
= noErr
;
224 // work around Nav Services "bug" (it returns invalid FSSpecs with empty names)
226 if ( spec
->name
[0] == 0 )
228 result
= FSMakeFSSpecCompat(spec
->vRefNum
, spec
->parID
, spec
->name
, &tempSpec
);
234 // Make a copy of the input FSSpec that can be modified
235 BlockMoveData( spec
, &tempSpec
, sizeof(FSSpec
) );
237 if ( result
== noErr
)
239 if ( tempSpec
.parID
== fsRtParID
)
241 // object is a volume
242 // Add a colon to make it a full pathname
244 tempSpec
.name
[tempSpec
.name
[0]] = ':';
247 result
= PtrToHand(&tempSpec
.name
[1], fullPath
, tempSpec
.name
[0]);
251 // object isn't a volume
253 // Is the object a file or a directory?
254 pb
.dirInfo
.ioNamePtr
= tempSpec
.name
;
255 pb
.dirInfo
.ioVRefNum
= tempSpec
.vRefNum
;
256 pb
.dirInfo
.ioDrDirID
= tempSpec
.parID
;
257 pb
.dirInfo
.ioFDirIndex
= 0;
258 result
= PBGetCatInfoSync( &pb
);
260 // Allow file/directory name at end of path to not exist.
262 if ((result
== noErr
) || (result
== fnfErr
))
264 // if the object is a directory, append a colon so full pathname ends with colon
265 if ((result
== noErr
) && (pb
.hFileInfo
.ioFlAttrib
& kioFlAttribDirMask
) != 0)
268 tempSpec
.name
[tempSpec
.name
[0]] = ':';
271 // Put the object name in first
272 result
= PtrToHand( &tempSpec
.name
[1], fullPath
, tempSpec
.name
[0] );
273 if ( result
== noErr
)
275 // Get the ancestor directory names
276 pb
.dirInfo
.ioNamePtr
= tempSpec
.name
;
277 pb
.dirInfo
.ioVRefNum
= tempSpec
.vRefNum
;
278 pb
.dirInfo
.ioDrParID
= tempSpec
.parID
;
280 // loop until we have an error or find the root directory
283 pb
.dirInfo
.ioFDirIndex
= -1;
284 pb
.dirInfo
.ioDrDirID
= pb
.dirInfo
.ioDrParID
;
285 result
= PBGetCatInfoSync(&pb
);
286 if ( result
== noErr
)
288 // Append colon to directory name
290 tempSpec
.name
[tempSpec
.name
[0]] = ':';
292 // Add directory name to beginning of fullPath
293 (void)Munger(*fullPath
, 0, NULL
, 0, &tempSpec
.name
[1], tempSpec
.name
[0]);
297 while ( (result
== noErr
) && (pb
.dirInfo
.ioDrDirID
!= fsRtDirID
) );
303 if ( result
== noErr
)
306 *fullPathLength
= GetHandleSize( *fullPath
);
307 result
= realResult
; // return realResult in case it was fnfErr
311 // Dispose of the handle and return NULL and zero length
312 if ( *fullPath
!= NULL
)
314 DisposeHandle( *fullPath
);
324 // On the mac there are two ways to open a file - one is through apple events and the
325 // finder, another is through mime types.
327 // So, really there are two ways to implement wxFileType...
329 // Mime types are only available on OS 8.1+ through the InternetConfig API
331 // Much like the old-style file manager, it has 3 levels of flexibility for its methods -
332 // Low - which means you have to iterate yourself through the mime database
333 // Medium - which lets you sort of cache the database if you want to use lowlevel functions
334 // High - which requires access to the database every time
336 // We want to be efficient (i.e. professional :) ) about it, so we use a combo of low
337 // and mid-level functions
339 // TODO: Should we call ICBegin/ICEnd? Then where?
343 inline void wxLogMimeDebug(const wxChar
* szMsg
, OSStatus status
)
345 wxLogDebug(wxString::Format(wxT("%s LINE:%i OSERROR:%i"), szMsg
, __LINE__
, (int)status
));
348 // in case we're compiling in non-GUI mode
349 class WXDLLEXPORT wxIcon
;
351 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
, const wxString
& verb
, bool overwriteprompt
)
353 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
358 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& strIcon
, int index
)
360 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
365 bool wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
366 const wxFileType::MessageParameters
& params
) const
368 wxString cmd
= GetCommand(wxT("open"));
370 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
372 return !openCmd
->empty();
376 wxFileTypeImpl::GetPrintCommand(
378 const wxFileType::MessageParameters
& params
) const
380 wxString cmd
= GetCommand(wxT("print"));
382 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
384 return !printCmd
->empty();
388 // Internet Config vs. Launch Services
390 // From OS 8 on there was internet config...
391 // However, OSX and its finder does not use info
392 // from Internet Config at all - the Internet Config
393 // database ONLY CONTAINS APPS THAT ARE CLASSIC APPS
394 // OR REGISTERED THROUGH INTERNET CONFIG
396 // Therefore on OSX in order for the open command to be useful
397 // we need to go straight to launch services
400 #if defined(__DARWIN__)
402 //on darwin, use launch services
403 #include <ApplicationServices/ApplicationServices.h>
405 wxString
wxFileTypeImpl::GetCommand(const wxString
& verb
) const
407 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
409 if (verb
== wxT("open"))
412 ICGetMapEntry( (ICInstance
) m_manager
->m_hIC
,
413 (Handle
) m_manager
->m_hDatabase
,
416 wxString sCurrentExtension
= wxMacMakeStringFromPascal(entry
.extension
);
417 sCurrentExtension
= sCurrentExtension
.Right(sCurrentExtension
.length()-1 );
419 //type, creator, ext, roles, outapp (FSRef), outappurl
420 CFURLRef cfurlAppPath
;
421 OSStatus status
= LSGetApplicationForInfo( kLSUnknownType
,
423 wxMacCFStringHolder(sCurrentExtension
, wxLocale::GetSystemEncoding()),
430 CFStringRef cfsUnixPath
= CFURLCopyFileSystemPath(cfurlAppPath
, kCFURLPOSIXPathStyle
);
431 CFRelease(cfurlAppPath
);
434 // Since a filename might have spaces in it, so surround it with quotes
441 + wxMacCFStringHolder(cfsUnixPath
).AsString(wxLocale::GetSystemEncoding())
442 + wxString(wxT("'"));
449 wxLogDebug(wxString::Format(wxT("%i - %s - %i"),
451 wxT("LSGetApplicationForInfo failed."),
456 return wxEmptyString
;
459 #else //carbon/classic implementation
461 wxString
wxFileTypeImpl::GetCommand(const wxString
& verb
) const
463 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
465 if (verb
== wxT("open"))
468 ICGetMapEntry( (ICInstance
) m_manager
->m_hIC
,
469 (Handle
) m_manager
->m_hDatabase
,
472 //The entry in the mimetype database only contains the app
473 //that's registered - it may not exist... we need to remap the creator
474 //type and find the right application
476 // THIS IS REALLY COMPLICATED :\.
477 // There are a lot of conversions going on here.
480 OSErr err
= FindApplication( entry
.fileCreator
, false, outName
, &outSpec
);
482 return wxEmptyString
;
484 Handle outPathHandle
;
486 err
= FSpGetFullPath( &outSpec
, &outPathSize
, &outPathHandle
);
489 char* szPath
= *outPathHandle
;
490 wxString
sClassicPath(szPath
, wxConvLocal
, outPathSize
);
492 #if defined(__DARWIN__)
493 // Classic Path --> Unix (OSX) Path
494 CFURLRef finalURL
= CFURLCreateWithFileSystemPath(
496 wxMacCFStringHolder(sClassicPath
, wxLocale::GetSystemEncoding()),
498 false ); //false == not a directory
500 //clean up memory from the classic path handle
501 DisposeHandle( outPathHandle
);
505 CFStringRef cfsUnixPath
= CFURLCopyFileSystemPath(finalURL
, kCFURLPOSIXPathStyle
);
510 return wxMacCFStringHolder(cfsUnixPath
).AsString(wxLocale::GetSystemEncoding());
512 #else //classic HFS path acceptable
518 wxLogMimeDebug(wxT("FSpGetFullPath failed."), (OSStatus
)err
);
522 return wxEmptyString
;
526 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
528 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
531 ICGetMapEntry( (ICInstance
) m_manager
->m_hIC
,
532 (Handle
) m_manager
->m_hDatabase
, m_lIndex
, &entry
);
534 *desc
= wxMacMakeStringFromPascal( entry
.entryName
);
539 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
541 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
544 ICGetMapEntry( (ICInstance
) m_manager
->m_hIC
,
545 (Handle
) m_manager
->m_hDatabase
, m_lIndex
, &entry
);
547 //entry has period in it
548 wxString sCurrentExtension
= wxMacMakeStringFromPascal( entry
.extension
);
549 extensions
.Add( sCurrentExtension
.Right( sCurrentExtension
.length() - 1 ) );
554 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
556 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
559 ICGetMapEntry( (ICInstance
) m_manager
->m_hIC
,
560 (Handle
) m_manager
->m_hDatabase
, m_lIndex
, &entry
);
562 *mimeType
= wxMacMakeStringFromPascal(entry
.MIMEType
);
567 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
582 bool wxFileTypeImpl::GetIcon(wxIconLocation
*WXUNUSED(icon
)) const
584 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
586 // no such file type or no value or incorrect icon entry
590 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
* verbs
,
591 wxArrayString
* commands
,
592 const wxFileType::MessageParameters
& params
) const
594 wxASSERT_MSG( m_manager
!= NULL
, wxT("Bad wxFileType") );
599 if (GetOpenCommand(&sCommand
, params
))
601 verbs
->Add(wxString(wxT("open")));
602 commands
->Add(sCommand
);
609 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles
, const wxString
& extraDir
)
611 wxASSERT_MSG(m_hIC
== NULL
, wxT("Already initialized wxMimeTypesManager!"));
613 // some apps (non-wx) use the 'plst' resource instead
615 CFBundleRef cfbMain
= CFBundleGetMainBundle();
616 wxCFDictionary
cfdInfo( CFBundleGetInfoDictionary(cfbMain
), wxCF_RETAIN
);
618 cfdInfo
.PrintOut(sLog
);
622 // start Internet Config - log if there's an error
623 // the second param is the signature of the application, also known
624 // as resource ID 0. However, as per some recent discussions, we may not
625 // have a signature for this app, so a generic 'APPL' which is the executable
626 // type will work for now.
627 OSStatus status
= ICStart( (ICInstance
*)&m_hIC
, 'APPL' );
631 wxLogDebug(wxT("Could not initialize wxMimeTypesManager!"));
639 m_hDatabase
= (void**) NewHandle(0);
640 status
= ICFindPrefHandle( (ICInstance
) m_hIC
, kICMapping
, &attr
, (Handle
) m_hDatabase
);
642 //the database file can be corrupt (on OSX its
643 //~/Library/Preferences/com.apple.internetconfig.plist)
648 wxLogDebug(wxT("Corrupt MIME database!"));
652 //obtain the number of entries in the map
653 status
= ICCountMapEntries( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, &m_lCount
);
654 wxASSERT( status
== noErr
);
661 for (long i
= 1; i
<= m_lCount
; ++i
)
663 OSStatus status
= ICGetIndMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, i
, &pos
, &entry
);
667 wxString sCreator
= wxMacMakeStringFromPascal(entry
.creatorAppName
);
668 wxString sCurrentExtension
= wxMacMakeStringFromPascal(entry
.extension
);
669 wxString sMIMEType
= wxMacMakeStringFromPascal(entry
.MIMEType
);
672 impl
.Init(this, pos
);
674 if (sMIMEType
== wxT("text/html") && sCurrentExtension
== wxT(".html"))
678 impl
.GetOpenCommand( &cmd
, wxFileType::MessageParameters (wxT("http://www.google.com")));
679 wxPrintf(wxT("APP: [%s]\n"), cmd
.c_str());
686 void wxMimeTypesManagerImpl::ClearData()
690 DisposeHandle( (Handle
)m_hDatabase
);
692 // this can return an error, but we don't really care that much about it
693 ICStop( (ICInstance
)m_hIC
);
699 // Q) Iterating through the map - why does it use if (err == noErr) instead of just asserting?
700 // A) Some intermediate indexes are bad while subsequent ones may be good. Its wierd, I know.
703 // extension -> file type
704 wxFileType
* wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& e
)
706 wxASSERT_MSG( m_hIC
!= NULL
, wxT("wxMimeTypesManager not Initialized!") );
708 //low level functions - iterate through the database
712 for (long i
= 1; i
<= m_lCount
; ++i
)
714 OSStatus status
= ICGetIndMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, i
, &pos
, &entry
);
718 wxString sCurrentExtension
= wxMacMakeStringFromPascal(entry
.extension
);
719 if ( sCurrentExtension
.Right(sCurrentExtension
.length() - 1) == e
) // entry has period in it
721 wxFileType
* pFileType
= new wxFileType();
722 pFileType
->m_impl
->Init((wxMimeTypesManagerImpl
*)this, pos
);
732 // MIME type -> extension -> file type
733 wxFileType
* wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
735 wxASSERT_MSG( m_hIC
!= NULL
, wxT("wxMimeTypesManager not Initialized!") );
740 // low level functions - iterate through the database
741 for (long i
= 1; i
<= m_lCount
; ++i
)
743 OSStatus status
= ICGetIndMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, i
, &pos
, &entry
);
744 wxASSERT_MSG( status
== noErr
, wxString::Format(wxT("Error: %d"), (int)status
) );
748 if ( wxMacMakeStringFromPascal(entry
.MIMEType
) == mimeType
)
750 wxFileType
* pFileType
= new wxFileType();
751 pFileType
->m_impl
->Init((wxMimeTypesManagerImpl
*)this, pos
);
761 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
763 wxASSERT_MSG( m_hIC
!= NULL
, wxT("wxMimeTypesManager not Initialized!") );
766 long pos
, lStartCount
;
768 // low level functions - iterate through the database
769 lStartCount
= (long) mimetypes
.GetCount();
770 for (long i
= 1; i
<= m_lCount
; ++i
)
772 OSStatus status
= ICGetIndMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, i
, &pos
, &entry
);
773 if ( status
== noErr
)
774 mimetypes
.Add( wxMacMakeStringFromPascal(entry
.MIMEType
) );
777 return mimetypes
.GetCount() - lStartCount
;
780 pascal OSStatus
MoreProcGetProcessTypeSignature(
781 const ProcessSerialNumberPtr pPSN
,
782 OSType
*pProcessType
,
785 OSStatus anErr
= noErr
;
786 ProcessInfoRec infoRec
;
787 ProcessSerialNumber localPSN
;
789 infoRec
.processInfoLength
= sizeof(ProcessInfoRec
);
790 infoRec
.processName
= NULL
;
791 infoRec
.processAppSpec
= NULL
;
795 localPSN
.highLongOfPSN
= 0;
796 localPSN
.lowLongOfPSN
= kCurrentProcess
;
803 anErr
= GetProcessInformation(&localPSN
, &infoRec
);
806 *pProcessType
= infoRec
.processType
;
807 *pCreator
= infoRec
.processSignature
;
815 // TODO: clean this up, its messy
820 #include "wx/mac/corefoundation/cfstring.h"
821 #include "wx/intl.h" //wxLocale for wxCFString
823 #define wxCF_RELEASE true
824 #define wxCF_RETAIN false
826 // ----------------------------------------------------------------------------
828 // ----------------------------------------------------------------------------
833 wxCFDictionary(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
)
835 m_cfmdRef
= (CFMutableDictionaryRef
) ref
;
836 if (bRetain
== wxCF_RETAIN
&& ref
)
840 wxCFDictionary(CFIndex cfiSize
= 0)
842 CFDictionaryKeyCallBacks kcbs
;
843 CFDictionaryValueCallBacks vcbs
;
844 BuildKeyCallbacks(&kcbs
);
845 BuildValueCallbacks(&vcbs
);
847 m_cfmdRef
= CFDictionaryCreateMutable(
848 kCFAllocatorDefault
, cfiSize
, &kcbs
, &vcbs
);
857 CFRelease(m_cfmdRef
);
860 static const void* RetainProc(CFAllocatorRef
, const void* v
)
861 { return (const void*) CFRetain(v
); }
863 static void ReleaseProc(CFAllocatorRef
, const void* v
)
866 void MakeMutable(CFIndex cfiSize
= 0)
868 CFDictionaryRef oldref
= (CFDictionaryRef
) m_cfmdRef
;
870 m_cfmdRef
= CFDictionaryCreateMutableCopy(
871 kCFAllocatorDefault
, cfiSize
, oldref
);
876 void BuildKeyCallbacks(CFDictionaryKeyCallBacks
* pCbs
)
879 pCbs
->retain
= RetainProc
;
880 pCbs
->release
= ReleaseProc
;
881 pCbs
->copyDescription
= NULL
;
886 void BuildValueCallbacks(CFDictionaryValueCallBacks
* pCbs
)
889 pCbs
->retain
= RetainProc
;
890 pCbs
->release
= ReleaseProc
;
891 pCbs
->copyDescription
= NULL
;
895 operator CFTypeRef () const
896 { return (CFTypeRef
)m_cfmdRef
; }
898 CFDictionaryRef
GetCFDictionary() const
899 { return (CFDictionaryRef
)m_cfmdRef
; }
901 CFMutableDictionaryRef
GetCFMutableDictionary()
902 { return (CFMutableDictionaryRef
) m_cfmdRef
; }
904 CFTypeRef
operator [] (CFTypeRef cftEntry
) const
907 return (CFTypeRef
) CFDictionaryGetValue((CFDictionaryRef
)m_cfmdRef
, cftEntry
);
910 CFIndex
GetCount() const
913 return CFDictionaryGetCount((CFDictionaryRef
)m_cfmdRef
);
916 void Add(CFTypeRef cftKey
, CFTypeRef cftValue
)
919 wxASSERT(Exists(cftKey
) == false);
920 CFDictionaryAddValue(m_cfmdRef
, cftKey
, cftValue
);
923 void Remove(CFTypeRef cftKey
)
926 wxASSERT(Exists(cftKey
));
927 CFDictionaryRemoveValue(m_cfmdRef
, cftKey
);
930 void Set(CFTypeRef cftKey
, CFTypeRef cftValue
)
933 wxASSERT(Exists(cftKey
));
934 CFDictionarySetValue(m_cfmdRef
, cftKey
, cftValue
);
937 bool Exists(CFTypeRef cftKey
) const
940 return CFDictionaryContainsKey((CFDictionaryRef
)m_cfmdRef
, cftKey
);
944 { return m_cfmdRef
!= NULL
; }
947 { return IsOk() && CFGetTypeID((CFTypeRef
)m_cfmdRef
) == CFDictionaryGetTypeID(); }
949 void PrintOut(wxString
& sMessage
)
951 PrintOutDictionary(sMessage
, m_cfmdRef
);
954 static void PrintOutDictionary(wxString
& sMessage
, CFDictionaryRef cfdRef
)
956 CFIndex cfiCount
= CFDictionaryGetCount(cfdRef
);
957 CFTypeRef
* pKeys
= new CFTypeRef
[cfiCount
];
958 CFTypeRef
* pValues
= new CFTypeRef
[cfiCount
];
960 CFDictionaryGetKeysAndValues(cfdRef
, pKeys
, pValues
);
962 for (CFIndex i
= 0; i
< cfiCount
; ++i
)
964 wxString sKey
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pKeys
[i
]))).AsString();
965 wxString sValue
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pValues
[i
]))).AsString();
968 wxString::Format(wxT("[{#%d} Key : %s]"), (int) i
,
971 PrintOutType(sMessage
, sKey
, pKeys
[i
]);
974 wxString::Format(wxT("\n\t[Value : %s]"),
977 PrintOutType(sMessage
, sValue
, pValues
[i
]);
979 sMessage
<< wxT("\n");
986 static void PrintOutArray(wxString
& sMessage
, CFArrayRef cfaRef
)
988 for (CFIndex i
= 0; i
< CFArrayGetCount(cfaRef
); ++i
)
990 wxString sValue
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(
991 CFArrayGetValueAtIndex(cfaRef
, i
)
995 wxString::Format(wxT("\t\t[{#%d} ArrayValue : %s]\n"), (int) i
,
998 PrintOutType(sMessage
, sValue
, CFArrayGetValueAtIndex(cfaRef
, i
));
1002 static void PrintOutType(wxString
& sMessage
, const wxString
& sValue
, CFTypeRef cfRef
)
1004 sMessage
<< wxT(" {");
1006 if (sValue
== wxT("CFString"))
1008 sMessage
<< wxMacCFStringHolder((CFStringRef
)cfRef
, false).AsString();
1010 else if (sValue
== wxT("CFNumber"))
1013 CFNumberGetValue((CFNumberRef
)cfRef
, kCFNumberIntType
, &nOut
);
1016 else if (sValue
== wxT("CFDictionary"))
1018 PrintOutDictionary(sMessage
, (CFDictionaryRef
)cfRef
);
1020 else if (sValue
== wxT("CFArray"))
1022 PrintOutArray(sMessage
, (CFArrayRef
)cfRef
);
1024 else if (sValue
== wxT("CFBoolean"))
1026 sMessage
<< (cfRef
== kCFBooleanTrue
? wxT("true") : wxT("false"));
1028 else if (sValue
== wxT("CFURL"))
1030 sMessage
<< wxMacCFStringHolder(CFURLCopyPath((CFURLRef
) cfRef
)).AsString();
1034 sMessage
<< wxT("*****UNKNOWN TYPE******");
1037 sMessage
<< wxT("} ");
1041 void MakeValidXML();
1044 CFTypeRef
WriteAsXML()
1046 return CFPropertyListCreateXMLData(kCFAllocatorDefault
, m_cfmdRef
);
1049 bool ReadAsXML(CFTypeRef cfData
, wxString
* pErrorMsg
= NULL
)
1052 CFStringRef cfsError
=NULL
;
1053 m_cfmdRef
= (CFMutableDictionaryRef
) CFPropertyListCreateFromXMLData(
1054 kCFAllocatorDefault
,
1056 kCFPropertyListMutableContainersAndLeaves
,
1061 *pErrorMsg
= wxMacCFStringHolder(cfsError
).AsString();
1063 CFRelease(cfsError
);
1066 return m_cfmdRef
!= NULL
;
1070 CFMutableDictionaryRef m_cfmdRef
;
1073 // ----------------------------------------------------------------------------
1075 // ----------------------------------------------------------------------------
1080 wxCFArray(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
)
1082 m_cfmaRef
= (CFMutableArrayRef
)ref
;
1083 if (bRetain
== wxCF_RETAIN
&& ref
)
1087 wxCFArray(CFIndex cfiSize
= 0) : m_cfmaRef(NULL
)
1088 { Create(cfiSize
); }
1093 void MakeMutable(CFIndex cfiSize
= 0)
1095 wxASSERT(IsValid());
1097 CFMutableArrayRef oldref
= m_cfmaRef
;
1098 m_cfmaRef
= CFArrayCreateMutableCopy(
1099 kCFAllocatorDefault
,
1101 (CFArrayRef
)oldref
);
1105 void BuildCallbacks(CFArrayCallBacks
* pCbs
)
1108 pCbs
->retain
= RetainProc
;
1109 pCbs
->release
= ReleaseProc
;
1110 pCbs
->copyDescription
= NULL
;
1114 void Create(CFIndex cfiSize
= 0)
1117 CFArrayCallBacks cb
;
1118 BuildCallbacks(&cb
);
1120 m_cfmaRef
= CFArrayCreateMutable(kCFAllocatorDefault
, cfiSize
, &cb
);
1124 { if (m_cfmaRef
) CFRelease(m_cfmaRef
); }
1126 static const void* RetainProc(CFAllocatorRef
, const void* v
)
1127 { return (const void*) CFRetain(v
); }
1129 static void ReleaseProc(CFAllocatorRef
, const void* v
)
1132 operator CFTypeRef () const
1133 { return (CFTypeRef
)m_cfmaRef
; }
1135 CFArrayRef
GetCFArray() const
1136 { return (CFArrayRef
)m_cfmaRef
; }
1138 CFMutableArrayRef
GetCFMutableArray()
1139 { return (CFMutableArrayRef
) m_cfmaRef
; }
1141 CFTypeRef
operator [] (CFIndex cfiIndex
) const
1143 wxASSERT(IsValid());
1144 return (CFTypeRef
) CFArrayGetValueAtIndex((CFArrayRef
)m_cfmaRef
, cfiIndex
);
1149 wxASSERT(IsValid());
1150 return CFArrayGetCount((CFArrayRef
)m_cfmaRef
);
1153 void Add(CFTypeRef cftValue
)
1155 wxASSERT(IsValid());
1156 CFArrayAppendValue(m_cfmaRef
, cftValue
);
1159 void Remove(CFIndex cfiIndex
)
1161 wxASSERT(IsValid());
1162 wxASSERT(cfiIndex
< GetCount());
1163 CFArrayRemoveValueAtIndex(m_cfmaRef
, cfiIndex
);
1166 void Set(CFIndex cfiIndex
, CFTypeRef cftValue
)
1168 wxASSERT(IsValid());
1169 wxASSERT(cfiIndex
< GetCount());
1170 CFArraySetValueAtIndex(m_cfmaRef
, cfiIndex
, cftValue
);
1174 { return m_cfmaRef
!= NULL
; }
1176 bool IsValid() const
1178 return IsOk() && CFGetTypeID((CFTypeRef
)m_cfmaRef
) == CFArrayGetTypeID();
1182 void MakeValidXML();
1186 CFMutableArrayRef m_cfmaRef
;
1189 // ----------------------------------------------------------------------------
1191 // ----------------------------------------------------------------------------
1196 wxCFString(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_Holder((CFStringRef
)ref
, bRetain
== wxCF_RELEASE
)
1199 wxCFString(const wxChar
* szString
) : m_Holder(wxString(szString
), wxLocale::GetSystemEncoding())
1202 wxCFString(const wxString
& sString
) : m_Holder(sString
, wxLocale::GetSystemEncoding())
1205 virtual ~wxCFString() {}
1207 operator CFTypeRef() const
1208 { return (CFTypeRef
) ((CFStringRef
) m_Holder
); }
1211 { return ((CFTypeRef
)(*this)) != NULL
; }
1213 wxString
BuildWXString()
1214 { return m_Holder
.AsString(); }
1217 wxMacCFStringHolder m_Holder
;
1220 // ----------------------------------------------------------------------------
1222 // ----------------------------------------------------------------------------
1227 wxCFNumber(int nValue
)
1229 m_cfnRef
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &nValue
);
1232 wxCFNumber(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_cfnRef((CFNumberRef
)ref
)
1234 if (bRetain
== wxCF_RETAIN
&& ref
)
1238 virtual ~wxCFNumber()
1241 CFRelease(m_cfnRef
);
1244 operator CFTypeRef() const
1245 { return (CFTypeRef
) m_cfnRef
; }
1250 CFNumberGetValue( m_cfnRef
, kCFNumberIntType
, &nOut
);
1256 { return m_cfnRef
!= NULL
; }
1259 CFNumberRef m_cfnRef
;
1262 // ----------------------------------------------------------------------------
1264 // ----------------------------------------------------------------------------
1269 wxCFURL(CFTypeRef ref
= NULL
, bool bRetain
= wxCF_RELEASE
) : m_cfurlRef((CFURLRef
)ref
)
1271 if (bRetain
== wxCF_RETAIN
&& ref
)
1275 wxCFURL(const wxCFString
& URLString
, CFTypeRef BaseURL
= NULL
)
1277 Create(URLString
, BaseURL
);
1280 void Create(const wxCFString
& URLString
, CFTypeRef BaseURL
= NULL
)
1282 m_cfurlRef
= CFURLCreateWithString(
1283 kCFAllocatorDefault
,
1284 (CFStringRef
)(CFTypeRef
)URLString
,
1285 (CFURLRef
) BaseURL
);
1291 CFRelease(m_cfurlRef
);
1294 wxString
BuildWXString()
1296 return wxCFString(CFURLCopyPath(m_cfurlRef
)).BuildWXString();
1299 operator CFTypeRef() const
1300 { return (CFTypeRef
)m_cfurlRef
; }
1303 { return m_cfurlRef
!= NULL
; }
1306 CFURLRef m_cfurlRef
;
1309 // ----------------------------------------------------------------------------
1311 // ----------------------------------------------------------------------------
1313 #define wxCFDATA_RELEASEBUFFER 1
1314 #define wxCFDATA_RETAINBUFFER 0
1319 wxCFData(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_cfdaRef((CFDataRef
)ref
)
1321 if (bRetain
== wxCF_RETAIN
&& ref
)
1325 wxCFData(const UInt8
* pBytes
, CFIndex len
, bool bKeep
= wxCFDATA_RELEASEBUFFER
)
1327 if (bKeep
== wxCFDATA_RELEASEBUFFER
)
1329 m_cfdaRef
= CFDataCreateWithBytesNoCopy
1330 (kCFAllocatorDefault
, pBytes
, len
, kCFAllocatorDefault
);
1334 m_cfdaRef
= CFDataCreate(kCFAllocatorDefault
, pBytes
, len
);
1341 CFRelease(m_cfdaRef
);
1344 const UInt8
* GetValue()
1345 { return CFDataGetBytePtr(m_cfdaRef
); }
1348 { return CFDataGetLength(m_cfdaRef
); }
1350 operator CFTypeRef() const
1351 { return (CFTypeRef
)m_cfdaRef
; }
1354 { return m_cfdaRef
!= NULL
; }
1357 CFDataRef m_cfdaRef
;
1360 void wxCFDictionary::MakeValidXML()
1362 CFIndex cfiCount
= GetCount();
1363 CFTypeRef
* pKeys
= new CFTypeRef
[cfiCount
];
1364 CFTypeRef
* pValues
= new CFTypeRef
[cfiCount
];
1366 CFDictionaryGetKeysAndValues(m_cfmdRef
, pKeys
, pValues
);
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
)
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
];
1377 CFGetTypeID(pKeys
[i
]) != CFStringGetTypeID() ||
1385 pKeys
= new CFTypeRef
[cfiCount
];
1386 pValues
= new CFTypeRef
[cfiCount
];
1387 CFDictionaryGetKeysAndValues(m_cfmdRef
, pKeys
, pValues
);
1389 else if (CFGetTypeID(cfRef
) == CFArrayGetTypeID())
1392 wxCFArray
cfaCurrent(cfRef
);
1393 cfaCurrent
.MakeMutable();
1394 cfaCurrent
.MakeValidXML();
1395 Set(pKeys
[i
], cfaCurrent
);
1397 else if (CFGetTypeID(cfRef
) == CFDictionaryGetTypeID())
1400 wxCFDictionary
cfdCurrent(cfRef
);
1401 cfdCurrent
.MakeMutable();
1402 cfdCurrent
.MakeValidXML();
1403 Set(pKeys
[i
], cfdCurrent
);
1405 else if ( CFGetTypeID(cfRef
) != CFStringGetTypeID() &&
1406 CFGetTypeID(cfRef
) != CFNumberGetTypeID() &&
1407 CFGetTypeID(cfRef
) != CFBooleanGetTypeID() )
1414 pKeys
= new CFTypeRef
[cfiCount
];
1415 pValues
= new CFTypeRef
[cfiCount
];
1416 CFDictionaryGetKeysAndValues(m_cfmdRef
, pKeys
, pValues
);
1424 void wxCFArray::MakeValidXML()
1426 for (CFIndex i
= 0; i
< GetCount(); ++i
)
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
];
1436 else if (CFGetTypeID(cfRef
) == CFArrayGetTypeID())
1439 wxCFArray
cfaCurrent(cfRef
);
1440 cfaCurrent
.MakeMutable();
1441 cfaCurrent
.MakeValidXML();
1444 else if (CFGetTypeID(cfRef
) == CFDictionaryGetTypeID())
1447 wxCFDictionary
cfdCurrent(cfRef
);
1448 cfdCurrent
.MakeMutable();
1449 cfdCurrent
.MakeValidXML();
1452 else if ( CFGetTypeID(cfRef
) != CFStringGetTypeID() &&
1453 CFGetTypeID(cfRef
) != CFNumberGetTypeID() &&
1454 CFGetTypeID(cfRef
) != CFBooleanGetTypeID() )
1470 wxFileType
* wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
1472 bool bInfoSuccess
= false;
1474 const wxArrayString
& asExtensions
= ftInfo
.GetExtensions();
1475 size_t dwFoundIndex
= 0;
1476 if (!asExtensions
.GetCount())
1478 wxLogDebug(wxT("Must have extension to associate with"));
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();
1487 UInt32 dwBundleType
, dwBundleCreator
;
1488 CFBundleGetPackageInfo(cfbMain
, &dwBundleType
, &dwBundleCreator
);
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')
1495 wxCFURL
cfurlBundleLoc((CFTypeRef
)CFBundleCopyBundleURL(cfbMain
));
1496 // wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyExecutableURL(cfbMain));
1498 // sInfoPath << wxT("file://");
1499 sInfoPath
<< cfurlBundleLoc
.BuildWXString();
1500 sInfoPath
<< wxT("Contents/Info.plist");
1502 // wxCFDictionary cfdInfo( CFBundleGetInfoDictionary(cfbMain), wxCF_RETAIN );
1503 wxCFDictionary cfdInfo
;
1504 bool bInfoOpenSuccess
= false;
1506 if (indictfile
.Open(sInfoPath
, wxFile::read
))
1508 CFIndex cfiBufLen
= (CFIndex
) indictfile
.Length();
1509 const UInt8
* pBuffer
= new UInt8
[cfiBufLen
];
1510 indictfile
.Read((void*)pBuffer
, cfiBufLen
);
1511 wxCFData
cfdaInDict(pBuffer
, cfiBufLen
);
1513 bInfoOpenSuccess
= cfdInfo
.ReadAsXML(cfdaInDict
, &sError
);
1514 if (!bInfoOpenSuccess
)
1519 if (bInfoOpenSuccess
)
1521 cfdInfo
.MakeMutable( cfdInfo
.GetCount() + 1 );
1523 wxCFArray
cfaDocTypes( cfdInfo
[ wxCFString(wxT("CFBundleDocumentTypes")) ], wxCF_RETAIN
);
1525 bool bAddDocTypesArrayToDictionary
= !cfaDocTypes
.IsOk();
1526 if (bAddDocTypesArrayToDictionary
)
1527 cfaDocTypes
.Create();
1529 cfaDocTypes
.MakeMutable( cfaDocTypes
.GetCount() + 1 );
1531 bool bEntryFound
= false;
1533 // search for duplicates
1535 for (i
= 0; i
< cfaDocTypes
.GetCount(); ++i
)
1537 wxCFDictionary
cfdDocTypeEntry( cfaDocTypes
[i
], wxCF_RETAIN
);
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")) ],
1544 if (!cfaExtensions
.IsOk())
1547 for (CFIndex iExt
= 0; iExt
< cfaExtensions
.GetCount(); ++iExt
)
1549 for (size_t iWXExt
= 0; iWXExt
< asExtensions
.GetCount(); ++iWXExt
)
1551 if (asExtensions
[iWXExt
] ==
1552 wxCFString(cfaExtensions
[iExt
], wxCF_RETAIN
).BuildWXString())
1555 dwFoundIndex
= iWXExt
;
1559 } //end of wxstring array
1563 } //end for cf array
1567 } //end for doctypes
1569 wxCFDictionary cfdNewEntry
;
1571 if (!ftInfo
.GetDescription().empty())
1573 cfdNewEntry
.Add( wxCFString(wxT("CFBundleTypeName")),
1574 wxCFString(ftInfo
.GetDescription()) );
1577 if (!ftInfo
.GetIconFile().empty())
1579 cfdNewEntry
.Add( wxCFString(wxT("CFBundleTypeIconFile")),
1580 wxCFString(ftInfo
.GetIconFile()) );
1583 wxCFArray cfaOSTypes
;
1584 wxCFArray cfaExtensions
;
1585 wxCFArray cfaMimeTypes
;
1587 //OSTypes is a cfarray of four-char-codes - '****' for unrestricted
1588 cfaOSTypes
.Add( wxCFString(wxT("****")) );
1589 cfdNewEntry
.Add( wxCFString(wxT("CFBundleTypeOSTypes")), cfaOSTypes
);
1591 //'*' for unrestricted
1592 if (ftInfo
.GetExtensionsCount() != 0)
1594 for (size_t iExtension
= 0; iExtension
< ftInfo
.GetExtensionsCount(); ++iExtension
)
1596 cfaExtensions
.Add( wxCFString( asExtensions
[iExtension
] ) );
1599 cfdNewEntry
.Add( wxCFString(wxT("CFBundleTypeExtensions")), cfaExtensions
);
1602 if (!ftInfo
.GetMimeType().empty())
1604 cfaMimeTypes
.Add( wxCFString(ftInfo
.GetMimeType()) );
1605 cfdNewEntry
.Add( wxCFString(wxT("CFBundleTypeMIMETypes")), cfaMimeTypes
);
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")) );
1613 // Is application bundled?
1614 cfdNewEntry
.Add( wxCFString(wxT("LSTypeIsPackage")), kCFBooleanTrue
);
1617 cfaDocTypes
.Set(i
, cfdNewEntry
);
1619 cfaDocTypes
.Add(cfdNewEntry
);
1621 // set the doc types array in the muted dictionary
1622 if (bAddDocTypesArrayToDictionary
)
1623 cfdInfo
.Add(wxCFString(wxT("CFBundleDocumentTypes")), cfaDocTypes
);
1625 cfdInfo
.Set(wxCFString(wxT("CFBundleDocumentTypes")), cfaDocTypes
);
1627 cfdInfo
.MakeValidXML();
1630 if (outdictfile
.Open(sInfoPath
, wxFile::write
))
1632 wxCFData
cfdaInfo(cfdInfo
.WriteAsXML());
1633 if (cfdaInfo
.IsOk())
1635 if (outdictfile
.Write(cfdaInfo
.GetValue(), cfdaInfo
.GetCount()) !=
1636 (wxFileOffset
)cfdaInfo
.GetCount())
1638 wxLogDebug(wxT("error in writing to file"));
1642 bInfoSuccess
= true;
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)
1649 // wxLogDebug(wxT("LSRegisterURL Failed."));
1653 outdictfile
.Close();
1657 outdictfile
.Close();
1658 wxLogDebug(wxT("Could not read in new dictionary"));
1663 wxLogDebug(wxString(wxT("Could not open [")) +
1664 sInfoPath
+ wxT("] for writing."));
1669 wxLogDebug(wxT("No info dictionary in main bundle"));
1674 wxLogDebug(wxT("Can only call associate from bundled app within XXX.app"));
1679 wxLogDebug(wxT("No main bundle"));
1682 #if defined(__DARWIN__)
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...
1691 OSType processType
, creator
;
1692 OSStatus status
= MoreProcGetProcessTypeSignature(NULL
, &processType
, &creator
);
1694 if (status
== noErr
)
1696 Str255 psCreatorName
;
1698 status
= FindApplication(creator
, false, psCreatorName
, &dummySpec
);
1700 if (status
== noErr
)
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
]);
1709 ICGetMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
,
1710 pFileType
->m_impl
->m_lIndex
, &entry
);
1712 memcpy(entry
.creatorAppName
, psCreatorName
, sizeof(Str255
));
1713 entry
.fileCreator
= creator
;
1715 status
= ICSetMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
,
1716 pFileType
->m_impl
->m_lIndex
, &entry
);
1719 if (status
== noErr
)
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;
1731 // wxLogDebug(wxString::Format(wxT("%i - %s"), (int)status, wxT("ICSetPrefHandle failed.")));
1736 wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__
, wxT("ICSetMapEntry failed.")));
1739 // failure - cleanup
1744 // TODO: Maybe force all 3 of these to be non-empty?
1745 Str255 psExtension
, psMimeType
, psDescription
;
1747 wxMacStringToPascal(wxString(wxT(".")) + ftInfo
.GetExtensions()[0], psExtension
);
1748 wxMacStringToPascal(ftInfo
.GetMimeType(), psMimeType
);
1749 wxMacStringToPascal(ftInfo
.GetDescription(), psDescription
);
1751 Str255 psPostCreatorName
;
1752 wxMacStringToPascal(wxEmptyString
, psPostCreatorName
);
1754 //add the entry to the database
1756 entry
.totalLength
= sizeof(ICMapEntry
);
1757 entry
.fixedLength
= kICMapFixedLength
;
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
));
1769 status
= ICAddMapEntry( (ICInstance
) m_hIC
, (Handle
) m_hDatabase
, &entry
);
1770 if (status
== noErr
)
1772 return GetFileTypeFromExtension(ftInfo
.GetMimeType());
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);
1779 // return the entry in the database if successful
1780 // if (status == noErr)
1781 // return GetFileTypeFromExtension(ftInfo.GetMimeType());
1784 // wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICSetPrefHandle failed.")));
1789 wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__
, wxT("ICAppMapEntry failed.")));
1792 } // end if FindApplcation was successful
1795 wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__
, wxT("FindApplication failed.")));
1797 } // end if it could obtain app's signature
1800 wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__
, wxT("GetProcessSignature failed.")));
1807 wxMimeTypesManagerImpl::Unassociate(wxFileType
*pFileType
)
1809 wxASSERT(pFileType
);
1810 bool bInfoSuccess
= false;
1812 wxArrayString asExtensions
;
1813 pFileType
->GetExtensions(asExtensions
);
1815 if (!asExtensions
.GetCount())
1817 wxLogDebug(wxT("Must have extension to disassociate"));
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();
1827 UInt32 dwBundleType
, dwBundleCreator
;
1828 CFBundleGetPackageInfo(cfbMain
, &dwBundleType
, &dwBundleCreator
);
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')
1836 wxCFURL
cfurlBundleLoc((CFTypeRef
)CFBundleCopyBundleURL(cfbMain
));
1837 // wxCFURL cfurlBundleLoc((CFTypeRef)CFBundleCopyExecutableURL(cfbMain));
1839 // sInfoPath << wxT("file://");
1840 sInfoPath
<< cfurlBundleLoc
.BuildWXString();
1841 sInfoPath
<< wxT("Contents/Info.plist");
1843 // wxCFDictionary cfdInfo( (CFTypeRef) CFBundleGetInfoDictionary(cfbMain), wxCF_RETAIN );
1844 wxCFDictionary cfdInfo
;
1845 bool bInfoOpenSuccess
= false;
1847 if (indictfile
.Open(sInfoPath
, wxFile::read
))
1849 CFIndex cfiBufLen
= (CFIndex
) indictfile
.Length();
1850 const UInt8
* pBuffer
= new UInt8
[cfiBufLen
];
1851 indictfile
.Read((void*)pBuffer
, cfiBufLen
);
1852 wxCFData
cfdaInDict(pBuffer
, cfiBufLen
);
1854 bInfoOpenSuccess
= cfdInfo
.ReadAsXML(cfdaInDict
, &sError
);
1855 if (!bInfoOpenSuccess
)
1860 if (bInfoOpenSuccess
)
1862 cfdInfo
.MakeMutable( cfdInfo
.GetCount() + 1 );
1864 wxCFArray
cfaDocTypes( cfdInfo
[ wxCFString(wxT("CFBundleDocumentTypes")) ], wxCF_RETAIN
);
1866 if (cfaDocTypes
.IsOk())
1868 bool bEntryFound
= false;
1870 //search for duplicate
1872 for (i
= 0; i
< cfaDocTypes
.GetCount(); ++i
)
1874 wxCFDictionary
cfdDocTypeEntry( cfaDocTypes
[i
], wxCF_RETAIN
);
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")) ],
1881 if (!cfaExtensions
.IsOk())
1884 for (CFIndex iExt
= 0; iExt
< cfaExtensions
.GetCount(); ++iExt
)
1886 for (size_t iWXExt
= 0; iWXExt
< asExtensions
.GetCount(); ++iWXExt
)
1888 if (asExtensions
[iWXExt
] ==
1889 wxCFString(cfaExtensions
[iExt
], wxCF_RETAIN
).BuildWXString())
1892 cfaDocTypes
.Remove(i
);
1893 cfdInfo
.Set( wxCFString(wxT("CFBundleDocumentTypes")) , cfaDocTypes
);
1896 } //end of wxstring array
1900 } //end for cf array
1908 cfdInfo
.MakeValidXML();
1911 if (outdictfile
.Open(sInfoPath
, wxFile::write
))
1913 wxCFData
cfdaInfo(cfdInfo
.WriteAsXML());
1914 if (cfdaInfo
.IsOk())
1916 if (outdictfile
.Write(cfdaInfo
.GetValue(), cfdaInfo
.GetCount()) !=
1917 (wxFileOffset
)cfdaInfo
.GetCount())
1919 wxLogDebug(wxT("error in writing to file"));
1923 bInfoSuccess
= true;
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)
1930 // wxLogDebug(wxT("LSRegisterURL Failed."));
1934 outdictfile
.Close();
1938 outdictfile
.Close();
1939 wxLogDebug(wxT("Could not read in new dictionary"));
1945 wxString(wxT("Could not open [")) +
1946 sInfoPath
+ wxT("] for writing."));
1951 wxLogDebug(wxT("Entry not found to remove"));
1954 wxCFDictionary::PrintOutArray(sPrintOut
, (CFArrayRef
)(CFTypeRef
)cfaDocTypes
);
1955 wxLogDebug(sPrintOut
);
1957 for (size_t i
= 0; i
< asExtensions
.GetCount(); ++i
)
1958 wxLogDebug(asExtensions
[i
]);
1963 wxLogDebug(wxT("No doc types array found"));
1964 wxString sPrintOut
; cfdInfo
.PrintOut(sPrintOut
); wxLogDebug(sPrintOut
);
1969 wxLogDebug(wxT("No info dictionary in main bundle"));
1974 wxLogDebug(wxT("Can only call associate from bundled app within XXX.app"));
1979 wxLogDebug(wxT("No main bundle"));
1982 #if defined(__DARWIN__)
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
);
1992 if (status
== noErr
)
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);
2001 // if (status == noErr)
2007 // wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__, wxT("ICSetPrefHandle failed.")));
2012 wxLogDebug(wxString::Format(wxT("%i - %s"), __LINE__
, wxT("ICDeleteMapEntry failed.")));
2019 CFWriteStreamRef cfwsInfo
= CFWriteStreamCreateWithFile(
2020 kCFAllocatorDefault
,
2021 (CFURLRef
) (CFTypeRef
)cfurlInfoLoc
);
2026 Boolean bOpened
= CFWriteStreamOpen(cfwsInfo
);
2029 CFStringRef cfsError
;
2030 CFIndex cfiWritten
= CFPropertyListWriteToStream((CFPropertyListRef
)(CFTypeRef
)cfdInfo
,
2032 kCFPropertyListXMLFormat_v1_0
, //100
2034 if (cfsError
&& cfiWritten
== 0)
2036 wxLogDebug(wxCFString(cfsError
).BuildWXString());
2038 cfdInfo
.PrintOut(sMessage
);
2039 wxLogDebug(sMessage
);
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)
2049 // wxLogDebug(wxT("LSRegisterURL Failed."));
2054 CFWriteStreamClose(cfwsInfo
);
2057 #endif //wxUSE_MIMETYPE