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 license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mimetype.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__)
30 #include "wx/string.h"
40 #include "wx/dynarray.h"
41 #include "wx/confbase.h"
44 #include "wx/textfile.h"
47 #include "wx/tokenzr.h"
49 #include "wx/unix/mimetype.h"
51 // other standard headers
54 // in case we're compiling in non-GUI mode
55 class WXDLLEXPORT wxIcon
;
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
62 // this class uses both mailcap and mime.types to gather information about file
65 // The information about mailcap file was extracted from metamail(1) sources and
68 // Format of mailcap file: spaces are ignored, each line is either a comment
69 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
70 // A backslash can be used to quote semicolons and newlines (and, in fact,
71 // anything else including itself).
73 // The first field is always the MIME type in the form of type/subtype (see RFC
74 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
75 // "type" which means the same as "type/*", although I'm not sure whether this
78 // The second field is always the command to run. It is subject to
79 // parameter/filename expansion described below.
81 // All the following fields are optional and may not be present at all. If
82 // they're present they may appear in any order, although each of them should
83 // appear only once. The optional fields are the following:
84 // * notes=xxx is an uninterpreted string which is silently ignored
85 // * test=xxx is the command to be used to determine whether this mailcap line
86 // applies to our data or not. The RHS of this field goes through the
87 // parameter/filename expansion (as the 2nd field) and the resulting string
88 // is executed. The line applies only if the command succeeds, i.e. returns 0
90 // * print=xxx is the command to be used to print (and not view) the data of
91 // this type (parameter/filename expansion is done here too)
92 // * edit=xxx is the command to open/edit the data of this type
93 // * needsterminal means that a new console must be created for the viewer
94 // * copiousoutput means that the viewer doesn't interact with the user but
95 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
96 // good example), thus it might be a good idea to use some kind of paging
98 // * textualnewlines means not to perform CR/LF translation (not honored)
99 // * compose and composetyped fields are used to determine the program to be
100 // called to create a new message pert in the specified format (unused).
102 // Parameter/filename xpansion:
103 // * %s is replaced with the (full) file name
104 // * %t is replaced with MIME type/subtype of the entry
105 // * for multipart type only %n is replaced with the nnumber of parts and %F is
106 // replaced by an array of (content-type, temporary file name) pairs for all
107 // message parts (TODO)
108 // * %{parameter} is replaced with the value of parameter taken from
109 // Content-type header line of the message.
111 // FIXME any docs with real descriptions of these files??
113 // There are 2 possible formats for mime.types file, one entry per line (used
114 // for global mime.types) and "expanded" format where an entry takes multiple
115 // lines (used for users mime.types).
117 // For both formats spaces are ignored and lines starting with a '#' are
118 // comments. Each record has one of two following forms:
119 // a) for "brief" format:
120 // <mime type> <space separated list of extensions>
121 // b) for "expanded" format:
122 // type=<mime type> \ desc="<description>" \ exts="ext"
124 // We try to autodetect the format of mime.types: if a non-comment line starts
125 // with "type=" we assume the second format, otherwise the first one.
127 // there may be more than one entry for one and the same mime type, to
128 // choose the right one we have to run the command specified in the test
129 // field on our data.
134 MailCapEntry(const wxString
& openCmd
,
135 const wxString
& printCmd
,
136 const wxString
& testCmd
)
137 : m_openCmd(openCmd
), m_printCmd(printCmd
), m_testCmd(testCmd
)
144 if (m_next
) delete m_next
;
148 const wxString
& GetOpenCmd() const { return m_openCmd
; }
149 const wxString
& GetPrintCmd() const { return m_printCmd
; }
150 const wxString
& GetTestCmd() const { return m_testCmd
; }
152 MailCapEntry
*GetNext() const { return m_next
; }
155 // prepend this element to the list
156 void Prepend(MailCapEntry
*next
) { m_next
= next
; }
157 // insert into the list at given position
158 void Insert(MailCapEntry
*next
, size_t pos
)
163 for ( cur
= next
; cur
!= NULL
; cur
= cur
->m_next
, n
++ ) {
168 wxASSERT_MSG( n
== pos
, wxT("invalid position in MailCapEntry::Insert") );
170 m_next
= cur
->m_next
;
173 // append this element to the list
174 void Append(MailCapEntry
*next
)
176 wxCHECK_RET( next
!= NULL
, wxT("Append()ing to what?") );
180 for ( cur
= next
; cur
->m_next
!= NULL
; cur
= cur
->m_next
)
185 wxASSERT_MSG( !m_next
, wxT("Append()ing element already in the list?") );
189 wxString m_openCmd
, // command to use to open/view the file
191 m_testCmd
; // only apply this entry if test yields
192 // true (i.e. the command returns 0)
194 MailCapEntry
*m_next
; // in the linked list
198 // the base class which may be used to find an icon for the MIME type
199 class wxMimeTypeIconHandler
202 virtual bool GetIcon(const wxString
& mimetype
, wxIcon
*icon
) = 0;
204 // this function fills manager with MIME types information gathered
205 // (as side effect) when searching for icons. This may be particularly
206 // useful if mime.types is incomplete (e.g. RedHat distributions).
207 virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl
*manager
) = 0;
211 // the icon handler which uses GNOME MIME database
212 class wxGNOMEIconHandler
: public wxMimeTypeIconHandler
215 virtual bool GetIcon(const wxString
& mimetype
, wxIcon
*icon
);
216 virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl
*manager
);
220 void LoadIconsFromKeyFile(const wxString
& filename
);
221 void LoadKeyFilesFromDir(const wxString
& dirbase
);
223 void LoadMimeTypesFromMimeFile(const wxString
& filename
, wxMimeTypesManagerImpl
*manager
);
224 void LoadMimeFilesFromDir(const wxString
& dirbase
, wxMimeTypesManagerImpl
*manager
);
226 static bool m_inited
;
228 static wxSortedArrayString ms_mimetypes
;
229 static wxArrayString ms_icons
;
232 // the icon handler which uses KDE MIME database
233 class wxKDEIconHandler
: public wxMimeTypeIconHandler
236 virtual bool GetIcon(const wxString
& mimetype
, wxIcon
*icon
);
237 virtual void GetMimeInfoRecords(wxMimeTypesManagerImpl
*manager
);
240 void LoadLinksForMimeSubtype(const wxString
& dirbase
,
241 const wxString
& subdir
,
242 const wxString
& filename
,
243 const wxArrayString
& icondirs
);
244 void LoadLinksForMimeType(const wxString
& dirbase
,
245 const wxString
& subdir
,
246 const wxArrayString
& icondirs
);
247 void LoadLinkFilesFromDir(const wxString
& dirbase
,
248 const wxArrayString
& icondirs
);
251 static bool m_inited
;
253 static wxSortedArrayString ms_mimetypes
;
254 static wxArrayString ms_icons
;
256 static wxArrayString ms_infoTypes
;
257 static wxArrayString ms_infoDescriptions
;
258 static wxArrayString ms_infoExtensions
;
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 static wxGNOMEIconHandler gs_iconHandlerGNOME
;
268 static wxKDEIconHandler gs_iconHandlerKDE
;
270 bool wxGNOMEIconHandler::m_inited
= FALSE
;
271 wxSortedArrayString
wxGNOMEIconHandler::ms_mimetypes
;
272 wxArrayString
wxGNOMEIconHandler::ms_icons
;
274 bool wxKDEIconHandler::m_inited
= FALSE
;
275 wxSortedArrayString
wxKDEIconHandler::ms_mimetypes
;
276 wxArrayString
wxKDEIconHandler::ms_icons
;
278 wxArrayString
wxKDEIconHandler::ms_infoTypes
;
279 wxArrayString
wxKDEIconHandler::ms_infoDescriptions
;
280 wxArrayString
wxKDEIconHandler::ms_infoExtensions
;
283 ArrayIconHandlers
wxMimeTypesManagerImpl::ms_iconHandlers
;
285 // ----------------------------------------------------------------------------
286 // wxGNOMEIconHandler
287 // ----------------------------------------------------------------------------
289 // GNOME stores the info we're interested in in several locations:
290 // 1. xxx.keys files under /usr/share/mime-info
291 // 2. xxx.keys files under ~/.gnome/mime-info
293 // The format of xxx.keys file is the following:
298 // with blank lines separating the entries and indented lines starting with
299 // TABs. We're interested in the field icon-filename whose value is the path
300 // containing the icon.
302 void wxGNOMEIconHandler::LoadIconsFromKeyFile(const wxString
& filename
)
304 wxTextFile
textfile(filename
);
305 if ( !textfile
.Open() )
308 // values for the entry being parsed
309 wxString curMimeType
, curIconFile
;
312 size_t nLineCount
= textfile
.GetLineCount();
313 for ( size_t nLine
= 0; ; nLine
++ )
315 if ( nLine
< nLineCount
)
317 pc
= textfile
[nLine
].c_str();
318 if ( *pc
== _T('#') )
326 // so that we will fall into the "if" below
333 if ( !!curMimeType
&& !!curIconFile
)
335 // do we already know this mimetype?
336 int i
= ms_mimetypes
.Index(curMimeType
);
337 if ( i
== wxNOT_FOUND
)
340 size_t n
= ms_mimetypes
.Add(curMimeType
);
341 ms_icons
.Insert(curIconFile
, n
);
345 // replace the existing one (this means that the directories
346 // should be searched in order of increased priority!)
347 ms_icons
[(size_t)i
] = curIconFile
;
353 // the end - this can only happen if nLine == nLineCount
362 // what do we have here?
363 if ( *pc
== _T('\t') )
365 // this is a field=value ling
366 pc
++; // skip leading TAB
368 static const int lenField
= 13; // strlen("icon-filename")
369 if ( wxStrncmp(pc
, _T("icon-filename"), lenField
) == 0 )
371 // skip '=' which follows and take everything left until the end
373 curIconFile
= pc
+ lenField
+ 1;
375 //else: some other field, we don't care
379 // this is the start of the new section
382 while ( *pc
!= _T(':') && *pc
!= _T('\0') )
384 curMimeType
+= *pc
++;
390 void wxGNOMEIconHandler::LoadKeyFilesFromDir(const wxString
& dirbase
)
392 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
393 _T("base directory shouldn't end with a slash") );
395 wxString dirname
= dirbase
;
396 dirname
<< _T("/mime-info");
398 if ( !wxDir::Exists(dirname
) )
402 if ( !dir
.IsOpened() )
405 // we will concatenate it with filename to get the full path below
409 bool cont
= dir
.GetFirst(&filename
, _T("*.keys"), wxDIR_FILES
);
412 LoadIconsFromKeyFile(dirname
+ filename
);
414 cont
= dir
.GetNext(&filename
);
419 void wxGNOMEIconHandler::LoadMimeTypesFromMimeFile(const wxString
& filename
, wxMimeTypesManagerImpl
*manager
)
421 wxTextFile
textfile(filename
);
422 if ( !textfile
.Open() )
425 // values for the entry being parsed
426 wxString curMimeType
, curExtList
;
429 size_t nLineCount
= textfile
.GetLineCount();
430 for ( size_t nLine
= 0; ; nLine
++ )
432 if ( nLine
< nLineCount
)
434 pc
= textfile
[nLine
].c_str();
435 if ( *pc
== _T('#') )
443 // so that we will fall into the "if" below
450 if ( !!curMimeType
&& !!curExtList
)
452 manager
-> AddMimeTypeInfo(curMimeType
, curExtList
, wxEmptyString
);
457 // the end - this can only happen if nLine == nLineCount
466 // what do we have here?
467 if ( *pc
== _T('\t') )
469 // this is a field=value ling
470 pc
++; // skip leading TAB
472 static const int lenField
= 4; // strlen("ext:")
473 if ( wxStrncmp(pc
, _T("ext:"), lenField
) == 0 )
475 // skip ' ' which follows and take everything left until the end
477 curExtList
= pc
+ lenField
+ 1;
479 //else: some other field, we don't care
483 // this is the start of the new section
486 while ( *pc
!= _T(':') && *pc
!= _T('\0') )
488 curMimeType
+= *pc
++;
495 void wxGNOMEIconHandler::LoadMimeFilesFromDir(const wxString
& dirbase
, wxMimeTypesManagerImpl
*manager
)
497 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
498 _T("base directory shouldn't end with a slash") );
500 wxString dirname
= dirbase
;
501 dirname
<< _T("/mime-info");
503 if ( !wxDir::Exists(dirname
) )
507 if ( !dir
.IsOpened() )
510 // we will concatenate it with filename to get the full path below
514 bool cont
= dir
.GetFirst(&filename
, _T("*.mime"), wxDIR_FILES
);
517 LoadMimeTypesFromMimeFile(dirname
+ filename
, manager
);
519 cont
= dir
.GetNext(&filename
);
524 void wxGNOMEIconHandler::Init()
527 dirs
.Add(_T("/usr/share"));
528 dirs
.Add(_T("/usr/local/share"));
531 wxGetHomeDir( &gnomedir
);
532 gnomedir
+= _T("/.gnome");
533 dirs
.Add( gnomedir
);
535 size_t nDirs
= dirs
.GetCount();
536 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
538 LoadKeyFilesFromDir(dirs
[nDir
]);
545 void wxGNOMEIconHandler::GetMimeInfoRecords(wxMimeTypesManagerImpl
*manager
)
553 dirs
.Add(_T("/usr/share"));
554 dirs
.Add(_T("/usr/local/share"));
557 wxGetHomeDir( &gnomedir
);
558 gnomedir
+= _T("/.gnome");
559 dirs
.Add( gnomedir
);
561 size_t nDirs
= dirs
.GetCount();
562 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
564 LoadMimeFilesFromDir(dirs
[nDir
], manager
);
569 #define WXUNUSED_UNLESS_GUI(p) p
571 #define WXUNUSED_UNLESS_GUI(p)
574 bool wxGNOMEIconHandler::GetIcon(const wxString
& mimetype
,
575 wxIcon
* WXUNUSED_UNLESS_GUI(icon
))
582 int index
= ms_mimetypes
.Index(mimetype
);
583 if ( index
== wxNOT_FOUND
)
586 wxString iconname
= ms_icons
[(size_t)index
];
591 if (iconname
.Right(4).MakeUpper() == _T(".XPM"))
592 icn
= wxIcon(iconname
);
594 icn
= wxIcon(iconname
, wxBITMAP_TYPE_ANY
);
601 // helpful for testing in console mode
602 wxLogDebug(_T("Found GNOME icon for '%s': '%s'\n"),
603 mimetype
.c_str(), iconname
.c_str());
609 // ----------------------------------------------------------------------------
611 // ----------------------------------------------------------------------------
613 // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype
614 // may be found in either of the following locations
616 // 1. $KDEDIR/share/mimelnk/mimetype/subtype.kdelnk
617 // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk
619 // The format of a .kdelnk file is almost the same as the one used by
620 // wxFileConfig, i.e. there are groups, comments and entries. The icon is the
621 // value for the entry "Type"
623 void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString
& dirbase
,
624 const wxString
& subdir
,
625 const wxString
& filename
,
626 const wxArrayString
& icondirs
)
628 wxFFile
file(dirbase
+ filename
);
629 if ( !file
.IsOpened() )
632 // construct mimetype from the directory name and the basename of the
633 // file (it always has .kdelnk extension)
635 mimetype
<< subdir
<< _T('/') << filename
.BeforeLast(_T('.'));
637 // these files are small, slurp the entire file at once
639 if ( !file
.ReadAll(&text
) )
645 // before trying to find an icon, grab mimetype information
646 // (because BFU's machine would hardly have well-edited mime.types but (s)he might
647 // have edited it in control panel...)
649 wxString mime_extension
, mime_desc
;
652 if (wxGetLocale() != NULL
)
653 mime_desc
= _T("Comment[") + wxGetLocale()->GetName() + _T("]=");
654 if (pos
== wxNOT_FOUND
) mime_desc
= _T("Comment=");
655 pos
= text
.Find(mime_desc
);
656 if (pos
== wxNOT_FOUND
) mime_desc
= wxEmptyString
;
659 pc
= text
.c_str() + pos
+ mime_desc
.Length();
660 mime_desc
= wxEmptyString
;
661 while ( *pc
&& *pc
!= _T('\n') ) mime_desc
+= *pc
++;
664 pos
= text
.Find(_T("Patterns="));
665 if (pos
!= wxNOT_FOUND
)
668 pc
= text
.c_str() + pos
+ 9;
669 while ( *pc
&& *pc
!= _T('\n') ) exts
+= *pc
++;
670 wxStringTokenizer
tokenizer(exts
, _T(";"));
673 while (tokenizer
.HasMoreTokens())
675 e
= tokenizer
.GetNextToken();
676 if (e
.Left(2) != _T("*.")) continue; // don't support too difficult patterns
677 mime_extension
<< e
.Mid(2);
678 mime_extension
<< _T(' ');
680 mime_extension
.RemoveLast();
683 ms_infoTypes
.Add(mimetype
);
684 ms_infoDescriptions
.Add(mime_desc
);
685 ms_infoExtensions
.Add(mime_extension
);
687 // ok, now we can take care of icon:
689 pos
= text
.Find(_T("Icon="));
690 if ( pos
== wxNOT_FOUND
)
698 pc
= text
.c_str() + pos
+ 5; // 5 == strlen("Icon=")
699 while ( *pc
&& *pc
!= _T('\n') )
706 // we must check if the file exists because it may be stored
707 // in many locations, at least ~/.kde and $KDEDIR
708 size_t nDir
, nDirs
= icondirs
.GetCount();
709 for ( nDir
= 0; nDir
< nDirs
; nDir
++ )
710 if (wxFileExists(icondirs
[nDir
] + icon
))
712 icon
.Prepend(icondirs
[nDir
]);
715 if (nDir
== nDirs
) return; //does not exist
717 // do we already have this MIME type?
718 int i
= ms_mimetypes
.Index(mimetype
);
719 if ( i
== wxNOT_FOUND
)
722 size_t n
= ms_mimetypes
.Add(mimetype
);
723 ms_icons
.Insert(icon
, n
);
727 // replace the old value
728 ms_icons
[(size_t)i
] = icon
;
733 void wxKDEIconHandler::LoadLinksForMimeType(const wxString
& dirbase
,
734 const wxString
& subdir
,
735 const wxArrayString
& icondirs
)
737 wxString dirname
= dirbase
;
740 if ( !dir
.IsOpened() )
746 bool cont
= dir
.GetFirst(&filename
, _T("*.kdelnk"), wxDIR_FILES
);
749 LoadLinksForMimeSubtype(dirname
, subdir
, filename
, icondirs
);
751 cont
= dir
.GetNext(&filename
);
755 void wxKDEIconHandler::LoadLinkFilesFromDir(const wxString
& dirbase
,
756 const wxArrayString
& icondirs
)
758 wxASSERT_MSG( !!dirbase
&& !wxEndsWithPathSeparator(dirbase
),
759 _T("base directory shouldn't end with a slash") );
761 wxString dirname
= dirbase
;
762 dirname
<< _T("/mimelnk");
764 if ( !wxDir::Exists(dirname
) )
768 if ( !dir
.IsOpened() )
771 // we will concatenate it with dir name to get the full path below
775 bool cont
= dir
.GetFirst(&subdir
, wxEmptyString
, wxDIR_DIRS
);
778 LoadLinksForMimeType(dirname
, subdir
, icondirs
);
780 cont
= dir
.GetNext(&subdir
);
784 void wxKDEIconHandler::Init()
787 wxArrayString icondirs
;
789 // settings in ~/.kde have maximal priority
790 dirs
.Add(wxGetHomeDir() + _T("/.kde/share"));
791 icondirs
.Add(wxGetHomeDir() + _T("/.kde/share/icons/"));
793 // the variable KDEDIR is set when KDE is running
794 const char *kdedir
= getenv("KDEDIR");
797 dirs
.Add(wxString(kdedir
) + _T("/share"));
798 icondirs
.Add(wxString(kdedir
) + _T("/share/icons/"));
802 // try to guess KDEDIR
803 dirs
.Add(_T("/usr/share"));
804 dirs
.Add(_T("/opt/kde/share"));
805 icondirs
.Add(_T("/usr/share/icons/"));
806 icondirs
.Add(_T("/usr/X11R6/share/icons/")); // Debian/Corel linux
807 icondirs
.Add(_T("/opt/kde/share/icons/"));
810 size_t nDirs
= dirs
.GetCount();
811 for ( size_t nDir
= 0; nDir
< nDirs
; nDir
++ )
813 LoadLinkFilesFromDir(dirs
[nDir
], icondirs
);
819 bool wxKDEIconHandler::GetIcon(const wxString
& mimetype
,
820 wxIcon
* WXUNUSED_UNLESS_GUI(icon
))
827 int index
= ms_mimetypes
.Index(mimetype
);
828 if ( index
== wxNOT_FOUND
)
831 wxString iconname
= ms_icons
[(size_t)index
];
836 if (iconname
.Right(4).MakeUpper() == _T(".XPM"))
837 icn
= wxIcon(iconname
);
839 icn
= wxIcon(iconname
, wxBITMAP_TYPE_ANY
);
847 // helpful for testing in console mode
848 wxLogDebug(_T("Found KDE icon for '%s': '%s'\n"),
849 mimetype
.c_str(), iconname
.c_str());
856 void wxKDEIconHandler::GetMimeInfoRecords(wxMimeTypesManagerImpl
*manager
)
858 if ( !m_inited
) Init();
860 size_t cnt
= ms_infoTypes
.GetCount();
861 for (unsigned i
= 0; i
< cnt
; i
++)
862 manager
-> AddMimeTypeInfo(ms_infoTypes
[i
], ms_infoExtensions
[i
], ms_infoDescriptions
[i
]);
866 // ----------------------------------------------------------------------------
867 // wxFileTypeImpl (Unix)
868 // ----------------------------------------------------------------------------
871 wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters
& params
) const
874 MailCapEntry
*entry
= m_manager
->m_aEntries
[m_index
[0]];
875 while ( entry
!= NULL
) {
876 // notice that an empty command would always succeed (it's ok)
877 command
= wxFileType::ExpandCommand(entry
->GetTestCmd(), params
);
879 if ( command
.IsEmpty() || (wxSystem(command
) == 0) ) {
881 wxLogTrace(wxT("Test '%s' for mime type '%s' succeeded."),
882 command
.c_str(), params
.GetMimeType().c_str());
886 wxLogTrace(wxT("Test '%s' for mime type '%s' failed."),
887 command
.c_str(), params
.GetMimeType().c_str());
890 entry
= entry
->GetNext();
896 bool wxFileTypeImpl::GetIcon(wxIcon
*icon
) const
898 wxArrayString mimetypes
;
899 GetMimeTypes(mimetypes
);
901 ArrayIconHandlers
& handlers
= m_manager
->GetIconHandlers();
902 size_t count
= handlers
.GetCount();
903 size_t counttypes
= mimetypes
.GetCount();
904 for ( size_t n
= 0; n
< count
; n
++ )
906 for ( size_t n2
= 0; n2
< counttypes
; n2
++ )
908 if ( handlers
[n
]->GetIcon(mimetypes
[n2
], icon
) )
918 wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
921 for (size_t i
= 0; i
< m_index
.GetCount(); i
++)
922 mimeTypes
.Add(m_manager
->m_aTypes
[m_index
[i
]]);
928 wxFileTypeImpl::GetExpandedCommand(wxString
*expandedCmd
,
929 const wxFileType::MessageParameters
& params
,
932 MailCapEntry
*entry
= GetEntry(params
);
933 if ( entry
== NULL
) {
934 // all tests failed...
938 wxString cmd
= open
? entry
->GetOpenCmd() : entry
->GetPrintCmd();
939 if ( cmd
.IsEmpty() ) {
940 // may happen, especially for "print"
944 *expandedCmd
= wxFileType::ExpandCommand(cmd
, params
);
948 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
950 wxString strExtensions
= m_manager
->GetExtension(m_index
[0]);
953 // one extension in the space or comma delimitid list
955 for ( const wxChar
*p
= strExtensions
; ; p
++ ) {
956 if ( *p
== wxT(' ') || *p
== wxT(',') || *p
== wxT('\0') ) {
957 if ( !strExt
.IsEmpty() ) {
958 extensions
.Add(strExt
);
961 //else: repeated spaces (shouldn't happen, but it's not that
962 // important if it does happen)
964 if ( *p
== wxT('\0') )
967 else if ( *p
== wxT('.') ) {
968 // remove the dot from extension (but only if it's the first char)
969 if ( !strExt
.IsEmpty() ) {
972 //else: no, don't append it
982 // ----------------------------------------------------------------------------
983 // wxMimeTypesManagerImpl (Unix)
984 // ----------------------------------------------------------------------------
987 ArrayIconHandlers
& wxMimeTypesManagerImpl::GetIconHandlers()
989 if ( ms_iconHandlers
.GetCount() == 0 )
991 ms_iconHandlers
.Add(&gs_iconHandlerGNOME
);
992 ms_iconHandlers
.Add(&gs_iconHandlerKDE
);
995 return ms_iconHandlers
;
998 // read system and user mailcaps (TODO implement mime.types support)
999 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1001 // directories where we look for mailcap and mime.types by default
1002 // (taken from metamail(1) sources)
1003 static const wxChar
*aStandardLocations
[] =
1007 wxT("/usr/local/etc"),
1009 wxT("/usr/public/lib")
1012 // first read the system wide file(s)
1014 for ( n
= 0; n
< WXSIZEOF(aStandardLocations
); n
++ ) {
1015 wxString dir
= aStandardLocations
[n
];
1017 wxString file
= dir
+ wxT("/mailcap");
1018 if ( wxFile::Exists(file
) ) {
1022 file
= dir
+ wxT("/mime.types");
1023 if ( wxFile::Exists(file
) ) {
1024 ReadMimeTypes(file
);
1028 wxString strHome
= wxGetenv(wxT("HOME"));
1030 // and now the users mailcap
1031 wxString strUserMailcap
= strHome
+ wxT("/.mailcap");
1032 if ( wxFile::Exists(strUserMailcap
) ) {
1033 ReadMailcap(strUserMailcap
);
1036 // read the users mime.types
1037 wxString strUserMimeTypes
= strHome
+ wxT("/.mime.types");
1038 if ( wxFile::Exists(strUserMimeTypes
) ) {
1039 ReadMimeTypes(strUserMimeTypes
);
1042 // read KDE/GNOME tables
1043 ArrayIconHandlers
& handlers
= GetIconHandlers();
1044 size_t count
= handlers
.GetCount();
1045 for ( size_t hn
= 0; hn
< count
; hn
++ )
1046 handlers
[hn
]->GetMimeInfoRecords(this);
1050 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
1052 size_t cnt
= m_aEntries
.GetCount();
1053 for (size_t i
= 0; i
< cnt
; i
++) delete m_aEntries
[i
];
1058 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
1060 wxFileType
*fileType
= NULL
;
1061 size_t count
= m_aExtensions
.GetCount();
1062 for ( size_t n
= 0; n
< count
; n
++ ) {
1063 wxString extensions
= m_aExtensions
[n
];
1064 while ( !extensions
.IsEmpty() ) {
1065 wxString field
= extensions
.BeforeFirst(wxT(' '));
1066 extensions
= extensions
.AfterFirst(wxT(' '));
1068 // consider extensions as not being case-sensitive
1069 if ( field
.IsSameAs(ext
, FALSE
/* no case */) ) {
1071 if (fileType
== NULL
) fileType
= new wxFileType
;
1072 fileType
->m_impl
->Init(this, n
);
1073 // adds this mime type to _list_ of mime types with this extension
1082 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
1084 // mime types are not case-sensitive
1085 wxString
mimetype(mimeType
);
1086 mimetype
.MakeLower();
1088 // first look for an exact match
1089 int index
= m_aTypes
.Index(mimetype
);
1090 if ( index
== wxNOT_FOUND
) {
1091 // then try to find "text/*" as match for "text/plain" (for example)
1092 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
1093 // the whole string - ok.
1094 wxString strCategory
= mimetype
.BeforeFirst(wxT('/'));
1096 size_t nCount
= m_aTypes
.Count();
1097 for ( size_t n
= 0; n
< nCount
; n
++ ) {
1098 if ( (m_aTypes
[n
].BeforeFirst(wxT('/')) == strCategory
) &&
1099 m_aTypes
[n
].AfterFirst(wxT('/')) == wxT("*") ) {
1106 if ( index
!= wxNOT_FOUND
) {
1107 wxFileType
*fileType
= new wxFileType
;
1108 fileType
->m_impl
->Init(this, index
);
1118 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo
& filetype
)
1120 wxString extensions
;
1121 const wxArrayString
& exts
= filetype
.GetExtensions();
1122 size_t nExts
= exts
.GetCount();
1123 for ( size_t nExt
= 0; nExt
< nExts
; nExt
++ ) {
1125 extensions
+= wxT(' ');
1127 extensions
+= exts
[nExt
];
1130 AddMimeTypeInfo(filetype
.GetMimeType(),
1132 filetype
.GetDescription());
1134 AddMailcapInfo(filetype
.GetMimeType(),
1135 filetype
.GetOpenCommand(),
1136 filetype
.GetPrintCommand(),
1138 filetype
.GetDescription());
1141 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString
& strMimeType
,
1142 const wxString
& strExtensions
,
1143 const wxString
& strDesc
)
1145 int index
= m_aTypes
.Index(strMimeType
);
1146 if ( index
== wxNOT_FOUND
) {
1148 m_aTypes
.Add(strMimeType
);
1149 m_aEntries
.Add(NULL
);
1150 m_aExtensions
.Add(strExtensions
);
1151 m_aDescriptions
.Add(strDesc
);
1154 // modify an existing one
1155 if ( !strDesc
.IsEmpty() ) {
1156 m_aDescriptions
[index
] = strDesc
; // replace old value
1158 m_aExtensions
[index
] += ' ' + strExtensions
;
1162 void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString
& strType
,
1163 const wxString
& strOpenCmd
,
1164 const wxString
& strPrintCmd
,
1165 const wxString
& strTest
,
1166 const wxString
& strDesc
)
1168 MailCapEntry
*entry
= new MailCapEntry(strOpenCmd
, strPrintCmd
, strTest
);
1170 int nIndex
= m_aTypes
.Index(strType
);
1171 if ( nIndex
== wxNOT_FOUND
) {
1173 m_aTypes
.Add(strType
);
1175 m_aEntries
.Add(entry
);
1176 m_aExtensions
.Add(wxT(""));
1177 m_aDescriptions
.Add(strDesc
);
1180 // always append the entry in the tail of the list - info added with
1181 // this function can only come from AddFallbacks()
1182 MailCapEntry
*entryOld
= m_aEntries
[nIndex
];
1184 entry
->Append(entryOld
);
1186 m_aEntries
[nIndex
] = entry
;
1190 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString
& strFileName
)
1192 wxLogTrace(wxT("--- Parsing mime.types file '%s' ---"), strFileName
.c_str());
1194 wxTextFile
file(strFileName
);
1198 // the information we extract
1199 wxString strMimeType
, strDesc
, strExtensions
;
1201 size_t nLineCount
= file
.GetLineCount();
1202 const wxChar
*pc
= NULL
;
1203 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
1205 // now we're at the start of the line
1206 pc
= file
[nLine
].c_str();
1209 // we didn't finish with the previous line yet
1214 while ( wxIsspace(*pc
) )
1217 // comment or blank line?
1218 if ( *pc
== wxT('#') || !*pc
) {
1219 // skip the whole line
1224 // detect file format
1225 const wxChar
*pEqualSign
= wxStrchr(pc
, wxT('='));
1226 if ( pEqualSign
== NULL
) {
1230 // first field is mime type
1231 for ( strMimeType
.Empty(); !wxIsspace(*pc
) && *pc
!= wxT('\0'); pc
++ ) {
1236 while ( wxIsspace(*pc
) )
1239 // take all the rest of the string
1242 // no description...
1249 // the string on the left of '=' is the field name
1250 wxString
strLHS(pc
, pEqualSign
- pc
);
1253 for ( pc
= pEqualSign
+ 1; wxIsspace(*pc
); pc
++ )
1257 if ( *pc
== wxT('"') ) {
1258 // the string is quoted and ends at the matching quote
1259 pEnd
= wxStrchr(++pc
, wxT('"'));
1260 if ( pEnd
== NULL
) {
1261 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
1263 strFileName
.c_str(), nLine
+ 1);
1267 // unquoted string ends at the first space
1268 for ( pEnd
= pc
; !wxIsspace(*pEnd
); pEnd
++ )
1272 // now we have the RHS (field value)
1273 wxString
strRHS(pc
, pEnd
- pc
);
1275 // check what follows this entry
1276 if ( *pEnd
== wxT('"') ) {
1281 for ( pc
= pEnd
; wxIsspace(*pc
); pc
++ )
1284 // if there is something left, it may be either a '\\' to continue
1285 // the line or the next field of the same entry
1286 bool entryEnded
= *pc
== wxT('\0'),
1287 nextFieldOnSameLine
= FALSE
;
1288 if ( !entryEnded
) {
1289 nextFieldOnSameLine
= ((*pc
!= wxT('\\')) || (pc
[1] != wxT('\0')));
1292 // now see what we got
1293 if ( strLHS
== wxT("type") ) {
1294 strMimeType
= strRHS
;
1296 else if ( strLHS
== wxT("desc") ) {
1299 else if ( strLHS
== wxT("exts") ) {
1300 strExtensions
= strRHS
;
1303 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
1304 strFileName
.c_str(), nLine
+ 1, strLHS
.c_str());
1307 if ( !entryEnded
) {
1308 if ( !nextFieldOnSameLine
)
1310 //else: don't reset it
1312 // as we don't reset strMimeType, the next field in this entry
1313 // will be interpreted correctly.
1319 // although it doesn't seem to be covered by RFCs, some programs
1320 // (notably Netscape) create their entries with several comma
1321 // separated extensions (RFC mention the spaces only)
1322 strExtensions
.Replace(wxT(","), wxT(" "));
1324 // also deal with the leading dot
1325 if ( !strExtensions
.IsEmpty() && strExtensions
[0u] == wxT('.') )
1327 strExtensions
.erase(0, 1);
1330 AddMimeTypeInfo(strMimeType
, strExtensions
, strDesc
);
1332 // finished with this line
1336 // check our data integriry
1337 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1338 m_aTypes
.Count() == m_aExtensions
.Count() &&
1339 m_aTypes
.Count() == m_aDescriptions
.Count() );
1344 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString
& strFileName
,
1347 wxLogTrace(wxT("--- Parsing mailcap file '%s' ---"), strFileName
.c_str());
1349 wxTextFile
file(strFileName
);
1353 // see the comments near the end of function for the reason we need these
1354 // variables (search for the next occurence of them)
1355 // indices of MIME types (in m_aTypes) we already found in this file
1356 wxArrayInt aEntryIndices
;
1357 // aLastIndices[n] is the index of last element in
1358 // m_aEntries[aEntryIndices[n]] from this file
1359 wxArrayInt aLastIndices
;
1361 size_t nLineCount
= file
.GetLineCount();
1362 for ( size_t nLine
= 0; nLine
< nLineCount
; nLine
++ ) {
1363 // now we're at the start of the line
1364 const wxChar
*pc
= file
[nLine
].c_str();
1367 while ( wxIsspace(*pc
) )
1370 // comment or empty string?
1371 if ( *pc
== wxT('#') || *pc
== wxT('\0') )
1376 // what field are we currently in? The first 2 are fixed and there may
1377 // be an arbitrary number of other fields -- currently, we are not
1378 // interested in any of them, but we should parse them as well...
1384 } currentToken
= Field_Type
;
1386 // the flags and field values on the current line
1387 bool needsterminal
= FALSE
,
1388 copiousoutput
= FALSE
;
1394 curField
; // accumulator
1395 for ( bool cont
= TRUE
; cont
; pc
++ ) {
1398 // interpret the next character literally (notice that
1399 // backslash can be used for line continuation)
1400 if ( *++pc
== wxT('\0') ) {
1401 // fetch the next line.
1403 // pc currently points to nowhere, but after the next
1404 // pc++ in the for line it will point to the beginning
1405 // of the next line in the file
1406 pc
= file
[++nLine
].c_str() - 1;
1409 // just a normal character
1415 cont
= FALSE
; // end of line reached, exit the loop
1420 // store this field and start looking for the next one
1422 // trim whitespaces from both sides
1423 curField
.Trim(TRUE
).Trim(FALSE
);
1425 switch ( currentToken
) {
1428 if ( strType
.Find(wxT('/')) == wxNOT_FOUND
) {
1429 // we interpret "type" as "type/*"
1430 strType
+= wxT("/*");
1433 currentToken
= Field_OpenCmd
;
1437 strOpenCmd
= curField
;
1439 currentToken
= Field_Other
;
1444 // "good" mailcap entry?
1447 // is this something of the form foo=bar?
1448 const wxChar
*pEq
= wxStrchr(curField
, wxT('='));
1449 if ( pEq
!= NULL
) {
1450 wxString lhs
= curField
.BeforeFirst(wxT('=')),
1451 rhs
= curField
.AfterFirst(wxT('='));
1453 lhs
.Trim(TRUE
); // from right
1454 rhs
.Trim(FALSE
); // from left
1456 if ( lhs
== wxT("print") )
1458 else if ( lhs
== wxT("test") )
1460 else if ( lhs
== wxT("description") ) {
1461 // it might be quoted
1462 if ( rhs
[0u] == wxT('"') &&
1463 rhs
.Last() == wxT('"') ) {
1464 strDesc
= wxString(rhs
.c_str() + 1,
1471 else if ( lhs
== wxT("compose") ||
1472 lhs
== wxT("composetyped") ||
1473 lhs
== wxT("notes") ||
1474 lhs
== wxT("edit") )
1481 // no, it's a simple flag
1482 // TODO support the flags:
1483 // 1. create an xterm for 'needsterminal'
1484 // 2. append "| $PAGER" for 'copiousoutput'
1485 if ( curField
== wxT("needsterminal") )
1486 needsterminal
= TRUE
;
1487 else if ( curField
== wxT("copiousoutput") )
1488 copiousoutput
= TRUE
;
1489 else if ( curField
== wxT("textualnewlines") )
1497 // we don't understand this field, but
1498 // Netscape stores info in it, so don't warn
1500 if ( curField
.Left(16u) != "x-mozilla-flags=" )
1502 // don't flood the user with error
1503 // messages if we don't understand
1504 // something in his mailcap, but give
1505 // them in debug mode because this might
1506 // be useful for the programmer
1509 wxT("Mailcap file %s, line %d: "
1510 "unknown field '%s' for the "
1511 "MIME type '%s' ignored."),
1512 strFileName
.c_str(),
1521 // it already has this value
1522 //currentToken = Field_Other;
1526 wxFAIL_MSG(wxT("unknown field type in mailcap"));
1529 // next token starts immediately after ';'
1538 // check that we really read something reasonable
1539 if ( currentToken
== Field_Type
|| currentToken
== Field_OpenCmd
) {
1540 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
1542 strFileName
.c_str(), nLine
+ 1);
1545 MailCapEntry
*entry
= new MailCapEntry(strOpenCmd
,
1549 // NB: because of complications below (we must get entries priority
1550 // right), we can't use AddMailcapInfo() here, unfortunately.
1551 strType
.MakeLower();
1552 int nIndex
= m_aTypes
.Index(strType
);
1553 if ( nIndex
== wxNOT_FOUND
) {
1555 m_aTypes
.Add(strType
);
1557 m_aEntries
.Add(entry
);
1558 m_aExtensions
.Add(wxT(""));
1559 m_aDescriptions
.Add(strDesc
);
1562 // modify the existing entry: the entries in one and the same
1563 // file are read in top-to-bottom order, i.e. the entries read
1564 // first should be tried before the entries below. However,
1565 // the files read later should override the settings in the
1566 // files read before (except if fallback is TRUE), thus we
1567 // Insert() the new entry to the list if it has already
1568 // occured in _this_ file, but Prepend() it if it occured in
1569 // some of the previous ones and Append() to it in the
1573 // 'fallback' parameter prevents the entries from this
1574 // file from overriding the other ones - always append
1575 MailCapEntry
*entryOld
= m_aEntries
[nIndex
];
1577 entry
->Append(entryOld
);
1579 m_aEntries
[nIndex
] = entry
;
1582 int entryIndex
= aEntryIndices
.Index(nIndex
);
1583 if ( entryIndex
== wxNOT_FOUND
) {
1584 // first time in this file
1585 aEntryIndices
.Add(nIndex
);
1586 aLastIndices
.Add(0);
1588 entry
->Prepend(m_aEntries
[nIndex
]);
1589 m_aEntries
[nIndex
] = entry
;
1592 // not the first time in _this_ file
1593 size_t nEntryIndex
= (size_t)entryIndex
;
1594 MailCapEntry
*entryOld
= m_aEntries
[nIndex
];
1596 entry
->Insert(entryOld
, aLastIndices
[nEntryIndex
]);
1598 m_aEntries
[nIndex
] = entry
;
1600 // the indices were shifted by 1
1601 aLastIndices
[nEntryIndex
]++;
1605 if ( !strDesc
.IsEmpty() ) {
1606 // replace the old one - what else can we do??
1607 m_aDescriptions
[nIndex
] = strDesc
;
1612 // check our data integriry
1613 wxASSERT( m_aTypes
.Count() == m_aEntries
.Count() &&
1614 m_aTypes
.Count() == m_aExtensions
.Count() &&
1615 m_aTypes
.Count() == m_aDescriptions
.Count() );
1621 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
1626 size_t count
= m_aTypes
.GetCount();
1627 for ( size_t n
= 0; n
< count
; n
++ )
1629 // don't return template types from here (i.e. anything containg '*')
1631 if ( type
.Find(_T('*')) == wxNOT_FOUND
)
1633 mimetypes
.Add(type
);
1637 return mimetypes
.GetCount();
1641 // wxUSE_FILE && wxUSE_TEXTFILE