X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b13d92d1c8840feca54b00e15ad558d77bda78df..1363811bb6338fb0dad6c16cfa47f46fb1eb3b99:/src/common/mimetype.cpp diff --git a/src/common/mimetype.cpp b/src/common/mimetype.cpp index 61310e14de..8e068c6f0f 100644 --- a/src/common/mimetype.cpp +++ b/src/common/mimetype.cpp @@ -31,14 +31,20 @@ // wxWindows #ifndef WX_PRECOMP #include "wx/string.h" + #include "wx/icon.h" #endif //WX_PRECOMP +// Doesn't compile in WIN16 mode +#ifndef __WIN16__ + #include "wx/log.h" #include "wx/intl.h" #include "wx/dynarray.h" +#include "wx/confbase.h" #ifdef __WXMSW__ #include "wx/msw/registry.h" + #include "windows.h" #else // Unix #include "wx/textfile.h" #endif // OS @@ -164,7 +170,7 @@ public: // * compose and composetyped fields are used to determine the program to be // called to create a new message pert in the specified format (unused). // -// Parameter/filename xpansion: +// Parameter/filename xpansion: // * %s is replaced with the (full) file name // * %t is replaced with MIME type/subtype of the entry // * for multipart type only %n is replaced with the nnumber of parts and %F is @@ -183,7 +189,7 @@ public: // comments. Each record has one of two following forms: // a) for "brief" format: // -// b) for "expanded" format: +// b) for "expanded" format: // type= \ desc="" \ exts="ext" // // We try to autodetect the format of mime.types: if a non-comment line starts @@ -279,7 +285,7 @@ public: bool GetExtensions(wxArrayString& extensions); bool GetMimeType(wxString *mimeType) const { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; } - bool GetIcon(wxIcon *icon) const + bool GetIcon(wxIcon * WXUNUSED(icon)) const { return FALSE; } // @@ maybe with Gnome/KDE integration... bool GetDescription(wxString *desc) const { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; } @@ -331,8 +337,8 @@ wxString wxFileType::ExpandCommand(const wxString& command, case 's': // '%s' expands into file name (quoted because it might // contain spaces) - except if there are already quotes - // there because otherwise some programs may get confused by - // double double quotes + // there because otherwise some programs may get confused + // by double double quotes #if 0 if ( *(pc - 2) == '"' ) str << params.GetFileName(); @@ -442,6 +448,28 @@ wxFileType::GetPrintCommand(wxString *printCmd, // wxMimeTypesManager // ---------------------------------------------------------------------------- +bool wxMimeTypesManager::IsOfType(const wxString& mimeType, + const wxString& wildcard) +{ + wxASSERT_MSG( mimeType.Find('*') == wxNOT_FOUND, + "first MIME type can't contain wildcards" ); + + // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE) + if ( wildcard.BeforeFirst('/').IsSameAs(mimeType.BeforeFirst('/'), FALSE) ) + { + wxString strSubtype = wildcard.AfterFirst('/'); + + if ( strSubtype == '*' || + strSubtype.IsSameAs(mimeType.AfterFirst('/'), FALSE) ) + { + // matches (either exactly or it's a wildcard) + return TRUE; + } + } + + return FALSE; +} + wxMimeTypesManager::wxMimeTypesManager() { m_impl = new wxMimeTypesManagerImpl; @@ -554,11 +582,11 @@ bool wxFileTypeImpl::GetIcon(wxIcon *icon) const // the format is the following: , // NB: icon index may be negative as well as positive and the full // path may contain the environment variables inside '%' - wxString strFullPath = strIcon.Before(','), - strIndex = strIcon.Right(','); + wxString strFullPath = strIcon.BeforeLast(','), + strIndex = strIcon.AfterLast(','); - // index may be omitted, in which case Before(',') is empty and - // Right(',') is the whole string + // index may be omitted, in which case BeforeLast(',') is empty and + // AfterLast(',') is the whole string if ( strFullPath.IsEmpty() ) { strFullPath = strIndex; strIndex = "0"; @@ -790,8 +818,25 @@ wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) { - wxFAIL_MSG("not implemented (must parse mime.types)"); + size_t count = m_aExtensions.GetCount(); + for ( size_t n = 0; n < count; n++ ) { + wxString extensions = m_aExtensions[n]; + while ( !extensions.IsEmpty() ) { + wxString field = extensions.BeforeFirst(' '); + extensions = extensions.AfterFirst(' '); + + // consider extensions as not being case-sensitive + if ( field.IsSameAs(ext, FALSE /* no case */) ) { + // found + wxFileType *fileType = new wxFileType; + fileType->m_impl->Init(this, n); + + return fileType; + } + } + } + // not found return NULL; } @@ -804,23 +849,23 @@ wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) // first look for an exact match int index = m_aTypes.Index(mimetype); - if ( index == NOT_FOUND ) { + if ( index == wxNOT_FOUND ) { // then try to find "text/*" as match for "text/plain" (for example) - // NB: if mimeType doesn't contain '/' at all, Left() will return the - // whole string - ok. - wxString strCategory = mimetype.Left('/'); + // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return + // the whole string - ok. + wxString strCategory = mimetype.BeforeFirst('/'); size_t nCount = m_aTypes.Count(); for ( size_t n = 0; n < nCount; n++ ) { - if ( (m_aTypes[n].Before('/') == strCategory ) && - m_aTypes[n].Right('/') == "*" ) { + if ( (m_aTypes[n].BeforeFirst('/') == strCategory ) && + m_aTypes[n].AfterFirst('/') == "*" ) { index = n; break; } } } - if ( index != NOT_FOUND ) { + if ( index != wxNOT_FOUND ) { wxFileType *fileType = new wxFileType; fileType->m_impl->Init(this, index); @@ -948,8 +993,18 @@ void wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) } } + // although it doesn't seem to be covered by RFCs, some programs + // (notably Netscape) create their entries with several comma + // separated extensions (RFC mention the spaces only) + strExtensions.Replace(",", " "); + + // also deal with the leading dot + if ( !strExtensions.IsEmpty() && strExtensions[0] == '.' ) { + strExtensions.erase(0, 1); + } + int index = m_aTypes.Index(strMimeType); - if ( index == NOT_FOUND ) { + if ( index == wxNOT_FOUND ) { // add a new entry m_aTypes.Add(strMimeType); m_aEntries.Add(NULL); @@ -1008,8 +1063,8 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) } currentToken = Field_Type; // the flags and field values on the current line - bool needsterminal = false, - copiousoutput = false; + bool needsterminal = FALSE, + copiousoutput = FALSE; wxString strType, strOpenCmd, strPrintCmd, @@ -1049,7 +1104,7 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) switch ( currentToken ) { case Field_Type: strType = curField; - if ( strType.Find('/') == NOT_FOUND ) { + if ( strType.Find('/') == wxNOT_FOUND ) { // we interpret "type" as "type/*" strType += "/*"; } @@ -1071,8 +1126,8 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) // is this something of the form foo=bar? const char *pEq = strchr(curField, '='); if ( pEq != NULL ) { - wxString lhs = curField.Left('='), - rhs = curField.After('='); + wxString lhs = curField.BeforeFirst('='), + rhs = curField.AfterFirst('='); lhs.Trim(TRUE); // from right rhs.Trim(FALSE); // from left @@ -1120,12 +1175,14 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) { // don't flood the user with error messages // if we don't understand something in his - // mailcap + // mailcap, but give them in debug mode + // because this might be useful for the + // programmer wxLogDebug ( - _("Mailcap file %s, line %d: unknown " - "field '%s' for the MIME type " - "'%s' ignored."), + "Mailcap file %s, line %d: unknown " + "field '%s' for the MIME type " + "'%s' ignored.", strFileName.c_str(), nLine + 1, curField.c_str(), @@ -1164,7 +1221,7 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) strType.MakeLower(); int nIndex = m_aTypes.Index(strType); - if ( nIndex == NOT_FOUND ) { + if ( nIndex == wxNOT_FOUND ) { // new file type m_aTypes.Add(strType); @@ -1180,7 +1237,7 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) // before, thus we Append() the new entry to the list if it has // already occured in _this_ file, but Prepend() it if it // occured in some of the previous ones. - if ( aEntryIndices.Index(nIndex) == NOT_FOUND ) { + if ( aEntryIndices.Index(nIndex) == wxNOT_FOUND ) { // first time in this file aEntryIndices.Add(nIndex); entry->Prepend(m_aEntries[nIndex]); @@ -1207,5 +1264,5 @@ void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName) #endif // OS type -/* vi: set cin tw=80 ts=4 sw=4: */ - +#endif + // __WIN16__