X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cf471cab8277f4cc2c19bce417356badf9e2ca49..83a4ad53fa4e0a71c1fcce86be470e0e9586a7a7:/src/unix/mimetype.cpp diff --git a/src/unix/mimetype.cpp b/src/unix/mimetype.cpp index fb55217c0c..ff2bb4218a 100644 --- a/src/unix/mimetype.cpp +++ b/src/unix/mimetype.cpp @@ -9,8 +9,16 @@ // Licence: wxWindows license (part of wxExtra library) ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "mimetype.h" +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#ifdef __GNUG__ + #pragma implementation "mimetype.h" #endif // for compilers that support precompilation, includes "wx.h". @@ -24,7 +32,7 @@ #include "wx/defs.h" #endif -#if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__) +#if wxUSE_FILE && wxUSE_TEXTFILE #ifndef WX_PRECOMP #include "wx/string.h" @@ -55,15 +63,31 @@ class WXDLLEXPORT wxIcon; // ---------------------------------------------------------------------------- -// private classes +// constants +// ---------------------------------------------------------------------------- + +// MIME code tracing mask +#define TRACE_MIME _T("mime") + +// ---------------------------------------------------------------------------- +// private functions // ---------------------------------------------------------------------------- +// there are some fields which we don't understand but for which we don't give +// warnings as we know that they're not important - this function is used to +// test for them +static bool IsKnownUnimportantField(const wxString& field); -// this class uses both mailcap and mime.types to gather information about file +// ---------------------------------------------------------------------------- +// private classes +// ---------------------------------------------------------------------------- + +// This class uses both mailcap and mime.types to gather information about file // types. // -// The information about mailcap file was extracted from metamail(1) sources and -// documentation. +// The information about mailcap file was extracted from metamail(1) sources +// and documentation and subsequently revised when I found the RFC 1524 +// describing it. // // Format of mailcap file: spaces are ignored, each line is either a comment // (starts with '#') or a line of the form ;;...;. @@ -90,7 +114,8 @@ class WXDLLEXPORT wxIcon; // * print=xxx is the command to be used to print (and not view) the data of // this type (parameter/filename expansion is done here too) // * edit=xxx is the command to open/edit the data of this type -// * needsterminal means that a new console must be created for the viewer +// * needsterminal means that a new interactive console must be created for +// the viewer // * copiousoutput means that the viewer doesn't interact with the user but // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a // good example), thus it might be a good idea to use some kind of paging @@ -99,7 +124,7 @@ class WXDLLEXPORT wxIcon; // * 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 expansion: // * %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 @@ -108,18 +133,20 @@ class WXDLLEXPORT wxIcon; // * %{parameter} is replaced with the value of parameter taken from // Content-type header line of the message. // -// FIXME any docs with real descriptions of these files?? // // There are 2 possible formats for mime.types file, one entry per line (used -// for global mime.types) and "expanded" format where an entry takes multiple -// lines (used for users mime.types). +// for global mime.types and called Mosaic format) and "expanded" format where +// an entry takes multiple lines (used for users mime.types and called +// Netscape format). // // For both formats spaces are ignored and lines starting with a '#' are // comments. Each record has one of two following forms: // a) for "brief" format: // // b) for "expanded" format: -// type= \ desc="" \ exts="ext" +// type= \ +// desc="" \ +// exts="" // // We try to autodetect the format of mime.types: if a non-comment line starts // with "type=" we assume the second format, otherwise the first one. @@ -298,6 +325,9 @@ ArrayIconHandlers wxMimeTypesManagerImpl::ms_iconHandlers; // with blank lines separating the entries and indented lines starting with // TABs. We're interested in the field icon-filename whose value is the path // containing the icon. +// +// Update (Chris Elliott): apparently there may be an optional "[lang]" prefix +// just before the field name. void wxGNOMEIconHandler::LoadIconsFromKeyFile(const wxString& filename) { @@ -365,6 +395,16 @@ void wxGNOMEIconHandler::LoadIconsFromKeyFile(const wxString& filename) // this is a field=value ling pc++; // skip leading TAB + // skip optional "[lang]" + if ( *pc == _T('[') ) + { + while ( *pc ) + { + if ( *pc++ == _T(']') ) + break; + } + } + static const int lenField = 13; // strlen("icon-filename") if ( wxStrncmp(pc, _T("icon-filename"), lenField) == 0 ) { @@ -599,7 +639,7 @@ bool wxGNOMEIconHandler::GetIcon(const wxString& mimetype, *icon = icn; #else // helpful for testing in console mode - wxLogDebug(_T("Found GNOME icon for '%s': '%s'\n"), + wxLogTrace(TRACE_MIME, _T("Found GNOME icon for '%s': '%s'\n"), mimetype.c_str(), iconname.c_str()); #endif @@ -845,7 +885,7 @@ bool wxKDEIconHandler::GetIcon(const wxString& mimetype, *icon = icn; #else // helpful for testing in console mode - wxLogDebug(_T("Found KDE icon for '%s': '%s'\n"), + wxLogTrace(TRACE_MIME, _T("Found KDE icon for '%s': '%s'\n"), mimetype.c_str(), iconname.c_str()); #endif @@ -873,17 +913,26 @@ wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const wxString command; MailCapEntry *entry = m_manager->m_aEntries[m_index[0]]; while ( entry != NULL ) { - // notice that an empty command would always succeed (it's ok) + // get the command to run as the test for this entry command = wxFileType::ExpandCommand(entry->GetTestCmd(), params); - if ( command.IsEmpty() || (wxSystem(command) == 0) ) { - // ok, passed - wxLogTrace(wxT("Test '%s' for mime type '%s' succeeded."), + // don't trace the test result if there is no test at all + if ( command.IsEmpty() ) + { + // no test at all, ok + break; + } + + if ( wxSystem(command) == 0 ) { + // ok, test passed + wxLogTrace(TRACE_MIME, + wxT("Test '%s' for mime type '%s' succeeded."), command.c_str(), params.GetMimeType().c_str()); break; } else { - wxLogTrace(wxT("Test '%s' for mime type '%s' failed."), + wxLogTrace(TRACE_MIME, + wxT("Test '%s' for mime type '%s' failed."), command.c_str(), params.GetMimeType().c_str()); } @@ -995,11 +1044,22 @@ ArrayIconHandlers& wxMimeTypesManagerImpl::GetIconHandlers() return ms_iconHandlers; } -// read system and user mailcaps (TODO implement mime.types support) wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() +{ + m_initialized = FALSE; +} + +// read system and user mailcaps and other files +void wxMimeTypesManagerImpl::Initialize() { // directories where we look for mailcap and mime.types by default // (taken from metamail(1) sources) + // + // although RFC 1524 specifies the search path of + // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more + // places - OTOH, the RFC also says that this path can be changed with + // MAILCAPS environment variable (containing the colon separated full + // filenames to try) which is not done yet (TODO?) static const wxChar *aStandardLocations[] = { wxT("/etc"), @@ -1050,13 +1110,23 @@ wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl() { size_t cnt = m_aEntries.GetCount(); - for (size_t i = 0; i < cnt; i++) delete m_aEntries[i]; + for (size_t i = 0; i < cnt; i++) + delete m_aEntries[i]; } +wxFileType * +wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo) +{ + wxFAIL_MSG( _T("unimplemented") ); // TODO + + return NULL; +} wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) { + InitIfNeeded(); + wxFileType *fileType = NULL; size_t count = m_aExtensions.GetCount(); for ( size_t n = 0; n < count; n++ ) { @@ -1081,6 +1151,8 @@ wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) { + InitIfNeeded(); + // mime types are not case-sensitive wxString mimetype(mimeType); mimetype.MakeLower(); @@ -1117,6 +1189,8 @@ wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype) { + InitIfNeeded(); + wxString extensions; const wxArrayString& exts = filetype.GetExtensions(); size_t nExts = exts.GetCount(); @@ -1142,6 +1216,8 @@ void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType, const wxString& strExtensions, const wxString& strDesc) { + InitIfNeeded(); + int index = m_aTypes.Index(strMimeType); if ( index == wxNOT_FOUND ) { // add a new entry @@ -1165,6 +1241,8 @@ void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType, const wxString& strTest, const wxString& strDesc) { + InitIfNeeded(); + MailCapEntry *entry = new MailCapEntry(strOpenCmd, strPrintCmd, strTest); int nIndex = m_aTypes.Index(strType); @@ -1189,7 +1267,8 @@ void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType, bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) { - wxLogTrace(wxT("--- Parsing mime.types file '%s' ---"), strFileName.c_str()); + wxLogTrace(TRACE_MIME, wxT("--- Parsing mime.types file '%s' ---"), + strFileName.c_str()); wxTextFile file(strFileName); if ( !file.Open() ) @@ -1264,8 +1343,9 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) } } else { - // unquoted string ends at the first space - for ( pEnd = pc; !wxIsspace(*pEnd); pEnd++ ) + // unquoted string ends at the first space or at the end of + // line + for ( pEnd = pc; *pEnd && !wxIsspace(*pEnd); pEnd++ ) ; } @@ -1316,9 +1396,8 @@ bool 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) + // depending on the format (Mosaic or Netscape) either space or comma + // is used to separate the extensions strExtensions.Replace(wxT(","), wxT(" ")); // also deal with the leading dot @@ -1344,7 +1423,8 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName) bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, bool fallback) { - wxLogTrace(wxT("--- Parsing mailcap file '%s' ---"), strFileName.c_str()); + wxLogTrace(TRACE_MIME, wxT("--- Parsing mailcap file '%s' ---"), + strFileName.c_str()); wxTextFile file(strFileName); if ( !file.Open() ) @@ -1479,25 +1559,23 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, } else { // no, it's a simple flag - // TODO support the flags: - // 1. create an xterm for 'needsterminal' - // 2. append "| $PAGER" for 'copiousoutput' if ( curField == wxT("needsterminal") ) needsterminal = TRUE; - else if ( curField == wxT("copiousoutput") ) + else if ( curField == wxT("copiousoutput")) { + // copiousoutput impies that the + // viewer is a console program + needsterminal = copiousoutput = TRUE; - else if ( curField == wxT("textualnewlines") ) - ; // ignore - else + } + else { + // unknown flag ok = FALSE; + } } if ( !ok ) { - // we don't understand this field, but - // Netscape stores info in it, so don't warn - // about it - if ( curField.Left(16u) != "x-mozilla-flags=" ) + if ( !IsKnownUnimportantField(curField) ) { // don't flood the user with error // messages if we don't understand @@ -1542,6 +1620,24 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, strFileName.c_str(), nLine + 1); } else { + // support for flags: + // 1. create an xterm for 'needsterminal' + // 2. append "| $PAGER" for 'copiousoutput' + // + // Note that the RFC says that having both needsterminal and + // copiousoutput is probably a mistake, so it seems that running + // programs with copiousoutput inside an xterm as it is done now + // is a bad idea (FIXME) + if ( copiousoutput ) { + const wxChar *p = wxGetenv(_T("PAGER")); + strOpenCmd << _T(" | ") << (p ? p : _T("more")); + } + + if ( needsterminal ) { + strOpenCmd.Printf(_T("xterm -e sh -c '%s'"), + strOpenCmd.c_str()); + } + MailCapEntry *entry = new MailCapEntry(strOpenCmd, strPrintCmd, strTest); @@ -1620,6 +1716,8 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) { + InitIfNeeded(); + mimetypes.Empty(); wxString type; @@ -1637,6 +1735,40 @@ size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) return mimetypes.GetCount(); } +// ---------------------------------------------------------------------------- +// writing to MIME type files +// ---------------------------------------------------------------------------- + +bool wxFileTypeImpl::Unassociate() +{ + wxFAIL_MSG( _T("unimplemented") ); // TODO + + return FALSE; +} + +// ---------------------------------------------------------------------------- +// private functions +// ---------------------------------------------------------------------------- + +static bool IsKnownUnimportantField(const wxString& fieldAll) +{ + static const wxChar *knownFields[] = + { + _T("x-mozilla-flags"), + _T("nametemplate"), + _T("textualnewlines"), + }; + + wxString field = fieldAll.BeforeFirst(_T('=')); + for ( size_t n = 0; n < WXSIZEOF(knownFields); n++ ) + { + if ( field.CmpNoCase(knownFields[n]) == 0 ) + return TRUE; + } + + return FALSE; +} + #endif // wxUSE_FILE && wxUSE_TEXTFILE