1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/mimetype.cpp
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
12 // known bugs; there may be others!! chris elliott, biol75@york.ac.uk 27 Mar 01
14 // 1) .mailcap and .mimetypes can be either in a netscape or metamail format
15 // and entries may get confused during writing (I've tried to fix this; please let me know
16 // any files that fail)
17 // 2) KDE and Gnome do not yet fully support international read/write
18 // 3) Gnome key lines like open.latex."LaTeX this file"=latex %f will have odd results
19 // 4) writing to files comments out the existing data; I hope this avoids losing
20 // any data which we could not read, and data which we did not store like test=
21 // 5) results from reading files with multiple entries (especially matches with type/* )
22 // may (or may not) work for getXXX commands
23 // 6) Loading the png icons in Gnome doesn't work for me...
24 // 7) In Gnome, if keys.mime exists but keys.users does not, there is
25 // an error message in debug mode, but the file is still written OK
26 // 8) Deleting entries is only allowed from the user file; sytem wide entries
27 // will be preserved during unassociate
28 // 9) KDE does not yet handle multiple actions; Netscape mode never will
31 TODO: this file is a mess, we need to split it and reformet/review
35 // ============================================================================
37 // ============================================================================
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
44 #pragma implementation "mimetype.h"
47 // for compilers that support precompilation, includes "wx.h".
48 #include "wx/wxprec.h"
58 #if wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE
61 #include "wx/string.h"
72 #include "wx/dynarray.h"
73 #include "wx/confbase.h"
76 #include "wx/textfile.h"
79 #include "wx/tokenzr.h"
80 #include "wx/iconloc.h"
82 #include "wx/unix/mimetype.h"
84 // other standard headers
88 /* silence warnings for comparing unsigned int's <0 */
89 # pragma message disable unscomzer
92 // wxMimeTypeCommands stores the verbs defined for the given MIME type with
94 class wxMimeTypeCommands
97 wxMimeTypeCommands() { }
99 wxMimeTypeCommands(const wxArrayString
& verbs
,
100 const wxArrayString
& commands
)
106 // add a new verb with the command or replace the old value
107 void AddOrReplaceVerb(const wxString
& verb
, const wxString
& cmd
)
109 int n
= m_verbs
.Index(verb
, FALSE
/* ignore case */);
110 if ( n
== wxNOT_FOUND
)
121 void Add(const wxString
& s
)
123 m_verbs
.Add(s
.BeforeFirst(_T('=')));
124 m_commands
.Add(s
.AfterFirst(_T('=')));
127 // access the commands
128 size_t GetCount() const { return m_verbs
.GetCount(); }
129 const wxString
& GetVerb(size_t n
) const { return m_verbs
[n
]; }
130 const wxString
& GetCmd(size_t n
) const { return m_commands
[n
]; }
132 bool HasVerb(const wxString
& verb
) const
133 { return m_verbs
.Index(verb
) != wxNOT_FOUND
; }
135 wxString
GetCommandForVerb(const wxString
& verb
, size_t *idx
= NULL
) const
139 int n
= m_verbs
.Index(verb
);
140 if ( n
!= wxNOT_FOUND
)
142 s
= m_commands
[(size_t)n
];
150 // get a "verb=command" string
151 wxString
GetVerbCmd(size_t n
) const
153 return m_verbs
[n
] + _T('=') + m_commands
[n
];
157 wxArrayString m_verbs
,
161 // this class extends wxTextFile
164 class wxMimeTextFile
: public wxTextFile
168 wxMimeTextFile () : wxTextFile () {};
169 wxMimeTextFile (const wxString
& strFile
) : wxTextFile (strFile
) { };
171 int pIndexOf(const wxString
& sSearch
, bool bIncludeComments
= FALSE
, int iStart
= 0)
174 int nResult
= wxNOT_FOUND
;
175 if (i
>=GetLineCount()) return wxNOT_FOUND
;
177 wxString sTest
= sSearch
;
181 if (bIncludeComments
)
183 while ( (i
< GetLineCount()) )
187 if (sLine
.Contains(sTest
)) nResult
= (int) i
;
193 while ( (i
< GetLineCount()) )
197 if ( ! sLine
.StartsWith(wxT("#")))
199 if (sLine
.Contains(sTest
)) nResult
= (int) i
;
207 bool CommentLine(int nIndex
)
209 if (nIndex
<0) return FALSE
;
210 if (nIndex
>= (int)GetLineCount() ) return FALSE
;
211 GetLine(nIndex
) = GetLine(nIndex
).Prepend(wxT("#"));
215 bool CommentLine(const wxString
& sTest
)
217 int nIndex
= pIndexOf(sTest
);
218 if (nIndex
<0) return FALSE
;
219 if (nIndex
>= (int)GetLineCount() ) return FALSE
;
220 GetLine(nIndex
) = GetLine(nIndex
).Prepend(wxT("#"));
224 wxString
GetVerb (size_t i
)
226 if (i
> GetLineCount() ) return wxEmptyString
;
227 wxString sTmp
= GetLine(i
).BeforeFirst(wxT('='));
231 wxString
GetCmd (size_t i
)
233 if (i
> GetLineCount() ) return wxEmptyString
;
234 wxString sTmp
= GetLine(i
).AfterFirst(wxT('='));
239 // in case we're compiling in non-GUI mode
240 class WXDLLEXPORT wxIcon
;
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 // MIME code tracing mask
247 #define TRACE_MIME _T("mime")
249 // give trace messages about the results of mailcap tests
250 #define TRACE_MIME_TEST _T("mimetest")
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 // there are some fields which we don't understand but for which we don't give
257 // warnings as we know that they're not important - this function is used to
259 static bool IsKnownUnimportantField(const wxString
& field
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
266 // This class uses both mailcap and mime.types to gather information about file
269 // The information about mailcap file was extracted from metamail(1) sources
270 // and documentation and subsequently revised when I found the RFC 1524
273 // Format of mailcap file: spaces are ignored, each line is either a comment
274 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
275 // A backslash can be used to quote semicolons and newlines (and, in fact,
276 // anything else including itself).
278 // The first field is always the MIME type in the form of type/subtype (see RFC
279 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
280 // "type" which means the same as "type/*", although I'm not sure whether this
283 // The second field is always the command to run. It is subject to
284 // parameter/filename expansion described below.
286 // All the following fields are optional and may not be present at all. If
287 // they're present they may appear in any order, although each of them should
288 // appear only once. The optional fields are the following:
289 // * notes=xxx is an uninterpreted string which is silently ignored
290 // * test=xxx is the command to be used to determine whether this mailcap line
291 // applies to our data or not. The RHS of this field goes through the
292 // parameter/filename expansion (as the 2nd field) and the resulting string
293 // is executed. The line applies only if the command succeeds, i.e. returns 0
295 // * print=xxx is the command to be used to print (and not view) the data of
296 // this type (parameter/filename expansion is done here too)
297 // * edit=xxx is the command to open/edit the data of this type
298 // * needsterminal means that a new interactive console must be created for
300 // * copiousoutput means that the viewer doesn't interact with the user but
301 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
302 // good example), thus it might be a good idea to use some kind of paging
304 // * textualnewlines means not to perform CR/LF translation (not honored)
305 // * compose and composetyped fields are used to determine the program to be
306 // called to create a new message pert in the specified format (unused).
308 // Parameter/filename expansion:
309 // * %s is replaced with the (full) file name
310 // * %t is replaced with MIME type/subtype of the entry
311 // * for multipart type only %n is replaced with the nnumber of parts and %F is
312 // replaced by an array of (content-type, temporary file name) pairs for all
313 // message parts (TODO)
314 // * %{parameter} is replaced with the value of parameter taken from
315 // Content-type header line of the message.
318 // There are 2 possible formats for mime.types file, one entry per line (used
319 // for global mime.types and called Mosaic format) and "expanded" format where
320 // an entry takes multiple lines (used for users mime.types and called
323 // For both formats spaces are ignored and lines starting with a '#' are
324 // comments. Each record has one of two following forms:
325 // a) for "brief" format:
326 // <mime type> <space separated list of extensions>
327 // b) for "expanded" format:
328 // type=<mime type> BACKSLASH
329 // desc="<description>" BACKSLASH
330 // exts="<comma separated list of extensions>"
332 // (where BACKSLASH is a literal '\\' which we can't put here because cpp
335 // We try to autodetect the format of mime.types: if a non-comment line starts
336 // with "type=" we assume the second format, otherwise the first one.
338 // there may be more than one entry for one and the same mime type, to
339 // choose the right one we have to run the command specified in the test
340 // field on our data.
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 // GNOME stores the info we're interested in in several locations:
347 // 1. xxx.keys files under /usr/share/mime-info
348 // 2. xxx.keys files under ~/.gnome/mime-info
350 // The format of xxx.keys file is the following:
355 // with blank lines separating the entries and indented lines starting with
356 // TABs. We're interested in the field icon-filename whose value is the path
357 // containing the icon.
359 // Update (Chris Elliott): apparently there may be an optional "[lang]" prefix
360 // just before the field name.
363 bool wxMimeTypesManagerImpl::CheckGnomeDirsExist ()
366 wxGetHomeDir( &gnomedir
);
367 wxString sTmp
= gnomedir
;
368 sTmp
= sTmp
+ wxT("/.gnome");
369 if (! wxDir::Exists ( sTmp
) )
371 if (!wxMkdir ( sTmp
))
373 wxLogError(_("Failed to create directory %s/.gnome."), sTmp
.c_str());
377 sTmp
= sTmp
+ wxT("/mime-info");
378 if (! wxDir::Exists ( sTmp
) )
380 if (!wxMkdir ( sTmp
))
382 wxLogError(_("Failed to create directory %s/mime-info."), sTmp
.c_str());
392 bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index
, bool delete_index
)
395 wxGetHomeDir( &gnomedir
);
397 wxMimeTextFile
outfile ( gnomedir
+ wxT("/.gnome/mime-info/user.keys"));
398 // if this fails probably Gnome is not installed ??
399 // create it anyway as a private mime store
401 #if defined(__WXGTK20__) && wxUSE_UNICODE
402 if (! outfile
.Open ( wxConvUTF8
) )
404 if (! outfile
.Open () )
407 if (delete_index
) return FALSE
;
408 if (!CheckGnomeDirsExist() ) return FALSE
;
412 wxString sTmp
, strType
= m_aTypes
[index
];
413 int nIndex
= outfile
.pIndexOf(strType
);
414 if ( nIndex
== wxNOT_FOUND
)
416 outfile
.AddLine ( strType
+ wxT(':') );
417 // see file:/usr/doc/gnome-libs-devel-1.0.40/devel-docs/mime-type-handling.txt
418 // as this does not deal with internationalisation
419 // wxT( "\t[en_US]") + verb + wxT ('=') + cmd + wxT(" %f");
420 wxMimeTypeCommands
* entries
= m_aEntries
[index
];
421 size_t count
= entries
->GetCount();
422 for ( size_t i
= 0; i
< count
; i
++ )
424 sTmp
= entries
->GetVerbCmd(i
);
425 sTmp
.Replace( wxT("%s"), wxT("%f") );
426 sTmp
= wxT ( "\t") + sTmp
;
427 outfile
.AddLine ( sTmp
);
429 //for international use do something like this
430 //outfile.AddLine ( wxString( "\t[en_US]icon-filename=") + cmd );
431 outfile
.AddLine ( wxT( "\ticon-filename=") + m_aIcons
[index
] );
436 outfile
.CommentLine(nIndex
);
438 wxMimeTypeCommands sOld
;
439 size_t nOld
= nIndex
+ 1;
440 bool oldEntryEnd
= FALSE
;
441 while ( (nOld
< outfile
.GetLineCount() )&& (oldEntryEnd
== FALSE
))
443 sTmp
= outfile
.GetLine(nOld
);
444 if ( (sTmp
[0u] == wxT('\t')) || (sTmp
[0u] == wxT('#')) )
446 // we have another line to deal with
447 outfile
.CommentLine(nOld
);
449 // add the line to our store
450 if ((!delete_index
) && (sTmp
[0u] == wxT('\t')))
453 // next mimetpye ??or blank line
457 // list of entries in our data; these should all be in sOld,
458 // though sOld may also contain other entries , eg flags
461 wxMimeTypeCommands
* entries
= m_aEntries
[index
];
463 for (i
=0; i
< entries
->GetCount(); i
++)
465 // replace any entries in sold that match verbs we know
466 sOld
.AddOrReplaceVerb ( entries
->GetVerb(i
), entries
->GetCmd (i
) );
468 //sOld should also contain the icon
469 if ( !m_aIcons
[index
].empty() )
470 sOld
.AddOrReplaceVerb ( wxT("icon-filename"), m_aIcons
[index
] );
472 for (i
=0; i
< sOld
.GetCount(); i
++)
474 sTmp
= sOld
.GetVerbCmd(i
);
475 sTmp
.Replace( wxT("%s"), wxT("%f") );
476 sTmp
= wxT("\t") + sTmp
;
478 outfile
.InsertLine ( sTmp
, nIndex
);
482 bool bTmp
= outfile
.Write ();
487 bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index
, bool delete_index
)
490 wxGetHomeDir( &gnomedir
);
492 wxMimeTextFile
outfile ( gnomedir
+ wxT("/.gnome/mime-info/user.mime"));
493 // if this fails probably Gnome is not installed ??
494 // create it anyway as a private mime store
495 if (! outfile
.Open () )
497 if (delete_index
) return FALSE
;
498 if (!CheckGnomeDirsExist() ) return FALSE
;
501 wxString strType
= m_aTypes
[index
];
502 int nIndex
= outfile
.pIndexOf(strType
);
503 if ( nIndex
== wxNOT_FOUND
)
505 outfile
.AddLine ( strType
);
506 outfile
.AddLine ( wxT("\text:") + m_aExtensions
.Item(index
) );
512 outfile
.CommentLine(nIndex
);
513 outfile
.CommentLine(nIndex
+1);
516 {// check for next line being the right one to replace ??
517 wxString sOld
= outfile
.GetLine(nIndex
+1);
518 if (sOld
.Contains( wxT("\text: ")))
520 outfile
.GetLine(nIndex
+1) = wxT("\text: ") + m_aExtensions
.Item(index
);
524 outfile
.InsertLine( wxT("\text: ") + m_aExtensions
.Item(index
), nIndex
+ 1 );
528 bool bTmp
= outfile
.Write ();
533 void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString
& filename
,
534 const wxArrayString
& dirs
)
536 wxTextFile
textfile(filename
);
537 #if defined(__WXGTK20__) && wxUSE_UNICODE
538 if ( !textfile
.Open( wxConvUTF8
) )
540 if ( !textfile
.Open() )
543 wxLogTrace(TRACE_MIME
, wxT("--- Opened Gnome file %s ---"),
546 // values for the entry being parsed
547 wxString curMimeType
, curIconFile
;
548 wxMimeTypeCommands
* entry
= new wxMimeTypeCommands
;
550 // these are always empty in this file
551 wxArrayString strExtensions
;
555 size_t nLineCount
= textfile
.GetLineCount();
557 while ( nLine
< nLineCount
)
559 pc
= textfile
[nLine
].c_str();
560 if ( *pc
!= _T('#') )
563 wxLogTrace(TRACE_MIME
, wxT("--- Reading from Gnome file %s '%s' ---"),
564 filename
.c_str(),pc
);
567 if (sTmp
.Contains(wxT("=")) )
570 if (sTmp
.Contains( wxT("icon-filename=") ) )
572 curIconFile
= sTmp
.AfterFirst(wxT('='));
575 else if (sTmp
.Contains( wxT("icon_filename=") ) )
577 curIconFile
= sTmp
.AfterFirst(wxT('='));
578 if (!wxFileExists(curIconFile
))
580 size_t nDirs
= dirs
.GetCount();
581 for (size_t nDir
= 0; nDir
< nDirs
; nDir
++)
584 newFile
.Printf(wxT("%s/pixmaps/document-icons/%s.png"),
586 curIconFile
.c_str());
587 if (wxFileExists(newFile
))
588 curIconFile
= newFile
;
592 else //: some other field,
594 //may contain lines like this (RH7)
595 // \t[lang]open.tex."TeX this file"=tex %f
596 // \tflags.tex.flags=needsterminal
597 // \topen.latex."LaTeX this file"=latex %f
598 // \tflags.latex.flags=needsterminal
602 // \topen.convert.Convert file to Postscript=dvips %f -o `basename %f .dvi`.ps
604 // for now ignore lines with flags in...FIX
605 sTmp
= sTmp
.AfterLast(wxT(']'));
606 sTmp
= sTmp
.AfterLast(wxT('\t'));
607 sTmp
.Trim(FALSE
).Trim();
608 if (0 == sTmp
.Replace ( wxT("%f"), wxT("%s") )) sTmp
= sTmp
+ wxT(" %s");
613 } // emd of has an equals sign
616 // not a comment and not an equals sign
617 if (sTmp
.Contains(wxT('/')))
619 // this is the start of the new mimetype
620 // overwrite any existing data
621 if (! curMimeType
.empty())
623 AddToMimeData ( curMimeType
, curIconFile
, entry
, strExtensions
, strDesc
);
625 // now get ready for next bit
626 entry
= new wxMimeTypeCommands
;
628 curMimeType
= sTmp
.BeforeFirst(wxT(':'));
631 } // end of not a comment
632 // ignore blank lines
634 } // end of while, save any data
635 if (! curMimeType
.empty())
637 AddToMimeData ( curMimeType
, curIconFile
, entry
, strExtensions
, strDesc
);
644 void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString
& filename
)
646 wxTextFile
textfile(filename
);
647 #if defined(__WXGTK20__) && wxUSE_UNICODE
648 if ( !textfile
.Open( wxConvUTF8
) )
650 if ( !textfile
.Open() )
653 wxLogTrace(TRACE_MIME
, wxT("--- Opened Gnome file %s ---"),
656 // values for the entry being parsed
657 wxString curMimeType
, curExtList
;
660 size_t nLineCount
= textfile
.GetLineCount();
661 for ( size_t nLine
= 0;; nLine
++ )
663 if ( nLine
< nLineCount
)
665 pc
= textfile
[nLine
].c_str();
666 if ( *pc
== wxT('#') )
674 // so that we will fall into the "if" below
681 if ( !!curMimeType
&& !!curExtList
)
683 wxLogTrace(TRACE_MIME
, wxT("--- At end of Gnome file finding mimetype %s ---"),
684 curMimeType
.c_str());
686 AddMimeTypeInfo(curMimeType
, curExtList
, wxEmptyString
);
691 // the end - this can only happen if nLine == nLineCount
700 // what do we have here?
701 if ( *pc
== wxT('\t') )
703 // this is a field=value ling
704 pc
++; // skip leading TAB
706 static const int lenField
= 4; // strlen("ext:")
707 if ( wxStrncmp(pc
, wxT("ext:"), lenField
) == 0 )
709 // skip ' ' which follows and take everything left until the end
711 curExtList
= pc
+ lenField
+ 1;
713 //else: some other field, we don't care
717 // this is the start of the new section
718 wxLogTrace(TRACE_MIME
, wxT("--- In Gnome file finding mimetype %s ---"),
719 curMimeType
.c_str());
721 if (! curMimeType
.empty())
722 AddMimeTypeInfo(curMimeType
, curExtList
, wxEmptyString
);
726 while ( *pc
!= wxT(':') && *pc
!= wxT('\0') )
728 curMimeType
+= *pc
++;
735 void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
736 const wxString
& dirbase
, const wxArrayString
& dirs
)
738 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
739 _T("base directory shouldn't end with a slash") );
741 wxString dirname
= dirbase
;
742 dirname
<< wxT("/mime-info");
744 if ( !wxDir::Exists(dirname
) )
748 if ( !dir
.IsOpened() )
751 // we will concatenate it with filename to get the full path below
755 bool cont
= dir
.GetFirst(&filename
, _T("*.mime"), wxDIR_FILES
);
758 LoadGnomeMimeTypesFromMimeFile(dirname
+ filename
);
760 cont
= dir
.GetNext(&filename
);
763 cont
= dir
.GetFirst(&filename
, _T("*.keys"), wxDIR_FILES
);
766 LoadGnomeDataFromKeyFile(dirname
+ filename
, dirs
);
768 cont
= dir
.GetNext(&filename
);
775 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString
& sExtraDir
)
778 dirs
.Add(wxT("/usr/share"));
779 dirs
.Add(wxT("/usr/local/share"));
782 wxGetHomeDir( &gnomedir
);
783 gnomedir
+= wxT("/.gnome");
784 dirs
.Add( gnomedir
);
785 if (!sExtraDir
.empty()) dirs
.Add( sExtraDir
);
787 size_t nDirs
= dirs
.GetCount();
788 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
790 LoadGnomeMimeFilesFromDir(dirs
[nDir
], dirs
);
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
799 // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype
800 // may be found in either of the following locations
802 // 1. $KDEDIR/share/mimelnk/mimetype/subtype.kdelnk
803 // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk
805 // The format of a .kdelnk file is almost the same as the one used by
806 // wxFileConfig, i.e. there are groups, comments and entries. The icon is the
807 // value for the entry "Type"
809 // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
810 // for now write to .kdelnk but should eventually do .desktop instead (in preference??)
812 bool wxMimeTypesManagerImpl::CheckKDEDirsExist ( const wxString
& sOK
, const wxString
& sTest
)
817 if (wxDir::Exists(sOK
)) return TRUE
;
822 wxString sStart
= sOK
+ wxT("/") + sTest
.BeforeFirst(wxT('/'));
823 if (!wxDir::Exists(sStart
)) wxMkdir(sStart
);
824 wxString sEnd
= sTest
.AfterFirst(wxT('/'));
825 return CheckKDEDirsExist(sStart
, sEnd
);
829 bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index
, bool delete_index
)
831 wxMimeTextFile appoutfile
, mimeoutfile
;
832 wxString sHome
= wxGetHomeDir();
833 wxString sTmp
= wxT(".kde/share/mimelnk/");
834 wxString sMime
= m_aTypes
[index
];
835 CheckKDEDirsExist (sHome
, sTmp
+ sMime
.BeforeFirst(wxT('/')) );
836 sTmp
= sHome
+ wxT('/') + sTmp
+ sMime
+ wxT(".kdelnk");
839 bool bMimeExists
= mimeoutfile
.Open (sTmp
);
842 bTemp
= mimeoutfile
.Create (sTmp
);
843 // some unknown error eg out of disk space
844 if (!bTemp
) return FALSE
;
847 sTmp
= wxT(".kde/share/applnk/");
848 CheckKDEDirsExist (sHome
, sTmp
+ sMime
.AfterFirst(wxT('/')) );
849 sTmp
= sHome
+ wxT('/') + sTmp
+ sMime
.AfterFirst(wxT('/')) + wxT(".kdelnk");
852 bAppExists
= appoutfile
.Open (sTmp
);
855 bTemp
= appoutfile
.Create (sTmp
);
856 // some unknown error eg out of disk space
857 if (!bTemp
) return FALSE
;
860 // fixed data; write if new file
863 mimeoutfile
.AddLine(wxT("#KDE Config File"));
864 mimeoutfile
.AddLine(wxT("[KDE Desktop Entry]"));
865 mimeoutfile
.AddLine(wxT("Version=1.0"));
866 mimeoutfile
.AddLine(wxT("Type=MimeType"));
867 mimeoutfile
.AddLine(wxT("MimeType=") + sMime
);
872 mimeoutfile
.AddLine(wxT("#KDE Config File"));
873 mimeoutfile
.AddLine(wxT("[KDE Desktop Entry]"));
874 appoutfile
.AddLine(wxT("Version=1.0"));
875 appoutfile
.AddLine(wxT("Type=Application"));
876 appoutfile
.AddLine(wxT("MimeType=") + sMime
+ wxT(';'));
881 mimeoutfile
.CommentLine(wxT("Comment="));
883 mimeoutfile
.AddLine(wxT("Comment=") + m_aDescriptions
[index
]);
884 appoutfile
.CommentLine(wxT("Name="));
886 appoutfile
.AddLine(wxT("Comment=") + m_aDescriptions
[index
]);
888 sTmp
= m_aIcons
[index
];
889 // we can either give the full path, or the shortfilename if its in
890 // one of the directories we search
891 mimeoutfile
.CommentLine(wxT("Icon=") );
892 if (!delete_index
) mimeoutfile
.AddLine(wxT("Icon=") + sTmp
);
893 appoutfile
.CommentLine(wxT("Icon=") );
894 if (!delete_index
) appoutfile
.AddLine(wxT("Icon=") + sTmp
);
896 sTmp
= wxT(" ") + m_aExtensions
[index
];
898 wxStringTokenizer
tokenizer(sTmp
, _T(" "));
899 sTmp
= wxT("Patterns=");
900 mimeoutfile
.CommentLine(sTmp
);
901 while ( tokenizer
.HasMoreTokens() )
903 // holds an extension; need to change it to *.ext;
904 wxString e
= wxT("*.") + tokenizer
.GetNextToken() + wxT(";");
907 if (!delete_index
) mimeoutfile
.AddLine(sTmp
);
909 wxMimeTypeCommands
* entries
= m_aEntries
[index
];
910 // if we don't find open just have an empty string ... FIX this
911 sTmp
= entries
->GetCommandForVerb(_T("open"));
912 sTmp
.Replace( wxT("%s"), wxT("%f") );
914 mimeoutfile
.CommentLine(wxT("DefaultApp=") );
915 if (!delete_index
) mimeoutfile
.AddLine(wxT("DefaultApp=") + sTmp
);
917 sTmp
.Replace( wxT("%f"), wxT("") );
918 appoutfile
.CommentLine(wxT("Exec="));
919 if (!delete_index
) appoutfile
.AddLine(wxT("Exec=") + sTmp
);
921 if (entries
->GetCount() > 1)
923 //other actions as well as open
927 if (mimeoutfile
.Write ()) bTemp
= TRUE
;
928 mimeoutfile
.Close ();
929 if (appoutfile
.Write ()) bTemp
= TRUE
;
937 void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString
& dirbase
,
938 const wxString
& subdir
,
939 const wxString
& filename
,
940 const wxArrayString
& icondirs
)
943 if ( !file
.Open(dirbase
+ filename
) ) return;
945 wxLogTrace(TRACE_MIME
, wxT("loading KDE file %s"),
946 (dirbase
+filename
).c_str());
948 wxMimeTypeCommands
* entry
= new wxMimeTypeCommands
;
950 wxString mimetype
, mime_desc
, strIcon
;
952 int nIndex
= file
.pIndexOf( wxT("MimeType=") );
953 if (nIndex
== wxNOT_FOUND
)
955 // construct mimetype from the directory name and the basename of the
956 // file (it always has .kdelnk extension)
957 mimetype
<< subdir
<< wxT('/') << filename
.BeforeLast( wxT('.') );
959 else mimetype
= file
.GetCmd (nIndex
);
961 // first find the description string: it is the value in either "Comment="
962 // line or "Comment[<locale_name>]=" one
963 nIndex
= wxNOT_FOUND
;
967 wxLocale
*locale
= wxGetLocale();
970 // try "Comment[locale name]" first
971 comment
<< _T("Comment[") + locale
->GetName() + _T("]=");
972 nIndex
= file
.pIndexOf(comment
);
976 if ( nIndex
== wxNOT_FOUND
)
978 comment
= _T("Comment=");
979 nIndex
= file
.pIndexOf(comment
);
982 if ( nIndex
!= wxNOT_FOUND
) mime_desc
= file
.GetCmd(nIndex
);
983 //else: no description
985 // next find the extensions
986 wxString mime_extension
;
988 nIndex
= file
.pIndexOf(_T("Patterns="));
989 if ( nIndex
!= wxNOT_FOUND
)
991 wxString exts
= file
.GetCmd (nIndex
);;
993 wxStringTokenizer
tokenizer(exts
, _T(";"));
994 while ( tokenizer
.HasMoreTokens() )
996 wxString e
= tokenizer
.GetNextToken();
997 if ( e
.Left(2) != _T("*.") )
998 continue; // don't support too difficult patterns
1000 if ( !mime_extension
.empty() )
1002 // separate from the previous ext
1003 mime_extension
<< _T(' ');
1006 mime_extension
<< e
.Mid(2);
1009 sExts
.Add(mime_extension
);
1012 // ok, now we can take care of icon:
1014 nIndex
= file
.pIndexOf(_T("Icon="));
1015 if ( nIndex
!= wxNOT_FOUND
)
1017 strIcon
= file
.GetCmd(nIndex
);
1018 wxLogTrace(TRACE_MIME
, wxT(" icon %s"), strIcon
.c_str());
1019 //it could be the real path, but more often a short name
1020 if (!wxFileExists(strIcon
))
1022 // icon is just the short name
1023 if ( !strIcon
.empty() )
1025 // we must check if the file exists because it may be stored
1026 // in many locations, at least ~/.kde and $KDEDIR
1027 size_t nDir
, nDirs
= icondirs
.GetCount();
1028 for ( nDir
= 0; nDir
< nDirs
; nDir
++ )
1029 if (wxFileExists(icondirs
[nDir
] + strIcon
))
1031 strIcon
.Prepend(icondirs
[nDir
]);
1032 wxLogTrace(TRACE_MIME
, wxT(" iconfile %s"), strIcon
.c_str());
1038 // now look for lines which know about the application
1039 // exec= or DefaultApp=
1041 nIndex
= file
.pIndexOf(wxT("DefaultApp"));
1043 if ( nIndex
== wxNOT_FOUND
)
1045 // no entry try exec
1046 nIndex
= file
.pIndexOf(wxT("Exec"));
1049 if ( nIndex
!= wxNOT_FOUND
)
1051 wxString sTmp
= file
.GetCmd(nIndex
);
1052 // we expect %f; others including %F and %U and %u are possible
1053 if (0 == sTmp
.Replace ( wxT("%f"), wxT("%s") ))
1054 sTmp
= sTmp
+ wxT(" %s");
1055 entry
->AddOrReplaceVerb (wxString(wxT("open")), sTmp
);
1058 AddToMimeData (mimetype
, strIcon
, entry
, sExts
, mime_desc
);
1061 void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString
& dirbase
,
1062 const wxString
& subdir
,
1063 const wxArrayString
& icondirs
)
1065 wxString dirname
= dirbase
;
1068 if ( !dir
.IsOpened() )
1071 wxLogTrace(TRACE_MIME
, wxT("--- Loading from KDE directory %s ---"),
1077 bool cont
= dir
.GetFirst(&filename
, _T("*.kdelnk"), wxDIR_FILES
);
1080 LoadKDELinksForMimeSubtype(dirname
, subdir
, filename
, icondirs
);
1082 cont
= dir
.GetNext(&filename
);
1084 // new standard for Gnome and KDE
1085 cont
= dir
.GetFirst(&filename
, _T("*.desktop"), wxDIR_FILES
);
1088 LoadKDELinksForMimeSubtype(dirname
, subdir
, filename
, icondirs
);
1090 cont
= dir
.GetNext(&filename
);
1094 void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString
& dirbase
,
1095 const wxArrayString
& icondirs
)
1097 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
1098 _T("base directory shouldn't end with a slash") );
1100 wxString dirname
= dirbase
;
1101 dirname
<< _T("/mimelnk");
1103 if ( !wxDir::Exists(dirname
) )
1107 if ( !dir
.IsOpened() )
1110 // we will concatenate it with dir name to get the full path below
1114 bool cont
= dir
.GetFirst(&subdir
, wxEmptyString
, wxDIR_DIRS
);
1117 LoadKDELinksForMimeType(dirname
, subdir
, icondirs
);
1119 cont
= dir
.GetNext(&subdir
);
1123 void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString
& sExtraDir
)
1126 wxArrayString icondirs
;
1128 // settings in ~/.kde have maximal priority
1129 dirs
.Add(wxGetHomeDir() + wxT("/.kde/share"));
1130 icondirs
.Add(wxGetHomeDir() + wxT("/.kde/share/icons/"));
1132 // the variable KDEDIR is set when KDE is running
1133 const wxChar
*kdedir
= wxGetenv( wxT("KDEDIR") );
1136 dirs
.Add( wxString(kdedir
) + wxT("/share") );
1137 icondirs
.Add( wxString(kdedir
) + wxT("/share/icons/") );
1141 // try to guess KDEDIR
1142 dirs
.Add(_T("/usr/share"));
1143 dirs
.Add(_T("/opt/kde/share"));
1144 icondirs
.Add(_T("/usr/share/icons/"));
1145 icondirs
.Add(_T("/usr/X11R6/share/icons/")); // Debian/Corel linux
1146 icondirs
.Add(_T("/opt/kde/share/icons/"));
1149 if (!sExtraDir
.empty()) dirs
.Add (sExtraDir
);
1150 icondirs
.Add(sExtraDir
+ wxT("/icons"));
1152 size_t nDirs
= dirs
.GetCount();
1153 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
1155 LoadKDELinkFilesFromDir(dirs
[nDir
], icondirs
);
1159 // ----------------------------------------------------------------------------
1160 // wxFileTypeImpl (Unix)
1161 // ----------------------------------------------------------------------------
1163 wxString
wxFileTypeImpl::GetExpandedCommand(const wxString
& verb
, const wxFileType::MessageParameters
& params
) const
1167 while ( (i
< m_index
.GetCount() ) && sTmp
.empty() )
1169 sTmp
= m_manager
->GetCommand ( verb
, m_index
[i
] );
1173 return wxFileType::ExpandCommand(sTmp
, params
);
1176 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
1181 while ( (i
< m_index
.GetCount() ) && sTmp
.empty() )
1183 sTmp
= m_manager
->m_aIcons
[m_index
[i
]];
1186 if ( sTmp
.empty () )
1191 iconLoc
->SetFileName(sTmp
);
1199 wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
1202 for (size_t i
= 0; i
< m_index
.GetCount(); i
++)
1203 mimeTypes
.Add(m_manager
->m_aTypes
[m_index
[i
]]);
1208 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
1209 wxArrayString
*commands
,
1210 const wxFileType::MessageParameters
& params
) const
1213 wxString vrb
, cmd
, sTmp
;
1215 wxMimeTypeCommands
* sPairs
;
1217 // verbs and commands have been cleared already in mimecmn.cpp...
1218 // if we find no entries in the exact match, try the inexact match
1219 for (size_t n
= 0; ((count
==0) && (n
< m_index
.GetCount())); n
++)
1221 // list of verb = command pairs for this mimetype
1222 sPairs
= m_manager
->m_aEntries
[m_index
[n
]];
1224 for ( i
= 0; i
< sPairs
->GetCount (); i
++ )
1226 vrb
= sPairs
->GetVerb(i
);
1227 // some gnome entries have . inside
1228 vrb
= vrb
.AfterLast(wxT('.'));
1229 cmd
= sPairs
->GetCmd (i
);
1232 cmd
= wxFileType::ExpandCommand(cmd
, params
);
1234 if ( vrb
.IsSameAs (wxT("open")))
1236 verbs
->Insert(vrb
,0u);
1237 commands
->Insert(cmd
,0u);
1242 commands
->Add (cmd
);
1253 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
1255 wxString strExtensions
= m_manager
->GetExtension(m_index
[0]);
1258 // one extension in the space or comma delimitid list
1260 for ( const wxChar
*p
= strExtensions
;; p
++ ) {
1261 if ( *p
== wxT(' ') || *p
== wxT(',') || *p
== wxT('\0') ) {
1262 if ( !strExt
.empty() ) {
1263 extensions
.Add(strExt
);
1266 //else: repeated spaces (shouldn't happen, but it's not that
1267 // important if it does happen)
1269 if ( *p
== wxT('\0') )
1272 else if ( *p
== wxT('.') ) {
1273 // remove the dot from extension (but only if it's the first char)
1274 if ( !strExt
.empty() ) {
1277 //else: no, don't append it
1287 // set an arbitrary command,
1288 // could adjust the code to ask confirmation if it already exists and
1289 // overwriteprompt is TRUE, but this is currently ignored as *Associate* has
1290 // no overwrite prompt
1291 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
, const wxString
& verb
, bool overwriteprompt
/*= TRUE*/)
1293 wxArrayString strExtensions
;
1294 wxString strDesc
, strIcon
;
1296 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands ();
1297 entry
->Add(verb
+ wxT("=") + cmd
+ wxT(" %s "));
1299 wxArrayString strTypes
;
1300 GetMimeTypes (strTypes
);
1301 if (strTypes
.GetCount() < 1) return FALSE
;
1305 for (i
= 0; i
< strTypes
.GetCount(); i
++)
1307 if (!m_manager
->DoAssociation (strTypes
[i
], strIcon
, entry
, strExtensions
, strDesc
))
1314 // ignore index on the grouds that we only have one icon in a Unix file
1315 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& strIcon
/*= wxEmptyString*/, int /*index = 0*/)
1317 if (strIcon
.empty()) return FALSE
;
1318 wxArrayString strExtensions
;
1321 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands ();
1323 wxArrayString strTypes
;
1324 GetMimeTypes (strTypes
);
1325 if (strTypes
.GetCount() < 1) return FALSE
;
1329 for (i
= 0; i
< strTypes
.GetCount(); i
++)
1331 if (!m_manager
->DoAssociation (strTypes
[i
], strIcon
, entry
, strExtensions
, strDesc
))
1338 // ----------------------------------------------------------------------------
1339 // wxMimeTypesManagerImpl (Unix)
1340 // ----------------------------------------------------------------------------
1343 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1345 m_initialized
= FALSE
;
1346 m_mailcapStylesInited
= 0;
1349 // read system and user mailcaps and other files
1350 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles
,
1351 const wxString
& sExtraDir
)
1353 // read mimecap amd mime.types
1354 if ( (mailcapStyles
& wxMAILCAP_NETSCAPE
) ||
1355 (mailcapStyles
& wxMAILCAP_STANDARD
) )
1356 GetMimeInfo(sExtraDir
);
1358 // read GNOME tables
1359 if ( mailcapStyles
& wxMAILCAP_GNOME
)
1360 GetGnomeMimeInfo(sExtraDir
);
1363 if ( mailcapStyles
& wxMAILCAP_KDE
)
1364 GetKDEMimeInfo(sExtraDir
);
1366 m_mailcapStylesInited
|= mailcapStyles
;
1369 // clear data so you can read another group of WM files
1370 void wxMimeTypesManagerImpl::ClearData()
1374 m_aExtensions
.Clear ();
1375 m_aDescriptions
.Clear ();
1377 WX_CLEAR_ARRAY(m_aEntries
);
1380 m_mailcapStylesInited
= 0;
1383 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
1389 void wxMimeTypesManagerImpl::GetMimeInfo (const wxString
& sExtraDir
)
1391 // read this for netscape or Metamail formats
1393 // directories where we look for mailcap and mime.types by default
1394 // used by netscape and pine and other mailers, using 2 different formats!
1396 // (taken from metamail(1) sources)
1398 // although RFC 1524 specifies the search path of
1399 // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more
1400 // places - OTOH, the RFC also says that this path can be changed with
1401 // MAILCAPS environment variable (containing the colon separated full
1402 // filenames to try) which is not done yet (TODO?)
1404 wxString strHome
= wxGetenv(wxT("HOME"));
1407 dirs
.Add ( strHome
+ wxT("/.") );
1408 dirs
.Add ( wxT("/etc/") );
1409 dirs
.Add ( wxT("/usr/etc/") );
1410 dirs
.Add ( wxT("/usr/local/etc/") );
1411 dirs
.Add ( wxT("/etc/mail/") );
1412 dirs
.Add ( wxT("/usr/public/lib/") );
1413 if (!sExtraDir
.empty()) dirs
.Add ( sExtraDir
+ wxT("/") );
1415 size_t nDirs
= dirs
.GetCount();
1416 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
1418 wxString file
= dirs
[nDir
] + wxT("mailcap");
1419 if ( wxFile::Exists(file
) ) {
1423 file
= dirs
[nDir
] + wxT("mime.types");
1424 if ( wxFile::Exists(file
) ) {
1425 ReadMimeTypes(file
);
1431 bool wxMimeTypesManagerImpl::WriteToMimeTypes (int index
, bool delete_index
)
1433 // check we have the right manager
1434 if (! ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) )
1438 wxString strHome
= wxGetenv(wxT("HOME"));
1440 // and now the users mailcap
1441 wxString strUserMailcap
= strHome
+ wxT("/.mime.types");
1443 wxMimeTextFile file
;
1444 if ( wxFile::Exists(strUserMailcap
) )
1446 bTemp
= file
.Open(strUserMailcap
);
1450 if (delete_index
) return FALSE
;
1451 bTemp
= file
.Create(strUserMailcap
);
1456 // test for netscape's header and return FALSE if its found
1457 nIndex
= file
.pIndexOf (wxT("#--Netscape"));
1458 if (nIndex
!= wxNOT_FOUND
)
1460 wxASSERT_MSG(FALSE
,wxT("Error in .mime.types \nTrying to mix Netscape and Metamail formats\nFile not modiifed"));
1463 // write it in alternative format
1464 // get rid of unwanted entries
1465 wxString strType
= m_aTypes
[index
];
1466 nIndex
= file
.pIndexOf (strType
);
1467 // get rid of all the unwanted entries...
1468 if (nIndex
!= wxNOT_FOUND
) file
.CommentLine (nIndex
);
1472 // add the new entries in
1473 wxString sTmp
= strType
.Append (wxT(' '), 40-strType
.Len() );
1474 sTmp
= sTmp
+ m_aExtensions
[index
];
1475 file
.AddLine (sTmp
);
1479 bTemp
= file
.Write ();
1485 bool wxMimeTypesManagerImpl::WriteToNSMimeTypes (int index
, bool delete_index
)
1487 //check we have the right managers
1488 if (! ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
) )
1492 wxString strHome
= wxGetenv(wxT("HOME"));
1494 // and now the users mailcap
1495 wxString strUserMailcap
= strHome
+ wxT("/.mime.types");
1497 wxMimeTextFile file
;
1498 if ( wxFile::Exists(strUserMailcap
) )
1500 bTemp
= file
.Open(strUserMailcap
);
1504 if (delete_index
) return FALSE
;
1505 bTemp
= file
.Create(strUserMailcap
);
1510 // write it in the format that Netscape uses
1512 // test for netscape's header and insert if required...
1513 // this is a comment so use TRUE
1514 nIndex
= file
.pIndexOf (wxT("#--Netscape"), TRUE
);
1515 if (nIndex
== wxNOT_FOUND
)
1517 // either empty file or metamail format
1518 // at present we can't cope with mixed formats, so exit to preseve
1519 // metamail entreies
1520 if (file
.GetLineCount () > 0)
1522 wxASSERT_MSG(FALSE
, wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap"));
1525 file
.InsertLine (wxT( "#--Netscape Communications Corporation MIME Information" ), 0);
1529 wxString strType
= wxT("type=") + m_aTypes
[index
];
1530 nIndex
= file
.pIndexOf (strType
);
1531 // get rid of all the unwanted entries...
1532 if (nIndex
!= wxNOT_FOUND
)
1534 wxString sOld
= file
[nIndex
];
1535 while ( (sOld
.Contains(wxT("\\"))) && (nIndex
< (int) file
.GetLineCount()) )
1537 file
.CommentLine(nIndex
);
1538 sOld
= file
[nIndex
];
1539 wxLogTrace(TRACE_MIME
, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex
, sOld
.c_str());
1542 if (nIndex
< (int) file
.GetLineCount()) file
.CommentLine (nIndex
);
1544 else nIndex
= (int) file
.GetLineCount();
1546 wxString sTmp
= strType
+ wxT(" \\");
1547 if (!delete_index
) file
.InsertLine (sTmp
, nIndex
);
1548 if ( ! m_aDescriptions
.Item(index
).empty() )
1550 sTmp
= wxT("desc=\"") + m_aDescriptions
[index
]+ wxT("\" \\"); //.trim ??
1554 file
.InsertLine (sTmp
, nIndex
);
1557 wxString sExts
= m_aExtensions
.Item(index
);
1558 sTmp
= wxT("exts=\"") + sExts
.Trim(FALSE
).Trim() + wxT("\"");
1562 file
.InsertLine (sTmp
, nIndex
);
1565 bTemp
= file
.Write ();
1572 bool wxMimeTypesManagerImpl::WriteToMailCap (int index
, bool delete_index
)
1574 //check we have the right managers
1575 if ( !( ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
) ||
1576 ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) ) )
1580 wxString strHome
= wxGetenv(wxT("HOME"));
1582 // and now the users mailcap
1583 wxString strUserMailcap
= strHome
+ wxT("/.mailcap");
1585 wxMimeTextFile file
;
1586 if ( wxFile::Exists(strUserMailcap
) )
1588 bTemp
= file
.Open(strUserMailcap
);
1592 if (delete_index
) return FALSE
;
1593 bTemp
= file
.Create(strUserMailcap
);
1597 // now got a file we can write to ....
1598 wxMimeTypeCommands
* entries
= m_aEntries
[index
];
1600 wxString sCmd
= entries
->GetCommandForVerb(_T("open"), &iOpen
);
1603 sTmp
= m_aTypes
[index
];
1605 int nIndex
= file
.pIndexOf(sTmp
);
1606 // get rid of all the unwanted entries...
1607 if (nIndex
== wxNOT_FOUND
)
1609 nIndex
= (int) file
.GetLineCount();
1613 sOld
= file
[nIndex
];
1614 wxLogTrace(TRACE_MIME
, wxT("--- Deleting from mailcap line '%d' ---"), nIndex
);
1616 while ( (sOld
.Contains(wxT("\\"))) && (nIndex
< (int) file
.GetLineCount()) )
1618 file
.CommentLine(nIndex
);
1619 if (nIndex
< (int) file
.GetLineCount()) sOld
= sOld
+ file
[nIndex
];
1621 if (nIndex
< (int) file
.GetLineCount()) file
.CommentLine (nIndex
);
1624 sTmp
= sTmp
+ wxT(";") + sCmd
; //includes wxT(" %s ");
1626 // write it in the format that Netscape uses (default)
1627 if (! ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
) )
1629 if (! delete_index
) file
.InsertLine (sTmp
, nIndex
);
1633 // write extended format
1636 // todo FIX this code;
1638 // sOld holds all the entries, but our data store only has some
1639 // eg test= is not stored
1641 // so far we have written the mimetype and command out
1642 wxStringTokenizer
sT (sOld
, wxT(";\\"));
1643 if (sT
.CountTokens () > 2)
1645 // first one mimetype; second one command, rest unknown...
1647 s
= sT
.GetNextToken();
1648 s
= sT
.GetNextToken();
1651 s
= sT
.GetNextToken();
1652 while ( ! s
.empty() )
1654 bool bKnownToken
= FALSE
;
1655 if (s
.Contains(wxT("description="))) bKnownToken
= TRUE
;
1656 if (s
.Contains(wxT("x11-bitmap="))) bKnownToken
= TRUE
;
1658 for (i
=0; i
< entries
->GetCount(); i
++)
1660 if (s
.Contains(entries
->GetVerb(i
))) bKnownToken
= TRUE
;
1664 sTmp
= sTmp
+ wxT("; \\");
1665 file
.InsertLine (sTmp
, nIndex
);
1668 s
= sT
.GetNextToken ();
1673 if (! m_aDescriptions
[index
].empty() )
1675 sTmp
= sTmp
+ wxT("; \\");
1676 file
.InsertLine (sTmp
, nIndex
);
1678 sTmp
= wxT(" description=\"") + m_aDescriptions
[index
] + wxT("\"");
1681 if (! m_aIcons
[index
].empty() )
1683 sTmp
= sTmp
+ wxT("; \\");
1684 file
.InsertLine (sTmp
, nIndex
);
1686 sTmp
= wxT(" x11-bitmap=\"") + m_aIcons
[index
] + wxT("\"");
1688 if ( entries
->GetCount() > 1 )
1692 for (i
=0; i
< entries
->GetCount(); i
++)
1695 sTmp
= sTmp
+ wxT("; \\");
1696 file
.InsertLine (sTmp
, nIndex
);
1698 sTmp
= wxT(" ") + entries
->GetVerbCmd(i
);
1702 file
.InsertLine (sTmp
, nIndex
);
1706 bTemp
= file
.Write ();
1713 wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
1717 wxString strType
= ftInfo
.GetMimeType ();
1718 wxString strDesc
= ftInfo
.GetDescription ();
1719 wxString strIcon
= ftInfo
.GetIconFile ();
1721 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands ();
1723 if ( ! ftInfo
.GetOpenCommand().empty())
1724 entry
->Add(wxT("open=") + ftInfo
.GetOpenCommand () + wxT(" %s "));
1725 if ( ! ftInfo
.GetPrintCommand ().empty())
1726 entry
->Add(wxT("print=") + ftInfo
.GetPrintCommand () + wxT(" %s "));
1728 // now find where these extensions are in the data store and remove them
1729 wxArrayString sA_Exts
= ftInfo
.GetExtensions ();
1730 wxString sExt
, sExtStore
;
1732 for (i
=0; i
< sA_Exts
.GetCount(); i
++)
1734 sExt
= sA_Exts
.Item(i
);
1735 //clean up to just a space before and after
1736 sExt
.Trim().Trim(FALSE
);
1737 sExt
= wxT(' ') + sExt
+ wxT(' ');
1738 for (nIndex
= 0; nIndex
< m_aExtensions
.GetCount(); nIndex
++)
1740 sExtStore
= m_aExtensions
.Item(nIndex
);
1741 if (sExtStore
.Replace(sExt
, wxT(" ") ) > 0) m_aExtensions
.Item(nIndex
) = sExtStore
;
1746 if ( !DoAssociation (strType
, strIcon
, entry
, sA_Exts
, strDesc
) )
1749 return GetFileTypeFromMimeType(strType
);
1753 bool wxMimeTypesManagerImpl::DoAssociation(const wxString
& strType
,
1754 const wxString
& strIcon
,
1755 wxMimeTypeCommands
*entry
,
1756 const wxArrayString
& strExtensions
,
1757 const wxString
& strDesc
)
1759 int nIndex
= AddToMimeData(strType
, strIcon
, entry
, strExtensions
, strDesc
, TRUE
);
1761 if ( nIndex
== wxNOT_FOUND
)
1764 return WriteMimeInfo (nIndex
, FALSE
);
1767 bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex
, bool delete_mime
)
1771 if ( m_mailcapStylesInited
& wxMAILCAP_STANDARD
)
1773 // write in metamail format;
1774 if (WriteToMimeTypes (nIndex
, delete_mime
) )
1775 if ( WriteToMailCap (nIndex
, delete_mime
) )
1778 if ( m_mailcapStylesInited
& wxMAILCAP_NETSCAPE
)
1780 // write in netsacpe format;
1781 if (WriteToNSMimeTypes (nIndex
, delete_mime
) )
1782 if ( WriteToMailCap (nIndex
, delete_mime
) )
1785 if (m_mailcapStylesInited
& wxMAILCAP_GNOME
)
1787 // write in Gnome format;
1788 if (WriteGnomeMimeFile (nIndex
, delete_mime
) )
1789 if (WriteGnomeKeyFile (nIndex
, delete_mime
) )
1792 if (m_mailcapStylesInited
& wxMAILCAP_KDE
)
1794 // write in KDE format;
1795 if (WriteKDEMimeFile (nIndex
, delete_mime
) )
1802 int wxMimeTypesManagerImpl::AddToMimeData(const wxString
& strType
,
1803 const wxString
& strIcon
,
1804 wxMimeTypeCommands
*entry
,
1805 const wxArrayString
& strExtensions
,
1806 const wxString
& strDesc
,
1807 bool replaceExisting
)
1811 // ensure mimetype is always lower case
1812 wxString mimeType
= strType
.Lower();
1814 // is this a known MIME type?
1815 int nIndex
= m_aTypes
.Index(mimeType
);
1816 if ( nIndex
== wxNOT_FOUND
)
1819 m_aTypes
.Add(mimeType
);
1820 m_aIcons
.Add(strIcon
);
1821 m_aEntries
.Add(entry
? entry
: new wxMimeTypeCommands
);
1823 // change nIndex so we can use it below to add the extensions
1824 nIndex
= m_aExtensions
.Add(wxEmptyString
);
1826 m_aDescriptions
.Add(strDesc
);
1828 else // yes, we already have it
1830 if ( replaceExisting
)
1832 // if new description change it
1833 if ( !strDesc
.empty())
1834 m_aDescriptions
[nIndex
] = strDesc
;
1836 // if new icon change it
1837 if ( !strIcon
.empty())
1838 m_aIcons
[nIndex
] = strIcon
;
1842 delete m_aEntries
[nIndex
];
1843 m_aEntries
[nIndex
] = entry
;
1846 else // add data we don't already have ...
1848 // if new description add only if none
1849 if ( m_aDescriptions
[nIndex
].empty() )
1850 m_aDescriptions
[nIndex
] = strDesc
;
1852 // if new icon and no existing icon
1853 if ( m_aIcons
[nIndex
].empty () )
1854 m_aIcons
[nIndex
] = strIcon
;
1856 // add any new entries...
1859 wxMimeTypeCommands
*entryOld
= m_aEntries
[nIndex
];
1861 size_t count
= entry
->GetCount();
1862 for ( size_t i
= 0; i
< count
; i
++ )
1864 const wxString
& verb
= entry
->GetVerb(i
);
1865 if ( !entryOld
->HasVerb(verb
) )
1867 entryOld
->AddOrReplaceVerb(verb
, entry
->GetCmd(i
));
1871 // as we don't store it anywhere, it won't be deleted later as
1872 // usual -- do it immediately instead
1878 // always add the extensions to this mimetype
1879 wxString
& exts
= m_aExtensions
[nIndex
];
1881 // add all extensions we don't have yet
1882 size_t count
= strExtensions
.GetCount();
1883 for ( size_t i
= 0; i
< count
; i
++ )
1885 wxString ext
= strExtensions
[i
] + _T(' ');
1887 if ( exts
.Find(ext
) == wxNOT_FOUND
)
1893 // check data integrity
1894 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1895 m_aTypes
.Count() == m_aExtensions
.Count() &&
1896 m_aTypes
.Count() == m_aIcons
.Count() &&
1897 m_aTypes
.Count() == m_aDescriptions
.Count() );
1904 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
1911 size_t count
= m_aExtensions
.GetCount();
1912 for ( size_t n
= 0; n
< count
; n
++ )
1914 wxStringTokenizer
tk(m_aExtensions
[n
], _T(' '));
1916 while ( tk
.HasMoreTokens() )
1918 // consider extensions as not being case-sensitive
1919 if ( tk
.GetNextToken().IsSameAs(ext
, FALSE
/* no case */) )
1922 wxFileType
*fileType
= new wxFileType
;
1923 fileType
->m_impl
->Init(this, n
);
1934 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
1938 wxFileType
* fileType
= NULL
;
1939 // mime types are not case-sensitive
1940 wxString
mimetype(mimeType
);
1941 mimetype
.MakeLower();
1943 // first look for an exact match
1944 int index
= m_aTypes
.Index(mimetype
);
1945 if ( index
!= wxNOT_FOUND
)
1947 fileType
= new wxFileType
;
1948 fileType
->m_impl
->Init(this, index
);
1951 // then try to find "text/*" as match for "text/plain" (for example)
1952 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
1953 // the whole string - ok.
1955 index
= wxNOT_FOUND
;
1956 wxString strCategory
= mimetype
.BeforeFirst(wxT('/'));
1958 size_t nCount
= m_aTypes
.Count();
1959 for ( size_t n
= 0; n
< nCount
; n
++ ) {
1960 if ( (m_aTypes
[n
].BeforeFirst(wxT('/')) == strCategory
) &&
1961 m_aTypes
[n
].AfterFirst(wxT('/')) == wxT("*") ) {
1968 if ( index
!= wxNOT_FOUND
)
1970 fileType
= new wxFileType
;
1971 fileType
->m_impl
->Init(this, index
);
1977 wxString
wxMimeTypesManagerImpl::GetCommand(const wxString
& verb
, size_t nIndex
) const
1979 wxString command
, testcmd
, sV
, sTmp
;
1980 sV
= verb
+ wxT("=");
1981 // list of verb = command pairs for this mimetype
1982 wxMimeTypeCommands
* sPairs
= m_aEntries
[nIndex
];
1985 for ( i
= 0; i
< sPairs
->GetCount (); i
++ )
1987 sTmp
= sPairs
->GetVerbCmd (i
);
1988 if ( sTmp
.Contains(sV
) )
1989 command
= sTmp
.AfterFirst(wxT('='));
1994 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo
& filetype
)
1998 wxString extensions
;
1999 const wxArrayString
& exts
= filetype
.GetExtensions();
2000 size_t nExts
= exts
.GetCount();
2001 for ( size_t nExt
= 0; nExt
< nExts
; nExt
++ ) {
2003 extensions
+= wxT(' ');
2005 extensions
+= exts
[nExt
];
2008 AddMimeTypeInfo(filetype
.GetMimeType(),
2010 filetype
.GetDescription());
2012 AddMailcapInfo(filetype
.GetMimeType(),
2013 filetype
.GetOpenCommand(),
2014 filetype
.GetPrintCommand(),
2016 filetype
.GetDescription());
2019 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString
& strMimeType
,
2020 const wxString
& strExtensions
,
2021 const wxString
& strDesc
)
2023 // reading mailcap may find image/* , while
2024 // reading mime.types finds image/gif and no match is made
2025 // this means all the get functions don't work fix this
2027 wxString sTmp
= strExtensions
;
2029 wxArrayString sExts
;
2030 sTmp
.Trim().Trim(FALSE
);
2032 while (!sTmp
.empty())
2034 sExts
.Add (sTmp
.AfterLast(wxT(' ')));
2035 sTmp
= sTmp
.BeforeLast(wxT(' '));
2038 AddToMimeData (strMimeType
, strIcon
, NULL
, sExts
, strDesc
, TRUE
);
2041 void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString
& strType
,
2042 const wxString
& strOpenCmd
,
2043 const wxString
& strPrintCmd
,
2044 const wxString
& strTest
,
2045 const wxString
& strDesc
)
2049 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands
;
2050 entry
->Add(wxT("open=") + strOpenCmd
);
2051 entry
->Add(wxT("print=") + strPrintCmd
);
2052 entry
->Add(wxT("test=") + strTest
);
2055 wxArrayString strExtensions
;
2057 AddToMimeData (strType
, strIcon
, entry
, strExtensions
, strDesc
, TRUE
);
2061 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString
& strFileName
)
2063 wxLogTrace(TRACE_MIME
, wxT("--- Parsing mime.types file '%s' ---"),
2064 strFileName
.c_str());
2066 wxTextFile
file(strFileName
);
2067 #if defined(__WXGTK20__) && wxUSE_UNICODE
2068 if ( !file
.Open( wxConvUTF8
) )
2074 // the information we extract
2075 wxString strMimeType
, strDesc
, strExtensions
;
2077 size_t nLineCount
= file
.GetLineCount();
2078 const wxChar
*pc
= NULL
;
2079 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ )
2082 // now we're at the start of the line
2083 pc
= file
[nLine
].c_str();
2086 // we didn't finish with the previous line yet
2091 while ( wxIsspace(*pc
) )
2094 // comment or blank line?
2095 if ( *pc
== wxT('#') || !*pc
) {
2096 // skip the whole line
2101 // detect file format
2102 const wxChar
*pEqualSign
= wxStrchr(pc
, wxT('='));
2103 if ( pEqualSign
== NULL
) {
2107 // first field is mime type
2108 for ( strMimeType
.Empty(); !wxIsspace(*pc
) && *pc
!= wxT('\0'); pc
++ ) {
2113 while ( wxIsspace(*pc
) )
2116 // take all the rest of the string
2119 // no description...
2126 // the string on the left of '=' is the field name
2127 wxString
strLHS(pc
, pEqualSign
- pc
);
2130 for ( pc
= pEqualSign
+ 1; wxIsspace(*pc
); pc
++ )
2134 if ( *pc
== wxT('"') ) {
2135 // the string is quoted and ends at the matching quote
2136 pEnd
= wxStrchr(++pc
, wxT('"'));
2137 if ( pEnd
== NULL
) {
2138 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
2140 strFileName
.c_str(), nLine
+ 1);
2144 // unquoted string ends at the first space or at the end of
2146 for ( pEnd
= pc
; *pEnd
&& !wxIsspace(*pEnd
); pEnd
++ )
2150 // now we have the RHS (field value)
2151 wxString
strRHS(pc
, pEnd
- pc
);
2153 // check what follows this entry
2154 if ( *pEnd
== wxT('"') ) {
2159 for ( pc
= pEnd
; wxIsspace(*pc
); pc
++ )
2162 // if there is something left, it may be either a '\\' to continue
2163 // the line or the next field of the same entry
2164 bool entryEnded
= *pc
== wxT('\0'),
2165 nextFieldOnSameLine
= FALSE
;
2166 if ( !entryEnded
) {
2167 nextFieldOnSameLine
= ((*pc
!= wxT('\\')) || (pc
[1] != wxT('\0')));
2170 // now see what we got
2171 if ( strLHS
== wxT("type") ) {
2172 strMimeType
= strRHS
;
2174 else if ( strLHS
.StartsWith(wxT("desc")) ) {
2177 else if ( strLHS
== wxT("exts") ) {
2178 strExtensions
= strRHS
;
2180 else if ( strLHS
!= _T("icon") )
2182 // this one is simply ignored: it usually refers to Netscape
2183 // built in icons which are useless for us anyhow
2185 else if ( !strLHS
.StartsWith(_T("x-")) )
2187 // we suppose that all fields starting with "X-" are
2188 // unregistered extensions according to the standard practice,
2189 // but it may be worth telling the user about other junk in
2190 // his mime.types file
2191 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
2192 strFileName
.c_str(), nLine
+ 1, strLHS
.c_str());
2195 if ( !entryEnded
) {
2196 if ( !nextFieldOnSameLine
)
2198 //else: don't reset it
2200 // as we don't reset strMimeType, the next field in this entry
2201 // will be interpreted correctly.
2207 // depending on the format (Mosaic or Netscape) either space or comma
2208 // is used to separate the extensions
2209 strExtensions
.Replace(wxT(","), wxT(" "));
2211 // also deal with the leading dot
2212 if ( !strExtensions
.empty() && strExtensions
[0u] == wxT('.') )
2214 strExtensions
.erase(0, 1);
2217 wxLogTrace(TRACE_MIME
, wxT("mime.types: '%s' => '%s' (%s)"),
2218 strExtensions
.c_str(),
2219 strMimeType
.c_str(),
2222 AddMimeTypeInfo(strMimeType
, strExtensions
, strDesc
);
2224 // finished with this line
2231 // ----------------------------------------------------------------------------
2232 // UNIX mailcap files parsing
2233 // ----------------------------------------------------------------------------
2235 // the data for a single MIME type
2236 struct MailcapLineData
2245 wxArrayString verbs
,
2253 MailcapLineData() { testfailed
= needsterminal
= copiousoutput
= FALSE
; }
2256 // process a non-standard (i.e. not the first or second one) mailcap field
2258 wxMimeTypesManagerImpl::ProcessOtherMailcapField(MailcapLineData
& data
,
2259 const wxString
& curField
)
2261 if ( curField
.empty() )
2267 // is this something of the form foo=bar?
2268 const wxChar
*pEq
= wxStrchr(curField
, wxT('='));
2271 // split "LHS = RHS" in 2
2272 wxString lhs
= curField
.BeforeFirst(wxT('=')),
2273 rhs
= curField
.AfterFirst(wxT('='));
2275 lhs
.Trim(TRUE
); // from right
2276 rhs
.Trim(FALSE
); // from left
2278 // it might be quoted
2279 if ( !rhs
.empty() && rhs
[0u] == wxT('"') && rhs
.Last() == wxT('"') )
2281 rhs
= rhs
.Mid(1, rhs
.length() - 2);
2284 // is it a command verb or something else?
2285 if ( lhs
== wxT("test") )
2287 if ( wxSystem(rhs
) == 0 )
2290 wxLogTrace(TRACE_MIME_TEST
,
2291 wxT("Test '%s' for mime type '%s' succeeded."),
2292 rhs
.c_str(), data
.type
.c_str());
2297 wxLogTrace(TRACE_MIME_TEST
,
2298 wxT("Test '%s' for mime type '%s' failed, skipping."),
2299 rhs
.c_str(), data
.type
.c_str());
2301 data
.testfailed
= TRUE
;
2304 else if ( lhs
== wxT("desc") )
2308 else if ( lhs
== wxT("x11-bitmap") )
2312 else if ( lhs
== wxT("notes") )
2316 else // not a (recognized) special case, must be a verb (e.g. "print")
2318 data
.verbs
.Add(lhs
);
2319 data
.commands
.Add(rhs
);
2322 else // '=' not found
2324 // so it must be a simple flag
2325 if ( curField
== wxT("needsterminal") )
2327 data
.needsterminal
= TRUE
;
2329 else if ( curField
== wxT("copiousoutput"))
2331 // copiousoutput impies that the viewer is a console program
2332 data
.needsterminal
=
2333 data
.copiousoutput
= TRUE
;
2335 else if ( !IsKnownUnimportantField(curField
) )
2344 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString
& strFileName
,
2347 wxLogTrace(TRACE_MIME
, wxT("--- Parsing mailcap file '%s' ---"),
2348 strFileName
.c_str());
2350 wxTextFile
file(strFileName
);
2351 #if defined(__WXGTK20__) && wxUSE_UNICODE
2352 if ( !file
.Open( wxConvUTF8
) )
2358 // indices of MIME types (in m_aTypes) we already found in this file
2360 // (see the comments near the end of function for the reason we need this)
2361 wxArrayInt aIndicesSeenHere
;
2363 // accumulator for the current field
2365 curField
.reserve(1024);
2367 size_t nLineCount
= file
.GetLineCount();
2368 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ )
2370 // now we're at the start of the line
2371 const wxChar
*pc
= file
[nLine
].c_str();
2374 while ( wxIsspace(*pc
) )
2377 // comment or empty string?
2378 if ( *pc
== wxT('#') || *pc
== wxT('\0') )
2384 // what field are we currently in? The first 2 are fixed and there may
2385 // be an arbitrary number of other fields parsed by
2386 // ProcessOtherMailcapField()
2388 // the first field is the MIME type
2394 } currentToken
= Field_Type
;
2396 // the flags and field values on the current line
2397 MailcapLineData data
;
2405 // interpret the next character literally (notice that
2406 // backslash can be used for line continuation)
2407 if ( *++pc
== wxT('\0') )
2409 // fetch the next line if there is one
2410 if ( nLine
== nLineCount
- 1 )
2412 // something is wrong, bail out
2415 wxLogDebug(wxT("Mailcap file %s, line %lu: "
2416 "'\\' on the end of the last line "
2418 strFileName
.c_str(),
2419 (unsigned long)nLine
+ 1);
2423 // pass to the beginning of the next line
2424 pc
= file
[++nLine
].c_str();
2426 // skip pc++ at the end of the loop
2432 // just a normal character
2438 cont
= FALSE
; // end of line reached, exit the loop
2440 // fall through to still process this field
2443 // trim whitespaces from both sides
2444 curField
.Trim(TRUE
).Trim(FALSE
);
2446 switch ( currentToken
)
2449 data
.type
= curField
.Lower();
2450 if ( data
.type
.empty() )
2452 // I don't think that this is a valid mailcap
2453 // entry, but try to interpret it somehow
2454 data
.type
= _T('*');
2457 if ( data
.type
.Find(wxT('/')) == wxNOT_FOUND
)
2459 // we interpret "type" as "type/*"
2460 data
.type
+= wxT("/*");
2463 currentToken
= Field_OpenCmd
;
2467 data
.cmdOpen
= curField
;
2469 currentToken
= Field_Other
;
2473 if ( !ProcessOtherMailcapField(data
, curField
) )
2475 // don't flood the user with error messages if
2476 // we don't understand something in his
2477 // mailcap, but give them in debug mode because
2478 // this might be useful for the programmer
2481 wxT("Mailcap file %s, line %lu: "
2482 "unknown field '%s' for the "
2483 "MIME type '%s' ignored."),
2484 strFileName
.c_str(),
2485 (unsigned long)nLine
+ 1,
2490 else if ( data
.testfailed
)
2492 // skip this entry entirely
2496 // it already has this value
2497 //currentToken = Field_Other;
2501 wxFAIL_MSG(wxT("unknown field type in mailcap"));
2504 // next token starts immediately after ';'
2512 // continue in the same line
2516 // we read the entire entry, check what have we got
2517 // ------------------------------------------------
2519 // check that we really read something reasonable
2520 if ( currentToken
< Field_Other
)
2522 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
2524 strFileName
.c_str(), nLine
+ 1);
2529 // if the test command failed, it's as if the entry were not there at
2531 if ( data
.testfailed
)
2536 // support for flags:
2537 // 1. create an xterm for 'needsterminal'
2538 // 2. append "| $PAGER" for 'copiousoutput'
2540 // Note that the RFC says that having both needsterminal and
2541 // copiousoutput is probably a mistake, so it seems that running
2542 // programs with copiousoutput inside an xterm as it is done now
2543 // is a bad idea (FIXME)
2544 if ( data
.copiousoutput
)
2546 const wxChar
*p
= wxGetenv(_T("PAGER"));
2547 data
.cmdOpen
<< _T(" | ") << (p
? p
: _T("more"));
2550 if ( data
.needsterminal
)
2552 data
.cmdOpen
= wxString::Format(_T("xterm -e sh -c '%s'"),
2553 data
.cmdOpen
.c_str());
2556 if ( !data
.cmdOpen
.empty() )
2558 data
.verbs
.Insert(_T("open"), 0);
2559 data
.commands
.Insert(data
.cmdOpen
, 0);
2562 // we have to decide whether the new entry should replace any entries
2563 // for the same MIME type we had previously found or not
2566 // the fall back entries have the lowest priority, by definition
2573 // have we seen this one before?
2574 int nIndex
= m_aTypes
.Index(data
.type
);
2576 // and if we have, was it in this file?
2577 overwrite
= nIndex
== wxNOT_FOUND
||
2578 aIndicesSeenHere
.Index(nIndex
) != wxNOT_FOUND
;
2581 wxLogTrace(TRACE_MIME
, _T("mailcap %s: %s [%s]"),
2582 data
.type
.c_str(), data
.cmdOpen
.c_str(),
2583 overwrite
? _T("replace") : _T("add"));
2585 int n
= AddToMimeData
2589 new wxMimeTypeCommands(data
.verbs
, data
.commands
),
2590 wxArrayString() /* extensions */,
2597 aIndicesSeenHere
.Add(n
);
2604 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
2611 size_t count
= m_aTypes
.GetCount();
2612 for ( size_t n
= 0; n
< count
; n
++ )
2614 // don't return template types from here (i.e. anything containg '*')
2616 if ( type
.Find(_T('*')) == wxNOT_FOUND
)
2618 mimetypes
.Add(type
);
2622 return mimetypes
.GetCount();
2625 // ----------------------------------------------------------------------------
2626 // writing to MIME type files
2627 // ----------------------------------------------------------------------------
2629 bool wxMimeTypesManagerImpl::Unassociate(wxFileType
*ft
)
2631 wxArrayString sMimeTypes
;
2632 ft
->GetMimeTypes (sMimeTypes
);
2636 for (i
= 0; i
< sMimeTypes
.GetCount(); i
++)
2638 sMime
= sMimeTypes
.Item(i
);
2639 int nIndex
= m_aTypes
.Index (sMime
);
2640 if ( nIndex
== wxNOT_FOUND
)
2642 // error if we get here ??
2647 WriteMimeInfo(nIndex
, TRUE
);
2648 m_aTypes
.RemoveAt(nIndex
);
2649 m_aEntries
.RemoveAt(nIndex
);
2650 m_aExtensions
.RemoveAt(nIndex
);
2651 m_aDescriptions
.RemoveAt(nIndex
);
2652 m_aIcons
.RemoveAt(nIndex
);
2655 // check data integrity
2656 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
2657 m_aTypes
.Count() == m_aExtensions
.Count() &&
2658 m_aTypes
.Count() == m_aIcons
.Count() &&
2659 m_aTypes
.Count() == m_aDescriptions
.Count() );
2664 // ----------------------------------------------------------------------------
2665 // private functions
2666 // ----------------------------------------------------------------------------
2668 static bool IsKnownUnimportantField(const wxString
& fieldAll
)
2670 static const wxChar
*knownFields
[] =
2672 _T("x-mozilla-flags"),
2674 _T("textualnewlines"),
2677 wxString field
= fieldAll
.BeforeFirst(_T('='));
2678 for ( size_t n
= 0; n
< WXSIZEOF(knownFields
); n
++ )
2680 if ( field
.CmpNoCase(knownFields
[n
]) == 0 )
2688 // wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE