]> git.saurik.com Git - wxWidgets.git/blob - src/common/mimetype.cpp
Added configure patch for DEC
[wxWidgets.git] / src / common / mimetype.cpp
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"
34 #include "wx/icon.h"
35 #endif //WX_PRECOMP
36
37 // Doesn't compile in WIN16 mode
38 #ifndef __WIN16__
39
40 #include "wx/log.h"
41 #include "wx/intl.h"
42 #include "wx/dynarray.h"
43 #include "wx/confbase.h"
44
45 #ifdef __WXMSW__
46 #include "wx/msw/registry.h"
47 #include "windows.h"
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
84 class wxFileTypeImpl
85 {
86 public:
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, _T("open")); }
104 bool GetPrintCommand(wxString *printCmd,
105 const wxFileType::MessageParameters&) const
106 { return GetCommand(printCmd, _T("print")); }
107
108 private:
109 // helper function
110 bool GetCommand(wxString *command, const wxChar *verb) const;
111
112 wxString m_strFileType, m_ext;
113 };
114
115 class wxMimeTypesManagerImpl
116 {
117 public:
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
127 bool ReadMailcap(const wxString& filename, bool fallback = TRUE)
128 { return TRUE; }
129 bool ReadMimeTypes(const wxString& filename)
130 { return TRUE; }
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 //
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.
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>
194 // b) for "expanded" format:
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.
203 class MailCapEntry
204 {
205 public:
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; }
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
236 wxASSERT_MSG( n == pos, _T("invalid position in MailCapEntry::Insert") );
237
238 m_next = cur->m_next;
239 cur->m_next = this;
240 }
241 // append this element to the list
242 void Append(MailCapEntry *next)
243 {
244 wxCHECK_RET( next != NULL, _T("Append()ing to what?") );
245
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
253 wxASSERT_MSG( !m_next, _T("Append()ing element already in the list?") );
254 }
255
256 private:
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
265 WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries);
266
267 class wxMimeTypesManagerImpl
268 {
269 friend class wxFileTypeImpl; // give it access to m_aXXX variables
270
271 public:
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();
275
276 // implement containing class functions
277 wxFileType *GetFileTypeFromExtension(const wxString& ext);
278 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
279
280 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
281 bool ReadMimeTypes(const wxString& filename);
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
288 private:
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
295 class wxFileTypeImpl
296 {
297 public:
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; }
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; }
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
323 private:
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
346 wxString wxFileType::ExpandCommand(const wxString& command,
347 const wxFileType::MessageParameters& params)
348 {
349 bool hasFilename = FALSE;
350
351 wxString str;
352 for ( const wxChar *pc = command.c_str(); *pc != _T('\0'); pc++ ) {
353 if ( *pc == _T('%') ) {
354 switch ( *++pc ) {
355 case _T('s'):
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
360 #if 0
361 if ( *(pc - 2) == _T('"') )
362 str << params.GetFileName();
363 else
364 str << _T('"') << params.GetFileName() << _T('"');
365 #endif
366 str << params.GetFileName();
367 hasFilename = TRUE;
368 break;
369
370 case _T('t'):
371 // '%t' expands into MIME type (quote it too just to be
372 // consistent)
373 str << _T('\'') << params.GetMimeType() << _T('\'');
374 break;
375
376 case _T('{'):
377 {
378 const wxChar *pEnd = wxStrchr(pc, _T('}'));
379 if ( pEnd == NULL ) {
380 wxString mimetype;
381 wxLogWarning(_("Unmatched '{' in an entry for "
382 "mime type %s."),
383 params.GetMimeType().c_str());
384 str << _T("%{");
385 }
386 else {
387 wxString param(pc + 1, pEnd - pc - 1);
388 str << _T('\'') << params.GetParamValue(param) << _T('\'');
389 pc = pEnd;
390 }
391 }
392 break;
393
394 case _T('n'):
395 case _T('F'):
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:
402 wxLogDebug(_T("Unknown field %%%c in command '%s'."),
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() ) {
415 str << _T(" < '") << params.GetFileName() << _T('\'');
416 }
417
418 return str;
419 }
420
421 wxFileType::wxFileType()
422 {
423 m_impl = new wxFileTypeImpl;
424 }
425
426 wxFileType::~wxFileType()
427 {
428 delete m_impl;
429 }
430
431 bool wxFileType::GetExtensions(wxArrayString& extensions)
432 {
433 return m_impl->GetExtensions(extensions);
434 }
435
436 bool wxFileType::GetMimeType(wxString *mimeType) const
437 {
438 return m_impl->GetMimeType(mimeType);
439 }
440
441 bool wxFileType::GetIcon(wxIcon *icon) const
442 {
443 return m_impl->GetIcon(icon);
444 }
445
446 bool wxFileType::GetDescription(wxString *desc) const
447 {
448 return m_impl->GetDescription(desc);
449 }
450
451 bool
452 wxFileType::GetOpenCommand(wxString *openCmd,
453 const wxFileType::MessageParameters& params) const
454 {
455 return m_impl->GetOpenCommand(openCmd, params);
456 }
457
458 bool
459 wxFileType::GetPrintCommand(wxString *printCmd,
460 const wxFileType::MessageParameters& params) const
461 {
462 return m_impl->GetPrintCommand(printCmd, params);
463 }
464
465 // ----------------------------------------------------------------------------
466 // wxMimeTypesManager
467 // ----------------------------------------------------------------------------
468
469 bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
470 const wxString& wildcard)
471 {
472 wxASSERT_MSG( mimeType.Find(_T('*')) == wxNOT_FOUND,
473 _T("first MIME type can't contain wildcards") );
474
475 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
476 if ( wildcard.BeforeFirst(_T('/')).IsSameAs(mimeType.BeforeFirst(_T('/')), FALSE) )
477 {
478 wxString strSubtype = wildcard.AfterFirst(_T('/'));
479
480 if ( strSubtype == _T("*") ||
481 strSubtype.IsSameAs(mimeType.AfterFirst(_T('/')), FALSE) )
482 {
483 // matches (either exactly or it's a wildcard)
484 return TRUE;
485 }
486 }
487
488 return FALSE;
489 }
490
491 wxMimeTypesManager::wxMimeTypesManager()
492 {
493 m_impl = new wxMimeTypesManagerImpl;
494 }
495
496 wxMimeTypesManager::~wxMimeTypesManager()
497 {
498 delete m_impl;
499 }
500
501 wxFileType *
502 wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
503 {
504 return m_impl->GetFileTypeFromExtension(ext);
505 }
506
507 wxFileType *
508 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
509 {
510 return m_impl->GetFileTypeFromMimeType(mimeType);
511 }
512
513 bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
514 {
515 return m_impl->ReadMailcap(filename, fallback);
516 }
517
518 bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
519 {
520 return m_impl->ReadMimeTypes(filename);
521 }
522
523 // ============================================================================
524 // real (OS specific) implementation
525 // ============================================================================
526
527 #ifdef __WXMSW__
528
529 bool wxFileTypeImpl::GetCommand(wxString *command, const wxChar *verb) const
530 {
531 // suppress possible error messages
532 wxLogNull nolog;
533 wxString strKey;
534 strKey << m_strFileType << _T("\\shell\\") << verb << _T("\\command");
535 wxRegKey key(wxRegKey::HKCR, strKey);
536
537 if ( key.Open() ) {
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'));
550
551 return TRUE;
552 }
553 }
554
555 // we didn't find any '%1'!
556 // HACK: append the filename at the end, hope that it will do
557 *command << _T(" %s");
558
559 return TRUE;
560 }
561 }
562
563 // no such file type or no value
564 return FALSE;
565 }
566
567 // TODO this function is half implemented
568 bool 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
584 bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
585 {
586 // suppress possible error messages
587 wxLogNull nolog;
588 wxRegKey key(wxRegKey::HKCR, /*m_strFileType*/ _T(".") + m_ext);
589 if ( key.Open() && key.QueryValue(_T("Content Type"), *mimeType) ) {
590 return TRUE;
591 }
592 else {
593 return FALSE;
594 }
595 }
596
597 bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
598 {
599 wxString strIconKey;
600 strIconKey << m_strFileType << _T("\\DefaultIcon");
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
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(','));
615
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;
620 strIndex = _T("0");
621 }
622
623 wxString strExpPath = wxExpandEnvVars(strFullPath);
624 int nIndex = wxAtoi(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/...
630 wxLogDebug(_T("incorrect registry entry '%s': no such icon."),
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
645 bool 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
653 if ( key.QueryValue(_T(""), *desc) ) {
654 return TRUE;
655 }
656 }
657
658 return FALSE;
659 }
660
661 // extension -> file type
662 wxFileType *
663 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
664 {
665 // add the leading point if necessary
666 wxString str;
667 if ( ext[0u] != _T('.') ) {
668 str = _T('.');
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
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);
684
685 return fileType;
686 }
687 }
688
689 // unknown extension
690 return NULL;
691 }
692
693 // MIME type -> extension -> file type
694 wxFileType *
695 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
696 {
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\\");
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() ) {
710 if ( key.QueryValue(_T("Extension"), ext) ) {
711 return GetFileTypeFromExtension(ext);
712 }
713 }
714
715 // unknown MIME type
716 return NULL;
717 }
718
719 #else // Unix
720
721 MailCapEntry *
722 wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const
723 {
724 wxString command;
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);
729
730 if ( command.IsEmpty() || (wxSystem(command) == 0) ) {
731 // ok, passed
732 wxLogTrace(_T("Test '%s' for mime type '%s' succeeded."),
733 command.c_str(), params.GetMimeType().c_str());
734 break;
735 }
736 else {
737 wxLogTrace(_T("Test '%s' for mime type '%s' failed."),
738 command.c_str(), params.GetMimeType().c_str());
739 }
740
741 entry = entry->GetNext();
742 }
743
744 return entry;
745 }
746
747 bool
748 wxFileTypeImpl::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
768 bool 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;
775 for ( const wxChar *p = strExtensions; ; p++ ) {
776 if ( *p == _T(' ') || *p == _T(',') || *p == _T('\0') ) {
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
784 if ( *p == _T('\0') )
785 break;
786 }
787 else if ( *p == _T('.') ) {
788 // remove the dot from extension (but only if it's the first char)
789 if ( !strExt.IsEmpty() ) {
790 strExt += _T('.');
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)
803 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
804 {
805 // directories where we look for mailcap and mime.types by default
806 // (taken from metamail(1) sources)
807 static const wxChar *aStandardLocations[] =
808 {
809 _T("/etc"),
810 _T("/usr/etc"),
811 _T("/usr/local/etc"),
812 _T("/etc/mail"),
813 _T("/usr/public/lib")
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
820 wxString file = dir + _T("/mailcap");
821 if ( wxFile::Exists(file) ) {
822 ReadMailcap(file);
823 }
824
825 file = dir + _T("/mime.types");
826 if ( wxFile::Exists(file) ) {
827 ReadMimeTypes(file);
828 }
829 }
830
831 wxString strHome = wxGetenv(_T("HOME"));
832
833 // and now the users mailcap
834 wxString strUserMailcap = strHome + _T("/.mailcap");
835 if ( wxFile::Exists(strUserMailcap) ) {
836 ReadMailcap(strUserMailcap);
837 }
838
839 // read the users mime.types
840 wxString strUserMimeTypes = strHome + _T("/.mime.types");
841 if ( wxFile::Exists(strUserMimeTypes) ) {
842 ReadMimeTypes(strUserMimeTypes);
843 }
844 }
845
846 wxFileType *
847 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
848 {
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(' '));
855
856 // consider extensions as not being case-sensitive
857 if ( field.IsSameAs(ext, FALSE /* no case */) ) {
858 // found
859 wxFileType *fileType = new wxFileType;
860 fileType->m_impl->Init(this, n);
861
862 return fileType;
863 }
864 }
865 }
866
867 // not found
868 return NULL;
869 }
870
871 wxFileType *
872 wxMimeTypesManagerImpl::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);
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('/'));
885
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("*") ) {
890 index = n;
891 break;
892 }
893 }
894 }
895
896 if ( index != wxNOT_FOUND ) {
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
908 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
909 {
910 wxLogTrace(_T("--- Parsing mime.types file '%s' ---"), strFileName.c_str());
911
912 wxTextFile file(strFileName);
913 if ( !file.Open() )
914 return FALSE;
915
916 // the information we extract
917 wxString strMimeType, strDesc, strExtensions;
918
919 size_t nLineCount = file.GetLineCount();
920 const wxChar *pc = NULL;
921 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
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 }
930
931 // skip whitespace
932 while ( wxIsspace(*pc) )
933 pc++;
934
935 // comment?
936 if ( *pc == _T('#') ) {
937 // skip the whole line
938 pc = NULL;
939 continue;
940 }
941
942 // detect file format
943 const wxChar *pEqualSign = wxStrchr(pc, _T('='));
944 if ( pEqualSign == NULL ) {
945 // brief format
946 // ------------
947
948 // first field is mime type
949 for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != _T('\0'); pc++ ) {
950 strMimeType += *pc;
951 }
952
953 // skip whitespace
954 while ( wxIsspace(*pc) )
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
971 for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ )
972 ;
973
974 const wxChar *pEnd;
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 "
980 "quoted string."),
981 strFileName.c_str(), nLine + 1);
982 }
983 }
984 else {
985 // unquoted string ends at the first space
986 for ( pEnd = pc; !wxIsspace(*pEnd); pEnd++ )
987 ;
988 }
989
990 // now we have the RHS (field value)
991 wxString strRHS(pc, pEnd - pc);
992
993 // check what follows this entry
994 if ( *pEnd == _T('"') ) {
995 // skip this quote
996 pEnd++;
997 }
998
999 for ( pc = pEnd; wxIsspace(*pc); pc++ )
1000 ;
1001
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')));
1008 }
1009
1010 // now see what we got
1011 if ( strLHS == _T("type") ) {
1012 strMimeType = strRHS;
1013 }
1014 else if ( strLHS == _T("desc") ) {
1015 strDesc = strRHS;
1016 }
1017 else if ( strLHS == _T("exts") ) {
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 ) {
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
1031 // will be interpreted correctly.
1032
1033 continue;
1034 }
1035 }
1036
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(" "));
1041
1042 // also deal with the leading dot
1043 if ( !strExtensions.IsEmpty() && strExtensions[0] == _T('.') ) {
1044 strExtensions.erase(0, 1);
1045 }
1046
1047 int index = m_aTypes.Index(strMimeType);
1048 if ( index == wxNOT_FOUND ) {
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 }
1062
1063 // finished with this line
1064 pc = NULL;
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() );
1071
1072 return TRUE;
1073 }
1074
1075 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
1076 bool fallback)
1077 {
1078 wxLogTrace(_T("--- Parsing mailcap file '%s' ---"), strFileName.c_str());
1079
1080 wxTextFile file(strFileName);
1081 if ( !file.Open() )
1082 return FALSE;
1083
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;
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
1095 const wxChar *pc = file[nLine].c_str();
1096
1097 // skip whitespace
1098 while ( wxIsspace(*pc) )
1099 pc++;
1100
1101 // comment or empty string?
1102 if ( *pc == _T('#') || *pc == _T('\0') )
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
1118 bool needsterminal = FALSE,
1119 copiousoutput = FALSE;
1120 wxString strType,
1121 strOpenCmd,
1122 strPrintCmd,
1123 strTest,
1124 strDesc,
1125 curField; // accumulator
1126 for ( bool cont = TRUE; cont; pc++ ) {
1127 switch ( *pc ) {
1128 case _T('\\'):
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.
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
1145 case _T('\0'):
1146 cont = FALSE; // end of line reached, exit the loop
1147
1148 // fall through
1149
1150 case _T(';'):
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;
1159 if ( strType.Find(_T('/')) == wxNOT_FOUND ) {
1160 // we interpret "type" as "type/*"
1161 strType += _T("/*");
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?
1179 const wxChar *pEq = wxStrchr(curField, _T('='));
1180 if ( pEq != NULL ) {
1181 wxString lhs = curField.BeforeFirst(_T('=')),
1182 rhs = curField.AfterFirst(_T('='));
1183
1184 lhs.Trim(TRUE); // from right
1185 rhs.Trim(FALSE); // from left
1186
1187 if ( lhs == _T("print") )
1188 strPrintCmd = rhs;
1189 else if ( lhs == _T("test") )
1190 strTest = rhs;
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,
1196 rhs.Len() - 2);
1197 }
1198 else {
1199 strDesc = rhs;
1200 }
1201 }
1202 else if ( lhs == _T("compose") ||
1203 lhs == _T("composetyped") ||
1204 lhs == _T("notes") ||
1205 lhs == _T("edit") )
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'
1216 if ( curField == _T("needsterminal") )
1217 needsterminal = TRUE;
1218 else if ( curField == _T("copiousoutput") )
1219 copiousoutput = TRUE;
1220 else if ( curField == _T("textualnewlines") )
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
1230 // mailcap, but give them in debug mode
1231 // because this might be useful for the
1232 // programmer
1233 wxLogDebug
1234 (
1235 _T("Mailcap file %s, line %d: unknown "
1236 "field '%s' for the MIME type "
1237 "'%s' ignored."),
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:
1251 wxFAIL_MSG(_T("unknown field type in mailcap"));
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);
1276 if ( nIndex == wxNOT_FOUND ) {
1277 // new file type
1278 m_aTypes.Add(strType);
1279
1280 m_aEntries.Add(entry);
1281 m_aExtensions.Add(_T(""));
1282 m_aDescriptions.Add(strDesc);
1283 }
1284 else {
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;
1303 }
1304 else {
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 }
1326 }
1327
1328 if ( !strDesc.IsEmpty() ) {
1329 // replace the old one - what else can we do??
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 }
1340
1341 return TRUE;
1342 }
1343
1344 #endif // OS type
1345
1346 #endif
1347 // __WIN16__