From 59f495db65a7428e7397966e41cf7c0c7e5badb2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Aug 2011 19:02:26 +0000 Subject: [PATCH] Implement wxFileType::GetOpenCommand() in wxOSX. This method used to work in 2.8 but was unimplemented in 2.9. Restore more or less the old implementation using the data that we already have in wxMimeTypesManager anyhow. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68563 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/osx/core/mimetype.h | 4 ++- src/osx/core/mimetype.cpp | 50 ++++++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 7e7620bf75..92f145027f 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -456,6 +456,7 @@ All (GUI): OSX: - Implement wxRegion::Equal() (Dr.Acula). +- Implement wxFileType::GetOpenCommand(). - wxGetOsVersion() now returns more sensible version numbers, e.g. 10 and 6 for OS X 10.6. diff --git a/include/wx/osx/core/mimetype.h b/include/wx/osx/core/mimetype.h index 3fc642ee99..f0758e7df4 100644 --- a/include/wx/osx/core/mimetype.h +++ b/include/wx/osx/core/mimetype.h @@ -56,6 +56,7 @@ private: bool GetMimeTypes(const wxString& uti, wxArrayString& mimeTypes); bool GetIcon(const wxString& uti, wxIconLocation *iconLoc); bool GetDescription(const wxString& uti, wxString *desc); + bool GetApplication(const wxString& uti, wxString *command); // Structure to represent file types typedef struct FileTypeData @@ -63,6 +64,7 @@ private: wxArrayString extensions; wxArrayString mimeTypes; wxIconLocation iconLoc; + wxString application; wxString description; } FileTypeInfo; @@ -95,9 +97,9 @@ public: bool GetMimeTypes(wxArrayString& mimeTypes) const ; bool GetIcon(wxIconLocation *iconLoc) const ; bool GetDescription(wxString *desc) const ; + bool GetOpenCommand(wxString *openCmd, const wxFileType::MessageParameters& params) const; // These functions are only stubs on Mac OS X - bool GetOpenCommand(wxString *openCmd, const wxFileType::MessageParameters& params) const; bool GetPrintCommand(wxString *printCmd, const wxFileType::MessageParameters& params) const; size_t GetAllCommands(wxArrayString *verbs, wxArrayString *commands, const wxFileType::MessageParameters& params) const; bool SetCommand(const wxString& cmd, const wxString& verb, bool overwriteprompt = TRUE); diff --git a/src/osx/core/mimetype.cpp b/src/osx/core/mimetype.cpp index c2661e22b0..4910f30ad4 100644 --- a/src/osx/core/mimetype.cpp +++ b/src/osx/core/mimetype.cpp @@ -447,7 +447,11 @@ void wxMimeTypesManagerImpl::LoadDisplayDataForUti(const wxString& uti) if( !bundle ) return; - // Get a all the document type data in this bundle + // Also get the open command while we have the bundle + wxCFStringRef cfsAppPath(CFURLCopyFileSystemPath(appUrl, kCFURLPOSIXPathStyle)); + m_utiMap[ uti ].application = cfsAppPath.AsString(); + + // Get all the document type data in this bundle CFTypeRef docTypeData; docTypeData = CFBundleGetValueForInfoDictionaryKey( bundle, docTypesKey ); @@ -587,6 +591,19 @@ bool wxMimeTypesManagerImpl::GetDescription(const wxString& uti, wxString *desc) return true; } +bool wxMimeTypesManagerImpl::GetApplication(const wxString& uti, wxString *command) +{ + const UtiMap::const_iterator itr = m_utiMap.find( uti ); + + if( itr == m_utiMap.end() ) + { + command->clear(); + return false; + } + + *command = itr->second.application; + return true; +} ///////////////////////////////////////////////////////////////////////////// // The remaining functionality has not yet been implemented for OS X @@ -626,9 +643,36 @@ bool wxFileTypeImpl::GetDescription(wxString *desc) const return m_manager->GetDescription( m_uti, desc ); } -bool wxFileTypeImpl::GetOpenCommand(wxString *WXUNUSED(openCmd), const wxFileType::MessageParameters& WXUNUSED(params)) const +namespace { - return false; + +// Helper function for GetOpenCommand(): returns the string surrounded by +// (singly) quotes if it contains spaces. +wxString QuoteIfNecessary(const wxString& path) +{ + wxString result(path); + + if ( path.find(' ') != wxString::npos ) + { + result.insert(0, "'"); + result.append("'"); + } + + return result; +} + +} // anonymous namespace + +bool wxFileTypeImpl::GetOpenCommand(wxString *openCmd, const wxFileType::MessageParameters& params) const +{ + wxString application; + if ( !m_manager->GetApplication(m_uti, &application) ) + return false; + + *openCmd << QuoteIfNecessary(application) + << ' ' << QuoteIfNecessary(params.GetFileName()); + + return true; } bool wxFileTypeImpl::GetPrintCommand(wxString *WXUNUSED(printCmd), const wxFileType::MessageParameters& WXUNUSED(params)) const -- 2.47.2