From 2d3ef92acce8ebc8540b21e39b54cd322e79504e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 5 Dec 2014 22:18:48 +0000 Subject: [PATCH] Prefer file types with a defined open command in Unix wxMimeTypesManager. It can happen that more than one file type maps to the given extension, in this case prefer the one which has an open command defined for it as it is typically more useful. See #16703. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78241 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/unix/mimetype.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/unix/mimetype.cpp b/src/unix/mimetype.cpp index 0ee6dcc3b5..b1c95644ef 100644 --- a/src/unix/mimetype.cpp +++ b/src/unix/mimetype.cpp @@ -830,6 +830,7 @@ wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ex InitIfNeeded(); + wxFileType* fileTypeFallback = NULL; size_t count = m_aExtensions.GetCount(); for ( size_t n = 0; n < count; n++ ) { @@ -844,12 +845,32 @@ wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ex wxFileType *fileType = new wxFileType; fileType->m_impl->Init(this, n); - return fileType; + // See if this one has a known open-command. If not, keep + // looking for another one that does, as a file that can't be + // opened is not very useful, but store this one as a fallback. + wxString type, desc, open; + fileType->GetMimeType(&type); + fileType->GetDescription(&desc); + wxFileType::MessageParameters params("filename."+ext, type); + if ( fileType->GetOpenCommand(&open, params) ) + { + delete fileTypeFallback; + return fileType; + } + else + { + // Override the previous fallback, if any, with the new + // one: we consider that later entries have priority. + delete fileTypeFallback; + fileTypeFallback = fileType; + } } } } - return NULL; + // If we couldn't find a filetype with a known open-command, return any + // without one + return fileTypeFallback; } wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)