+
+ 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 ++;
+ }
+ if (nIndex < (int) file.GetLineCount()) file.CommentLine (nIndex);
+ }
+ else nIndex = (int) file.GetLineCount();
+
+ wxString sTmp = strType + wxT(" \\");
+ if (!delete_index) file.InsertLine (sTmp, nIndex);
+ if ( ! m_aDescriptions.Item(index).empty() )
+ {
+ sTmp = wxT("desc=\"") + m_aDescriptions[index]+ wxT("\" \\"); //.trim ??
+ if (!delete_index)
+ {
+ nIndex ++;
+ file.InsertLine (sTmp, nIndex);
+ }
+ }
+ wxString sExts = m_aExtensions.Item(index);
+ sTmp = wxT("exts=\"") + sExts.Trim(FALSE).Trim() + wxT("\"");
+ if (!delete_index)
+ {
+ nIndex ++;
+ file.InsertLine (sTmp, nIndex);
+ }
+
+ bTemp = file.Write ();
+ file.Close ();
+ }
+ return bTemp;
+}
+
+
+bool wxMimeTypesManagerImpl::WriteToMailCap (int index, bool delete_index)
+{
+ //check we have the right managers
+ if ( !( ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) ||
+ ( m_mailcapStylesInited & wxMAILCAP_STANDARD) ) )
+ return FALSE;
+
+ bool bTemp;
+ wxString strHome = wxGetenv(wxT("HOME"));
+
+ // and now the users mailcap
+ wxString strUserMailcap = strHome + wxT("/.mailcap");
+
+ wxMimeTextFile file;
+ if ( wxFile::Exists(strUserMailcap) )
+ {
+ bTemp = file.Open(strUserMailcap);
+ }
+ else
+ {
+ if (delete_index) return FALSE;
+ bTemp = file.Create(strUserMailcap);
+ }
+ if (bTemp)
+ {
+ // now got a file we can write to ....
+ wxMimeTypeCommands * entries = m_aEntries[index];
+ size_t iOpen;
+ wxString sCmd = entries->GetCommandForVerb(_T("open"), &iOpen);
+ wxString sTmp;
+
+ sTmp = m_aTypes[index];
+ wxString sOld;
+ int nIndex = file.pIndexOf(sTmp);
+ // get rid of all the unwanted entries...
+ if (nIndex == wxNOT_FOUND)
+ {
+ nIndex = (int) file.GetLineCount();
+ }
+ else
+ {
+ sOld = file[nIndex];
+ wxLogTrace(TRACE_MIME, wxT("--- Deleting from mailcap line '%d' ---"), nIndex);
+
+ while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
+ {
+ file.CommentLine(nIndex);
+ if (nIndex < (int) file.GetLineCount()) sOld = sOld + file[nIndex];
+ }
+ if (nIndex < (int) file.GetLineCount()) file.CommentLine (nIndex);
+ }
+
+ sTmp = sTmp + wxT(";") + sCmd; //includes wxT(" %s ");
+
+ // write it in the format that Netscape uses (default)
+ if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) )
+ {
+ if (! delete_index) file.InsertLine (sTmp, nIndex);
+ nIndex ++;
+ }
+
+ // write extended format
+ else
+ {
+ // todo FIX this code;
+ // ii) lost entries
+ // sOld holds all the entries, but our data store only has some
+ // eg test= is not stored
+
+ // so far we have written the mimetype and command out
+ wxStringTokenizer sT (sOld, wxT(";\\"));
+ if (sT.CountTokens () > 2)
+ {
+ // first one mimetype; second one command, rest unknown...
+ wxString s;
+ s = sT.GetNextToken();
+ s = sT.GetNextToken();
+
+ // first unknown
+ s = sT.GetNextToken();
+ while ( ! s.empty() )
+ {
+ bool bKnownToken = FALSE;
+ if (s.Contains(wxT("description="))) bKnownToken = TRUE;
+ if (s.Contains(wxT("x11-bitmap="))) bKnownToken = TRUE;
+ size_t i;
+ for (i=0; i < entries->GetCount(); i++)
+ {
+ if (s.Contains(entries->GetVerb(i))) bKnownToken = TRUE;
+ }
+ if (!bKnownToken)
+ {
+ sTmp = sTmp + wxT("; \\");
+ file.InsertLine (sTmp, nIndex);
+ sTmp = s;
+ }
+ s = sT.GetNextToken ();
+ }
+
+ }
+
+ if (! m_aDescriptions[index].empty() )
+ {
+ sTmp = sTmp + wxT("; \\");
+ file.InsertLine (sTmp, nIndex);
+ nIndex ++;
+ sTmp = wxT(" description=\"") + m_aDescriptions[index] + wxT("\"");
+ }
+
+ if (! m_aIcons[index].empty() )
+ {
+ sTmp = sTmp + wxT("; \\");
+ file.InsertLine (sTmp, nIndex);
+ nIndex ++;
+ sTmp = wxT(" x11-bitmap=\"") + m_aIcons[index] + wxT("\"");
+ }
+ if ( entries->GetCount() > 1 )
+
+ {
+ size_t i;
+ for (i=0; i < entries->GetCount(); i++)
+ if ( i != iOpen )
+ {
+ sTmp = sTmp + wxT("; \\");
+ file.InsertLine (sTmp, nIndex);
+ nIndex ++;
+ sTmp = wxT(" ") + entries->GetVerbCmd(i);
+ }
+ }
+
+ file.InsertLine (sTmp, nIndex);
+ nIndex ++;
+
+ }
+ bTemp = file.Write ();
+ file.Close ();
+ }
+ return bTemp;
+}
+
+wxFileType *
+wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
+{
+ InitIfNeeded();
+
+ wxString strType = ftInfo.GetMimeType ();
+ wxString strDesc = ftInfo.GetDescription ();
+ wxString strIcon = ftInfo.GetIconFile ();
+
+ wxMimeTypeCommands *entry = new wxMimeTypeCommands ();
+
+ if ( ! ftInfo.GetOpenCommand().empty())
+ entry->Add(wxT("open=") + ftInfo.GetOpenCommand () + wxT(" %s "));
+ if ( ! ftInfo.GetPrintCommand ().empty())
+ entry->Add(wxT("print=") + ftInfo.GetPrintCommand () + wxT(" %s "));
+
+ // now find where these extensions are in the data store and remove them
+ wxArrayString sA_Exts = ftInfo.GetExtensions ();
+ wxString sExt, sExtStore;
+ size_t i, nIndex;
+ for (i=0; i < sA_Exts.GetCount(); i++)
+ {
+ sExt = sA_Exts.Item(i);
+ //clean up to just a space before and after
+ sExt.Trim().Trim(FALSE);
+ sExt = wxT(' ') + sExt + wxT(' ');
+ for (nIndex = 0; nIndex < m_aExtensions.GetCount(); nIndex ++)
+ {
+ sExtStore = m_aExtensions.Item(nIndex);
+ if (sExtStore.Replace(sExt, wxT(" ") ) > 0) m_aExtensions.Item(nIndex) = sExtStore;
+ }
+