+ if ( !m_initialized )
+ {
+ // set the flag first to prevent recursion
+ m_initialized = true;
+
+#if 1
+ Initialize();
+#else
+ wxString wm = wxGetenv( wxT("WINDOWMANAGER") );
+
+ if (wm.Find( wxT("kde") ) != wxNOT_FOUND)
+ Initialize( wxMAILCAP_KDE | wxMAILCAP_STANDARD );
+ else if (wm.Find( wxT("gnome") ) != wxNOT_FOUND)
+ Initialize( wxMAILCAP_GNOME | wxMAILCAP_STANDARD );
+ else
+ Initialize();
+#endif
+ }
+}
+
+// read system and user mailcaps and other files
+void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
+ const wxString& sExtraDir)
+{
+ // read mimecap amd mime.types
+ if ( (mailcapStyles & wxMAILCAP_NETSCAPE) ||
+ (mailcapStyles & wxMAILCAP_STANDARD) )
+ GetMimeInfo(sExtraDir);
+
+ // read GNOME tables
+ if (mailcapStyles & wxMAILCAP_GNOME)
+ GetGnomeMimeInfo(sExtraDir);
+
+ // read KDE tables
+ if (mailcapStyles & wxMAILCAP_KDE)
+ GetKDEMimeInfo(sExtraDir);
+
+ m_mailcapStylesInited |= mailcapStyles;
+}
+
+// clear data so you can read another group of WM files
+void wxMimeTypesManagerImpl::ClearData()
+{
+ m_aTypes.Clear();
+ m_aIcons.Clear();
+ m_aExtensions.Clear();
+ m_aDescriptions.Clear();
+
+ WX_CLEAR_ARRAY(m_aEntries);
+ m_aEntries.Empty();
+
+ m_mailcapStylesInited = 0;
+}
+
+wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
+{
+ ClearData();
+}
+
+void wxMimeTypesManagerImpl::GetMimeInfo(const wxString& sExtraDir)
+{
+ // read this for netscape or Metamail formats
+
+ // directories where we look for mailcap and mime.types by default
+ // used by netscape and pine and other mailers, using 2 different formats!
+
+ // (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?)
+
+ wxString strHome = wxGetenv(wxT("HOME"));
+
+ wxArrayString dirs;
+ dirs.Add( strHome + wxT("/.") );
+ dirs.Add( wxT("/etc/") );
+ dirs.Add( wxT("/usr/etc/") );
+ dirs.Add( wxT("/usr/local/etc/") );
+ dirs.Add( wxT("/etc/mail/") );
+ dirs.Add( wxT("/usr/public/lib/") );
+ if (!sExtraDir.empty())
+ dirs.Add( sExtraDir + wxT("/") );
+
+ size_t nDirs = dirs.GetCount();
+ for ( size_t nDir = 0; nDir < nDirs; nDir++ )
+ {
+ wxString file = dirs[nDir] + wxT("mailcap");
+ if ( wxFile::Exists(file) )
+ {
+ ReadMailcap(file);
+ }
+
+ file = dirs[nDir] + wxT("mime.types");
+ if ( wxFile::Exists(file) )
+ {
+ ReadMimeTypes(file);
+ }
+ }
+}
+
+bool wxMimeTypesManagerImpl::WriteToMimeTypes(int index, bool delete_index)
+{
+ // check we have the right manager
+ if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD) )
+ return false;
+
+ bool bTemp;
+ wxString strHome = wxGetenv(wxT("HOME"));
+
+ // and now the users mailcap
+ wxString strUserMailcap = strHome + wxT("/.mime.types");
+
+ wxMimeTextFile file;
+ if ( wxFile::Exists(strUserMailcap) )
+ {
+ bTemp = file.Open(strUserMailcap);
+ }
+ else
+ {
+ if (delete_index)
+ return false;
+
+ bTemp = file.Create(strUserMailcap);
+ }
+
+ if (bTemp)
+ {
+ int nIndex;
+ // test for netscape's header and return false if its found
+ nIndex = file.pIndexOf(wxT("#--Netscape"));
+ if (nIndex != wxNOT_FOUND)
+ {
+ wxASSERT_MSG(false,wxT("Error in .mime.types \nTrying to mix Netscape and Metamail formats\nFile not modiifed"));
+ return false;
+ }
+
+ // write it in alternative format
+ // get rid of unwanted entries
+ wxString strType = m_aTypes[index];
+ nIndex = file.pIndexOf(strType);
+
+ // get rid of all the unwanted entries...
+ if (nIndex != wxNOT_FOUND)
+ file.CommentLine(nIndex);
+
+ if (!delete_index)
+ {
+ // add the new entries in
+ wxString sTmp = strType.Append( wxT(' '), 40 - strType.Len() );
+ sTmp = sTmp + m_aExtensions[index];
+ file.AddLine(sTmp);
+ }
+
+ bTemp = file.Write();
+ file.Close();
+ }
+
+ return bTemp;
+}
+
+bool wxMimeTypesManagerImpl::WriteToNSMimeTypes(int index, bool delete_index)
+{
+ //check we have the right managers
+ if (! ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) )
+ return false;
+
+ bool bTemp;
+ wxString strHome = wxGetenv(wxT("HOME"));
+
+ // and now the users mailcap
+ wxString strUserMailcap = strHome + wxT("/.mime.types");
+
+ wxMimeTextFile file;
+ if ( wxFile::Exists(strUserMailcap) )
+ {
+ bTemp = file.Open(strUserMailcap);
+ }
+ else
+ {
+ if (delete_index)
+ return false;
+
+ bTemp = file.Create(strUserMailcap);
+ }
+
+ if (bTemp)
+ {
+ // write it in the format that Netscape uses
+ int nIndex;
+ // test for netscape's header and insert if required...
+ // this is a comment so use true
+ nIndex = file.pIndexOf(wxT("#--Netscape"), true);
+ if (nIndex == wxNOT_FOUND)
+ {
+ // either empty file or metamail format
+ // at present we can't cope with mixed formats, so exit to preseve
+ // metamail entreies
+ if (file.GetLineCount() > 0)
+ {
+ wxASSERT_MSG(false, wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap"));
+ return false;
+ }
+
+ file.InsertLine(wxT( "#--Netscape Communications Corporation MIME Information" ), 0);
+ nIndex = 0;
+ }
+
+ wxString strType = wxT("type=") + m_aTypes[index];
+ nIndex = file.pIndexOf(strType);
+
+ // get rid of all the unwanted entries...
+ if (nIndex != wxNOT_FOUND)
+ {
+ wxString sOld = file[nIndex];
+ while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
+ {
+ file.CommentLine(nIndex);
+ sOld = file[nIndex];
+
+ wxLogTrace(TRACE_MIME, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex, sOld.c_str());
+
+ nIndex++;