X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7dc3cc31af71522b8fc55b7f97bcba7f5ef9eac5..0fa7981012f56dda16963b23699a2c65f8c61354:/src/unix/mimetype.cpp diff --git a/src/unix/mimetype.cpp b/src/unix/mimetype.cpp index ece8e422eb..fffd9d50dd 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 +// ---------------------------------------------------------------------------- -// this class uses both mailcap and mime.types to gather information about file +// 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); + +// ---------------------------------------------------------------------------- +// 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. @@ -138,6 +165,11 @@ public: { m_next = NULL; } + + ~MailCapEntry() + { + if (m_next) delete m_next; + } // accessors const wxString& GetOpenCmd() const { return m_openCmd; } @@ -208,13 +240,16 @@ class wxGNOMEIconHandler : public wxMimeTypeIconHandler { public: virtual bool GetIcon(const wxString& mimetype, wxIcon *icon); - virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl *manager) {} + virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl *manager); private: void Init(); void LoadIconsFromKeyFile(const wxString& filename); void LoadKeyFilesFromDir(const wxString& dirbase); + void LoadMimeTypesFromMimeFile(const wxString& filename, wxMimeTypesManagerImpl *manager); + void LoadMimeFilesFromDir(const wxString& dirbase, wxMimeTypesManagerImpl *manager); + static bool m_inited; static wxSortedArrayString ms_mimetypes; @@ -290,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) { @@ -357,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 ) { @@ -375,21 +423,116 @@ void wxGNOMEIconHandler::LoadIconsFromKeyFile(const wxString& filename) { curMimeType += *pc++; } + } + } +} + +void wxGNOMEIconHandler::LoadKeyFilesFromDir(const wxString& dirbase) +{ + wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase), + _T("base directory shouldn't end with a slash") ); + + wxString dirname = dirbase; + dirname << _T("/mime-info"); + + if ( !wxDir::Exists(dirname) ) + return; + + wxDir dir(dirname); + if ( !dir.IsOpened() ) + return; + + // we will concatenate it with filename to get the full path below + dirname += _T('/'); + + wxString filename; + bool cont = dir.GetFirst(&filename, _T("*.keys"), wxDIR_FILES); + while ( cont ) + { + LoadIconsFromKeyFile(dirname + filename); + + cont = dir.GetNext(&filename); + } +} + + +void wxGNOMEIconHandler::LoadMimeTypesFromMimeFile(const wxString& filename, wxMimeTypesManagerImpl *manager) +{ + wxTextFile textfile(filename); + if ( !textfile.Open() ) + return; - if ( !*pc ) + // values for the entry being parsed + wxString curMimeType, curExtList; + + const wxChar *pc; + size_t nLineCount = textfile.GetLineCount(); + for ( size_t nLine = 0; ; nLine++ ) + { + if ( nLine < nLineCount ) + { + pc = textfile[nLine].c_str(); + if ( *pc == _T('#') ) + { + // skip comments + continue; + } + } + else + { + // so that we will fall into the "if" below + pc = NULL; + } + + if ( !pc || !*pc ) + { + // end of the entry + if ( !!curMimeType && !!curExtList ) { - // we reached the end of line without finding the colon, - // something is wrong - ignore this line completely - wxLogDebug(_T("Unreckognized line %d in file '%s' ignored"), - nLine + 1, filename.c_str()); + manager -> AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString); + } + if ( !pc ) + { + // the end - this can only happen if nLine == nLineCount break; } + + curExtList.Empty(); + + continue; + } + + // what do we have here? + if ( *pc == _T('\t') ) + { + // this is a field=value ling + pc++; // skip leading TAB + + static const int lenField = 4; // strlen("ext:") + if ( wxStrncmp(pc, _T("ext:"), lenField) == 0 ) + { + // skip ' ' which follows and take everything left until the end + // of line + curExtList = pc + lenField + 1; + } + //else: some other field, we don't care + } + else + { + // this is the start of the new section + curMimeType.Empty(); + + while ( *pc != _T(':') && *pc != _T('\0') ) + { + curMimeType += *pc++; + } } } } -void wxGNOMEIconHandler::LoadKeyFilesFromDir(const wxString& dirbase) + +void wxGNOMEIconHandler::LoadMimeFilesFromDir(const wxString& dirbase, wxMimeTypesManagerImpl *manager) { wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase), _T("base directory shouldn't end with a slash") ); @@ -408,19 +551,21 @@ void wxGNOMEIconHandler::LoadKeyFilesFromDir(const wxString& dirbase) dirname += _T('/'); wxString filename; - bool cont = dir.GetFirst(&filename, _T("*.keys"), wxDIR_FILES); + bool cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES); while ( cont ) { - LoadIconsFromKeyFile(dirname + filename); + LoadMimeTypesFromMimeFile(dirname + filename, manager); cont = dir.GetNext(&filename); } } + void wxGNOMEIconHandler::Init() { wxArrayString dirs; dirs.Add(_T("/usr/share")); + dirs.Add(_T("/usr/local/share")); wxString gnomedir; wxGetHomeDir( &gnomedir ); @@ -436,7 +581,38 @@ void wxGNOMEIconHandler::Init() m_inited = TRUE; } -bool wxGNOMEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon) + +void wxGNOMEIconHandler::GetMimeInfoRecords(wxMimeTypesManagerImpl *manager) +{ + if ( !m_inited ) + { + Init(); + } + + wxArrayString dirs; + dirs.Add(_T("/usr/share")); + dirs.Add(_T("/usr/local/share")); + + wxString gnomedir; + wxGetHomeDir( &gnomedir ); + gnomedir += _T("/.gnome"); + dirs.Add( gnomedir ); + + size_t nDirs = dirs.GetCount(); + for ( size_t nDir = 0; nDir < nDirs; nDir++ ) + { + LoadMimeFilesFromDir(dirs[nDir], manager); + } +} + +#if wxUSE_GUI + #define WXUNUSED_UNLESS_GUI(p) p +#else + #define WXUNUSED_UNLESS_GUI(p) +#endif + +bool wxGNOMEIconHandler::GetIcon(const wxString& mimetype, + wxIcon * WXUNUSED_UNLESS_GUI(icon)) { if ( !m_inited ) { @@ -450,10 +626,20 @@ bool wxGNOMEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon) wxString iconname = ms_icons[(size_t)index]; #if wxUSE_GUI - *icon = wxIcon(iconname); + wxLogNull nolog; + wxIcon icn; + if (iconname.Right(4).MakeUpper() == _T(".XPM")) + icn = wxIcon(iconname); + else + icn = wxIcon(iconname, wxBITMAP_TYPE_ANY); + if ( !icn.Ok() ) + return FALSE; + + if ( icon ) + *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 @@ -493,45 +679,69 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase, if ( !file.ReadAll(&text) ) return; - int pos; - const wxChar *pc; + // first find the description string: it is the value in either "Comment=" + // line or "Comment[]=" one + int posComment = wxNOT_FOUND; - // before trying to find an icon, grab mimetype information - // (because BFU's machine would hardly have well-edited mime.types but (s)he might - // have edited it in control panel...) + wxString comment; +#if wxUSE_INTL + wxLocale *locale = wxGetLocale(); + if ( locale ) + { + // try "Comment[locale name]" first + comment << _T("Comment[") + locale->GetName() + _T("]="); - wxString mime_extension, mime_desc; + posComment = text.Find(comment); + } +#endif // wxUSE_INTL - pos = wxNOT_FOUND; - if (wxGetLocale() != NULL) - mime_desc = _T("Comment[") + wxGetLocale()->GetName() + _T("]="); - if (pos == wxNOT_FOUND) mime_desc = _T("Comment="); - pos = text.Find(mime_desc); - if (pos == wxNOT_FOUND) mime_desc = wxEmptyString; - else + if ( posComment == wxNOT_FOUND ) { - pc = text.c_str() + pos + mime_desc.Length(); - mime_desc = wxEmptyString; - while ( *pc && *pc != _T('\n') ) mime_desc += *pc++; + comment = _T("Comment="); + + posComment = text.Find(comment); + } + + wxString mime_desc; + if ( posComment != wxNOT_FOUND ) + { + // found desc: it follows the comment until the end of line + const wxChar *pc = text.c_str() + posComment + comment.length(); + while ( *pc && *pc != _T('\n') ) + { + mime_desc += *pc++; + } } + //else: no description - pos = text.Find(_T("Patterns=")); - if (pos != wxNOT_FOUND) + // next find the extensions + wxString mime_extension; + + int posExts = text.Find(_T("Patterns=")); + if ( posExts != wxNOT_FOUND ) { wxString exts; - pc = text.c_str() + pos + 9; - while ( *pc && *pc != _T('\n') ) exts += *pc++; - wxStringTokenizer tokenizer(exts, _T(";")); - wxString e; + const wxChar *pc = text.c_str() + posExts + 9; // strlen("Patterns=") + while ( *pc && *pc != _T('\n') ) + { + exts += *pc++; + } - while (tokenizer.HasMoreTokens()) + wxStringTokenizer tokenizer(exts, _T(";")); + while ( tokenizer.HasMoreTokens() ) { - e = tokenizer.GetNextToken(); - if (e.Left(2) != _T("*.")) continue; // don't support too difficult patterns + wxString e = tokenizer.GetNextToken(); + if ( e.Left(2) != _T("*.") ) + continue; // don't support too difficult patterns + + if ( !mime_extension.empty() ) + { + // separate from the previous ext + mime_extension << _T(' '); + } + mime_extension << e.Mid(2); - mime_extension << _T(' '); } - mime_extension.RemoveLast(); } ms_infoTypes.Add(mimetype); @@ -540,8 +750,8 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase, // ok, now we can take care of icon: - pos = text.Find(_T("Icon=")); - if ( pos == wxNOT_FOUND ) + int posIcon = text.Find(_T("Icon=")); + if ( posIcon == wxNOT_FOUND ) { // no icon info return; @@ -549,7 +759,7 @@ void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase, wxString icon; - pc = text.c_str() + pos + 5; // 5 == strlen("Icon=") + const wxChar *pc = text.c_str() + posIcon + 5; // 5 == strlen("Icon=") while ( *pc && *pc != _T('\n') ) { icon += *pc++; @@ -657,6 +867,7 @@ void wxKDEIconHandler::Init() dirs.Add(_T("/usr/share")); dirs.Add(_T("/opt/kde/share")); icondirs.Add(_T("/usr/share/icons/")); + icondirs.Add(_T("/usr/X11R6/share/icons/")); // Debian/Corel linux icondirs.Add(_T("/opt/kde/share/icons/")); } @@ -669,7 +880,8 @@ void wxKDEIconHandler::Init() m_inited = TRUE; } -bool wxKDEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon) +bool wxKDEIconHandler::GetIcon(const wxString& mimetype, + wxIcon * WXUNUSED_UNLESS_GUI(icon)) { if ( !m_inited ) { @@ -683,10 +895,21 @@ bool wxKDEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon) wxString iconname = ms_icons[(size_t)index]; #if wxUSE_GUI - *icon = wxIcon(iconname); + wxLogNull nolog; + wxIcon icn; + if (iconname.Right(4).MakeUpper() == _T(".XPM")) + icn = wxIcon(iconname); + else + icn = wxIcon(iconname, wxBITMAP_TYPE_ANY); + + if ( !icn.Ok() ) + return FALSE; + + if ( icon ) + *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 @@ -712,19 +935,28 @@ MailCapEntry * wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const { wxString command; - MailCapEntry *entry = m_manager->m_aEntries[m_index]; + 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()); } @@ -736,20 +968,35 @@ wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const bool wxFileTypeImpl::GetIcon(wxIcon *icon) const { - wxString mimetype; - (void)GetMimeType(&mimetype); + wxArrayString mimetypes; + GetMimeTypes(mimetypes); ArrayIconHandlers& handlers = m_manager->GetIconHandlers(); size_t count = handlers.GetCount(); + size_t counttypes = mimetypes.GetCount(); for ( size_t n = 0; n < count; n++ ) { - if ( handlers[n]->GetIcon(mimetype, icon) ) - return TRUE; + for ( size_t n2 = 0; n2 < counttypes; n2++ ) + { + if ( handlers[n]->GetIcon(mimetypes[n2], icon) ) + return TRUE; + } } return FALSE; } + +bool +wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const +{ + mimeTypes.Clear(); + for (size_t i = 0; i < m_index.GetCount(); i++) + mimeTypes.Add(m_manager->m_aTypes[m_index[i]]); + return TRUE; +} + + bool wxFileTypeImpl::GetExpandedCommand(wxString *expandedCmd, const wxFileType::MessageParameters& params, @@ -773,7 +1020,7 @@ wxFileTypeImpl::GetExpandedCommand(wxString *expandedCmd, bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) { - wxString strExtensions = m_manager->GetExtension(m_index); + wxString strExtensions = m_manager->GetExtension(m_index[0]); extensions.Empty(); // one extension in the space or comma delimitid list @@ -821,11 +1068,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"), @@ -868,13 +1126,32 @@ wxMimeTypesManagerImpl::wxMimeTypesManagerImpl() // read KDE/GNOME tables ArrayIconHandlers& handlers = GetIconHandlers(); size_t count = handlers.GetCount(); - for ( n = 0; n < count; n++ ) - handlers[n]->GetMimeInfoRecords(this); + for ( size_t hn = 0; hn < count; hn++ ) + handlers[hn]->GetMimeInfoRecords(this); +} + + +wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl() +{ + size_t cnt = m_aEntries.GetCount(); + 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++ ) { wxString extensions = m_aExtensions[n]; @@ -885,21 +1162,21 @@ wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) // consider extensions as not being case-sensitive if ( field.IsSameAs(ext, FALSE /* no case */) ) { // found - wxFileType *fileType = new wxFileType; + if (fileType == NULL) fileType = new wxFileType; fileType->m_impl->Init(this, n); - - return fileType; + // adds this mime type to _list_ of mime types with this extension } } } - // not found - return NULL; + return fileType; } wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) { + InitIfNeeded(); + // mime types are not case-sensitive wxString mimetype(mimeType); mimetype.MakeLower(); @@ -936,6 +1213,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(); @@ -961,6 +1240,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 @@ -984,6 +1265,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); @@ -1008,7 +1291,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() ) @@ -1083,8 +1367,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++ ) ; } @@ -1135,9 +1420,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 @@ -1163,7 +1447,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() ) @@ -1211,18 +1496,31 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, strTest, strDesc, curField; // accumulator - for ( bool cont = TRUE; cont; pc++ ) { + bool cont = TRUE; + while ( cont ) { switch ( *pc ) { case wxT('\\'): // interpret the next character literally (notice that // backslash can be used for line continuation) if ( *++pc == wxT('\0') ) { - // fetch the next line. - - // pc currently points to nowhere, but after the next - // pc++ in the for line it will point to the beginning - // of the next line in the file - pc = file[++nLine].c_str() - 1; + // fetch the next line if there is one + if ( nLine == nLineCount - 1 ) { + // something is wrong, bail out + cont = FALSE; + + wxLogDebug(wxT("Mailcap file %s, line %d: " + "'\\' on the end of the last line " + "ignored."), + strFileName.c_str(), + nLine + 1); + } + else { + // pass to the beginning of the next line + pc = file[++nLine].c_str(); + + // skip pc++ at the end of the loop + continue; + } } else { // just a normal character @@ -1244,6 +1542,12 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, switch ( currentToken ) { case Field_Type: strType = curField; + if ( strType.empty() ) { + // I don't think that this is a valid mailcap + // entry, but try to interpret it somehow + strType = _T('*'); + } + if ( strType.Find(wxT('/')) == wxNOT_FOUND ) { // we interpret "type" as "type/*" strType += wxT("/*"); @@ -1259,7 +1563,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, break; case Field_Other: - { + if ( !curField.empty() ) { // "good" mailcap entry? bool ok = TRUE; @@ -1298,25 +1602,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 @@ -1336,6 +1638,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, } } } + //else: the field is empty, ignore silently // it already has this value //currentToken = Field_Other; @@ -1352,6 +1655,9 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, default: curField += *pc; } + + // continue in the same line + pc++; } // check that we really read something reasonable @@ -1361,6 +1667,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); @@ -1439,6 +1763,8 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName, size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) { + InitIfNeeded(); + mimetypes.Empty(); wxString type; @@ -1456,6 +1782,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