- // constructors
- wxMimeTextFile () : wxTextFile () {};
- wxMimeTextFile (const wxString& strFile) : wxTextFile (strFile) { };
-
- int pIndexOf(const wxString & sSearch, bool bIncludeComments = false, int iStart = 0)
- {
- size_t i = iStart;
- int nResult = wxNOT_FOUND;
- if (i>=GetLineCount()) return wxNOT_FOUND;
-
- wxString sTest = sSearch;
- sTest.MakeLower();
- wxString sLine;
-
- if (bIncludeComments)
- {
- while ( (i < GetLineCount()) )
- {
- sLine = GetLine (i);
- sLine.MakeLower();
- if (sLine.Contains(sTest)) nResult = (int) i;
- i++;
- }
- }
- else
- {
- while ( (i < GetLineCount()) )
- {
- sLine = GetLine (i);
- sLine.MakeLower();
- if ( ! sLine.StartsWith(wxT("#")))
- {
- if (sLine.Contains(sTest)) nResult = (int) i;
- }
- i++;
- }
- }
- return nResult;
- }
-
- bool CommentLine(int nIndex)
- {
- if (nIndex <0) return false;
- if (nIndex >= (int)GetLineCount() ) return false;
- GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#"));
- return true;
- }
-
- bool CommentLine(const wxString & sTest)
- {
- int nIndex = pIndexOf(sTest);
- if (nIndex <0) return false;
- if (nIndex >= (int)GetLineCount() ) return false;
- GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#"));
- return true;
- }
-
- wxString GetVerb (size_t i)
- {
- if (i > GetLineCount() ) return wxEmptyString;
- wxString sTmp = GetLine(i).BeforeFirst(wxT('='));
- return sTmp;
- }
-
- wxString GetCmd (size_t i)
- {
- if (i > GetLineCount() ) return wxEmptyString;
- wxString sTmp = GetLine(i).AfterFirst(wxT('='));
- return sTmp;
- }
-};
-
-// in case we're compiling in non-GUI mode
-class WXDLLEXPORT wxIcon;
-
-// ----------------------------------------------------------------------------
-// constants
-// ----------------------------------------------------------------------------
-
-// MIME code tracing mask
-#define TRACE_MIME _T("mime")
-
-// give trace messages about the results of mailcap tests
-#define TRACE_MIME_TEST _T("mimetest")
-
-// ----------------------------------------------------------------------------
-// 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);
-
-// ----------------------------------------------------------------------------
-// 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 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 <field1>;<field2>;...;<fieldN>.
-// A backslash can be used to quote semicolons and newlines (and, in fact,
-// anything else including itself).
-//
-// The first field is always the MIME type in the form of type/subtype (see RFC
-// 822) where subtype may be '*' meaning "any". Following metamail, we accept
-// "type" which means the same as "type/*", although I'm not sure whether this
-// is standard.
-//
-// The second field is always the command to run. It is subject to
-// parameter/filename expansion described below.
-//
-// All the following fields are optional and may not be present at all. If
-// they're present they may appear in any order, although each of them should
-// appear only once. The optional fields are the following:
-// * notes=xxx is an uninterpreted string which is silently ignored
-// * test=xxx is the command to be used to determine whether this mailcap line
-// applies to our data or not. The RHS of this field goes through the
-// parameter/filename expansion (as the 2nd field) and the resulting string
-// is executed. The line applies only if the command succeeds, i.e. returns 0
-// exit code.
-// * 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 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
-// mechanism.
-// * textualnewlines means not to perform CR/LF translation (not honored)
-// * 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 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
-// replaced by an array of (content-type, temporary file name) pairs for all
-// message parts (TODO)
-// * %{parameter} is replaced with the value of parameter taken from
-// Content-type header line of the message.
-//
-//
-// There are 2 possible formats for mime.types file, one entry per line (used
-// 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:
-// <mime type> <space separated list of extensions>
-// b) for "expanded" format:
-// type=<mime type> BACKSLASH
-// desc="<description>" BACKSLASH
-// exts="<comma separated list of extensions>"
-//
-// (where BACKSLASH is a literal '\\' which we can't put here because cpp
-// misinterprets it)
-//
-// 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.
-
-// there may be more than one entry for one and the same mime type, to
-// choose the right one we have to run the command specified in the test
-// field on our data.
-
-// ----------------------------------------------------------------------------
-// wxGNOME
-// ----------------------------------------------------------------------------
-
-// GNOME stores the info we're interested in in several locations:
-// 1. xxx.keys files under /usr/share/mime-info
-// 2. xxx.keys files under ~/.gnome/mime-info
-//
-// The format of xxx.keys file is the following:
-//
-// mimetype/subtype:
-// field=value
-//
-// 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.
-
-
-bool wxMimeTypesManagerImpl::CheckGnomeDirsExist ()
-{
- wxString gnomedir;
- wxGetHomeDir( &gnomedir );
- wxString sTmp = gnomedir;
- sTmp = sTmp + wxT("/.gnome");
- if (! wxDir::Exists ( sTmp ) )
- {
- if (!wxMkdir ( sTmp ))
- {
- wxLogError(_("Failed to create directory %s/.gnome."), sTmp.c_str());
- return false;
- }
- }
- sTmp = sTmp + wxT("/mime-info");
- if (! wxDir::Exists ( sTmp ) )
- {
- if (!wxMkdir ( sTmp ))
- {
- wxLogError(_("Failed to create directory %s/mime-info."), sTmp.c_str());
- return false;
- }
- }
- return true;
-
-}
-
-
-
-bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index, bool delete_index)
-{
- wxString gnomedir;
- wxGetHomeDir( &gnomedir );
-
- wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.keys"));
- // if this fails probably Gnome is not installed ??
- // create it anyway as a private mime store
-
-#if defined(__WXGTK20__) && wxUSE_UNICODE
- if (! outfile.Open ( wxConvUTF8) )
-#else
- if (! outfile.Open () )
-#endif
- {
- if (delete_index) return false;
- if (!CheckGnomeDirsExist() ) return false;
- outfile.Create ();
- }
-
- wxString sTmp, strType = m_aTypes[index];
- int nIndex = outfile.pIndexOf(strType);
- if ( nIndex == wxNOT_FOUND )
- {
- outfile.AddLine ( strType + wxT(':') );
- // see file:/usr/doc/gnome-libs-devel-1.0.40/devel-docs/mime-type-handling.txt
- // as this does not deal with internationalisation
- // wxT( "\t[en_US]") + verb + wxT ('=') + cmd + wxT(" %f");
- wxMimeTypeCommands * entries = m_aEntries[index];
- size_t count = entries->GetCount();
- for ( size_t i = 0; i < count; i++ )
- {
- sTmp = entries->GetVerbCmd(i);
- sTmp.Replace( wxT("%s"), wxT("%f") );
- sTmp = wxT ( "\t") + sTmp;
- outfile.AddLine ( sTmp );
- }
- //for international use do something like this
- //outfile.AddLine ( wxString( "\t[en_US]icon-filename=") + cmd );
- outfile.AddLine ( wxT( "\ticon-filename=") + m_aIcons[index] );
- }
- else
- {
- if (delete_index)
- outfile.CommentLine(nIndex);
-
- wxMimeTypeCommands sOld;
- size_t nOld = nIndex + 1;
- bool oldEntryEnd = false;
- while ( (nOld < outfile.GetLineCount() )&& (!oldEntryEnd))
- {
- sTmp = outfile.GetLine(nOld);
- if ( (sTmp[0u] == wxT('\t')) || (sTmp[0u] == wxT('#')) )
- {
- // we have another line to deal with
- outfile.CommentLine(nOld);
- nOld ++;
- // add the line to our store
- if ((!delete_index) && (sTmp[0u] == wxT('\t')))
- sOld.Add(sTmp);
- }
- // next mimetpye ??or blank line
- else
- oldEntryEnd = true;
- }
- // list of entries in our data; these should all be in sOld,
- // though sOld may also contain other entries , eg flags
- if (!delete_index)
- {
- wxMimeTypeCommands * entries = m_aEntries[index];
- size_t i;
- for (i=0; i < entries->GetCount(); i++)
- {
- // replace any entries in sold that match verbs we know
- sOld.AddOrReplaceVerb ( entries->GetVerb(i), entries->GetCmd (i) );
- }
- //sOld should also contain the icon
- if ( !m_aIcons[index].empty() )
- sOld.AddOrReplaceVerb ( wxT("icon-filename"), m_aIcons[index] );
-
- for (i=0; i < sOld.GetCount(); i++)
- {
- sTmp = sOld.GetVerbCmd(i);
- sTmp.Replace( wxT("%s"), wxT("%f") );
- sTmp = wxT("\t") + sTmp;
- nIndex ++;
- outfile.InsertLine ( sTmp, nIndex );
- }
- }
- }
- bool bTmp = outfile.Write ();
- return bTmp;
-}
-
-
-bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index)
-{
- wxString gnomedir;
- wxGetHomeDir( &gnomedir );
-
- wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.mime"));
- // if this fails probably Gnome is not installed ??
- // create it anyway as a private mime store
- if (! outfile.Open () )
- {
- if (delete_index) return false;
- if (!CheckGnomeDirsExist() ) return false;
- outfile.Create ();
- }
- wxString strType = m_aTypes[index];
- int nIndex = outfile.pIndexOf(strType);
- if ( nIndex == wxNOT_FOUND )
- {
- outfile.AddLine ( strType );
- outfile.AddLine ( wxT("\text:") + m_aExtensions.Item(index) );
- }
- else
- {
- if (delete_index)
- {
- outfile.CommentLine(nIndex);
- outfile.CommentLine(nIndex+1);
- }
- else
- {// check for next line being the right one to replace ??
- wxString sOld = outfile.GetLine(nIndex+1);
- if (sOld.Contains( wxT("\text: ")))
- {
- outfile.GetLine(nIndex+1) = wxT("\text: ") + m_aExtensions.Item(index);
- }
- else
- {
- outfile.InsertLine( wxT("\text: ") + m_aExtensions.Item(index), nIndex + 1 );
- }
- }
- }
- bool bTmp = outfile.Write ();
- return bTmp;
-}
-
-
-void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename,
- const wxArrayString& dirs)
-{
- wxTextFile textfile(filename);
-#if defined(__WXGTK20__) && wxUSE_UNICODE
- if ( !textfile.Open( wxConvUTF8) )
-#else
- if ( !textfile.Open() )
-#endif
- return;
- wxLogTrace(TRACE_MIME, wxT("--- Opened Gnome file %s ---"),
- filename.c_str());
-
- // values for the entry being parsed
- wxString curMimeType, curIconFile;
- wxMimeTypeCommands * entry = new wxMimeTypeCommands;
-
- // these are always empty in this file
- wxArrayString strExtensions;
- wxString strDesc;
-
- const wxChar *pc;
- size_t nLineCount = textfile.GetLineCount();
- size_t nLine = 0;
- while ( nLine < nLineCount)
- {
- pc = textfile[nLine].c_str();
- if ( *pc != _T('#') )
- {
-
- wxLogTrace(TRACE_MIME, wxT("--- Reading from Gnome file %s '%s' ---"),
- filename.c_str(),pc);
-
- wxString sTmp(pc);
- if (sTmp.Contains(wxT("=")) )
- {
- // GNOME 1:
- if (sTmp.Contains( wxT("icon-filename=") ) )
- {
- curIconFile = sTmp.AfterFirst(wxT('='));
- }
- // GNOME 2:
- else if (sTmp.Contains( wxT("icon_filename=") ) )
- {
- curIconFile = sTmp.AfterFirst(wxT('='));
-
- if (!wxFileExists(curIconFile))
- {
- size_t nDirs = dirs.GetCount();
- for (size_t nDir = 0; nDir < nDirs; nDir++)
- {
- wxFileName newFile( curIconFile );
- newFile.SetPath( dirs[nDir] );
- newFile.AppendDir( wxT("pixmaps") );
- newFile.AppendDir( wxT("document-icons") );
- newFile.SetExt( wxT("png") );
- if (newFile.FileExists())
- curIconFile = newFile.GetFullPath();
- }
- }
- }
- else //: some other field,
- {
- //may contain lines like this (RH7)
- // \t[lang]open.tex."TeX this file"=tex %f
- // \tflags.tex.flags=needsterminal
- // \topen.latex."LaTeX this file"=latex %f
- // \tflags.latex.flags=needsterminal
-
- // \topen=xdvi %f
- // \tview=xdvi %f
- // \topen.convert.Convert file to Postscript=dvips %f -o `basename %f .dvi`.ps
-
- // for now ignore lines with flags in...FIX
- sTmp = sTmp.AfterLast(wxT(']'));
- sTmp = sTmp.AfterLast(wxT('\t'));
- sTmp.Trim(false).Trim();
- if (0 == sTmp.Replace ( wxT("%f"), wxT("%s") )) sTmp = sTmp + wxT(" %s");
- entry->Add(sTmp);
-
- }
-
- } // emd of has an equals sign
- else
- {
- // not a comment and not an equals sign
- if (sTmp.Contains(wxT('/')))
- {
- // this is the start of the new mimetype
- // overwrite any existing data
- if (! curMimeType.empty())
- {
- AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc);
-
- // now get ready for next bit
- entry = new wxMimeTypeCommands;
- }
- curMimeType = sTmp.BeforeFirst(wxT(':'));
- }
- }
- } // end of not a comment
- // ignore blank lines
- nLine ++;
- } // end of while, save any data
-
- if (! curMimeType.empty())
- AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc);
-}
-
-
-
-void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& filename)
-{
- wxTextFile textfile(filename);
- if ( !textfile.Open() )
- return;
-
- wxLogTrace(TRACE_MIME,
- wxT("--- Opened Gnome file %s ---"),
- filename.c_str());
-
- // 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 == wxT('#') )
- {
- // skip comments
- continue;
- }
- }
- else
- {
- // so that we will fall into the "if" below
- pc = NULL;
- }
-
- if ( !pc || !*pc )
- {
- // end of the entry
- if ( !curMimeType.empty() && !curExtList.empty() )
- {
- wxLogTrace(TRACE_MIME,
- wxT("--- At end of Gnome file finding mimetype %s ---"),
- curMimeType.c_str());
-
- 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 == wxT('\t') )
- {
- // this is a field=value ling
- pc++; // skip leading TAB
-
- static const int lenField = 5; // strlen("ext: ")
- if ( wxStrncmp(pc, wxT("ext: "), lenField) == 0 )
- {
- // skip it and take everything left until the end of line
- curExtList = pc + lenField;
- }
- //else: some other field, we don't care
- }
- else
- {
- // this is the start of the new section
- wxLogTrace(TRACE_MIME,
- wxT("--- In Gnome file finding mimetype %s ---"),
- curMimeType.c_str());
-
- if (! curMimeType.empty())
- AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString);
-
- curMimeType.Empty();
-
- while ( *pc != wxT(':') && *pc != wxT('\0') )
- {
- curMimeType += *pc++;
- }
- }
- }
-}
-
-
-void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
- const wxString& dirbase, const wxArrayString& dirs)
-{
- wxASSERT_MSG( !dirbase.empty() && !wxEndsWithPathSeparator(dirbase),
- _T("base directory shouldn't end with a slash") );
-
- wxString dirname = dirbase;
- dirname << wxT("/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 += wxT('/');
-
- wxString filename;
- bool cont;
- cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES);
- while ( cont )