2 /////////////////////////////////////////////////////////////////////////////
3 // Name: unix/mimetype.cpp
4 // Purpose: classes and functions to manage MIME types
5 // Author: Vadim Zeitlin
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
13 // known bugs; there may be others!! chris elliott, biol75@york.ac.uk 27 Mar 01
15 // 1) .mailcap and .mimetypes can be either in a netscape or metamail format
16 // and entries may get confused during writing (I've tried to fix this; please let me know
17 // any files that fail)
18 // 2) KDE and Gnome do not yet fully support international read/write
19 // 3) Gnome key lines like open.latex."LaTeX this file"=latex %f will have odd results
20 // 4) writing to files comments out the existing data; I hope this avoids losing
21 // any data which we could not read, and data which we did not store like test=
22 // 5) results from reading files with multiple entries (especially matches with type/* )
23 // may (or may not) work for getXXX commands
24 // 6) Loading the png icons in Gnome doesn't work for me...
25 // 7) In Gnome, if keys.mime exists but keys.users does not, there is
26 // an error message in debug mode, but the file is still written OK
27 // 8) Deleting entries is only allowed from the user file; sytem wide entries
28 // will be preserved during unassociate
29 // 9) KDE does not yet handle multiple actions; Netscape mode n??ever will
31 // ============================================================================
33 // ============================================================================
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 #pragma implementation "mimetype.h"
43 // for compilers that support precompilation, includes "wx.h".
44 #include "wx/wxprec.h"
54 #if wxUSE_FILE && wxUSE_TEXTFILE
57 #include "wx/string.h"
68 #include "wx/dynarray.h"
69 #include "wx/confbase.h"
72 #include "wx/textfile.h"
75 #include "wx/tokenzr.h"
77 #include "wx/unix/mimetype.h"
79 // other standard headers
83 /* silence warnings for comparing unsigned int's <0 */
84 # pragma message disable unscomzer
87 // this is a class to extend wxArrayString...
88 class wxMimeArrayString
: public wxArrayString
96 size_t pIndexOf (const wxString
& verb
)
100 // avoid a problem with modifying const parameter
102 while ( (i
< GetCount()) && (! Item(i
).MakeLower().Contains(sTmp
)) )
106 if ( i
==GetCount() ) i
= (size_t)wxNOT_FOUND
;
110 bool ReplaceOrAddLineCmd (const wxString verb
, const wxString
& cmd
)
112 size_t nIndex
= pIndexOf (verb
);
113 if (nIndex
== (size_t)wxNOT_FOUND
)
114 Add(verb
+ wxT("=") + cmd
);
116 Item(nIndex
) = verb
+ wxT("=") + cmd
;
120 wxString
GetVerb (size_t i
)
122 if (i
< 0) return wxEmptyString
;
123 if (i
> GetCount() ) return wxEmptyString
;
124 wxString sTmp
= Item(i
).BeforeFirst(wxT('='));
128 wxString
GetCmd (size_t i
)
130 if (i
< 0) return wxEmptyString
;
131 if (i
> GetCount() ) return wxEmptyString
;
132 wxString sTmp
= Item(i
).AfterFirst(wxT('='));
137 //this class extends wxTextFile
138 class wxMimeTextFile
: public wxTextFile
142 wxMimeTextFile () : wxTextFile () {};
143 wxMimeTextFile (const wxString
& strFile
) : wxTextFile (strFile
) { };
145 int pIndexOf(const wxString
& sSearch
, bool bIncludeComments
= FALSE
, int iStart
= 0)
148 int nResult
= wxNOT_FOUND
;
149 if (i
>=GetLineCount()) return wxNOT_FOUND
;
151 wxString sTest
= sSearch
;
155 if (bIncludeComments
)
157 while ( (i
< GetLineCount()) )
161 if (sLine
.Contains(sTest
)) nResult
= (int) i
;
167 while ( (i
< GetLineCount()) )
171 if ( ! sLine
.StartsWith(wxT("#")))
173 if (sLine
.Contains(sTest
)) nResult
= (int) i
;
181 bool CommentLine(int nIndex
)
183 if (nIndex
<0) return FALSE
;
184 if (nIndex
>= (int)GetLineCount() ) return FALSE
;
185 GetLine(nIndex
) = GetLine(nIndex
).Prepend(wxT("#"));
189 bool CommentLine(const wxString
& sTest
)
191 int nIndex
= pIndexOf(sTest
);
192 if (nIndex
<0) return FALSE
;
193 if (nIndex
>= (int)GetLineCount() ) return FALSE
;
194 GetLine(nIndex
) = GetLine(nIndex
).Prepend(wxT("#"));
198 wxString
GetVerb (size_t i
)
200 if (i
< 0) return wxEmptyString
;
201 if (i
> GetLineCount() ) return wxEmptyString
;
202 wxString sTmp
= GetLine(i
).BeforeFirst(wxT('='));
206 wxString
GetCmd (size_t i
)
208 if (i
< 0) return wxEmptyString
;
209 if (i
> GetLineCount() ) return wxEmptyString
;
210 wxString sTmp
= GetLine(i
).AfterFirst(wxT('='));
215 // in case we're compiling in non-GUI mode
216 class WXDLLEXPORT wxIcon
;
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 // MIME code tracing mask
223 #define TRACE_MIME _T("mime")
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 // there are some fields which we don't understand but for which we don't give
230 // warnings as we know that they're not important - this function is used to
232 static bool IsKnownUnimportantField(const wxString
& field
);
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
240 // This class uses both mailcap and mime.types to gather information about file
243 // The information about mailcap file was extracted from metamail(1) sources
244 // and documentation and subsequently revised when I found the RFC 1524
247 // Format of mailcap file: spaces are ignored, each line is either a comment
248 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
249 // A backslash can be used to quote semicolons and newlines (and, in fact,
250 // anything else including itself).
252 // The first field is always the MIME type in the form of type/subtype (see RFC
253 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
254 // "type" which means the same as "type/*", although I'm not sure whether this
257 // The second field is always the command to run. It is subject to
258 // parameter/filename expansion described below.
260 // All the following fields are optional and may not be present at all. If
261 // they're present they may appear in any order, although each of them should
262 // appear only once. The optional fields are the following:
263 // * notes=xxx is an uninterpreted string which is silently ignored
264 // * test=xxx is the command to be used to determine whether this mailcap line
265 // applies to our data or not. The RHS of this field goes through the
266 // parameter/filename expansion (as the 2nd field) and the resulting string
267 // is executed. The line applies only if the command succeeds, i.e. returns 0
269 // * print=xxx is the command to be used to print (and not view) the data of
270 // this type (parameter/filename expansion is done here too)
271 // * edit=xxx is the command to open/edit the data of this type
272 // * needsterminal means that a new interactive console must be created for
274 // * copiousoutput means that the viewer doesn't interact with the user but
275 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
276 // good example), thus it might be a good idea to use some kind of paging
278 // * textualnewlines means not to perform CR/LF translation (not honored)
279 // * compose and composetyped fields are used to determine the program to be
280 // called to create a new message pert in the specified format (unused).
282 // Parameter/filename expansion:
283 // * %s is replaced with the (full) file name
284 // * %t is replaced with MIME type/subtype of the entry
285 // * for multipart type only %n is replaced with the nnumber of parts and %F is
286 // replaced by an array of (content-type, temporary file name) pairs for all
287 // message parts (TODO)
288 // * %{parameter} is replaced with the value of parameter taken from
289 // Content-type header line of the message.
292 // There are 2 possible formats for mime.types file, one entry per line (used
293 // for global mime.types and called Mosaic format) and "expanded" format where
294 // an entry takes multiple lines (used for users mime.types and called
297 // For both formats spaces are ignored and lines starting with a '#' are
298 // comments. Each record has one of two following forms:
299 // a) for "brief" format:
300 // <mime type> <space separated list of extensions>
301 // b) for "expanded" format:
302 // type=<mime type> \
303 // desc="<description>" \
304 // exts="<comma separated list of extensions>"
306 // We try to autodetect the format of mime.types: if a non-comment line starts
307 // with "type=" we assume the second format, otherwise the first one.
309 // there may be more than one entry for one and the same mime type, to
310 // choose the right one we have to run the command specified in the test
311 // field on our data.
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 // GNOME stores the info we're interested in in several locations:
318 // 1. xxx.keys files under /usr/share/mime-info
319 // 2. xxx.keys files under ~/.gnome/mime-info
321 // The format of xxx.keys file is the following:
326 // with blank lines separating the entries and indented lines starting with
327 // TABs. We're interested in the field icon-filename whose value is the path
328 // containing the icon.
330 // Update (Chris Elliott): apparently there may be an optional "[lang]" prefix
331 // just before the field name.
334 bool wxMimeTypesManagerImpl::CheckGnomeDirsExist ()
337 wxGetHomeDir( &gnomedir
);
338 wxString sTmp
= gnomedir
;
339 sTmp
= sTmp
+ "/.gnome" ;
340 if (! wxDir::Exists ( sTmp
) )
342 if (!wxMkdir ( sTmp
))
344 wxFAIL_MSG (wxString ("Failed to create directory\n.gnome in \nCheckGnomeDirsExist") + sTmp
);
348 sTmp
= sTmp
+ "/mime-info";
349 if (! wxDir::Exists ( sTmp
) )
351 if (!wxMkdir ( sTmp
))
353 wxFAIL_MSG (wxString ("Failed to create directory\nmime-info in \nCheckGnomeDirsExist") + sTmp
);
363 bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index
, bool delete_index
)
366 wxGetHomeDir( &gnomedir
);
368 wxMimeTextFile
outfile ( gnomedir
+ "/.gnome/mime-info/user.keys");
369 // if this fails probably Gnome is not installed ??
370 // create it anyway as a private mime store
372 if (! outfile
.Open () )
374 if (delete_index
) return FALSE
;
375 if (!CheckGnomeDirsExist() ) return FALSE
;
379 wxString sTmp
, strType
= m_aTypes
[index
];
380 int nIndex
= outfile
.pIndexOf(strType
);
381 if ( nIndex
== wxNOT_FOUND
)
383 outfile
.AddLine ( strType
+ wxT(':') );
384 // see file:/usr/doc/gnome-libs-devel-1.0.40/devel-docs/mime-type-handling.txt
385 // as this does not deal with internationalisation
386 // wxT( "\t[en_US]") + verb + wxT ('=') + cmd + wxT(" %f");
387 wxMimeArrayString
* entries
= m_aEntries
[index
];
389 for (i
=0; i
< entries
->GetCount(); i
++)
391 sTmp
= entries
->Item(i
);
392 sTmp
.Replace( wxT("%s"), wxT("%f") );
393 sTmp
= wxT ( "\t") + sTmp
;
394 outfile
.AddLine ( sTmp
);
396 //for international use do something like this
397 //outfile.AddLine ( wxString( "\t[en_US]icon-filename=") + cmd );
398 outfile
.AddLine ( wxT( "\ticon-filename=") + m_aIcons
[index
] );
402 if (delete_index
) outfile
.CommentLine(nIndex
);
403 wxMimeArrayString sOld
;
404 size_t nOld
= nIndex
+ 1;
405 bool oldEntryEnd
= FALSE
;
406 while ( (nOld
< outfile
.GetLineCount() )&& (oldEntryEnd
== FALSE
))
408 sTmp
= outfile
.GetLine(nOld
);
409 if ( (sTmp
[0u] == wxT('\t')) || (sTmp
[0u] == wxT('#')) )
411 // we have another line to deal with
412 outfile
.CommentLine(nOld
);
414 // add the line to our store
415 if ((!delete_index
) && (sTmp
[0u] == wxT('\t'))) sOld
.Add(sTmp
);
417 // next mimetpye ??or blank line
418 else oldEntryEnd
= TRUE
;
420 // list of entries in our data; these should all be in sOld,
421 // though sOld may also contain other entries , eg flags
424 wxMimeArrayString
* entries
= m_aEntries
[index
];
426 for (i
=0; i
< entries
->GetCount(); i
++)
428 // replace any entries in sold that match verbs we know
429 sOld
.ReplaceOrAddLineCmd ( entries
->GetVerb(i
), entries
->GetCmd (i
) );
431 //sOld should also contain the icon
432 if ( !m_aIcons
[index
].IsEmpty() )
433 sOld
.ReplaceOrAddLineCmd ( wxT( "icon-filename"), m_aIcons
[index
] );
435 for (i
=0; i
< sOld
.GetCount(); i
++)
438 sTmp
.Replace( wxT("%s"), wxT("%f") );
439 sTmp
= wxT ( "\t") + sTmp
;
441 outfile
.InsertLine ( sTmp
, nIndex
);
445 bool bTmp
= outfile
.Write ();
450 bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index
, bool delete_index
)
453 wxGetHomeDir( &gnomedir
);
455 wxMimeTextFile
outfile ( gnomedir
+ "/.gnome/mime-info/user.mime");
456 // if this fails probably Gnome is not installed ??
457 // create it anyway as a private mime store
458 if (! outfile
.Open () )
460 if (delete_index
) return FALSE
;
461 if (!CheckGnomeDirsExist() ) return FALSE
;
464 wxString strType
= m_aTypes
[index
];
465 int nIndex
= outfile
.pIndexOf(strType
);
466 if ( nIndex
== wxNOT_FOUND
)
468 outfile
.AddLine ( strType
);
469 outfile
.AddLine ( wxT( "\text:") + m_aExtensions
.Item(index
) );
475 outfile
.CommentLine(nIndex
);
476 outfile
.CommentLine(nIndex
+1);
479 {// check for next line being the right one to replace ??
480 wxString sOld
= outfile
.GetLine(nIndex
+1);
481 if (sOld
.Contains(wxT("\text: ")))
483 outfile
.GetLine(nIndex
+1) = wxT( "\text: ") + m_aExtensions
.Item(index
);
487 outfile
.InsertLine(wxT( "\text: ") + m_aExtensions
.Item(index
), nIndex
+ 1 );
491 bool bTmp
= outfile
.Write ();
496 void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString
& filename
)
498 wxTextFile
textfile(filename
);
499 if ( !textfile
.Open() )
501 wxLogTrace(TRACE_MIME
, wxT("--- Opened Gnome file %s ---"),
504 // values for the entry being parsed
505 wxString curMimeType
, curIconFile
;
506 wxMimeArrayString
* entry
= new wxMimeArrayString
;
508 // these are always empty in this file
509 wxArrayString strExtensions
;
513 size_t nLineCount
= textfile
.GetLineCount();
515 while ( nLine
< nLineCount
)
517 pc
= textfile
[nLine
].c_str();
518 if ( *pc
!= _T('#') )
521 wxLogTrace(TRACE_MIME
, wxT("--- Reading from Gnome file %s '%s' ---"),
522 filename
.c_str(),pc
);
525 if (sTmp
.Contains(wxT("=")) )
527 if (sTmp
.Contains( wxT("icon-filename=") ) )
529 curIconFile
= sTmp
.AfterFirst(wxT('='));
531 else //: some other field,
533 //may contain lines like this (RH7)
534 // \t[lang]open.tex."TeX this file"=tex %f
535 // \tflags.tex.flags=needsterminal
536 // \topen.latex."LaTeX this file"=latex %f
537 // \tflags.latex.flags=needsterminal
541 // \topen.convert.Convert file to Postscript=dvips %f -o `basename %f .dvi`.ps
543 // for now ignore lines with flags in...FIX
544 sTmp
= sTmp
.AfterLast(wxT(']'));
545 sTmp
= sTmp
.AfterLast(wxT('\t'));
546 sTmp
.Trim(FALSE
).Trim();
547 if (0 == sTmp
.Replace ( wxT("%f"), wxT("%s") )) sTmp
= sTmp
+ wxT(" %s");
552 } // emd of has an equals sign
555 // not a comment and not an equals sign
556 if (sTmp
.Contains(wxT('/')))
558 // this is the start of the new mimetype
559 // overwrite any existing data
560 if (! curMimeType
.IsEmpty())
562 AddToMimeData ( curMimeType
, curIconFile
, entry
, strExtensions
, strDesc
);
564 // now get ready for next bit
565 entry
= new wxMimeArrayString
;
567 curMimeType
= sTmp
.BeforeFirst(wxT(':'));
570 } // end of not a comment
571 // ignore blank lines
573 } // end of while, save any data
574 if (! curMimeType
.IsEmpty())
576 AddToMimeData ( curMimeType
, curIconFile
, entry
, strExtensions
, strDesc
);
583 void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString
& filename
)
585 wxTextFile
textfile(filename
);
586 if ( !textfile
.Open() )
588 wxLogTrace(TRACE_MIME
, wxT("--- Opened Gnome file %s ---"),
591 // values for the entry being parsed
592 wxString curMimeType
, curExtList
;
595 size_t nLineCount
= textfile
.GetLineCount();
596 for ( size_t nLine
= 0; ; nLine
++ )
598 if ( nLine
< nLineCount
)
600 pc
= textfile
[nLine
].c_str();
601 if ( *pc
== _T('#') )
609 // so that we will fall into the "if" below
616 if ( !!curMimeType
&& !!curExtList
)
618 wxLogTrace(TRACE_MIME
, wxT("--- At end of Gnome file finding mimetype %s ---"),
619 curMimeType
.c_str());
621 AddMimeTypeInfo(curMimeType
, curExtList
, wxEmptyString
);
626 // the end - this can only happen if nLine == nLineCount
635 // what do we have here?
636 if ( *pc
== _T('\t') )
638 // this is a field=value ling
639 pc
++; // skip leading TAB
641 static const int lenField
= 4; // strlen("ext:")
642 if ( wxStrncmp(pc
, _T("ext:"), lenField
) == 0 )
644 // skip ' ' which follows and take everything left until the end
646 curExtList
= pc
+ lenField
+ 1;
648 //else: some other field, we don't care
652 // this is the start of the new section
653 wxLogTrace(TRACE_MIME
, wxT("--- In Gnome file finding mimetype %s ---"),
654 curMimeType
.c_str());
656 if (! curMimeType
.IsEmpty()) AddMimeTypeInfo(curMimeType
, curExtList
, wxEmptyString
);
660 while ( *pc
!= _T(':') && *pc
!= _T('\0') )
662 curMimeType
+= *pc
++;
669 void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(const wxString
& dirbase
)
671 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
672 _T("base directory shouldn't end with a slash") );
674 wxString dirname
= dirbase
;
675 dirname
<< _T("/mime-info");
677 if ( !wxDir::Exists(dirname
) )
681 if ( !dir
.IsOpened() )
684 // we will concatenate it with filename to get the full path below
688 bool cont
= dir
.GetFirst(&filename
, _T("*.mime"), wxDIR_FILES
);
691 LoadGnomeMimeTypesFromMimeFile(dirname
+ filename
);
693 cont
= dir
.GetNext(&filename
);
696 cont
= dir
.GetFirst(&filename
, _T("*.keys"), wxDIR_FILES
);
699 LoadGnomeDataFromKeyFile(dirname
+ filename
);
701 cont
= dir
.GetNext(&filename
);
708 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString
& sExtraDir
)
712 dirs
.Add(_T("/usr/share"));
713 dirs
.Add(_T("/usr/local/share"));
716 wxGetHomeDir( &gnomedir
);
717 gnomedir
+= _T("/.gnome");
718 dirs
.Add( gnomedir
);
719 if (!sExtraDir
.IsEmpty()) dirs
.Add( sExtraDir
);
721 size_t nDirs
= dirs
.GetCount();
722 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
724 LoadGnomeMimeFilesFromDir(dirs
[nDir
]);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype
738 // may be found in either of the following locations
740 // 1. $KDEDIR/share/mimelnk/mimetype/subtype.kdelnk
741 // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk
743 // The format of a .kdelnk file is almost the same as the one used by
744 // wxFileConfig, i.e. there are groups, comments and entries. The icon is the
745 // value for the entry "Type"
747 // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
748 // for now write to .kdelnk but should eventually do .desktop instead (in preference??)
750 bool wxMimeTypesManagerImpl::CheckKDEDirsExist ( const wxString
& sOK
, const wxString
& sTest
)
755 if (wxDir::Exists(sOK
)) return TRUE
;
760 wxString sStart
= sOK
+ wxT("/") + sTest
.BeforeFirst(wxT('/'));
761 if (!wxDir::Exists(sStart
)) wxMkdir(sStart
);
762 wxString sEnd
= sTest
.AfterFirst(wxT('/'));
763 return CheckKDEDirsExist(sStart
, sEnd
);
767 bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index
, bool delete_index
)
769 wxMimeTextFile appoutfile
, mimeoutfile
;
770 wxString sHome
= wxGetHomeDir();
771 wxString sTmp
= wxT(".kde/share/mimelnk/");
772 wxString sMime
= m_aTypes
[index
] ;
773 CheckKDEDirsExist (sHome
, sTmp
+ sMime
.BeforeFirst(wxT('/')) );
774 sTmp
= sHome
+ wxT('/') + sTmp
+ sMime
+ wxT(".kdelnk");
777 bool bMimeExists
= mimeoutfile
.Open (sTmp
);
780 bTemp
= mimeoutfile
.Create (sTmp
);
781 // some unknown error eg out of disk space
782 if (!bTemp
) return FALSE
;
785 sTmp
= wxT(".kde/share/applnk/");
786 CheckKDEDirsExist (sHome
, sTmp
+ sMime
.AfterFirst(wxT('/')) );
787 sTmp
= sHome
+ wxT('/') + sTmp
+ sMime
.AfterFirst(wxT('/')) + wxT(".kdelnk");
790 bAppExists
= appoutfile
.Open (sTmp
);
793 bTemp
= appoutfile
.Create (sTmp
);
794 // some unknown error eg out of disk space
795 if (!bTemp
) return FALSE
;
798 // fixed data; write if new file
801 mimeoutfile
.AddLine(wxT("#KDE Config File"));
802 mimeoutfile
.AddLine(wxT("[KDE Desktop Entry]"));
803 mimeoutfile
.AddLine(wxT("Version=1.0"));
804 mimeoutfile
.AddLine(wxT("Type=MimeType"));
805 mimeoutfile
.AddLine(wxT("MimeType=") + sMime
);
810 mimeoutfile
.AddLine(wxT("#KDE Config File"));
811 mimeoutfile
.AddLine(wxT("[KDE Desktop Entry]"));
812 appoutfile
.AddLine(wxT("Version=1.0"));
813 appoutfile
.AddLine(wxT("Type=Application"));
814 appoutfile
.AddLine(wxT("MimeType=") + sMime
+ wxT(';'));
819 mimeoutfile
.CommentLine(wxT("Comment="));
821 mimeoutfile
.AddLine(wxT("Comment=") + m_aDescriptions
[index
]);
822 appoutfile
.CommentLine(wxT("Name="));
824 appoutfile
.AddLine(wxT("Comment=") + m_aDescriptions
[index
]);
826 sTmp
= m_aIcons
[index
];
827 // we can either give the full path, or the shortfilename if its in
828 // one of the directories we search
829 mimeoutfile
.CommentLine(wxT("Icon=") );
830 if (!delete_index
) mimeoutfile
.AddLine(wxT("Icon=") + sTmp
);
831 appoutfile
.CommentLine(wxT("Icon=") );
832 if (!delete_index
) appoutfile
.AddLine(wxT("Icon=") + sTmp
);
834 sTmp
= wxT(" ") + m_aExtensions
[index
];
836 wxStringTokenizer
tokenizer(sTmp
, _T(" "));
837 sTmp
= wxT("Patterns=");
838 mimeoutfile
.CommentLine(sTmp
);
839 while ( tokenizer
.HasMoreTokens() )
841 // holds an extension; need to change it to *.ext;
842 wxString e
= wxT("*.") + tokenizer
.GetNextToken() + wxT(";");
845 if (!delete_index
) mimeoutfile
.AddLine(sTmp
);
847 wxMimeArrayString
* entries
= m_aEntries
[index
];
848 // if we don't find open just have an empty string ... FIX this
849 size_t iOpen
= entries
->pIndexOf(wxT("open"));
850 sTmp
= entries
->GetCmd(iOpen
);
851 sTmp
.Replace( wxT("%s"), wxT("%f") );
853 mimeoutfile
.CommentLine(wxT("DefaultApp=") );
854 if (!delete_index
) mimeoutfile
.AddLine(wxT("DefaultApp=") + sTmp
);
856 sTmp
.Replace( wxT("%f"), wxT("") );
857 appoutfile
.CommentLine(wxT("Exec="));
858 if (!delete_index
) appoutfile
.AddLine(wxT("Exec=") + sTmp
);
860 if (entries
->GetCount() > 1)
862 //other actions as well as open
866 if (mimeoutfile
.Write ()) bTemp
= TRUE
;
867 mimeoutfile
.Close ();
868 if (appoutfile
.Write ()) bTemp
= TRUE
;
876 void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString
& dirbase
,
877 const wxString
& subdir
,
878 const wxString
& filename
,
879 const wxArrayString
& icondirs
)
882 if ( !file
.Open(dirbase
+ filename
) ) return;
884 wxMimeArrayString
* entry
= new wxMimeArrayString
;
886 wxString mimetype
, mime_desc
, strIcon
;
888 int nIndex
= file
.pIndexOf ("MimeType=");
889 if (nIndex
== wxNOT_FOUND
)
891 // construct mimetype from the directory name and the basename of the
892 // file (it always has .kdelnk extension)
893 mimetype
<< subdir
<< _T('/') << filename
.BeforeLast(_T('.'));
895 else mimetype
= file
.GetCmd (nIndex
);
897 // first find the description string: it is the value in either "Comment="
898 // line or "Comment[<locale_name>]=" one
899 nIndex
= wxNOT_FOUND
;
903 wxLocale
*locale
= wxGetLocale();
906 // try "Comment[locale name]" first
907 comment
<< _T("Comment[") + locale
->GetName() + _T("]=");
908 nIndex
= file
.pIndexOf(comment
);
912 if ( nIndex
== wxNOT_FOUND
)
914 comment
= _T("Comment=");
915 nIndex
= file
.pIndexOf(comment
);
918 if ( nIndex
!= wxNOT_FOUND
) mime_desc
= file
.GetCmd(nIndex
);
919 //else: no description
921 // next find the extensions
922 wxString mime_extension
;
924 nIndex
= file
.pIndexOf(_T("Patterns="));
925 if ( nIndex
!= wxNOT_FOUND
)
927 wxString exts
= file
.GetCmd (nIndex
) ;;
929 wxStringTokenizer
tokenizer(exts
, _T(";"));
930 while ( tokenizer
.HasMoreTokens() )
932 wxString e
= tokenizer
.GetNextToken();
933 if ( e
.Left(2) != _T("*.") )
934 continue; // don't support too difficult patterns
936 if ( !mime_extension
.empty() )
938 // separate from the previous ext
939 mime_extension
<< _T(' ');
942 mime_extension
<< e
.Mid(2);
945 sExts
.Add(mime_extension
);
948 // ok, now we can take care of icon:
950 nIndex
= file
.pIndexOf(_T("Icon="));
951 if ( nIndex
!= wxNOT_FOUND
)
953 strIcon
= file
.GetCmd(nIndex
);
954 //it could be the real path, but more often a short name
955 if (!wxFileExists(strIcon
))
957 // icon is just the short name
958 if ( !strIcon
.IsEmpty() )
960 // we must check if the file exists because it may be stored
961 // in many locations, at least ~/.kde and $KDEDIR
962 size_t nDir
, nDirs
= icondirs
.GetCount();
963 for ( nDir
= 0; nDir
< nDirs
; nDir
++ )
964 if (wxFileExists(icondirs
[nDir
] + strIcon
))
966 strIcon
.Prepend(icondirs
[nDir
]);
972 // now look for lines which know about the application
973 // exec= or DefaultApp=
975 nIndex
= file
.pIndexOf(wxT("DefaultApp"));
977 if ( nIndex
== wxNOT_FOUND
)
980 nIndex
= file
.pIndexOf(wxT("Exec"));
983 if ( nIndex
!= wxNOT_FOUND
)
985 wxString sTmp
= file
.GetCmd(nIndex
);
986 // we expect %f; others including %F and %U and %u are possible
987 if (0 == sTmp
.Replace ( wxT("%f"), wxT("%s") )) sTmp
= sTmp
+ wxT(" %s");
988 entry
->ReplaceOrAddLineCmd (wxString(wxT("open")), sTmp
);
991 AddToMimeData (mimetype
, strIcon
, entry
, sExts
, mime_desc
);
996 void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString
& dirbase
,
997 const wxString
& subdir
,
998 const wxArrayString
& icondirs
)
1000 wxString dirname
= dirbase
;
1003 if ( !dir
.IsOpened() )
1009 bool cont
= dir
.GetFirst(&filename
, _T("*.kdelnk"), wxDIR_FILES
);
1012 LoadKDELinksForMimeSubtype(dirname
, subdir
, filename
, icondirs
);
1014 cont
= dir
.GetNext(&filename
);
1016 // new standard for Gnome and KDE
1017 cont
= dir
.GetFirst(&filename
, _T("*.desktop"), wxDIR_FILES
);
1020 LoadKDELinksForMimeSubtype(dirname
, subdir
, filename
, icondirs
);
1022 cont
= dir
.GetNext(&filename
);
1026 void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString
& dirbase
,
1027 const wxArrayString
& icondirs
)
1029 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
1030 _T("base directory shouldn't end with a slash") );
1032 wxString dirname
= dirbase
;
1033 dirname
<< _T("/mimelnk");
1035 if ( !wxDir::Exists(dirname
) )
1039 if ( !dir
.IsOpened() )
1042 // we will concatenate it with dir name to get the full path below
1046 bool cont
= dir
.GetFirst(&subdir
, wxEmptyString
, wxDIR_DIRS
);
1049 LoadKDELinksForMimeType(dirname
, subdir
, icondirs
);
1051 cont
= dir
.GetNext(&subdir
);
1055 void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString
& sExtraDir
)
1058 wxArrayString icondirs
;
1060 // settings in ~/.kde have maximal priority
1061 dirs
.Add(wxGetHomeDir() + _T("/.kde/share"));
1062 icondirs
.Add(wxGetHomeDir() + _T("/.kde/share/icons/"));
1064 // the variable KDEDIR is set when KDE is running
1065 const char *kdedir
= getenv("KDEDIR");
1068 dirs
.Add(wxString(kdedir
) + _T("/share"));
1069 icondirs
.Add(wxString(kdedir
) + _T("/share/icons/"));
1073 // try to guess KDEDIR
1074 dirs
.Add(_T("/usr/share"));
1075 dirs
.Add(_T("/opt/kde/share"));
1076 icondirs
.Add(_T("/usr/share/icons/"));
1077 icondirs
.Add(_T("/usr/X11R6/share/icons/")); // Debian/Corel linux
1078 icondirs
.Add(_T("/opt/kde/share/icons/"));
1081 if (!sExtraDir
.IsEmpty()) dirs
.Add (sExtraDir
);
1082 icondirs
.Add(sExtraDir
+ wxT("/icons"));
1084 size_t nDirs
= dirs
.GetCount();
1085 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
1087 LoadKDELinkFilesFromDir(dirs
[nDir
], icondirs
);
1095 // ----------------------------------------------------------------------------
1096 // wxFileTypeImpl (Unix)
1097 // ----------------------------------------------------------------------------
1100 wxString
wxFileTypeImpl::GetExpandedCommand(const wxString
& verb
, const wxFileType::MessageParameters
& params
) const
1104 while ( (i
< m_index
.GetCount() ) && sTmp
.IsEmpty() )
1106 sTmp
= m_manager
->GetCommand ( verb
, m_index
[i
] );
1110 return wxFileType::ExpandCommand(sTmp
, params
);
1113 bool wxFileTypeImpl::GetIcon(wxIcon
*icon
,
1114 wxString
*iconFile
/*= NULL */,
1115 int *iconIndex
/*= NULL*/) const
1121 while ( (i
< m_index
.GetCount() ) && sTmp
.IsEmpty() )
1123 sTmp
= m_manager
->m_aIcons
[m_index
[i
]];
1126 if ( sTmp
.IsEmpty () ) return FALSE
;
1130 if (sTmp
.Right(4).MakeUpper() == _T(".XPM"))
1133 icn
= wxIcon(sTmp
, wxBITMAP_TYPE_ANY
);
1138 if (iconFile
) *iconFile
= sTmp
;
1139 if (iconIndex
) *iconIndex
= 0;
1149 wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
1152 for (size_t i
= 0; i
< m_index
.GetCount(); i
++)
1153 mimeTypes
.Add(m_manager
->m_aTypes
[m_index
[i
]]);
1158 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
1159 wxArrayString
*commands
,
1160 const wxFileType::MessageParameters
& params
) const
1163 wxString vrb
, cmd
, sTmp
;
1165 wxMimeArrayString
* sPairs
;
1167 // verbs and commands have been cleared already in mimecmn.cpp...
1168 // if we find no entries in the exact match, try the inexact match
1169 for (size_t n
= 0; ((count
==0) && (n
< m_index
.GetCount())); n
++)
1171 // list of verb = command pairs for this mimetype
1172 sPairs
= m_manager
->m_aEntries
[m_index
[n
]];
1174 for ( i
= 0; i
< sPairs
->GetCount () ; i
++ )
1176 vrb
= sPairs
->GetVerb(i
);
1177 // some gnome entries have . inside
1178 vrb
= vrb
.AfterLast(wxT('.'));
1179 cmd
= sPairs
->GetCmd (i
);
1180 if (! cmd
.IsEmpty() )
1182 cmd
= wxFileType::ExpandCommand(cmd
, params
);
1184 if ( vrb
.IsSameAs (wxT("open")))
1186 verbs
->Insert(vrb
,0u);
1187 commands
->Insert(cmd
,0u);
1192 commands
->Add (cmd
);
1203 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
1205 wxString strExtensions
= m_manager
->GetExtension(m_index
[0]);
1208 // one extension in the space or comma delimitid list
1210 for ( const wxChar
*p
= strExtensions
; ; p
++ ) {
1211 if ( *p
== wxT(' ') || *p
== wxT(',') || *p
== wxT('\0') ) {
1212 if ( !strExt
.IsEmpty() ) {
1213 extensions
.Add(strExt
);
1216 //else: repeated spaces (shouldn't happen, but it's not that
1217 // important if it does happen)
1219 if ( *p
== wxT('\0') )
1222 else if ( *p
== wxT('.') ) {
1223 // remove the dot from extension (but only if it's the first char)
1224 if ( !strExt
.IsEmpty() ) {
1227 //else: no, don't append it
1237 // set an arbitrary command,
1238 // could adjust the code to ask confirmation if it already exists and
1239 // overwriteprompt is TRUE, but this is currently ignored as *Associate* has
1240 // no overwrite prompt
1241 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
, const wxString
& verb
, bool overwriteprompt
/*= TRUE*/)
1243 wxArrayString strExtensions
;
1244 wxString strDesc
, strIcon
;
1246 wxMimeArrayString
*entry
= new wxMimeArrayString ();
1247 entry
->Add(verb
+ wxT("=") + cmd
+ wxT(" %s "));
1249 wxArrayString strTypes
;
1250 GetMimeTypes (strTypes
);
1251 if (strTypes
.GetCount() < 1) return FALSE
;
1255 for (i
= 0; i
< strTypes
.GetCount(); i
++)
1257 if (!m_manager
->DoAssociation (strTypes
[i
], strIcon
, entry
, strExtensions
, strDesc
))
1264 // ignore index on the grouds that we only have one icon in a Unix file
1265 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& strIcon
/*= wxEmptyString*/, int /*index = 0*/)
1267 if (strIcon
.IsEmpty()) return FALSE
;
1268 wxArrayString strExtensions
;
1271 wxMimeArrayString
*entry
= new wxMimeArrayString ();
1273 wxArrayString strTypes
;
1274 GetMimeTypes (strTypes
);
1275 if (strTypes
.GetCount() < 1) return FALSE
;
1279 for (i
= 0; i
< strTypes
.GetCount(); i
++)
1281 if (!m_manager
->DoAssociation (strTypes
[i
], strIcon
, entry
, strExtensions
, strDesc
))
1287 // ----------------------------------------------------------------------------
1288 // wxMimeTypesManagerImpl (Unix)
1289 // ----------------------------------------------------------------------------
1292 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1294 m_initialized
= FALSE
;
1295 m_mailcapStylesInited
= 0;
1298 // read system and user mailcaps and other files
1299 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles
,
1300 const wxString
& sExtraDir
)
1302 // read mimecap amd mime.types
1303 if ( (mailcapStyles
& wxMAILCAP_NETSCAPE
) ||
1304 (mailcapStyles
& wxMAILCAP_STANDARD
) )
1305 GetMimeInfo(sExtraDir
);
1307 // read GNOME tables
1308 if ( mailcapStyles
& wxMAILCAP_GNOME
)
1309 GetGnomeMimeInfo(sExtraDir
);
1312 if ( mailcapStyles
& wxMAILCAP_KDE
)
1313 GetKDEMimeInfo(sExtraDir
);
1315 m_mailcapStylesInited
|= mailcapStyles
;
1318 // clear data so you can read another group of WM files
1319 void wxMimeTypesManagerImpl::ClearData()
1323 m_aExtensions
.Clear ();
1324 m_aDescriptions
.Clear ();
1326 size_t cnt
= m_aTypes
.GetCount();
1327 for (size_t i
= 0; i
< cnt
; i
++)
1329 m_aEntries
[i
]->Clear ();
1331 m_aEntries
.Clear ();
1332 m_mailcapStylesInited
= 0;
1335 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
1337 ClearData(); // do we need to delete the ArrayStrings too to avoid a leak
1339 // delete m_aEntries //fix a leak here ?;
1343 void wxMimeTypesManagerImpl::GetMimeInfo (const wxString
& sExtraDir
)
1345 // read this for netscape or Metamail formats
1347 // directories where we look for mailcap and mime.types by default
1348 // used by netscape and pine and other mailers, using 2 different formats!
1350 // (taken from metamail(1) sources)
1352 // although RFC 1524 specifies the search path of
1353 // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more
1354 // places - OTOH, the RFC also says that this path can be changed with
1355 // MAILCAPS environment variable (containing the colon separated full
1356 // filenames to try) which is not done yet (TODO?)
1358 wxString strHome
= wxGetenv(wxT("HOME"));
1361 dirs
.Add ( wxT("/etc/") );
1362 dirs
.Add ( wxT("/usr/etc/") );
1363 dirs
.Add ( wxT("/usr/local/etc/") );
1364 dirs
.Add ( wxT("/etc/mail/") );
1365 dirs
.Add ( wxT("/usr/public/lib/") );
1366 dirs
.Add ( strHome
+ wxT("/.") );
1367 if (!sExtraDir
.IsEmpty()) dirs
.Add ( sExtraDir
+ wxT("/") );
1369 size_t nDirs
= dirs
.GetCount();
1370 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
1372 wxString file
= dirs
[nDir
] + wxT("mailcap");
1373 if ( wxFile::Exists(file
) ) {
1377 file
= dirs
[nDir
] + wxT("mime.types");
1378 if ( wxFile::Exists(file
) ) {
1379 ReadMimeTypes(file
);
1385 bool wxMimeTypesManagerImpl::WriteToMimeTypes (int index
, bool delete_index
)
1387 // check we have the right manager
1388 if (! ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) )
1392 wxString strHome
= wxGetenv(wxT("HOME"));
1394 // and now the users mailcap
1395 wxString strUserMailcap
= strHome
+ wxT("/.mime.types");
1397 wxMimeTextFile file
;
1398 if ( wxFile::Exists(strUserMailcap
) )
1400 bTemp
= file
.Open(strUserMailcap
);
1404 if (delete_index
) return FALSE
;
1405 bTemp
= file
.Create(strUserMailcap
);
1410 // test for netscape's header and return FALSE if its found
1411 nIndex
= file
.pIndexOf (wxT("#--Netscape"));
1412 if (nIndex
!= wxNOT_FOUND
)
1414 wxASSERT_MSG(FALSE
,wxT("Error in .mime.types \nTrying to mix Netscape and Metamail formats\nFile not modiifed"));
1417 // write it in alternative format
1418 // get rid of unwanted entries
1419 wxString strType
= m_aTypes
[index
];
1420 nIndex
= file
.pIndexOf (strType
);
1421 // get rid of all the unwanted entries...
1422 if (nIndex
!= wxNOT_FOUND
) file
.CommentLine (nIndex
);
1426 // add the new entries in
1427 wxString sTmp
= strType
.Append (wxT(' '), 40-strType
.Len() );
1428 sTmp
= sTmp
+ m_aExtensions
[index
];
1429 file
.AddLine (sTmp
);
1433 bTemp
= file
.Write ();
1439 bool wxMimeTypesManagerImpl::WriteToNSMimeTypes (int index
, bool delete_index
)
1441 //check we have the right managers
1442 if (! ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
) )
1446 wxString strHome
= wxGetenv(wxT("HOME"));
1448 // and now the users mailcap
1449 wxString strUserMailcap
= strHome
+ wxT("/.mime.types");
1451 wxMimeTextFile file
;
1452 if ( wxFile::Exists(strUserMailcap
) )
1454 bTemp
= file
.Open(strUserMailcap
);
1458 if (delete_index
) return FALSE
;
1459 bTemp
= file
.Create(strUserMailcap
);
1464 // write it in the format that Netscape uses
1466 // test for netscape's header and insert if required...
1467 // this is a comment so use TRUE
1468 nIndex
= file
.pIndexOf (wxT("#--Netscape"), TRUE
);
1469 if (nIndex
== wxNOT_FOUND
)
1471 // either empty file or metamail format
1472 // at present we can't cope with mixed formats, so exit to preseve
1473 // metamail entreies
1474 if (file
.GetLineCount () > 0)
1476 wxASSERT_MSG(FALSE
, wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap"));
1479 file
.InsertLine (wxT( "#--Netscape Communications Corporation MIME Information" ), 0);
1483 wxString strType
= wxT("type=") + m_aTypes
[index
];
1484 nIndex
= file
.pIndexOf (strType
);
1485 // get rid of all the unwanted entries...
1486 if (nIndex
!= wxNOT_FOUND
)
1488 wxString sOld
= file
[nIndex
];
1489 while ( (sOld
.Contains(wxT("\\"))) && (nIndex
< (int) file
.GetLineCount()) )
1491 file
.CommentLine(nIndex
);
1492 sOld
= file
[nIndex
];
1493 wxLogTrace(TRACE_MIME
, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex
, sOld
.c_str());
1496 if (nIndex
< (int) file
.GetLineCount()) file
.CommentLine (nIndex
);
1498 else nIndex
= (int) file
.GetLineCount();
1500 wxString sTmp
= strType
+ wxT(" \\");
1501 if (!delete_index
) file
.InsertLine (sTmp
, nIndex
);
1502 if ( ! m_aDescriptions
.Item(index
).IsEmpty() )
1504 sTmp
= wxT("desc=\"") + m_aDescriptions
[index
]+ wxT("\" \\") ; //.trim ??
1508 file
.InsertLine (sTmp
, nIndex
);
1511 wxString sExts
= m_aExtensions
.Item(index
);
1512 sTmp
= wxT("exts=\"") + sExts
.Trim(FALSE
).Trim() + wxT("\"");
1516 file
.InsertLine (sTmp
, nIndex
);
1519 bTemp
= file
.Write ();
1526 bool wxMimeTypesManagerImpl::WriteToMailCap (int index
, bool delete_index
)
1528 //check we have the right managers
1529 if ( !( ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
) ||
1530 ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) ) )
1534 wxString strHome
= wxGetenv(wxT("HOME"));
1536 // and now the users mailcap
1537 wxString strUserMailcap
= strHome
+ wxT("/.mailcap");
1539 wxMimeTextFile file
;
1540 if ( wxFile::Exists(strUserMailcap
) )
1542 bTemp
= file
.Open(strUserMailcap
);
1546 if (delete_index
) return FALSE
;
1547 bTemp
= file
.Create(strUserMailcap
);
1551 // now got a file we can write to ....
1552 wxMimeArrayString
* entries
= m_aEntries
[index
];
1553 size_t iOpen
= entries
->pIndexOf(wxT("open"));
1554 wxString sCmd
= entries
->GetCmd(iOpen
);
1557 sTmp
= m_aTypes
[index
];
1559 int nIndex
= file
.pIndexOf(sTmp
);
1560 // get rid of all the unwanted entries...
1561 if (nIndex
== wxNOT_FOUND
)
1563 nIndex
= (int) file
.GetLineCount();
1567 sOld
= file
[nIndex
];
1568 wxLogTrace(TRACE_MIME
, wxT("--- Deleting from mailcap line '%d' ---"), nIndex
);
1570 while ( (sOld
.Contains(wxT("\\"))) && (nIndex
< (int) file
.GetLineCount()) )
1572 file
.CommentLine(nIndex
);
1573 if (nIndex
< (int) file
.GetLineCount()) sOld
= sOld
+ file
[nIndex
];
1575 if (nIndex
< (int) file
.GetLineCount()) file
.CommentLine (nIndex
);
1578 sTmp
= sTmp
+ wxT(";") + sCmd
; //includes wxT(" %s ");
1580 // write it in the format that Netscape uses (default)
1581 if (! ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) )
1583 if (! delete_index
) file
.InsertLine (sTmp
, nIndex
);
1587 // write extended format
1590 // todo FIX this code;
1592 // sOld holds all the entries, but our data store only has some
1593 // eg test= is not stored
1595 // so far we have written the mimetype and command out
1596 wxStringTokenizer
sT (sOld
, wxT(";\\"));
1597 if (sT
.CountTokens () > 2)
1599 // first one mimetype; second one command, rest unknown...
1601 s
= sT
.GetNextToken();
1602 s
= sT
.GetNextToken();
1605 s
= sT
.GetNextToken();
1606 while ( ! s
.IsEmpty() )
1608 bool bKnownToken
= FALSE
;
1609 if (s
.Contains(wxT("description="))) bKnownToken
= TRUE
;
1610 if (s
.Contains(wxT("x11-bitmap="))) bKnownToken
= TRUE
;
1612 for (i
=0; i
< entries
->GetCount(); i
++)
1614 if (s
.Contains(entries
->GetVerb(i
))) bKnownToken
= TRUE
;
1618 sTmp
= sTmp
+ wxT("; \\");
1619 file
.InsertLine (sTmp
, nIndex
);
1622 s
= sT
.GetNextToken ();
1627 if (! m_aDescriptions
[index
].IsEmpty() )
1629 sTmp
= sTmp
+ wxT("; \\");
1630 file
.InsertLine (sTmp
, nIndex
);
1632 sTmp
= wxT(" description=\"") + m_aDescriptions
[index
] + wxT("\"");
1635 if (! m_aIcons
[index
].IsEmpty() )
1637 sTmp
= sTmp
+ wxT("; \\");
1638 file
.InsertLine (sTmp
, nIndex
);
1640 sTmp
= wxT(" x11-bitmap=\"") + m_aIcons
[index
] + wxT("\"");
1642 if ( entries
->GetCount() > 1 )
1646 for (i
=0; i
< entries
->GetCount(); i
++)
1649 sTmp
= sTmp
+ wxT("; \\");
1650 file
.InsertLine (sTmp
, nIndex
);
1652 sTmp
= wxT(" ") + entries
->Item(i
);
1656 file
.InsertLine (sTmp
, nIndex
);
1660 bTemp
= file
.Write ();
1667 wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
1671 wxString strType
= ftInfo
.GetMimeType ();
1672 wxString strDesc
= ftInfo
.GetDescription ();
1673 wxString strIcon
= ftInfo
.GetIconFile ();
1675 wxMimeArrayString
*entry
= new wxMimeArrayString ();
1677 if ( ! ftInfo
.GetOpenCommand().IsEmpty())
1678 entry
->Add(wxT("open=") + ftInfo
.GetOpenCommand () + wxT(" %s "));
1679 if ( ! ftInfo
.GetPrintCommand ().IsEmpty())
1680 entry
->Add(wxT("print=") + ftInfo
.GetPrintCommand () + wxT(" %s "));
1682 // now find where these extensions are in the data store and remove them
1683 wxArrayString sA_Exts
= ftInfo
.GetExtensions ();
1684 wxString sExt
, sExtStore
;
1686 for (i
=0; i
< sA_Exts
.GetCount(); i
++)
1688 sExt
= sA_Exts
.Item(i
);
1689 //clean up to just a space before and after
1690 sExt
.Trim().Trim(FALSE
);
1691 sExt
= wxT(' ') + sExt
+ wxT(' ');
1692 for (nIndex
= 0; nIndex
< m_aExtensions
.GetCount(); nIndex
++)
1694 sExtStore
= m_aExtensions
.Item(nIndex
);
1695 if (sExtStore
.Replace(sExt
, wxT(" ") ) > 0) m_aExtensions
.Item(nIndex
) = sExtStore
;
1700 if ( !DoAssociation (strType
, strIcon
, entry
, sA_Exts
, strDesc
) )
1703 return GetFileTypeFromMimeType(strType
);
1707 bool wxMimeTypesManagerImpl::DoAssociation(const wxString
& strType
,
1708 const wxString
& strIcon
,
1709 wxMimeArrayString
*entry
,
1710 const wxArrayString
& strExtensions
,
1711 const wxString
& strDesc
)
1713 int nIndex
= AddToMimeData(strType
, strIcon
, entry
, strExtensions
, strDesc
, TRUE
);
1715 if ( nIndex
== wxNOT_FOUND
)
1718 return WriteMimeInfo (nIndex
, FALSE
);
1721 bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex
, bool delete_mime
)
1725 if ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
)
1727 // write in metamail format;
1728 if (WriteToMimeTypes (nIndex
, delete_mime
) )
1729 if ( WriteToMailCap (nIndex
, delete_mime
) )
1732 if ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
)
1734 // write in netsacpe format;
1735 if (WriteToNSMimeTypes (nIndex
, delete_mime
) )
1736 if ( WriteToMailCap (nIndex
, delete_mime
) )
1739 if (m_mailcapStylesInited
& wxMAILCAP_GNOME
)
1741 // write in Gnome format;
1742 if (WriteGnomeMimeFile (nIndex
, delete_mime
) )
1743 if (WriteGnomeKeyFile (nIndex
, delete_mime
) )
1746 if (m_mailcapStylesInited
& wxMAILCAP_KDE
)
1748 // write in KDE format;
1749 if (WriteKDEMimeFile (nIndex
, delete_mime
) )
1756 int wxMimeTypesManagerImpl::AddToMimeData(const wxString
& strType
,
1757 const wxString
& strIcon
,
1758 wxMimeArrayString
*entry
,
1759 const wxArrayString
& strExtensions
,
1760 const wxString
& strDesc
,
1761 bool ReplaceExisting
)
1765 wxLogTrace(TRACE_MIME
, wxT("In Add to Mime data '%s' with %d entries and %d exts ---"),
1766 strType
.c_str(), entry
->GetCount(), strExtensions
.GetCount() );
1768 // ensure mimetype is always lower case
1769 wxString mimeType
= strType
;
1770 mimeType
.MakeLower();
1771 int nIndex
= m_aTypes
.Index(mimeType
);
1772 if ( nIndex
== wxNOT_FOUND
)
1775 m_aTypes
.Add(mimeType
);
1776 m_aIcons
.Add(strIcon
);
1777 m_aEntries
.Add(entry
);
1779 // change nIndex so we can add to the correct line
1780 nIndex
= m_aExtensions
.Add(wxT(' '));
1781 for (i
= 0; i
< strExtensions
.GetCount(); i
++)
1783 if (! m_aExtensions
.Item(nIndex
).Contains(wxT(' ') + strExtensions
.Item(i
) + wxT(' ')))
1784 m_aExtensions
.Item(nIndex
) += strExtensions
.Item(i
) + wxT(' ');
1786 m_aDescriptions
.Add(strDesc
);
1791 // nIndex has the existing data
1792 // always add the extensions to this mimetype
1794 for (i
= 0; i
< strExtensions
.GetCount(); i
++)
1796 if (! m_aExtensions
.Item(nIndex
).Contains(wxT(' ') + strExtensions
.Item(i
) + wxT(' ')))
1797 m_aExtensions
.Item(nIndex
) += strExtensions
.Item(i
) + wxT(' ');
1799 if (ReplaceExisting
)
1801 // if new description change it
1802 if ( ! strDesc
.IsEmpty())
1803 m_aDescriptions
[nIndex
] = strDesc
;
1805 // if new icon change it
1806 if ( ! strIcon
.IsEmpty())
1807 m_aIcons
[nIndex
] = strIcon
;
1809 wxMimeArrayString
*entryOld
= m_aEntries
[nIndex
];
1810 // replace any matching entries...
1811 for (i
=0; i
< entry
->GetCount(); i
++)
1812 entryOld
->ReplaceOrAddLineCmd (entry
->GetVerb(i
),
1813 entry
->GetCmd (i
) );
1817 // add data we don't already have ...
1818 // if new description add only if none
1819 if ( ! strDesc
.IsEmpty() && m_aDescriptions
.Item(i
).IsEmpty() )
1820 m_aDescriptions
[nIndex
] = strDesc
;
1822 // if new icon and no existing icon
1823 if ( ! strIcon
.IsEmpty() && m_aIcons
.Item(i
). IsEmpty () )
1824 m_aIcons
[nIndex
] = strIcon
;
1826 wxMimeArrayString
*entryOld
= m_aEntries
[nIndex
];
1827 // add any new entries...
1828 for (i
=0; i
< entry
->GetCount(); i
++)
1830 wxString sVerb
= entry
->GetVerb(i
);
1831 if ( entryOld
->pIndexOf ( sVerb
) == (size_t) wxNOT_FOUND
)
1832 entryOld
->Add (entry
->Item(i
));
1837 // check data integrity
1838 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1839 m_aTypes
.Count() == m_aExtensions
.Count() &&
1840 m_aTypes
.Count() == m_aIcons
.Count() &&
1841 m_aTypes
.Count() == m_aDescriptions
.Count() );
1848 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
1855 wxFileType
*fileType
= NULL
;
1856 size_t count
= m_aExtensions
.GetCount();
1857 for ( size_t n
= 0; n
< count
; n
++ )
1859 wxString extensions
= m_aExtensions
[n
];
1860 while ( !extensions
.IsEmpty() ) {
1861 wxString field
= extensions
.BeforeFirst(wxT(' '));
1862 extensions
= extensions
.AfterFirst(wxT(' '));
1864 // consider extensions as not being case-sensitive
1865 if ( field
.IsSameAs(ext
, FALSE
/* no case */) )
1868 if (fileType
== NULL
) fileType
= new wxFileType
;
1869 fileType
->m_impl
->Init(this, n
);
1870 // adds this mime type to _list_ of mime types with this extension
1879 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
1883 wxFileType
* fileType
= NULL
;
1884 // mime types are not case-sensitive
1885 wxString
mimetype(mimeType
);
1886 mimetype
.MakeLower();
1888 // first look for an exact match
1889 int index
= m_aTypes
.Index(mimetype
);
1890 if ( index
!= wxNOT_FOUND
)
1892 fileType
= new wxFileType
;
1893 fileType
->m_impl
->Init(this, index
);
1896 // then try to find "text/*" as match for "text/plain" (for example)
1897 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
1898 // the whole string - ok.
1900 index
= wxNOT_FOUND
;
1901 wxString strCategory
= mimetype
.BeforeFirst(wxT('/'));
1903 size_t nCount
= m_aTypes
.Count();
1904 for ( size_t n
= 0; n
< nCount
; n
++ ) {
1905 if ( (m_aTypes
[n
].BeforeFirst(wxT('/')) == strCategory
) &&
1906 m_aTypes
[n
].AfterFirst(wxT('/')) == wxT("*") ) {
1913 if ( index
!= wxNOT_FOUND
)
1915 fileType
= new wxFileType
;
1916 fileType
->m_impl
->Init(this, index
);
1922 wxString
wxMimeTypesManagerImpl::GetCommand(const wxString
& verb
, size_t nIndex
) const
1924 wxString command
, testcmd
, sV
, sTmp
;
1925 sV
= verb
+ wxT("=");
1926 // list of verb = command pairs for this mimetype
1927 wxMimeArrayString
* sPairs
= m_aEntries
[nIndex
];
1930 for ( i
= 0; i
< sPairs
->GetCount () ; i
++ )
1932 sTmp
= sPairs
->Item (i
);
1933 if ( sTmp
.Contains(sV
) ) command
= sTmp
.AfterFirst(wxT('='));
1938 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo
& filetype
)
1942 wxString extensions
;
1943 const wxArrayString
& exts
= filetype
.GetExtensions();
1944 size_t nExts
= exts
.GetCount();
1945 for ( size_t nExt
= 0; nExt
< nExts
; nExt
++ ) {
1947 extensions
+= wxT(' ');
1949 extensions
+= exts
[nExt
];
1952 AddMimeTypeInfo(filetype
.GetMimeType(),
1954 filetype
.GetDescription());
1956 AddMailcapInfo(filetype
.GetMimeType(),
1957 filetype
.GetOpenCommand(),
1958 filetype
.GetPrintCommand(),
1960 filetype
.GetDescription());
1963 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString
& strMimeType
,
1964 const wxString
& strExtensions
,
1965 const wxString
& strDesc
)
1967 // reading mailcap may find image/* , while
1968 // reading mime.types finds image/gif and no match is made
1969 // this means all the get functions don't work fix this
1971 wxString sTmp
= strExtensions
;
1972 wxMimeArrayString
* entry
= new wxMimeArrayString () ;
1974 wxArrayString sExts
;
1975 sTmp
.Trim().Trim(FALSE
);
1977 while (!sTmp
.IsEmpty())
1979 sExts
.Add (sTmp
.AfterLast(wxT(' ')));
1980 sTmp
= sTmp
.BeforeLast(wxT(' '));
1983 AddToMimeData (strMimeType
, strIcon
, entry
, sExts
, strDesc
, (bool)TRUE
);
1986 void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString
& strType
,
1987 const wxString
& strOpenCmd
,
1988 const wxString
& strPrintCmd
,
1989 const wxString
& strTest
,
1990 const wxString
& strDesc
)
1994 wxMimeArrayString
*entry
= new wxMimeArrayString
;
1995 entry
->Add(wxT("open=") + strOpenCmd
);
1996 entry
->Add(wxT("print=") + strPrintCmd
);
1997 entry
->Add(wxT("test=") + strTest
);
2000 wxArrayString strExtensions
;
2002 AddToMimeData (strType
, strIcon
, entry
, strExtensions
, strDesc
, TRUE
);
2006 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString
& strFileName
)
2008 wxLogTrace(TRACE_MIME
, wxT("--- Parsing mime.types file '%s' ---"),
2009 strFileName
.c_str());
2011 wxTextFile
file(strFileName
);
2015 // the information we extract
2016 wxString strMimeType
, strDesc
, strExtensions
;
2018 size_t nLineCount
= file
.GetLineCount();
2019 const wxChar
*pc
= NULL
;
2020 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
2022 // now we're at the start of the line
2023 pc
= file
[nLine
].c_str();
2026 // we didn't finish with the previous line yet
2031 while ( wxIsspace(*pc
) )
2034 // comment or blank line?
2035 if ( *pc
== wxT('#') || !*pc
) {
2036 // skip the whole line
2041 // detect file format
2042 const wxChar
*pEqualSign
= wxStrchr(pc
, wxT('='));
2043 if ( pEqualSign
== NULL
) {
2047 // first field is mime type
2048 for ( strMimeType
.Empty(); !wxIsspace(*pc
) && *pc
!= wxT('\0'); pc
++ ) {
2053 while ( wxIsspace(*pc
) )
2056 // take all the rest of the string
2059 // no description...
2066 // the string on the left of '=' is the field name
2067 wxString
strLHS(pc
, pEqualSign
- pc
);
2070 for ( pc
= pEqualSign
+ 1; wxIsspace(*pc
); pc
++ )
2074 if ( *pc
== wxT('"') ) {
2075 // the string is quoted and ends at the matching quote
2076 pEnd
= wxStrchr(++pc
, wxT('"'));
2077 if ( pEnd
== NULL
) {
2078 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
2080 strFileName
.c_str(), nLine
+ 1);
2084 // unquoted string ends at the first space or at the end of
2086 for ( pEnd
= pc
; *pEnd
&& !wxIsspace(*pEnd
); pEnd
++ )
2090 // now we have the RHS (field value)
2091 wxString
strRHS(pc
, pEnd
- pc
);
2093 // check what follows this entry
2094 if ( *pEnd
== wxT('"') ) {
2099 for ( pc
= pEnd
; wxIsspace(*pc
); pc
++ )
2102 // if there is something left, it may be either a '\\' to continue
2103 // the line or the next field of the same entry
2104 bool entryEnded
= *pc
== wxT('\0'),
2105 nextFieldOnSameLine
= FALSE
;
2106 if ( !entryEnded
) {
2107 nextFieldOnSameLine
= ((*pc
!= wxT('\\')) || (pc
[1] != wxT('\0')));
2110 // now see what we got
2111 if ( strLHS
== wxT("type") ) {
2112 strMimeType
= strRHS
;
2114 else if ( strLHS
== wxT("desc") ) {
2117 else if ( strLHS
== wxT("exts") ) {
2118 strExtensions
= strRHS
;
2121 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
2122 strFileName
.c_str(), nLine
+ 1, strLHS
.c_str());
2125 if ( !entryEnded
) {
2126 if ( !nextFieldOnSameLine
)
2128 //else: don't reset it
2130 // as we don't reset strMimeType, the next field in this entry
2131 // will be interpreted correctly.
2137 // depending on the format (Mosaic or Netscape) either space or comma
2138 // is used to separate the extensions
2139 strExtensions
.Replace(wxT(","), wxT(" "));
2141 // also deal with the leading dot
2142 if ( !strExtensions
.IsEmpty() && strExtensions
[0u] == wxT('.') )
2144 strExtensions
.erase(0, 1);
2147 wxLogTrace(TRACE_MIME
, wxT("--- Found Mimetype '%s' ---"),
2148 strMimeType
.c_str());
2150 AddMimeTypeInfo(strMimeType
, strExtensions
, strDesc
);
2152 // finished with this line
2159 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString
& strFileName
,
2162 // wxLog::AddTraceMask (TRACE_MIME);
2163 wxLogTrace(TRACE_MIME
, wxT("--- Parsing mailcap file '%s' ---"),
2164 strFileName
.c_str());
2166 wxTextFile
file(strFileName
);
2170 // see the comments near the end of function for the reason we need these
2171 // variables (search for the next occurence of them)
2172 // indices of MIME types (in m_aTypes) we already found in this file
2173 wxArrayInt aEntryIndices
;
2174 // aLastIndices[n] is the index of last element in
2175 // m_aEntries[aEntryIndices[n]] from this file
2176 // wxArrayInt aLastIndices;
2178 size_t nLineCount
= file
.GetLineCount();
2179 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
2180 // now we're at the start of the line
2181 const wxChar
*pc
= file
[nLine
].c_str();
2184 while ( wxIsspace(*pc
) )
2187 // comment or empty string?
2188 if ( *pc
== wxT('#') || *pc
== wxT('\0') )
2193 // what field are we currently in? The first 2 are fixed and there may
2194 // be an arbitrary number of other fields -- currently, we are not
2195 // interested in any of them, but we should parse them as well...
2201 } currentToken
= Field_Type
;
2203 // the flags and field values on the current line
2204 bool needsterminal
= FALSE
,
2205 copiousoutput
= FALSE
;
2206 wxMimeArrayString
*entry
;
2213 curField
; // accumulator
2215 bool test_passed
= TRUE
;
2219 // interpret the next character literally (notice that
2220 // backslash can be used for line continuation)
2221 if ( *++pc
== wxT('\0') ) {
2222 // fetch the next line if there is one
2223 if ( nLine
== nLineCount
- 1 ) {
2224 // something is wrong, bail out
2227 wxLogDebug(wxT("Mailcap file %s, line %d: "
2228 "'\\' on the end of the last line "
2230 strFileName
.c_str(),
2234 // pass to the beginning of the next line
2235 pc
= file
[++nLine
].c_str();
2237 // skip pc++ at the end of the loop
2242 // just a normal character
2248 cont
= FALSE
; // end of line reached, exit the loop
2253 // store this field and start looking for the next one
2255 // trim whitespaces from both sides
2256 curField
.Trim(TRUE
).Trim(FALSE
);
2258 switch ( currentToken
) {
2261 if ( strType
.empty() ) {
2262 // I don't think that this is a valid mailcap
2263 // entry, but try to interpret it somehow
2267 if ( strType
.Find(wxT('/')) == wxNOT_FOUND
) {
2268 // we interpret "type" as "type/*"
2269 strType
+= wxT("/*");
2272 currentToken
= Field_OpenCmd
;
2276 strOpenCmd
= curField
;
2277 entry
= new wxMimeArrayString ();
2278 entry
->Add(wxT("open=") + strOpenCmd
);
2280 currentToken
= Field_Other
;
2284 if ( !curField
.empty() ) {
2285 // "good" mailcap entry?
2288 if ( IsKnownUnimportantField(curField
) ) ok
= FALSE
;
2290 // is this something of the form foo=bar?
2291 const wxChar
*pEq
= wxStrchr(curField
, wxT('='));
2296 wxString lhs
= curField
.BeforeFirst(wxT('=')),
2297 rhs
= curField
.AfterFirst(wxT('='));
2299 lhs
.Trim(TRUE
); // from right
2300 rhs
.Trim(FALSE
); // from left
2302 // it might be quoted
2303 if ( rhs
[0u] == wxT('"') && rhs
.Last() == wxT('"') )
2305 wxString sTmp
= wxString(rhs
.c_str() + 1, rhs
.Len() - 2);
2308 bool verbfound
= TRUE
;
2309 if ( lhs
.Contains (wxT("test")))
2311 if ( ! rhs
.IsEmpty() )
2313 if ( wxSystem(rhs
) == 0 ) {
2316 wxLogTrace(TRACE_MIME
,
2317 wxT("Test '%s' for mime type '%s' succeeded."),
2318 rhs
.c_str(), strType
.c_str());
2322 test_passed
= FALSE
;
2323 wxLogTrace(TRACE_MIME
,
2324 wxT("Test '%s' for mime type '%s' failed."),
2325 rhs
.c_str(), strType
.c_str());
2330 if ( lhs
.Contains (wxT("desc")))
2335 if ( lhs
.Contains (wxT("x11-bitmap")))
2340 if ( lhs
.Contains (wxT("notes")))
2345 if (verbfound
) entry
->Add ( lhs
+ wxT('=') + rhs
);
2350 // no, it's a simple flag
2351 if ( curField
== wxT("needsterminal") ) {
2352 needsterminal
= TRUE
;
2355 if ( curField
== wxT("copiousoutput")) {
2356 // copiousoutput impies that the
2357 // viewer is a console program
2364 // don't flood the user with error
2365 // messages if we don't understand
2366 // something in his mailcap, but give
2367 // them in debug mode because this might
2368 // be useful for the programmer
2371 wxT("Mailcap file %s, line %d: "
2372 "unknown field '%s' for the "
2373 "MIME type '%s' ignored."),
2374 strFileName
.c_str(),
2391 // it already has this value
2392 //currentToken = Field_Other;
2396 wxFAIL_MSG(wxT("unknown field type in mailcap"));
2399 // next token starts immediately after ';'
2407 // continue in the same line
2411 // check that we really read something reasonable
2412 if ( currentToken
== Field_Type
|| currentToken
== Field_OpenCmd
) {
2413 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
2415 strFileName
.c_str(), nLine
+ 1);
2418 // support for flags:
2419 // 1. create an xterm for 'needsterminal'
2420 // 2. append "| $PAGER" for 'copiousoutput'
2422 // Note that the RFC says that having both needsterminal and
2423 // copiousoutput is probably a mistake, so it seems that running
2424 // programs with copiousoutput inside an xterm as it is done now
2425 // is a bad idea (FIXME)
2426 if ( copiousoutput
)
2428 const wxChar
*p
= wxGetenv(_T("PAGER"));
2429 strOpenCmd
<< _T(" | ") << (p
? p
: _T("more"));
2430 wxLogTrace(TRACE_MIME
, wxT("Replacing .(for pager)...") + entry
->Item(0u) + wxT("with") + strOpenCmd
);
2432 entry
->ReplaceOrAddLineCmd (wxString(wxT("open")), strOpenCmd
);
2435 if ( needsterminal
)
2437 strOpenCmd
.Printf(_T("xterm -e sh -c '%s'"), strOpenCmd
.c_str());
2438 wxLogTrace(TRACE_MIME
, wxT("Replacing .(for needs term)...") + entry
->Item(0u) + wxT("with") + strOpenCmd
);
2440 entry
->ReplaceOrAddLineCmd (wxString(wxT("open")), strOpenCmd
);
2443 // NB: because of complications below (we must get entries priority
2444 // right), we can't use AddMailcapInfo() here, unfortunately.
2447 strType
.MakeLower();
2448 bool overwrite
= TRUE
;
2454 int nIndex
= m_aTypes
.Index(strType
);
2455 entryIndex
= aEntryIndices
.Index(nIndex
);
2456 if ( entryIndex
== wxNOT_FOUND
)
2459 // first time in this file, so replace the icons, entries
2460 // and description (no extensions to replace so ignore these
2462 aEntryIndices
.Add(nIndex
);
2463 //aLastIndices.Add(0);
2466 // not the first time in _this_ file
2467 // so we don't want to overwrite
2468 // existing entries,but want to add to them
2469 // so we don't alter the mimetype
2470 // the indices were shifted by 1
2476 wxArrayString strExtensions
;
2477 AddToMimeData (strType
, strIcon
, entry
, strExtensions
, strDesc
, !overwrite
);
2487 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
2494 size_t count
= m_aTypes
.GetCount();
2495 for ( size_t n
= 0; n
< count
; n
++ )
2497 // don't return template types from here (i.e. anything containg '*')
2499 if ( type
.Find(_T('*')) == wxNOT_FOUND
)
2501 mimetypes
.Add(type
);
2505 return mimetypes
.GetCount();
2508 // ----------------------------------------------------------------------------
2509 // writing to MIME type files
2510 // ----------------------------------------------------------------------------
2512 bool wxMimeTypesManagerImpl::Unassociate(wxFileType
*ft
)
2514 wxArrayString sMimeTypes
;
2515 ft
->GetMimeTypes (sMimeTypes
);
2519 for (i
= 0; i
< sMimeTypes
.GetCount(); i
++)
2521 sMime
= sMimeTypes
.Item(i
);
2522 int nIndex
= m_aTypes
.Index (sMime
);
2523 if ( nIndex
== wxNOT_FOUND
)
2525 // error if we get here ??
2530 WriteMimeInfo(nIndex
, TRUE
);
2531 m_aTypes
.Remove (nIndex
);
2532 m_aEntries
.Remove (nIndex
);
2533 m_aExtensions
.Remove (nIndex
);
2534 m_aDescriptions
.Remove (nIndex
);
2535 m_aIcons
.Remove (nIndex
);
2538 // check data integrity
2539 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
2540 m_aTypes
.Count() == m_aExtensions
.Count() &&
2541 m_aTypes
.Count() == m_aIcons
.Count() &&
2542 m_aTypes
.Count() == m_aDescriptions
.Count() );
2547 // ----------------------------------------------------------------------------
2548 // private functions
2549 // ----------------------------------------------------------------------------
2551 static bool IsKnownUnimportantField(const wxString
& fieldAll
)
2553 static const wxChar
*knownFields
[] =
2555 _T("x-mozilla-flags"),
2557 _T("textualnewlines"),
2560 wxString field
= fieldAll
.BeforeFirst(_T('='));
2561 for ( size_t n
= 0; n
< WXSIZEOF(knownFields
); n
++ )
2563 if ( field
.CmpNoCase(knownFields
[n
]) == 0 )
2571 // wxUSE_FILE && wxUSE_TEXTFILE