]> git.saurik.com Git - wxWidgets.git/blame - src/common/mimetype.cpp
wxConv classes and instances, including wxConv_gdk.
[wxWidgets.git] / src / common / mimetype.cpp
CommitLineData
b13d92d1
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/mimetype.cpp
3// Purpose: classes and functions to manage MIME types
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 23.09.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license (part of wxExtra library)
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13 #pragma implementation "mimetype.h"
14#endif
15
16// ============================================================================
17// declarations
18// ============================================================================
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
24// for compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31// wxWindows
32#ifndef WX_PRECOMP
33 #include "wx/string.h"
2432b92d 34 #include "wx/icon.h"
b13d92d1
VZ
35#endif //WX_PRECOMP
36
3d05544e
JS
37// Doesn't compile in WIN16 mode
38#ifndef __WIN16__
39
b13d92d1
VZ
40#include "wx/log.h"
41#include "wx/intl.h"
42#include "wx/dynarray.h"
2432b92d 43#include "wx/confbase.h"
b13d92d1
VZ
44
45#ifdef __WXMSW__
46 #include "wx/msw/registry.h"
2432b92d 47 #include "windows.h"
b13d92d1
VZ
48#else // Unix
49 #include "wx/textfile.h"
50#endif // OS
51
52#include "wx/mimetype.h"
53
54// other standard headers
55#include <ctype.h>
56
57// ----------------------------------------------------------------------------
58// private classes
59// ----------------------------------------------------------------------------
60
61// implementation classes, platform dependent
62#ifdef __WXMSW__
63
64// These classes use Windows registry to retrieve the required information.
65//
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.
71//
72// 2. "HKCR\.ext" contains
73// a) unnamed value containing the "filetype"
74// b) value "Content Type" containing the MIME type
75//
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
79// an icon file
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)
83
84class wxFileTypeImpl
85{
86public:
87 // ctor
88 wxFileTypeImpl() { }
89
90 // initialize us with our file type name
91 void SetFileType(const wxString& strFileType)
92 { m_strFileType = strFileType; }
93 void SetExt(const wxString& ext)
94 { m_ext = ext; }
95
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"); }
107
108private:
109 // helper function
110 bool GetCommand(wxString *command, const char *verb) const;
111
112 wxString m_strFileType, m_ext;
113};
114
115class wxMimeTypesManagerImpl
116{
117public:
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() { }
121
122 // implement containing class functions
123 wxFileType *GetFileTypeFromExtension(const wxString& ext);
124 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
125
126 // this are NOPs under Windows
be1023d5
VZ
127 bool ReadMailcap(const wxString& filename, bool fallback = TRUE)
128 { return TRUE; }
129 bool ReadMimeTypes(const wxString& filename)
130 { return TRUE; }
b13d92d1
VZ
131};
132
133#else // Unix
134
135// this class uses both mailcap and mime.types to gather information about file
136// types.
137//
138// The information about mailcap file was extracted from metamail(1) sources and
139// documentation.
140//
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).
145//
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
149// is standard.
150//
151// The second field is always the command to run. It is subject to
152// parameter/filename expansion described below.
153//
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
162// exit code.
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
170// mechanism.
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).
174//
ec4768ef 175// Parameter/filename xpansion:
b13d92d1
VZ
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.
183//
184// FIXME any docs with real descriptions of these files??
185//
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).
189//
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>
ec4768ef 194// b) for "expanded" format:
b13d92d1
VZ
195// type=<mime type> \ desc="<description>" \ exts="ext"
196//
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.
199
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.
203class MailCapEntry
204{
205public:
206 // ctor
207 MailCapEntry(const wxString& openCmd,
208 const wxString& printCmd,
209 const wxString& testCmd)
210 : m_openCmd(openCmd), m_printCmd(printCmd), m_testCmd(testCmd)
211 {
212 m_next = NULL;
213 }
214
215 // accessors
216 const wxString& GetOpenCmd() const { return m_openCmd; }
217 const wxString& GetPrintCmd() const { return m_printCmd; }
218 const wxString& GetTestCmd() const { return m_testCmd; }
219
220 MailCapEntry *GetNext() const { return m_next; }
221
222 // operations
223 // prepend this element to the list
224 void Prepend(MailCapEntry *next) { m_next = next; }
cc385968
VZ
225 // insert into the list at given position
226 void Insert(MailCapEntry *next, size_t pos)
227 {
228 // FIXME slooow...
229 MailCapEntry *cur;
230 size_t n = 0;
231 for ( cur = next; cur != NULL; cur = cur->m_next, n++ ) {
232 if ( n == pos )
233 break;
234 }
235
50920146 236 wxASSERT_MSG( n == pos, _T("invalid position in MailCapEntry::Insert") );
cc385968
VZ
237
238 m_next = cur->m_next;
239 cur->m_next = this;
240 }
241 // append this element to the list
b13d92d1
VZ
242 void Append(MailCapEntry *next)
243 {
50920146 244 wxCHECK_RET( next != NULL, _T("Append()ing to what?") );
cc385968 245
b13d92d1
VZ
246 // FIXME slooow...
247 MailCapEntry *cur;
248 for ( cur = next; cur->m_next != NULL; cur = cur->m_next )
249 ;
250
251 cur->m_next = this;
252
50920146 253 wxASSERT_MSG( !m_next, _T("Append()ing element already in the list?") );
b13d92d1
VZ
254 }
255
256private:
257 wxString m_openCmd, // command to use to open/view the file
258 m_printCmd, // print
259 m_testCmd; // only apply this entry if test yields
260 // true (i.e. the command returns 0)
261
262 MailCapEntry *m_next; // in the linked list
263};
264
265WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries);
266
267class wxMimeTypesManagerImpl
268{
269friend class wxFileTypeImpl; // give it access to m_aXXX variables
270
271public:
272 // ctor loads all info into memory for quicker access later on
cc385968 273 // TODO it would be nice to load them all, but parse on demand only...
b13d92d1
VZ
274 wxMimeTypesManagerImpl();
275
276 // implement containing class functions
277 wxFileType *GetFileTypeFromExtension(const wxString& ext);
278 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
279
cc385968
VZ
280 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
281 bool ReadMimeTypes(const wxString& filename);
b13d92d1
VZ
282
283 // accessors
284 // get the string containing space separated extensions for the given
285 // file type
286 wxString GetExtension(size_t index) { return m_aExtensions[index]; }
287
288private:
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
293};
294
295class wxFileTypeImpl
296{
297public:
298 // initialization functions
299 void Init(wxMimeTypesManagerImpl *manager, size_t index)
300 { m_manager = manager; m_index = index; }
301
302 // accessors
303 bool GetExtensions(wxArrayString& extensions);
304 bool GetMimeType(wxString *mimeType) const
305 { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; }
ac91b9d2 306 bool GetIcon(wxIcon * WXUNUSED(icon)) const
cc385968 307 { return FALSE; } // TODO maybe with Gnome/KDE integration...
b13d92d1
VZ
308 bool GetDescription(wxString *desc) const
309 { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; }
310
311 bool GetOpenCommand(wxString *openCmd,
312 const wxFileType::MessageParameters& params) const
313 {
314 return GetExpandedCommand(openCmd, params, TRUE);
315 }
316
317 bool GetPrintCommand(wxString *printCmd,
318 const wxFileType::MessageParameters& params) const
319 {
320 return GetExpandedCommand(printCmd, params, FALSE);
321 }
322
323private:
324 // get the entry which passes the test (may return NULL)
325 MailCapEntry *GetEntry(const wxFileType::MessageParameters& params) const;
326
327 // choose the correct entry to use and expand the command
328 bool GetExpandedCommand(wxString *expandedCmd,
329 const wxFileType::MessageParameters& params,
330 bool open) const;
331
332 wxMimeTypesManagerImpl *m_manager;
333 size_t m_index; // in the wxMimeTypesManagerImpl arrays
334};
335
336#endif // OS type
337
338// ============================================================================
339// implementation of the wrapper classes
340// ============================================================================
341
342// ----------------------------------------------------------------------------
343// wxFileType
344// ----------------------------------------------------------------------------
345
346wxString wxFileType::ExpandCommand(const wxString& command,
347 const wxFileType::MessageParameters& params)
348{
349 bool hasFilename = FALSE;
350
351 wxString str;
50920146
OK
352 for ( const wxChar *pc = command.c_str(); *pc != _T('\0'); pc++ ) {
353 if ( *pc == _T('%') ) {
b13d92d1 354 switch ( *++pc ) {
50920146 355 case _T('s'):
b13d92d1
VZ
356 // '%s' expands into file name (quoted because it might
357 // contain spaces) - except if there are already quotes
341c92a8
VZ
358 // there because otherwise some programs may get confused
359 // by double double quotes
b13d92d1 360#if 0
50920146 361 if ( *(pc - 2) == _T('"') )
b13d92d1
VZ
362 str << params.GetFileName();
363 else
50920146 364 str << _T('"') << params.GetFileName() << _T('"');
b13d92d1
VZ
365#endif
366 str << params.GetFileName();
367 hasFilename = TRUE;
368 break;
369
50920146 370 case _T('t'):
b13d92d1
VZ
371 // '%t' expands into MIME type (quote it too just to be
372 // consistent)
50920146 373 str << _T('\'') << params.GetMimeType() << _T('\'');
b13d92d1
VZ
374 break;
375
50920146 376 case _T('{'):
b13d92d1 377 {
50920146 378 const wxChar *pEnd = wxStrchr(pc, _T('}'));
b13d92d1
VZ
379 if ( pEnd == NULL ) {
380 wxString mimetype;
381 wxLogWarning(_("Unmatched '{' in an entry for "
382 "mime type %s."),
383 params.GetMimeType().c_str());
50920146 384 str << _T("%{");
b13d92d1
VZ
385 }
386 else {
387 wxString param(pc + 1, pEnd - pc - 1);
50920146 388 str << _T('\'') << params.GetParamValue(param) << _T('\'');
b13d92d1
VZ
389 pc = pEnd;
390 }
391 }
392 break;
393
50920146
OK
394 case _T('n'):
395 case _T('F'):
b13d92d1
VZ
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.
399 break;
400
401 default:
50920146 402 wxLogDebug(_T("Unknown field %%%c in command '%s'."),
b13d92d1
VZ
403 *pc, command.c_str());
404 str << *pc;
405 }
406 }
407 else {
408 str << *pc;
409 }
410 }
411
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() ) {
50920146 415 str << _T(" < '") << params.GetFileName() << _T('\'');
b13d92d1
VZ
416 }
417
418 return str;
419}
420
421wxFileType::wxFileType()
422{
423 m_impl = new wxFileTypeImpl;
424}
425
426wxFileType::~wxFileType()
427{
428 delete m_impl;
429}
430
431bool wxFileType::GetExtensions(wxArrayString& extensions)
432{
433 return m_impl->GetExtensions(extensions);
434}
435
436bool wxFileType::GetMimeType(wxString *mimeType) const
437{
438 return m_impl->GetMimeType(mimeType);
439}
440
441bool wxFileType::GetIcon(wxIcon *icon) const
442{
443 return m_impl->GetIcon(icon);
444}
445
446bool wxFileType::GetDescription(wxString *desc) const
447{
448 return m_impl->GetDescription(desc);
449}
450
451bool
452wxFileType::GetOpenCommand(wxString *openCmd,
453 const wxFileType::MessageParameters& params) const
454{
455 return m_impl->GetOpenCommand(openCmd, params);
456}
457
458bool
459wxFileType::GetPrintCommand(wxString *printCmd,
460 const wxFileType::MessageParameters& params) const
461{
462 return m_impl->GetPrintCommand(printCmd, params);
463}
464
465// ----------------------------------------------------------------------------
466// wxMimeTypesManager
467// ----------------------------------------------------------------------------
468
a5a19b83
VZ
469bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
470 const wxString& wildcard)
471{
50920146
OK
472 wxASSERT_MSG( mimeType.Find(_T('*')) == wxNOT_FOUND,
473 _T("first MIME type can't contain wildcards") );
a5a19b83
VZ
474
475 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
50920146 476 if ( wildcard.BeforeFirst(_T('/')).IsSameAs(mimeType.BeforeFirst(_T('/')), FALSE) )
a5a19b83 477 {
50920146 478 wxString strSubtype = wildcard.AfterFirst(_T('/'));
a5a19b83 479
50920146
OK
480 if ( strSubtype == _T('*') ||
481 strSubtype.IsSameAs(mimeType.AfterFirst(_T('/')), FALSE) )
a5a19b83
VZ
482 {
483 // matches (either exactly or it's a wildcard)
484 return TRUE;
485 }
486 }
487
488 return FALSE;
489}
490
b13d92d1
VZ
491wxMimeTypesManager::wxMimeTypesManager()
492{
493 m_impl = new wxMimeTypesManagerImpl;
494}
495
496wxMimeTypesManager::~wxMimeTypesManager()
497{
498 delete m_impl;
499}
500
501wxFileType *
502wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
503{
504 return m_impl->GetFileTypeFromExtension(ext);
505}
506
507wxFileType *
508wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
509{
510 return m_impl->GetFileTypeFromMimeType(mimeType);
511}
512
cc385968 513bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
22b4634c 514{
cc385968 515 return m_impl->ReadMailcap(filename, fallback);
22b4634c
VZ
516}
517
cc385968 518bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
22b4634c 519{
cc385968 520 return m_impl->ReadMimeTypes(filename);
22b4634c
VZ
521}
522
b13d92d1
VZ
523// ============================================================================
524// real (OS specific) implementation
525// ============================================================================
526
527#ifdef __WXMSW__
528
50920146 529bool wxFileTypeImpl::GetCommand(wxString *command, const wxChar *verb) const
b13d92d1
VZ
530{
531 // suppress possible error messages
532 wxLogNull nolog;
533 wxString strKey;
50920146 534 strKey << m_strFileType << _T("\\shell\\") << verb << _T("\\command");
b13d92d1
VZ
535 wxRegKey key(wxRegKey::HKCR, strKey);
536
537 if ( key.Open() ) {
538 // it's the default value of the key
50920146 539 if ( key.QueryValue(_T(""), *command) ) {
b13d92d1 540 // transform it from '%1' to '%s' style format string
cc385968
VZ
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'!
b13d92d1
VZ
544 size_t len = command->Len();
545 for ( size_t n = 0; n < len; n++ ) {
50920146
OK
546 if ( command->GetChar(n) == _T('%') &&
547 (n + 1 < len) && command->GetChar(n + 1) == _T('1') ) {
b13d92d1 548 // replace it with '%s'
50920146 549 command->SetChar(n + 1, _T('s'));
b13d92d1
VZ
550
551 return TRUE;
552 }
553 }
554
555 // we didn't find any '%1'!
cc385968 556 // HACK: append the filename at the end, hope that it will do
50920146 557 *command << _T(" %s");
b13d92d1
VZ
558
559 return TRUE;
560 }
561 }
562
563 // no such file type or no value
564 return FALSE;
565}
566
cc385968 567// TODO this function is half implemented
b13d92d1
VZ
568bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
569{
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...
573 return FALSE;
574 }
575 else {
576 extensions.Empty();
577 extensions.Add(m_ext);
578
579 // it's a lie too, we don't return _all_ extensions...
580 return TRUE;
581 }
582}
583
584bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
585{
586 // suppress possible error messages
587 wxLogNull nolog;
50920146
OK
588 wxRegKey key(wxRegKey::HKCR, /*m_strFileType*/ _T(".") + m_ext);
589 if ( key.Open() && key.QueryValue(_T("Content Type"), *mimeType) ) {
b13d92d1
VZ
590 return TRUE;
591 }
592 else {
593 return FALSE;
594 }
595}
596
597bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
598{
599 wxString strIconKey;
50920146 600 strIconKey << m_strFileType << _T("\\DefaultIcon");
b13d92d1
VZ
601
602 // suppress possible error messages
603 wxLogNull nolog;
604 wxRegKey key(wxRegKey::HKCR, strIconKey);
605
606 if ( key.Open() ) {
607 wxString strIcon;
608 // it's the default value of the key
50920146 609 if ( key.QueryValue(_T(""), strIcon) ) {
b13d92d1
VZ
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 '%'
50920146
OK
613 wxString strFullPath = strIcon.BeforeLast(_T(',')),
614 strIndex = strIcon.AfterLast(_T(','));
b13d92d1 615
3c67202d
VZ
616 // index may be omitted, in which case BeforeLast(',') is empty and
617 // AfterLast(',') is the whole string
b13d92d1
VZ
618 if ( strFullPath.IsEmpty() ) {
619 strFullPath = strIndex;
50920146 620 strIndex = _T("0");
b13d92d1
VZ
621 }
622
623 wxString strExpPath = wxExpandEnvVars(strFullPath);
624 int nIndex = atoi(strIndex);
625
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/...
50920146 630 wxLogDebug(_T("incorrect registry entry '%s': no such icon."),
b13d92d1
VZ
631 key.GetName().c_str());
632 break;
633
634 default:
635 icon->SetHICON((WXHICON)hIcon);
636 return TRUE;
637 }
638 }
639 }
640
641 // no such file type or no value or incorrect icon entry
642 return FALSE;
643}
644
645bool wxFileTypeImpl::GetDescription(wxString *desc) const
646{
647 // suppress possible error messages
648 wxLogNull nolog;
649 wxRegKey key(wxRegKey::HKCR, m_strFileType);
650
651 if ( key.Open() ) {
652 // it's the default value of the key
50920146 653 if ( key.QueryValue(_T(""), *desc) ) {
b13d92d1
VZ
654 return TRUE;
655 }
656 }
657
658 return FALSE;
659}
660
661// extension -> file type
662wxFileType *
663wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
664{
665 // add the leading point if necessary
666 wxString str;
50920146
OK
667 if ( ext[0u] != _T('.') ) {
668 str = _T('.');
b13d92d1
VZ
669 }
670 str << ext;
671
672 // suppress possible error messages
673 wxLogNull nolog;
674
675 wxString strFileType;
676 wxRegKey key(wxRegKey::HKCR, str);
677 if ( key.Open() ) {
678 // it's the default value of the key
50920146 679 if ( key.QueryValue(_T(""), strFileType) ) {
b13d92d1
VZ
680 // create the new wxFileType object
681 wxFileType *fileType = new wxFileType;
682 fileType->m_impl->SetFileType(strFileType);
683 fileType->m_impl->SetExt(ext);
684
685 return fileType;
686 }
687 }
688
689 // unknown extension
690 return NULL;
691}
692
693// MIME type -> extension -> file type
694wxFileType *
695wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
696{
cc385968
VZ
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?
50920146 699 static const wxChar *szMimeDbase = _T("MIME\\Database\\Content Type\\");
b13d92d1
VZ
700
701 wxString strKey = szMimeDbase;
702 strKey << mimeType;
703
704 // suppress possible error messages
705 wxLogNull nolog;
706
707 wxString ext;
708 wxRegKey key(wxRegKey::HKCR, strKey);
709 if ( key.Open() ) {
50920146 710 if ( key.QueryValue(_T("Extension"), ext) ) {
b13d92d1
VZ
711 return GetFileTypeFromExtension(ext);
712 }
713 }
714
715 // unknown MIME type
716 return NULL;
717}
718
719#else // Unix
720
721MailCapEntry *
722wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const
723{
724 wxString command;
725 MailCapEntry *entry = m_manager->m_aEntries[m_index];
726 while ( entry != NULL ) {
cc385968 727 // notice that an empty command would always succeed (it's ok)
b13d92d1
VZ
728 command = wxFileType::ExpandCommand(entry->GetTestCmd(), params);
729
50920146 730 if ( command.IsEmpty() || (wxSystem(command) == 0) ) {
b13d92d1 731 // ok, passed
50920146 732 wxLogTrace(_T("Test '%s' for mime type '%s' succeeded."),
b13d92d1
VZ
733 command.c_str(), params.GetMimeType().c_str());
734 break;
735 }
736 else {
50920146 737 wxLogTrace(_T("Test '%s' for mime type '%s' failed."),
b13d92d1
VZ
738 command.c_str(), params.GetMimeType().c_str());
739 }
740
741 entry = entry->GetNext();
742 }
743
744 return entry;
745}
746
747bool
748wxFileTypeImpl::GetExpandedCommand(wxString *expandedCmd,
749 const wxFileType::MessageParameters& params,
750 bool open) const
751{
752 MailCapEntry *entry = GetEntry(params);
753 if ( entry == NULL ) {
754 // all tests failed...
755 return FALSE;
756 }
757
758 wxString cmd = open ? entry->GetOpenCmd() : entry->GetPrintCmd();
759 if ( cmd.IsEmpty() ) {
760 // may happen, especially for "print"
761 return FALSE;
762 }
763
764 *expandedCmd = wxFileType::ExpandCommand(cmd, params);
765 return TRUE;
766}
767
768bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
769{
770 wxString strExtensions = m_manager->GetExtension(m_index);
771 extensions.Empty();
772
773 // one extension in the space or comma delimitid list
774 wxString strExt;
50920146
OK
775 for ( const wxChar *p = strExtensions; ; p++ ) {
776 if ( *p == _T(' ') || *p == _T(',') || *p == _T('\0') ) {
b13d92d1
VZ
777 if ( !strExt.IsEmpty() ) {
778 extensions.Add(strExt);
779 strExt.Empty();
780 }
781 //else: repeated spaces (shouldn't happen, but it's not that
782 // important if it does happen)
783
50920146 784 if ( *p == _T('\0') )
b13d92d1
VZ
785 break;
786 }
50920146 787 else if ( *p == _T('.') ) {
b13d92d1
VZ
788 // remove the dot from extension (but only if it's the first char)
789 if ( !strExt.IsEmpty() ) {
50920146 790 strExt += _T('.');
b13d92d1
VZ
791 }
792 //else: no, don't append it
793 }
794 else {
795 strExt += *p;
796 }
797 }
798
799 return TRUE;
800}
801
802// read system and user mailcaps (TODO implement mime.types support)
803wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
804{
805 // directories where we look for mailcap and mime.types by default
806 // (taken from metamail(1) sources)
50920146 807 static const wxChar *aStandardLocations[] =
b13d92d1 808 {
50920146
OK
809 _T("/etc"),
810 _T("/usr/etc"),
811 _T("/usr/local/etc"),
812 _T("/etc/mail"),
813 _T("/usr/public/lib")
b13d92d1
VZ
814 };
815
816 // first read the system wide file(s)
817 for ( size_t n = 0; n < WXSIZEOF(aStandardLocations); n++ ) {
818 wxString dir = aStandardLocations[n];
819
50920146 820 wxString file = dir + _T("/mailcap");
b13d92d1
VZ
821 if ( wxFile::Exists(file) ) {
822 ReadMailcap(file);
823 }
824
50920146 825 file = dir + _T("/mime.types");
b13d92d1
VZ
826 if ( wxFile::Exists(file) ) {
827 ReadMimeTypes(file);
828 }
829 }
830
50920146 831 wxString strHome = wxGetenv(_T("HOME"));
b13d92d1
VZ
832
833 // and now the users mailcap
50920146 834 wxString strUserMailcap = strHome + _T("/.mailcap");
b13d92d1
VZ
835 if ( wxFile::Exists(strUserMailcap) ) {
836 ReadMailcap(strUserMailcap);
837 }
838
839 // read the users mime.types
50920146 840 wxString strUserMimeTypes = strHome + _T("/.mime.types");
b13d92d1
VZ
841 if ( wxFile::Exists(strUserMimeTypes) ) {
842 ReadMimeTypes(strUserMimeTypes);
843 }
844}
845
846wxFileType *
847wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
848{
1ee17e1c
VZ
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() ) {
50920146
OK
853 wxString field = extensions.BeforeFirst(_T(' '));
854 extensions = extensions.AfterFirst(_T(' '));
1ee17e1c
VZ
855
856 // consider extensions as not being case-sensitive
ec4768ef 857 if ( field.IsSameAs(ext, FALSE /* no case */) ) {
1ee17e1c
VZ
858 // found
859 wxFileType *fileType = new wxFileType;
860 fileType->m_impl->Init(this, n);
ec4768ef 861
1ee17e1c
VZ
862 return fileType;
863 }
864 }
865 }
b13d92d1 866
1ee17e1c 867 // not found
b13d92d1
VZ
868 return NULL;
869}
870
871wxFileType *
872wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
873{
874 // mime types are not case-sensitive
875 wxString mimetype(mimeType);
876 mimetype.MakeLower();
877
878 // first look for an exact match
879 int index = m_aTypes.Index(mimetype);
3c67202d 880 if ( index == wxNOT_FOUND ) {
b13d92d1 881 // then try to find "text/*" as match for "text/plain" (for example)
3c67202d
VZ
882 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
883 // the whole string - ok.
50920146 884 wxString strCategory = mimetype.BeforeFirst(_T('/'));
b13d92d1
VZ
885
886 size_t nCount = m_aTypes.Count();
887 for ( size_t n = 0; n < nCount; n++ ) {
50920146
OK
888 if ( (m_aTypes[n].BeforeFirst(_T('/')) == strCategory ) &&
889 m_aTypes[n].AfterFirst(_T('/')) == _T("*") ) {
b13d92d1
VZ
890 index = n;
891 break;
892 }
893 }
894 }
895
3c67202d 896 if ( index != wxNOT_FOUND ) {
b13d92d1
VZ
897 wxFileType *fileType = new wxFileType;
898 fileType->m_impl->Init(this, index);
899
900 return fileType;
901 }
902 else {
903 // not found...
904 return NULL;
905 }
906}
907
cc385968 908bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
b13d92d1 909{
50920146 910 wxLogTrace(_T("--- Parsing mime.types file '%s' ---"), strFileName.c_str());
b13d92d1
VZ
911
912 wxTextFile file(strFileName);
913 if ( !file.Open() )
cc385968 914 return FALSE;
b13d92d1
VZ
915
916 // the information we extract
917 wxString strMimeType, strDesc, strExtensions;
918
919 size_t nLineCount = file.GetLineCount();
50920146 920 const wxChar *pc = NULL;
b13d92d1 921 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
22b4634c
VZ
922 if ( pc == NULL ) {
923 // now we're at the start of the line
924 pc = file[nLine].c_str();
925 }
926 else {
927 // we didn't finish with the previous line yet
928 nLine--;
929 }
b13d92d1
VZ
930
931 // skip whitespace
50920146 932 while ( wxIsspace(*pc) )
b13d92d1
VZ
933 pc++;
934
935 // comment?
50920146 936 if ( *pc == _T('#') ) {
22b4634c
VZ
937 // skip the whole line
938 pc = NULL;
b13d92d1 939 continue;
22b4634c 940 }
b13d92d1
VZ
941
942 // detect file format
50920146 943 const wxChar *pEqualSign = wxStrchr(pc, _T('='));
b13d92d1
VZ
944 if ( pEqualSign == NULL ) {
945 // brief format
946 // ------------
947
948 // first field is mime type
50920146 949 for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != _T('\0'); pc++ ) {
b13d92d1
VZ
950 strMimeType += *pc;
951 }
952
953 // skip whitespace
50920146 954 while ( wxIsspace(*pc) )
b13d92d1
VZ
955 pc++;
956
957 // take all the rest of the string
958 strExtensions = pc;
959
960 // no description...
961 strDesc.Empty();
962 }
963 else {
964 // expanded format
965 // ---------------
966
967 // the string on the left of '=' is the field name
968 wxString strLHS(pc, pEqualSign - pc);
969
970 // eat whitespace
50920146 971 for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ )
b13d92d1
VZ
972 ;
973
50920146
OK
974 const wxChar *pEnd;
975 if ( *pc == _T('"') ) {
b13d92d1 976 // the string is quoted and ends at the matching quote
50920146 977 pEnd = wxStrchr(++pc, _T('"'));
b13d92d1
VZ
978 if ( pEnd == NULL ) {
979 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
980 "quoted string."),
981 strFileName.c_str(), nLine + 1);
982 }
983 }
984 else {
22b4634c 985 // unquoted string ends at the first space
50920146 986 for ( pEnd = pc; !wxIsspace(*pEnd); pEnd++ )
b13d92d1
VZ
987 ;
988 }
989
990 // now we have the RHS (field value)
991 wxString strRHS(pc, pEnd - pc);
992
22b4634c 993 // check what follows this entry
50920146 994 if ( *pEnd == _T('"') ) {
b13d92d1
VZ
995 // skip this quote
996 pEnd++;
997 }
998
50920146 999 for ( pc = pEnd; wxIsspace(*pc); pc++ )
b13d92d1
VZ
1000 ;
1001
22b4634c
VZ
1002 // if there is something left, it may be either a '\\' to continue
1003 // the line or the next field of the same entry
50920146 1004 bool entryEnded = *pc == _T('\0'),
22b4634c
VZ
1005 nextFieldOnSameLine = FALSE;
1006 if ( !entryEnded ) {
50920146 1007 nextFieldOnSameLine = ((*pc != _T('\\')) || (pc[1] != _T('\0')));
b13d92d1 1008 }
b13d92d1
VZ
1009
1010 // now see what we got
50920146 1011 if ( strLHS == _T("type") ) {
b13d92d1
VZ
1012 strMimeType = strRHS;
1013 }
50920146 1014 else if ( strLHS == _T("desc") ) {
b13d92d1
VZ
1015 strDesc = strRHS;
1016 }
50920146 1017 else if ( strLHS == _T("exts") ) {
b13d92d1
VZ
1018 strExtensions = strRHS;
1019 }
1020 else {
1021 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
1022 strFileName.c_str(), nLine + 1, strLHS.c_str());
1023 }
1024
1025 if ( !entryEnded ) {
22b4634c
VZ
1026 if ( !nextFieldOnSameLine )
1027 pc = NULL;
1028 //else: don't reset it
1029
1030 // as we don't reset strMimeType, the next field in this entry
b13d92d1 1031 // will be interpreted correctly.
22b4634c 1032
b13d92d1
VZ
1033 continue;
1034 }
1035 }
1036
a1d8eaf7
VZ
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)
50920146 1040 strExtensions.Replace(_T(","), _T(" "));
a1d8eaf7
VZ
1041
1042 // also deal with the leading dot
50920146 1043 if ( !strExtensions.IsEmpty() && strExtensions[0] == _T('.') ) {
a1d8eaf7
VZ
1044 strExtensions.erase(0, 1);
1045 }
1046
b13d92d1 1047 int index = m_aTypes.Index(strMimeType);
3c67202d 1048 if ( index == wxNOT_FOUND ) {
b13d92d1
VZ
1049 // add a new entry
1050 m_aTypes.Add(strMimeType);
1051 m_aEntries.Add(NULL);
1052 m_aExtensions.Add(strExtensions);
1053 m_aDescriptions.Add(strDesc);
1054 }
1055 else {
1056 // modify an existing one
1057 if ( !strDesc.IsEmpty() ) {
1058 m_aDescriptions[index] = strDesc; // replace old value
1059 }
1060 m_aExtensions[index] += strExtensions;
1061 }
22b4634c
VZ
1062
1063 // finished with this line
1064 pc = NULL;
b13d92d1
VZ
1065 }
1066
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() );
cc385968
VZ
1071
1072 return TRUE;
b13d92d1
VZ
1073}
1074
cc385968
VZ
1075bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
1076 bool fallback)
b13d92d1 1077{
50920146 1078 wxLogTrace(_T("--- Parsing mailcap file '%s' ---"), strFileName.c_str());
b13d92d1
VZ
1079
1080 wxTextFile file(strFileName);
1081 if ( !file.Open() )
cc385968 1082 return FALSE;
b13d92d1 1083
cc385968
VZ
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
b13d92d1 1087 wxArrayInt aEntryIndices;
cc385968
VZ
1088 // aLastIndices[n] is the index of last element in
1089 // m_aEntries[aEntryIndices[n]] from this file
1090 wxArrayInt aLastIndices;
b13d92d1
VZ
1091
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
50920146 1095 const wxChar *pc = file[nLine].c_str();
b13d92d1
VZ
1096
1097 // skip whitespace
50920146 1098 while ( wxIsspace(*pc) )
b13d92d1
VZ
1099 pc++;
1100
1101 // comment or empty string?
50920146 1102 if ( *pc == _T('#') || *pc == _T('\0') )
b13d92d1
VZ
1103 continue;
1104
1105 // no, do parse
1106
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...
1110 enum
1111 {
1112 Field_Type,
1113 Field_OpenCmd,
1114 Field_Other
1115 } currentToken = Field_Type;
1116
1117 // the flags and field values on the current line
8fc613f1
RR
1118 bool needsterminal = FALSE,
1119 copiousoutput = FALSE;
b13d92d1
VZ
1120 wxString strType,
1121 strOpenCmd,
1122 strPrintCmd,
1123 strTest,
1124 strDesc,
1125 curField; // accumulator
1126 for ( bool cont = TRUE; cont; pc++ ) {
1127 switch ( *pc ) {
50920146 1128 case _T('\\'):
b13d92d1
VZ
1129 // interpret the next character literally (notice that
1130 // backslash can be used for line continuation)
50920146 1131 if ( *++pc == _T('\0') ) {
b13d92d1
VZ
1132 // fetch the next line.
1133
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;
1138 }
1139 else {
1140 // just a normal character
1141 curField += *pc;
1142 }
1143 break;
1144
50920146 1145 case _T('\0'):
b13d92d1
VZ
1146 cont = FALSE; // end of line reached, exit the loop
1147
1148 // fall through
1149
50920146 1150 case _T(';'):
b13d92d1
VZ
1151 // store this field and start looking for the next one
1152
1153 // trim whitespaces from both sides
1154 curField.Trim(TRUE).Trim(FALSE);
1155
1156 switch ( currentToken ) {
1157 case Field_Type:
1158 strType = curField;
50920146 1159 if ( strType.Find(_T('/')) == wxNOT_FOUND ) {
b13d92d1 1160 // we interpret "type" as "type/*"
50920146 1161 strType += _T("/*");
b13d92d1
VZ
1162 }
1163
1164 currentToken = Field_OpenCmd;
1165 break;
1166
1167 case Field_OpenCmd:
1168 strOpenCmd = curField;
1169
1170 currentToken = Field_Other;
1171 break;
1172
1173 case Field_Other:
1174 {
1175 // "good" mailcap entry?
1176 bool ok = TRUE;
1177
1178 // is this something of the form foo=bar?
50920146 1179 const wxChar *pEq = wxStrchr(curField, _T('='));
b13d92d1 1180 if ( pEq != NULL ) {
50920146
OK
1181 wxString lhs = curField.BeforeFirst(_T('=')),
1182 rhs = curField.AfterFirst(_T('='));
b13d92d1
VZ
1183
1184 lhs.Trim(TRUE); // from right
1185 rhs.Trim(FALSE); // from left
1186
50920146 1187 if ( lhs == _T("print") )
b13d92d1 1188 strPrintCmd = rhs;
50920146 1189 else if ( lhs == _T("test") )
b13d92d1 1190 strTest = rhs;
50920146 1191 else if ( lhs == _T("description") ) {
b13d92d1 1192 // it might be quoted
50920146
OK
1193 if ( rhs[0u] == _T('"') &&
1194 rhs.Last() == _T('"') ) {
b13d92d1
VZ
1195 strDesc = wxString(rhs.c_str() + 1,
1196 rhs.Len() - 2);
1197 }
1198 else {
1199 strDesc = rhs;
1200 }
1201 }
50920146
OK
1202 else if ( lhs == _T("compose") ||
1203 lhs == _T("composetyped") ||
1204 lhs == _T("notes") ||
1205 lhs == _T("edit") )
b13d92d1
VZ
1206 ; // ignore
1207 else
1208 ok = FALSE;
1209
1210 }
1211 else {
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'
50920146 1216 if ( curField == _T("needsterminal") )
b13d92d1 1217 needsterminal = TRUE;
50920146 1218 else if ( curField == _T("copiousoutput") )
b13d92d1 1219 copiousoutput = TRUE;
50920146 1220 else if ( curField == _T("textualnewlines") )
b13d92d1
VZ
1221 ; // ignore
1222 else
1223 ok = FALSE;
1224 }
1225
1226 if ( !ok )
1227 {
1228 // don't flood the user with error messages
1229 // if we don't understand something in his
ec4768ef
VZ
1230 // mailcap, but give them in debug mode
1231 // because this might be useful for the
1232 // programmer
b13d92d1
VZ
1233 wxLogDebug
1234 (
50920146 1235 _T("Mailcap file %s, line %d: unknown "
dd0e574a 1236 "field '%s' for the MIME type "
50920146 1237 "'%s' ignored."),
b13d92d1
VZ
1238 strFileName.c_str(),
1239 nLine + 1,
1240 curField.c_str(),
1241 strType.c_str()
1242 );
1243 }
1244 }
1245
1246 // it already has this value
1247 //currentToken = Field_Other;
1248 break;
1249
1250 default:
50920146 1251 wxFAIL_MSG(_T("unknown field type in mailcap"));
b13d92d1
VZ
1252 }
1253
1254 // next token starts immediately after ';'
1255 curField.Empty();
1256 break;
1257
1258 default:
1259 curField += *pc;
1260 }
1261 }
1262
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 "
1266 "ignored."),
1267 strFileName.c_str(), nLine + 1);
1268 }
1269 else {
1270 MailCapEntry *entry = new MailCapEntry(strOpenCmd,
1271 strPrintCmd,
1272 strTest);
1273
1274 strType.MakeLower();
1275 int nIndex = m_aTypes.Index(strType);
3c67202d 1276 if ( nIndex == wxNOT_FOUND ) {
b13d92d1
VZ
1277 // new file type
1278 m_aTypes.Add(strType);
1279
1280 m_aEntries.Add(entry);
50920146 1281 m_aExtensions.Add(_T(""));
b13d92d1
VZ
1282 m_aDescriptions.Add(strDesc);
1283 }
1284 else {
cc385968
VZ
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
1293 // fallback case
1294
1295 if ( fallback ) {
1296 // 'fallback' parameter prevents the entries from this
1297 // file from overriding the other ones - always append
1298 MailCapEntry *entryOld = m_aEntries[nIndex];
1299 if ( entryOld )
1300 entry->Append(entryOld);
1301 else
1302 m_aEntries[nIndex] = entry;
b13d92d1
VZ
1303 }
1304 else {
cc385968
VZ
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);
1310
1311 entry->Prepend(m_aEntries[nIndex]);
1312 m_aEntries[nIndex] = entry;
1313 }
1314 else {
1315 // not the first time in _this_ file
1316 size_t nEntryIndex = (size_t)entryIndex;
1317 MailCapEntry *entryOld = m_aEntries[nIndex];
1318 if ( entryOld )
1319 entry->Insert(entryOld, aLastIndices[nEntryIndex]);
1320 else
1321 m_aEntries[nIndex] = entry;
1322
1323 // the indices were shifted by 1
1324 aLastIndices[nEntryIndex]++;
1325 }
b13d92d1
VZ
1326 }
1327
1328 if ( !strDesc.IsEmpty() ) {
cc385968 1329 // replace the old one - what else can we do??
b13d92d1
VZ
1330 m_aDescriptions[nIndex] = strDesc;
1331 }
1332 }
1333 }
1334
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() );
1339 }
cc385968
VZ
1340
1341 return TRUE;
b13d92d1
VZ
1342}
1343
1344#endif // OS type
1345
3d05544e
JS
1346#endif
1347 // __WIN16__