1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/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 license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mimetype.h"
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // for compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
37 // Doesn't compile in WIN16 mode
42 #include "wx/dynarray.h"
43 #include "wx/confbase.h"
46 #include "wx/msw/registry.h"
49 #include "wx/textfile.h"
52 #include "wx/mimetype.h"
54 // other standard headers
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // implementation classes, platform dependent
64 // These classes use Windows registry to retrieve the required information.
66 // Keys used (not all of them are documented, so it might actually stop working
67 // in futur versions of Windows...):
68 // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME
69 // types, each key has a string value "Extension" which gives (dot preceded)
70 // extension for the files of this MIME type.
72 // 2. "HKCR\.ext" contains
73 // a) unnamed value containing the "filetype"
74 // b) value "Content Type" containing the MIME type
76 // 3. "HKCR\filetype" contains
77 // a) unnamed value containing the description
78 // b) subkey "DefaultIcon" with single unnamed value giving the icon index in
80 // c) shell\open\command and shell\open\print subkeys containing the commands
81 // to open/print the file (the positional parameters are introduced by %1,
82 // %2, ... in these strings, we change them to %s ourselves)
90 // initialize us with our file type name
91 void SetFileType(const wxString
& strFileType
)
92 { m_strFileType
= strFileType
; }
93 void SetExt(const wxString
& ext
)
96 // implement accessor functions
97 bool GetExtensions(wxArrayString
& extensions
);
98 bool GetMimeType(wxString
*mimeType
) const;
99 bool GetIcon(wxIcon
*icon
) const;
100 bool GetDescription(wxString
*desc
) const;
101 bool GetOpenCommand(wxString
*openCmd
,
102 const wxFileType::MessageParameters
&) const
103 { return GetCommand(openCmd
, "open"); }
104 bool GetPrintCommand(wxString
*printCmd
,
105 const wxFileType::MessageParameters
&) const
106 { return GetCommand(printCmd
, "print"); }
110 bool GetCommand(wxString
*command
, const char *verb
) const;
112 wxString m_strFileType
, m_ext
;
115 class wxMimeTypesManagerImpl
118 // nothing to do here, we don't load any data but just go and fetch it from
119 // the registry when asked for
120 wxMimeTypesManagerImpl() { }
122 // implement containing class functions
123 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
124 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
126 // this are NOPs under Windows
127 bool ReadMailcap(const wxString
& filename
, bool fallback
= TRUE
)
129 bool ReadMimeTypes(const wxString
& filename
)
135 // this class uses both mailcap and mime.types to gather information about file
138 // The information about mailcap file was extracted from metamail(1) sources and
141 // Format of mailcap file: spaces are ignored, each line is either a comment
142 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
143 // A backslash can be used to quote semicolons and newlines (and, in fact,
144 // anything else including itself).
146 // The first field is always the MIME type in the form of type/subtype (see RFC
147 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
148 // "type" which means the same as "type/*", although I'm not sure whether this
151 // The second field is always the command to run. It is subject to
152 // parameter/filename expansion described below.
154 // All the following fields are optional and may not be present at all. If
155 // they're present they may appear in any order, although each of them should
156 // appear only once. The optional fields are the following:
157 // * notes=xxx is an uninterpreted string which is silently ignored
158 // * test=xxx is the command to be used to determine whether this mailcap line
159 // applies to our data or not. The RHS of this field goes through the
160 // parameter/filename expansion (as the 2nd field) and the resulting string
161 // is executed. The line applies only if the command succeeds, i.e. returns 0
163 // * print=xxx is the command to be used to print (and not view) the data of
164 // this type (parameter/filename expansion is done here too)
165 // * edit=xxx is the command to open/edit the data of this type
166 // * needsterminal means that a new console must be created for the viewer
167 // * copiousoutput means that the viewer doesn't interact with the user but
168 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
169 // good example), thus it might be a good idea to use some kind of paging
171 // * textualnewlines means not to perform CR/LF translation (not honored)
172 // * compose and composetyped fields are used to determine the program to be
173 // called to create a new message pert in the specified format (unused).
175 // Parameter/filename xpansion:
176 // * %s is replaced with the (full) file name
177 // * %t is replaced with MIME type/subtype of the entry
178 // * for multipart type only %n is replaced with the nnumber of parts and %F is
179 // replaced by an array of (content-type, temporary file name) pairs for all
180 // message parts (TODO)
181 // * %{parameter} is replaced with the value of parameter taken from
182 // Content-type header line of the message.
184 // FIXME any docs with real descriptions of these files??
186 // There are 2 possible formats for mime.types file, one entry per line (used
187 // for global mime.types) and "expanded" format where an entry takes multiple
188 // lines (used for users mime.types).
190 // For both formats spaces are ignored and lines starting with a '#' are
191 // comments. Each record has one of two following forms:
192 // a) for "brief" format:
193 // <mime type> <space separated list of extensions>
194 // b) for "expanded" format:
195 // type=<mime type> \ desc="<description>" \ exts="ext"
197 // We try to autodetect the format of mime.types: if a non-comment line starts
198 // with "type=" we assume the second format, otherwise the first one.
200 // there may be more than one entry for one and the same mime type, to
201 // choose the right one we have to run the command specified in the test
202 // field on our data.
207 MailCapEntry(const wxString
& openCmd
,
208 const wxString
& printCmd
,
209 const wxString
& testCmd
)
210 : m_openCmd(openCmd
), m_printCmd(printCmd
), m_testCmd(testCmd
)
216 const wxString
& GetOpenCmd() const { return m_openCmd
; }
217 const wxString
& GetPrintCmd() const { return m_printCmd
; }
218 const wxString
& GetTestCmd() const { return m_testCmd
; }
220 MailCapEntry
*GetNext() const { return m_next
; }
223 // prepend this element to the list
224 void Prepend(MailCapEntry
*next
) { m_next
= next
; }
225 // insert into the list at given position
226 void Insert(MailCapEntry
*next
, size_t pos
)
231 for ( cur
= next
; cur
!= NULL
; cur
= cur
->m_next
, n
++ ) {
236 wxASSERT_MSG( n
== pos
, _T("invalid position in MailCapEntry::Insert") );
238 m_next
= cur
->m_next
;
241 // append this element to the list
242 void Append(MailCapEntry
*next
)
244 wxCHECK_RET( next
!= NULL
, _T("Append()ing to what?") );
248 for ( cur
= next
; cur
->m_next
!= NULL
; cur
= cur
->m_next
)
253 wxASSERT_MSG( !m_next
, _T("Append()ing element already in the list?") );
257 wxString m_openCmd
, // command to use to open/view the file
259 m_testCmd
; // only apply this entry if test yields
260 // true (i.e. the command returns 0)
262 MailCapEntry
*m_next
; // in the linked list
265 WX_DEFINE_ARRAY(MailCapEntry
*, ArrayTypeEntries
);
267 class wxMimeTypesManagerImpl
269 friend class wxFileTypeImpl
; // give it access to m_aXXX variables
272 // ctor loads all info into memory for quicker access later on
273 // TODO it would be nice to load them all, but parse on demand only...
274 wxMimeTypesManagerImpl();
276 // implement containing class functions
277 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
278 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
280 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
281 bool ReadMimeTypes(const wxString
& filename
);
284 // get the string containing space separated extensions for the given
286 wxString
GetExtension(size_t index
) { return m_aExtensions
[index
]; }
289 wxArrayString m_aTypes
, // MIME types
290 m_aDescriptions
, // descriptions (just some text)
291 m_aExtensions
; // space separated list of extensions
292 ArrayTypeEntries m_aEntries
; // commands and tests for this file type
298 // initialization functions
299 void Init(wxMimeTypesManagerImpl
*manager
, size_t index
)
300 { m_manager
= manager
; m_index
= index
; }
303 bool GetExtensions(wxArrayString
& extensions
);
304 bool GetMimeType(wxString
*mimeType
) const
305 { *mimeType
= m_manager
->m_aTypes
[m_index
]; return TRUE
; }
306 bool GetIcon(wxIcon
* WXUNUSED(icon
)) const
307 { return FALSE
; } // TODO maybe with Gnome/KDE integration...
308 bool GetDescription(wxString
*desc
) const
309 { *desc
= m_manager
->m_aDescriptions
[m_index
]; return TRUE
; }
311 bool GetOpenCommand(wxString
*openCmd
,
312 const wxFileType::MessageParameters
& params
) const
314 return GetExpandedCommand(openCmd
, params
, TRUE
);
317 bool GetPrintCommand(wxString
*printCmd
,
318 const wxFileType::MessageParameters
& params
) const
320 return GetExpandedCommand(printCmd
, params
, FALSE
);
324 // get the entry which passes the test (may return NULL)
325 MailCapEntry
*GetEntry(const wxFileType::MessageParameters
& params
) const;
327 // choose the correct entry to use and expand the command
328 bool GetExpandedCommand(wxString
*expandedCmd
,
329 const wxFileType::MessageParameters
& params
,
332 wxMimeTypesManagerImpl
*m_manager
;
333 size_t m_index
; // in the wxMimeTypesManagerImpl arrays
338 // ============================================================================
339 // implementation of the wrapper classes
340 // ============================================================================
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 wxString
wxFileType::ExpandCommand(const wxString
& command
,
347 const wxFileType::MessageParameters
& params
)
349 bool hasFilename
= FALSE
;
352 for ( const wxChar
*pc
= command
.c_str(); *pc
!= _T('\0'); pc
++ ) {
353 if ( *pc
== _T('%') ) {
356 // '%s' expands into file name (quoted because it might
357 // contain spaces) - except if there are already quotes
358 // there because otherwise some programs may get confused
359 // by double double quotes
361 if ( *(pc
- 2) == _T('"') )
362 str
<< params
.GetFileName();
364 str
<< _T('"') << params
.GetFileName() << _T('"');
366 str
<< params
.GetFileName();
371 // '%t' expands into MIME type (quote it too just to be
373 str
<< _T('\'') << params
.GetMimeType() << _T('\'');
378 const wxChar
*pEnd
= wxStrchr(pc
, _T('}'));
379 if ( pEnd
== NULL
) {
381 wxLogWarning(_("Unmatched '{' in an entry for "
383 params
.GetMimeType().c_str());
387 wxString
param(pc
+ 1, pEnd
- pc
- 1);
388 str
<< _T('\'') << params
.GetParamValue(param
) << _T('\'');
396 // TODO %n is the number of parts, %F is an array containing
397 // the names of temp files these parts were written to
398 // and their mime types.
402 wxLogDebug(_T("Unknown field %%%c in command '%s'."),
403 *pc
, command
.c_str());
412 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
413 // the program will accept the data on stdin: so give it to it!
414 if ( !hasFilename
&& !str
.IsEmpty() ) {
415 str
<< _T(" < '") << params
.GetFileName() << _T('\'');
421 wxFileType::wxFileType()
423 m_impl
= new wxFileTypeImpl
;
426 wxFileType::~wxFileType()
431 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
433 return m_impl
->GetExtensions(extensions
);
436 bool wxFileType::GetMimeType(wxString
*mimeType
) const
438 return m_impl
->GetMimeType(mimeType
);
441 bool wxFileType::GetIcon(wxIcon
*icon
) const
443 return m_impl
->GetIcon(icon
);
446 bool wxFileType::GetDescription(wxString
*desc
) const
448 return m_impl
->GetDescription(desc
);
452 wxFileType::GetOpenCommand(wxString
*openCmd
,
453 const wxFileType::MessageParameters
& params
) const
455 return m_impl
->GetOpenCommand(openCmd
, params
);
459 wxFileType::GetPrintCommand(wxString
*printCmd
,
460 const wxFileType::MessageParameters
& params
) const
462 return m_impl
->GetPrintCommand(printCmd
, params
);
465 // ----------------------------------------------------------------------------
466 // wxMimeTypesManager
467 // ----------------------------------------------------------------------------
469 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
470 const wxString
& wildcard
)
472 wxASSERT_MSG( mimeType
.Find(_T('*')) == wxNOT_FOUND
,
473 _T("first MIME type can't contain wildcards") );
475 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
476 if ( wildcard
.BeforeFirst(_T('/')).IsSameAs(mimeType
.BeforeFirst(_T('/')), FALSE
) )
478 wxString strSubtype
= wildcard
.AfterFirst(_T('/'));
480 if ( strSubtype
== _T('*') ||
481 strSubtype
.IsSameAs(mimeType
.AfterFirst(_T('/')), FALSE
) )
483 // matches (either exactly or it's a wildcard)
491 wxMimeTypesManager::wxMimeTypesManager()
493 m_impl
= new wxMimeTypesManagerImpl
;
496 wxMimeTypesManager::~wxMimeTypesManager()
502 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
504 return m_impl
->GetFileTypeFromExtension(ext
);
508 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
510 return m_impl
->GetFileTypeFromMimeType(mimeType
);
513 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
515 return m_impl
->ReadMailcap(filename
, fallback
);
518 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
520 return m_impl
->ReadMimeTypes(filename
);
523 // ============================================================================
524 // real (OS specific) implementation
525 // ============================================================================
529 bool wxFileTypeImpl::GetCommand(wxString
*command
, const wxChar
*verb
) const
531 // suppress possible error messages
534 strKey
<< m_strFileType
<< _T("\\shell\\") << verb
<< _T("\\command");
535 wxRegKey
key(wxRegKey::HKCR
, strKey
);
538 // it's the default value of the key
539 if ( key
.QueryValue(_T(""), *command
) ) {
540 // transform it from '%1' to '%s' style format string
541 // NB: we don't make any attempt to verify that the string is valid,
542 // i.e. doesn't contain %2, or second %1 or .... But we do make
543 // sure that we return a string with _exactly_ one '%s'!
544 size_t len
= command
->Len();
545 for ( size_t n
= 0; n
< len
; n
++ ) {
546 if ( command
->GetChar(n
) == _T('%') &&
547 (n
+ 1 < len
) && command
->GetChar(n
+ 1) == _T('1') ) {
548 // replace it with '%s'
549 command
->SetChar(n
+ 1, _T('s'));
555 // we didn't find any '%1'!
556 // HACK: append the filename at the end, hope that it will do
557 *command
<< _T(" %s");
563 // no such file type or no value
567 // TODO this function is half implemented
568 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
570 if ( m_ext
.IsEmpty() ) {
571 // the only way to get the list of extensions from the file type is to
572 // scan through all extensions in the registry - too slow...
577 extensions
.Add(m_ext
);
579 // it's a lie too, we don't return _all_ extensions...
584 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
586 // suppress possible error messages
588 wxRegKey
key(wxRegKey::HKCR
, /*m_strFileType*/ _T(".") + m_ext
);
589 if ( key
.Open() && key
.QueryValue(_T("Content Type"), *mimeType
) ) {
597 bool wxFileTypeImpl::GetIcon(wxIcon
*icon
) const
600 strIconKey
<< m_strFileType
<< _T("\\DefaultIcon");
602 // suppress possible error messages
604 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
608 // it's the default value of the key
609 if ( key
.QueryValue(_T(""), strIcon
) ) {
610 // the format is the following: <full path to file>, <icon index>
611 // NB: icon index may be negative as well as positive and the full
612 // path may contain the environment variables inside '%'
613 wxString strFullPath
= strIcon
.BeforeLast(_T(',')),
614 strIndex
= strIcon
.AfterLast(_T(','));
616 // index may be omitted, in which case BeforeLast(',') is empty and
617 // AfterLast(',') is the whole string
618 if ( strFullPath
.IsEmpty() ) {
619 strFullPath
= strIndex
;
623 wxString strExpPath
= wxExpandEnvVars(strFullPath
);
624 int nIndex
= atoi(strIndex
);
626 HICON hIcon
= ExtractIcon(GetModuleHandle(NULL
), strExpPath
, nIndex
);
627 switch ( (int)hIcon
) {
628 case 0: // means no icons were found
629 case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/...
630 wxLogDebug(_T("incorrect registry entry '%s': no such icon."),
631 key
.GetName().c_str());
635 icon
->SetHICON((WXHICON
)hIcon
);
641 // no such file type or no value or incorrect icon entry
645 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
647 // suppress possible error messages
649 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
652 // it's the default value of the key
653 if ( key
.QueryValue(_T(""), *desc
) ) {
661 // extension -> file type
663 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
665 // add the leading point if necessary
667 if ( ext
[0u] != _T('.') ) {
672 // suppress possible error messages
675 wxString strFileType
;
676 wxRegKey
key(wxRegKey::HKCR
, str
);
678 // it's the default value of the key
679 if ( key
.QueryValue(_T(""), strFileType
) ) {
680 // create the new wxFileType object
681 wxFileType
*fileType
= new wxFileType
;
682 fileType
->m_impl
->SetFileType(strFileType
);
683 fileType
->m_impl
->SetExt(ext
);
693 // MIME type -> extension -> file type
695 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
697 // HACK I don't know of any official documentation which mentions this
698 // location, but as a matter of fact IE uses it, so why not we?
699 static const wxChar
*szMimeDbase
= _T("MIME\\Database\\Content Type\\");
701 wxString strKey
= szMimeDbase
;
704 // suppress possible error messages
708 wxRegKey
key(wxRegKey::HKCR
, strKey
);
710 if ( key
.QueryValue(_T("Extension"), ext
) ) {
711 return GetFileTypeFromExtension(ext
);
722 wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters
& params
) const
725 MailCapEntry
*entry
= m_manager
->m_aEntries
[m_index
];
726 while ( entry
!= NULL
) {
727 // notice that an empty command would always succeed (it's ok)
728 command
= wxFileType::ExpandCommand(entry
->GetTestCmd(), params
);
730 if ( command
.IsEmpty() || (wxSystem(command
) == 0) ) {
732 wxLogTrace(_T("Test '%s' for mime type '%s' succeeded."),
733 command
.c_str(), params
.GetMimeType().c_str());
737 wxLogTrace(_T("Test '%s' for mime type '%s' failed."),
738 command
.c_str(), params
.GetMimeType().c_str());
741 entry
= entry
->GetNext();
748 wxFileTypeImpl::GetExpandedCommand(wxString
*expandedCmd
,
749 const wxFileType::MessageParameters
& params
,
752 MailCapEntry
*entry
= GetEntry(params
);
753 if ( entry
== NULL
) {
754 // all tests failed...
758 wxString cmd
= open
? entry
->GetOpenCmd() : entry
->GetPrintCmd();
759 if ( cmd
.IsEmpty() ) {
760 // may happen, especially for "print"
764 *expandedCmd
= wxFileType::ExpandCommand(cmd
, params
);
768 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
770 wxString strExtensions
= m_manager
->GetExtension(m_index
);
773 // one extension in the space or comma delimitid list
775 for ( const wxChar
*p
= strExtensions
; ; p
++ ) {
776 if ( *p
== _T(' ') || *p
== _T(',') || *p
== _T('\0') ) {
777 if ( !strExt
.IsEmpty() ) {
778 extensions
.Add(strExt
);
781 //else: repeated spaces (shouldn't happen, but it's not that
782 // important if it does happen)
784 if ( *p
== _T('\0') )
787 else if ( *p
== _T('.') ) {
788 // remove the dot from extension (but only if it's the first char)
789 if ( !strExt
.IsEmpty() ) {
792 //else: no, don't append it
802 // read system and user mailcaps (TODO implement mime.types support)
803 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
805 // directories where we look for mailcap and mime.types by default
806 // (taken from metamail(1) sources)
807 static const wxChar
*aStandardLocations
[] =
811 _T("/usr/local/etc"),
813 _T("/usr/public/lib")
816 // first read the system wide file(s)
817 for ( size_t n
= 0; n
< WXSIZEOF(aStandardLocations
); n
++ ) {
818 wxString dir
= aStandardLocations
[n
];
820 wxString file
= dir
+ _T("/mailcap");
821 if ( wxFile::Exists(file
) ) {
825 file
= dir
+ _T("/mime.types");
826 if ( wxFile::Exists(file
) ) {
831 wxString strHome
= wxGetenv(_T("HOME"));
833 // and now the users mailcap
834 wxString strUserMailcap
= strHome
+ _T("/.mailcap");
835 if ( wxFile::Exists(strUserMailcap
) ) {
836 ReadMailcap(strUserMailcap
);
839 // read the users mime.types
840 wxString strUserMimeTypes
= strHome
+ _T("/.mime.types");
841 if ( wxFile::Exists(strUserMimeTypes
) ) {
842 ReadMimeTypes(strUserMimeTypes
);
847 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
849 size_t count
= m_aExtensions
.GetCount();
850 for ( size_t n
= 0; n
< count
; n
++ ) {
851 wxString extensions
= m_aExtensions
[n
];
852 while ( !extensions
.IsEmpty() ) {
853 wxString field
= extensions
.BeforeFirst(_T(' '));
854 extensions
= extensions
.AfterFirst(_T(' '));
856 // consider extensions as not being case-sensitive
857 if ( field
.IsSameAs(ext
, FALSE
/* no case */) ) {
859 wxFileType
*fileType
= new wxFileType
;
860 fileType
->m_impl
->Init(this, n
);
872 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
874 // mime types are not case-sensitive
875 wxString
mimetype(mimeType
);
876 mimetype
.MakeLower();
878 // first look for an exact match
879 int index
= m_aTypes
.Index(mimetype
);
880 if ( index
== wxNOT_FOUND
) {
881 // then try to find "text/*" as match for "text/plain" (for example)
882 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
883 // the whole string - ok.
884 wxString strCategory
= mimetype
.BeforeFirst(_T('/'));
886 size_t nCount
= m_aTypes
.Count();
887 for ( size_t n
= 0; n
< nCount
; n
++ ) {
888 if ( (m_aTypes
[n
].BeforeFirst(_T('/')) == strCategory
) &&
889 m_aTypes
[n
].AfterFirst(_T('/')) == _T("*") ) {
896 if ( index
!= wxNOT_FOUND
) {
897 wxFileType
*fileType
= new wxFileType
;
898 fileType
->m_impl
->Init(this, index
);
908 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString
& strFileName
)
910 wxLogTrace(_T("--- Parsing mime.types file '%s' ---"), strFileName
.c_str());
912 wxTextFile
file(strFileName
);
916 // the information we extract
917 wxString strMimeType
, strDesc
, strExtensions
;
919 size_t nLineCount
= file
.GetLineCount();
920 const wxChar
*pc
= NULL
;
921 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
923 // now we're at the start of the line
924 pc
= file
[nLine
].c_str();
927 // we didn't finish with the previous line yet
932 while ( wxIsspace(*pc
) )
936 if ( *pc
== _T('#') ) {
937 // skip the whole line
942 // detect file format
943 const wxChar
*pEqualSign
= wxStrchr(pc
, _T('='));
944 if ( pEqualSign
== NULL
) {
948 // first field is mime type
949 for ( strMimeType
.Empty(); !wxIsspace(*pc
) && *pc
!= _T('\0'); pc
++ ) {
954 while ( wxIsspace(*pc
) )
957 // take all the rest of the string
967 // the string on the left of '=' is the field name
968 wxString
strLHS(pc
, pEqualSign
- pc
);
971 for ( pc
= pEqualSign
+ 1; wxIsspace(*pc
); pc
++ )
975 if ( *pc
== _T('"') ) {
976 // the string is quoted and ends at the matching quote
977 pEnd
= wxStrchr(++pc
, _T('"'));
978 if ( pEnd
== NULL
) {
979 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
981 strFileName
.c_str(), nLine
+ 1);
985 // unquoted string ends at the first space
986 for ( pEnd
= pc
; !wxIsspace(*pEnd
); pEnd
++ )
990 // now we have the RHS (field value)
991 wxString
strRHS(pc
, pEnd
- pc
);
993 // check what follows this entry
994 if ( *pEnd
== _T('"') ) {
999 for ( pc
= pEnd
; wxIsspace(*pc
); pc
++ )
1002 // if there is something left, it may be either a '\\' to continue
1003 // the line or the next field of the same entry
1004 bool entryEnded
= *pc
== _T('\0'),
1005 nextFieldOnSameLine
= FALSE
;
1006 if ( !entryEnded
) {
1007 nextFieldOnSameLine
= ((*pc
!= _T('\\')) || (pc
[1] != _T('\0')));
1010 // now see what we got
1011 if ( strLHS
== _T("type") ) {
1012 strMimeType
= strRHS
;
1014 else if ( strLHS
== _T("desc") ) {
1017 else if ( strLHS
== _T("exts") ) {
1018 strExtensions
= strRHS
;
1021 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
1022 strFileName
.c_str(), nLine
+ 1, strLHS
.c_str());
1025 if ( !entryEnded
) {
1026 if ( !nextFieldOnSameLine
)
1028 //else: don't reset it
1030 // as we don't reset strMimeType, the next field in this entry
1031 // will be interpreted correctly.
1037 // although it doesn't seem to be covered by RFCs, some programs
1038 // (notably Netscape) create their entries with several comma
1039 // separated extensions (RFC mention the spaces only)
1040 strExtensions
.Replace(_T(","), _T(" "));
1042 // also deal with the leading dot
1043 if ( !strExtensions
.IsEmpty() && strExtensions
[0] == _T('.') ) {
1044 strExtensions
.erase(0, 1);
1047 int index
= m_aTypes
.Index(strMimeType
);
1048 if ( index
== wxNOT_FOUND
) {
1050 m_aTypes
.Add(strMimeType
);
1051 m_aEntries
.Add(NULL
);
1052 m_aExtensions
.Add(strExtensions
);
1053 m_aDescriptions
.Add(strDesc
);
1056 // modify an existing one
1057 if ( !strDesc
.IsEmpty() ) {
1058 m_aDescriptions
[index
] = strDesc
; // replace old value
1060 m_aExtensions
[index
] += strExtensions
;
1063 // finished with this line
1067 // check our data integriry
1068 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1069 m_aTypes
.Count() == m_aExtensions
.Count() &&
1070 m_aTypes
.Count() == m_aDescriptions
.Count() );
1075 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString
& strFileName
,
1078 wxLogTrace(_T("--- Parsing mailcap file '%s' ---"), strFileName
.c_str());
1080 wxTextFile
file(strFileName
);
1084 // see the comments near the end of function for the reason we need these
1085 // variables (search for the next occurence of them)
1086 // indices of MIME types (in m_aTypes) we already found in this file
1087 wxArrayInt aEntryIndices
;
1088 // aLastIndices[n] is the index of last element in
1089 // m_aEntries[aEntryIndices[n]] from this file
1090 wxArrayInt aLastIndices
;
1092 size_t nLineCount
= file
.GetLineCount();
1093 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
1094 // now we're at the start of the line
1095 const wxChar
*pc
= file
[nLine
].c_str();
1098 while ( wxIsspace(*pc
) )
1101 // comment or empty string?
1102 if ( *pc
== _T('#') || *pc
== _T('\0') )
1107 // what field are we currently in? The first 2 are fixed and there may
1108 // be an arbitrary number of other fields -- currently, we are not
1109 // interested in any of them, but we should parse them as well...
1115 } currentToken
= Field_Type
;
1117 // the flags and field values on the current line
1118 bool needsterminal
= FALSE
,
1119 copiousoutput
= FALSE
;
1125 curField
; // accumulator
1126 for ( bool cont
= TRUE
; cont
; pc
++ ) {
1129 // interpret the next character literally (notice that
1130 // backslash can be used for line continuation)
1131 if ( *++pc
== _T('\0') ) {
1132 // fetch the next line.
1134 // pc currently points to nowhere, but after the next
1135 // pc++ in the for line it will point to the beginning
1136 // of the next line in the file
1137 pc
= file
[++nLine
].c_str() - 1;
1140 // just a normal character
1146 cont
= FALSE
; // end of line reached, exit the loop
1151 // store this field and start looking for the next one
1153 // trim whitespaces from both sides
1154 curField
.Trim(TRUE
).Trim(FALSE
);
1156 switch ( currentToken
) {
1159 if ( strType
.Find(_T('/')) == wxNOT_FOUND
) {
1160 // we interpret "type" as "type/*"
1161 strType
+= _T("/*");
1164 currentToken
= Field_OpenCmd
;
1168 strOpenCmd
= curField
;
1170 currentToken
= Field_Other
;
1175 // "good" mailcap entry?
1178 // is this something of the form foo=bar?
1179 const wxChar
*pEq
= wxStrchr(curField
, _T('='));
1180 if ( pEq
!= NULL
) {
1181 wxString lhs
= curField
.BeforeFirst(_T('=')),
1182 rhs
= curField
.AfterFirst(_T('='));
1184 lhs
.Trim(TRUE
); // from right
1185 rhs
.Trim(FALSE
); // from left
1187 if ( lhs
== _T("print") )
1189 else if ( lhs
== _T("test") )
1191 else if ( lhs
== _T("description") ) {
1192 // it might be quoted
1193 if ( rhs
[0u] == _T('"') &&
1194 rhs
.Last() == _T('"') ) {
1195 strDesc
= wxString(rhs
.c_str() + 1,
1202 else if ( lhs
== _T("compose") ||
1203 lhs
== _T("composetyped") ||
1204 lhs
== _T("notes") ||
1212 // no, it's a simple flag
1213 // TODO support the flags:
1214 // 1. create an xterm for 'needsterminal'
1215 // 2. append "| $PAGER" for 'copiousoutput'
1216 if ( curField
== _T("needsterminal") )
1217 needsterminal
= TRUE
;
1218 else if ( curField
== _T("copiousoutput") )
1219 copiousoutput
= TRUE
;
1220 else if ( curField
== _T("textualnewlines") )
1228 // don't flood the user with error messages
1229 // if we don't understand something in his
1230 // mailcap, but give them in debug mode
1231 // because this might be useful for the
1235 _T("Mailcap file %s, line %d: unknown "
1236 "field '%s' for the MIME type "
1238 strFileName
.c_str(),
1246 // it already has this value
1247 //currentToken = Field_Other;
1251 wxFAIL_MSG(_T("unknown field type in mailcap"));
1254 // next token starts immediately after ';'
1263 // check that we really read something reasonable
1264 if ( currentToken
== Field_Type
|| currentToken
== Field_OpenCmd
) {
1265 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
1267 strFileName
.c_str(), nLine
+ 1);
1270 MailCapEntry
*entry
= new MailCapEntry(strOpenCmd
,
1274 strType
.MakeLower();
1275 int nIndex
= m_aTypes
.Index(strType
);
1276 if ( nIndex
== wxNOT_FOUND
) {
1278 m_aTypes
.Add(strType
);
1280 m_aEntries
.Add(entry
);
1281 m_aExtensions
.Add(_T(""));
1282 m_aDescriptions
.Add(strDesc
);
1285 // modify the existing entry: the entries in one and the same
1286 // file are read in top-to-bottom order, i.e. the entries read
1287 // first should be tried before the entries below. However,
1288 // the files read later should override the settings in the
1289 // files read before (except if fallback is TRUE), thus we
1290 // Insert() the new entry to the list if it has already
1291 // occured in _this_ file, but Prepend() it if it occured in
1292 // some of the previous ones and Append() to it in the
1296 // 'fallback' parameter prevents the entries from this
1297 // file from overriding the other ones - always append
1298 MailCapEntry
*entryOld
= m_aEntries
[nIndex
];
1300 entry
->Append(entryOld
);
1302 m_aEntries
[nIndex
] = entry
;
1305 int entryIndex
= aEntryIndices
.Index(nIndex
);
1306 if ( entryIndex
== wxNOT_FOUND
) {
1307 // first time in this file
1308 aEntryIndices
.Add(nIndex
);
1309 aLastIndices
.Add(0);
1311 entry
->Prepend(m_aEntries
[nIndex
]);
1312 m_aEntries
[nIndex
] = entry
;
1315 // not the first time in _this_ file
1316 size_t nEntryIndex
= (size_t)entryIndex
;
1317 MailCapEntry
*entryOld
= m_aEntries
[nIndex
];
1319 entry
->Insert(entryOld
, aLastIndices
[nEntryIndex
]);
1321 m_aEntries
[nIndex
] = entry
;
1323 // the indices were shifted by 1
1324 aLastIndices
[nEntryIndex
]++;
1328 if ( !strDesc
.IsEmpty() ) {
1329 // replace the old one - what else can we do??
1330 m_aDescriptions
[nIndex
] = strDesc
;
1335 // check our data integriry
1336 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1337 m_aTypes
.Count() == m_aExtensions
.Count() &&
1338 m_aTypes
.Count() == m_aDescriptions
.Count() );