]> git.saurik.com Git - wxWidgets.git/blob - src/common/mimetype.cpp
cc3415d7e126745a725ed4cc8289b56d0daefd8f
[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, "open"); }
104 bool GetPrintCommand(wxString *printCmd,
105 const wxFileType::MessageParameters&) const
106 { return GetCommand(printCmd, "print"); }
107
108 private:
109 // helper function
110 bool GetCommand(wxString *command, const char *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 void ReadMailcap(const wxString& filename) { }
128 void ReadMimeTypes(const wxString& filename) { }
129 };
130
131 #else // Unix
132
133 // this class uses both mailcap and mime.types to gather information about file
134 // types.
135 //
136 // The information about mailcap file was extracted from metamail(1) sources and
137 // documentation.
138 //
139 // Format of mailcap file: spaces are ignored, each line is either a comment
140 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
141 // A backslash can be used to quote semicolons and newlines (and, in fact,
142 // anything else including itself).
143 //
144 // The first field is always the MIME type in the form of type/subtype (see RFC
145 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
146 // "type" which means the same as "type/*", although I'm not sure whether this
147 // is standard.
148 //
149 // The second field is always the command to run. It is subject to
150 // parameter/filename expansion described below.
151 //
152 // All the following fields are optional and may not be present at all. If
153 // they're present they may appear in any order, although each of them should
154 // appear only once. The optional fields are the following:
155 // * notes=xxx is an uninterpreted string which is silently ignored
156 // * test=xxx is the command to be used to determine whether this mailcap line
157 // applies to our data or not. The RHS of this field goes through the
158 // parameter/filename expansion (as the 2nd field) and the resulting string
159 // is executed. The line applies only if the command succeeds, i.e. returns 0
160 // exit code.
161 // * print=xxx is the command to be used to print (and not view) the data of
162 // this type (parameter/filename expansion is done here too)
163 // * edit=xxx is the command to open/edit the data of this type
164 // * needsterminal means that a new console must be created for the viewer
165 // * copiousoutput means that the viewer doesn't interact with the user but
166 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
167 // good example), thus it might be a good idea to use some kind of paging
168 // mechanism.
169 // * textualnewlines means not to perform CR/LF translation (not honored)
170 // * compose and composetyped fields are used to determine the program to be
171 // called to create a new message pert in the specified format (unused).
172 //
173 // Parameter/filename xpansion:
174 // * %s is replaced with the (full) file name
175 // * %t is replaced with MIME type/subtype of the entry
176 // * for multipart type only %n is replaced with the nnumber of parts and %F is
177 // replaced by an array of (content-type, temporary file name) pairs for all
178 // message parts (TODO)
179 // * %{parameter} is replaced with the value of parameter taken from
180 // Content-type header line of the message.
181 //
182 // FIXME any docs with real descriptions of these files??
183 //
184 // There are 2 possible formats for mime.types file, one entry per line (used
185 // for global mime.types) and "expanded" format where an entry takes multiple
186 // lines (used for users mime.types).
187 //
188 // For both formats spaces are ignored and lines starting with a '#' are
189 // comments. Each record has one of two following forms:
190 // a) for "brief" format:
191 // <mime type> <space separated list of extensions>
192 // b) for "expanded" format:
193 // type=<mime type> \ desc="<description>" \ exts="ext"
194 //
195 // We try to autodetect the format of mime.types: if a non-comment line starts
196 // with "type=" we assume the second format, otherwise the first one.
197
198 // there may be more than one entry for one and the same mime type, to
199 // choose the right one we have to run the command specified in the test
200 // field on our data.
201 class MailCapEntry
202 {
203 public:
204 // ctor
205 MailCapEntry(const wxString& openCmd,
206 const wxString& printCmd,
207 const wxString& testCmd)
208 : m_openCmd(openCmd), m_printCmd(printCmd), m_testCmd(testCmd)
209 {
210 m_next = NULL;
211 }
212
213 // accessors
214 const wxString& GetOpenCmd() const { return m_openCmd; }
215 const wxString& GetPrintCmd() const { return m_printCmd; }
216 const wxString& GetTestCmd() const { return m_testCmd; }
217
218 MailCapEntry *GetNext() const { return m_next; }
219
220 // operations
221 // prepend this element to the list
222 void Prepend(MailCapEntry *next) { m_next = next; }
223 // append to the list
224 void Append(MailCapEntry *next)
225 {
226 // FIXME slooow...
227 MailCapEntry *cur;
228 for ( cur = next; cur->m_next != NULL; cur = cur->m_next )
229 ;
230
231 cur->m_next = this;
232
233 // we initialize it in the ctor and there is no reason to both Prepend()
234 // and Append() one and the same entry
235 wxASSERT( m_next == NULL );
236 }
237
238 private:
239 wxString m_openCmd, // command to use to open/view the file
240 m_printCmd, // print
241 m_testCmd; // only apply this entry if test yields
242 // true (i.e. the command returns 0)
243
244 MailCapEntry *m_next; // in the linked list
245 };
246
247 WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries);
248
249 class wxMimeTypesManagerImpl
250 {
251 friend class wxFileTypeImpl; // give it access to m_aXXX variables
252
253 public:
254 // ctor loads all info into memory for quicker access later on
255 // @@ it would be nice to load them all, but parse on demand only...
256 wxMimeTypesManagerImpl();
257
258 // implement containing class functions
259 wxFileType *GetFileTypeFromExtension(const wxString& ext);
260 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
261
262 void ReadMailcap(const wxString& filename);
263 void ReadMimeTypes(const wxString& filename);
264
265 // accessors
266 // get the string containing space separated extensions for the given
267 // file type
268 wxString GetExtension(size_t index) { return m_aExtensions[index]; }
269
270 private:
271 wxArrayString m_aTypes, // MIME types
272 m_aDescriptions, // descriptions (just some text)
273 m_aExtensions; // space separated list of extensions
274 ArrayTypeEntries m_aEntries; // commands and tests for this file type
275 };
276
277 class wxFileTypeImpl
278 {
279 public:
280 // initialization functions
281 void Init(wxMimeTypesManagerImpl *manager, size_t index)
282 { m_manager = manager; m_index = index; }
283
284 // accessors
285 bool GetExtensions(wxArrayString& extensions);
286 bool GetMimeType(wxString *mimeType) const
287 { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; }
288 bool GetIcon(wxIcon * WXUNUSED(icon)) const
289 { return FALSE; } // @@ maybe with Gnome/KDE integration...
290 bool GetDescription(wxString *desc) const
291 { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; }
292
293 bool GetOpenCommand(wxString *openCmd,
294 const wxFileType::MessageParameters& params) const
295 {
296 return GetExpandedCommand(openCmd, params, TRUE);
297 }
298
299 bool GetPrintCommand(wxString *printCmd,
300 const wxFileType::MessageParameters& params) const
301 {
302 return GetExpandedCommand(printCmd, params, FALSE);
303 }
304
305 private:
306 // get the entry which passes the test (may return NULL)
307 MailCapEntry *GetEntry(const wxFileType::MessageParameters& params) const;
308
309 // choose the correct entry to use and expand the command
310 bool GetExpandedCommand(wxString *expandedCmd,
311 const wxFileType::MessageParameters& params,
312 bool open) const;
313
314 wxMimeTypesManagerImpl *m_manager;
315 size_t m_index; // in the wxMimeTypesManagerImpl arrays
316 };
317
318 #endif // OS type
319
320 // ============================================================================
321 // implementation of the wrapper classes
322 // ============================================================================
323
324 // ----------------------------------------------------------------------------
325 // wxFileType
326 // ----------------------------------------------------------------------------
327
328 wxString wxFileType::ExpandCommand(const wxString& command,
329 const wxFileType::MessageParameters& params)
330 {
331 bool hasFilename = FALSE;
332
333 wxString str;
334 for ( const char *pc = command.c_str(); *pc != '\0'; pc++ ) {
335 if ( *pc == '%' ) {
336 switch ( *++pc ) {
337 case 's':
338 // '%s' expands into file name (quoted because it might
339 // contain spaces) - except if there are already quotes
340 // there because otherwise some programs may get confused
341 // by double double quotes
342 #if 0
343 if ( *(pc - 2) == '"' )
344 str << params.GetFileName();
345 else
346 str << '"' << params.GetFileName() << '"';
347 #endif
348 str << params.GetFileName();
349 hasFilename = TRUE;
350 break;
351
352 case 't':
353 // '%t' expands into MIME type (quote it too just to be
354 // consistent)
355 str << '\'' << params.GetMimeType() << '\'';
356 break;
357
358 case '{':
359 {
360 const char *pEnd = strchr(pc, '}');
361 if ( pEnd == NULL ) {
362 wxString mimetype;
363 wxLogWarning(_("Unmatched '{' in an entry for "
364 "mime type %s."),
365 params.GetMimeType().c_str());
366 str << "%{";
367 }
368 else {
369 wxString param(pc + 1, pEnd - pc - 1);
370 str << '\'' << params.GetParamValue(param) << '\'';
371 pc = pEnd;
372 }
373 }
374 break;
375
376 case 'n':
377 case 'F':
378 // TODO %n is the number of parts, %F is an array containing
379 // the names of temp files these parts were written to
380 // and their mime types.
381 break;
382
383 default:
384 wxLogDebug("Unknown field %%%c in command '%s'.",
385 *pc, command.c_str());
386 str << *pc;
387 }
388 }
389 else {
390 str << *pc;
391 }
392 }
393
394 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
395 // the program will accept the data on stdin: so give it to it!
396 if ( !hasFilename && !str.IsEmpty() ) {
397 str << " < '" << params.GetFileName() << '\'';
398 }
399
400 return str;
401 }
402
403 wxFileType::wxFileType()
404 {
405 m_impl = new wxFileTypeImpl;
406 }
407
408 wxFileType::~wxFileType()
409 {
410 delete m_impl;
411 }
412
413 bool wxFileType::GetExtensions(wxArrayString& extensions)
414 {
415 return m_impl->GetExtensions(extensions);
416 }
417
418 bool wxFileType::GetMimeType(wxString *mimeType) const
419 {
420 return m_impl->GetMimeType(mimeType);
421 }
422
423 bool wxFileType::GetIcon(wxIcon *icon) const
424 {
425 return m_impl->GetIcon(icon);
426 }
427
428 bool wxFileType::GetDescription(wxString *desc) const
429 {
430 return m_impl->GetDescription(desc);
431 }
432
433 bool
434 wxFileType::GetOpenCommand(wxString *openCmd,
435 const wxFileType::MessageParameters& params) const
436 {
437 return m_impl->GetOpenCommand(openCmd, params);
438 }
439
440 bool
441 wxFileType::GetPrintCommand(wxString *printCmd,
442 const wxFileType::MessageParameters& params) const
443 {
444 return m_impl->GetPrintCommand(printCmd, params);
445 }
446
447 // ----------------------------------------------------------------------------
448 // wxMimeTypesManager
449 // ----------------------------------------------------------------------------
450
451 bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
452 const wxString& wildcard)
453 {
454 wxASSERT_MSG( mimeType.Find('*') == wxNOT_FOUND,
455 "first MIME type can't contain wildcards" );
456
457 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
458 if ( wildcard.BeforeFirst('/').IsSameAs(mimeType.BeforeFirst('/'), FALSE) )
459 {
460 wxString strSubtype = wildcard.AfterFirst('/');
461
462 if ( strSubtype == '*' ||
463 strSubtype.IsSameAs(mimeType.AfterFirst('/'), FALSE) )
464 {
465 // matches (either exactly or it's a wildcard)
466 return TRUE;
467 }
468 }
469
470 return FALSE;
471 }
472
473 wxMimeTypesManager::wxMimeTypesManager()
474 {
475 m_impl = new wxMimeTypesManagerImpl;
476 }
477
478 wxMimeTypesManager::~wxMimeTypesManager()
479 {
480 delete m_impl;
481 }
482
483 wxFileType *
484 wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
485 {
486 return m_impl->GetFileTypeFromExtension(ext);
487 }
488
489 wxFileType *
490 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
491 {
492 return m_impl->GetFileTypeFromMimeType(mimeType);
493 }
494
495 void wxMimeTypesManager::ReadMailcap(const wxString& filename)
496 {
497 m_impl->ReadMailcap(filename);
498 }
499
500 void wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
501 {
502 m_impl->ReadMimeTypes(filename);
503 }
504
505 // ============================================================================
506 // real (OS specific) implementation
507 // ============================================================================
508
509 #ifdef __WXMSW__
510
511 bool wxFileTypeImpl::GetCommand(wxString *command, const char *verb) const
512 {
513 // suppress possible error messages
514 wxLogNull nolog;
515 wxString strKey;
516 strKey << m_strFileType << "\\shell\\" << verb << "\\command";
517 wxRegKey key(wxRegKey::HKCR, strKey);
518
519 if ( key.Open() ) {
520 // it's the default value of the key
521 if ( key.QueryValue("", *command) ) {
522 // transform it from '%1' to '%s' style format string
523 // @@ we don't make any attempt to verify that the string is valid,
524 // i.e. doesn't contain %2, or second %1 or .... But we do make
525 // sure that we return a string with _exactly_ one '%s'!
526 size_t len = command->Len();
527 for ( size_t n = 0; n < len; n++ ) {
528 if ( command->GetChar(n) == '%' &&
529 (n + 1 < len) && command->GetChar(n + 1) == '1' ) {
530 // replace it with '%s'
531 command->SetChar(n + 1, 's');
532
533 return TRUE;
534 }
535 }
536
537 // we didn't find any '%1'!
538 // @@@ hack: append the filename at the end, hope that it will do
539 *command << " %s";
540
541 return TRUE;
542 }
543 }
544
545 // no such file type or no value
546 return FALSE;
547 }
548
549 // @@ this function is half implemented
550 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
551 {
552 if ( m_ext.IsEmpty() ) {
553 // the only way to get the list of extensions from the file type is to
554 // scan through all extensions in the registry - too slow...
555 return FALSE;
556 }
557 else {
558 extensions.Empty();
559 extensions.Add(m_ext);
560
561 // it's a lie too, we don't return _all_ extensions...
562 return TRUE;
563 }
564 }
565
566 bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
567 {
568 // suppress possible error messages
569 wxLogNull nolog;
570 wxRegKey key(wxRegKey::HKCR, /*m_strFileType*/ "." + m_ext);
571 if ( key.Open() && key.QueryValue("Content Type", *mimeType) ) {
572 return TRUE;
573 }
574 else {
575 return FALSE;
576 }
577 }
578
579 bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
580 {
581 wxString strIconKey;
582 strIconKey << m_strFileType << "\\DefaultIcon";
583
584 // suppress possible error messages
585 wxLogNull nolog;
586 wxRegKey key(wxRegKey::HKCR, strIconKey);
587
588 if ( key.Open() ) {
589 wxString strIcon;
590 // it's the default value of the key
591 if ( key.QueryValue("", strIcon) ) {
592 // the format is the following: <full path to file>, <icon index>
593 // NB: icon index may be negative as well as positive and the full
594 // path may contain the environment variables inside '%'
595 wxString strFullPath = strIcon.BeforeLast(','),
596 strIndex = strIcon.AfterLast(',');
597
598 // index may be omitted, in which case BeforeLast(',') is empty and
599 // AfterLast(',') is the whole string
600 if ( strFullPath.IsEmpty() ) {
601 strFullPath = strIndex;
602 strIndex = "0";
603 }
604
605 wxString strExpPath = wxExpandEnvVars(strFullPath);
606 int nIndex = atoi(strIndex);
607
608 HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex);
609 switch ( (int)hIcon ) {
610 case 0: // means no icons were found
611 case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/...
612 wxLogDebug("incorrect registry entry '%s': no such icon.",
613 key.GetName().c_str());
614 break;
615
616 default:
617 icon->SetHICON((WXHICON)hIcon);
618 return TRUE;
619 }
620 }
621 }
622
623 // no such file type or no value or incorrect icon entry
624 return FALSE;
625 }
626
627 bool wxFileTypeImpl::GetDescription(wxString *desc) const
628 {
629 // suppress possible error messages
630 wxLogNull nolog;
631 wxRegKey key(wxRegKey::HKCR, m_strFileType);
632
633 if ( key.Open() ) {
634 // it's the default value of the key
635 if ( key.QueryValue("", *desc) ) {
636 return TRUE;
637 }
638 }
639
640 return FALSE;
641 }
642
643 // extension -> file type
644 wxFileType *
645 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
646 {
647 // add the leading point if necessary
648 wxString str;
649 if ( ext[0u] != '.' ) {
650 str = '.';
651 }
652 str << ext;
653
654 // suppress possible error messages
655 wxLogNull nolog;
656
657 wxString strFileType;
658 wxRegKey key(wxRegKey::HKCR, str);
659 if ( key.Open() ) {
660 // it's the default value of the key
661 if ( key.QueryValue("", strFileType) ) {
662 // create the new wxFileType object
663 wxFileType *fileType = new wxFileType;
664 fileType->m_impl->SetFileType(strFileType);
665 fileType->m_impl->SetExt(ext);
666
667 return fileType;
668 }
669 }
670
671 // unknown extension
672 return NULL;
673 }
674
675 // MIME type -> extension -> file type
676 wxFileType *
677 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
678 {
679 // @@@ I don't know of any official documentation which mentions this
680 // location, but as a matter of fact IE uses it, so why not we?
681 static const char *szMimeDbase = "MIME\\Database\\Content Type\\";
682
683 wxString strKey = szMimeDbase;
684 strKey << mimeType;
685
686 // suppress possible error messages
687 wxLogNull nolog;
688
689 wxString ext;
690 wxRegKey key(wxRegKey::HKCR, strKey);
691 if ( key.Open() ) {
692 if ( key.QueryValue("Extension", ext) ) {
693 return GetFileTypeFromExtension(ext);
694 }
695 }
696
697 // unknown MIME type
698 return NULL;
699 }
700
701 #else // Unix
702
703 MailCapEntry *
704 wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const
705 {
706 wxString command;
707 MailCapEntry *entry = m_manager->m_aEntries[m_index];
708 while ( entry != NULL ) {
709 // notice that an empty command would always succeed (@@ is it ok?)
710 command = wxFileType::ExpandCommand(entry->GetTestCmd(), params);
711
712 if ( command.IsEmpty() || (system(command) == 0) ) {
713 // ok, passed
714 wxLogTrace("Test '%s' for mime type '%s' succeeded.",
715 command.c_str(), params.GetMimeType().c_str());
716 break;
717 }
718 else {
719 wxLogTrace("Test '%s' for mime type '%s' failed.",
720 command.c_str(), params.GetMimeType().c_str());
721 }
722
723 entry = entry->GetNext();
724 }
725
726 return entry;
727 }
728
729 bool
730 wxFileTypeImpl::GetExpandedCommand(wxString *expandedCmd,
731 const wxFileType::MessageParameters& params,
732 bool open) const
733 {
734 MailCapEntry *entry = GetEntry(params);
735 if ( entry == NULL ) {
736 // all tests failed...
737 return FALSE;
738 }
739
740 wxString cmd = open ? entry->GetOpenCmd() : entry->GetPrintCmd();
741 if ( cmd.IsEmpty() ) {
742 // may happen, especially for "print"
743 return FALSE;
744 }
745
746 *expandedCmd = wxFileType::ExpandCommand(cmd, params);
747 return TRUE;
748 }
749
750 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
751 {
752 wxString strExtensions = m_manager->GetExtension(m_index);
753 extensions.Empty();
754
755 // one extension in the space or comma delimitid list
756 wxString strExt;
757 for ( const char *p = strExtensions; ; p++ ) {
758 if ( *p == ' ' || *p == ',' || *p == '\0' ) {
759 if ( !strExt.IsEmpty() ) {
760 extensions.Add(strExt);
761 strExt.Empty();
762 }
763 //else: repeated spaces (shouldn't happen, but it's not that
764 // important if it does happen)
765
766 if ( *p == '\0' )
767 break;
768 }
769 else if ( *p == '.' ) {
770 // remove the dot from extension (but only if it's the first char)
771 if ( !strExt.IsEmpty() ) {
772 strExt += '.';
773 }
774 //else: no, don't append it
775 }
776 else {
777 strExt += *p;
778 }
779 }
780
781 return TRUE;
782 }
783
784 // read system and user mailcaps (TODO implement mime.types support)
785 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
786 {
787 // directories where we look for mailcap and mime.types by default
788 // (taken from metamail(1) sources)
789 static const char *aStandardLocations[] =
790 {
791 "/etc",
792 "/usr/etc",
793 "/usr/local/etc",
794 "/etc/mail",
795 "/usr/public/lib"
796 };
797
798 // first read the system wide file(s)
799 for ( size_t n = 0; n < WXSIZEOF(aStandardLocations); n++ ) {
800 wxString dir = aStandardLocations[n];
801
802 wxString file = dir + "/mailcap";
803 if ( wxFile::Exists(file) ) {
804 ReadMailcap(file);
805 }
806
807 file = dir + "/mime.types";
808 if ( wxFile::Exists(file) ) {
809 ReadMimeTypes(file);
810 }
811 }
812
813 wxString strHome = getenv("HOME");
814
815 // and now the users mailcap
816 wxString strUserMailcap = strHome + "/.mailcap";
817 if ( wxFile::Exists(strUserMailcap) ) {
818 ReadMailcap(strUserMailcap);
819 }
820
821 // read the users mime.types
822 wxString strUserMimeTypes = strHome + "/.mime.types";
823 if ( wxFile::Exists(strUserMimeTypes) ) {
824 ReadMimeTypes(strUserMimeTypes);
825 }
826 }
827
828 wxFileType *
829 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
830 {
831 size_t count = m_aExtensions.GetCount();
832 for ( size_t n = 0; n < count; n++ ) {
833 wxString extensions = m_aExtensions[n];
834 while ( !extensions.IsEmpty() ) {
835 wxString field = extensions.BeforeFirst(' ');
836 extensions = extensions.AfterFirst(' ');
837
838 // consider extensions as not being case-sensitive
839 if ( field.IsSameAs(ext, FALSE /* no case */) ) {
840 // found
841 wxFileType *fileType = new wxFileType;
842 fileType->m_impl->Init(this, n);
843
844 return fileType;
845 }
846 }
847 }
848
849 // not found
850 return NULL;
851 }
852
853 wxFileType *
854 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
855 {
856 // mime types are not case-sensitive
857 wxString mimetype(mimeType);
858 mimetype.MakeLower();
859
860 // first look for an exact match
861 int index = m_aTypes.Index(mimetype);
862 if ( index == wxNOT_FOUND ) {
863 // then try to find "text/*" as match for "text/plain" (for example)
864 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
865 // the whole string - ok.
866 wxString strCategory = mimetype.BeforeFirst('/');
867
868 size_t nCount = m_aTypes.Count();
869 for ( size_t n = 0; n < nCount; n++ ) {
870 if ( (m_aTypes[n].BeforeFirst('/') == strCategory ) &&
871 m_aTypes[n].AfterFirst('/') == "*" ) {
872 index = n;
873 break;
874 }
875 }
876 }
877
878 if ( index != wxNOT_FOUND ) {
879 wxFileType *fileType = new wxFileType;
880 fileType->m_impl->Init(this, index);
881
882 return fileType;
883 }
884 else {
885 // not found...
886 return NULL;
887 }
888 }
889
890 void wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
891 {
892 wxLogTrace("--- Parsing mime.types file '%s' ---", strFileName.c_str());
893
894 wxTextFile file(strFileName);
895 if ( !file.Open() )
896 return;
897
898 // the information we extract
899 wxString strMimeType, strDesc, strExtensions;
900
901 size_t nLineCount = file.GetLineCount();
902 const char *pc = NULL;
903 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
904 if ( pc == NULL ) {
905 // now we're at the start of the line
906 pc = file[nLine].c_str();
907 }
908 else {
909 // we didn't finish with the previous line yet
910 nLine--;
911 }
912
913 // skip whitespace
914 while ( isspace(*pc) )
915 pc++;
916
917 // comment?
918 if ( *pc == '#' ) {
919 // skip the whole line
920 pc = NULL;
921 continue;
922 }
923
924 // detect file format
925 const char *pEqualSign = strchr(pc, '=');
926 if ( pEqualSign == NULL ) {
927 // brief format
928 // ------------
929
930 // first field is mime type
931 for ( strMimeType.Empty(); !isspace(*pc) && *pc != '\0'; pc++ ) {
932 strMimeType += *pc;
933 }
934
935 // skip whitespace
936 while ( isspace(*pc) )
937 pc++;
938
939 // take all the rest of the string
940 strExtensions = pc;
941
942 // no description...
943 strDesc.Empty();
944 }
945 else {
946 // expanded format
947 // ---------------
948
949 // the string on the left of '=' is the field name
950 wxString strLHS(pc, pEqualSign - pc);
951
952 // eat whitespace
953 for ( pc = pEqualSign + 1; isspace(*pc); pc++ )
954 ;
955
956 const char *pEnd;
957 if ( *pc == '"' ) {
958 // the string is quoted and ends at the matching quote
959 pEnd = strchr(++pc, '"');
960 if ( pEnd == NULL ) {
961 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
962 "quoted string."),
963 strFileName.c_str(), nLine + 1);
964 }
965 }
966 else {
967 // unquoted string ends at the first space
968 for ( pEnd = pc; !isspace(*pEnd); pEnd++ )
969 ;
970 }
971
972 // now we have the RHS (field value)
973 wxString strRHS(pc, pEnd - pc);
974
975 // check what follows this entry
976 if ( *pEnd == '"' ) {
977 // skip this quote
978 pEnd++;
979 }
980
981 for ( pc = pEnd; isspace(*pc); pc++ )
982 ;
983
984 // if there is something left, it may be either a '\\' to continue
985 // the line or the next field of the same entry
986 bool entryEnded = *pc == '\0',
987 nextFieldOnSameLine = FALSE;
988 if ( !entryEnded ) {
989 nextFieldOnSameLine = ((*pc != '\\') || (pc[1] != '\0'));
990 }
991
992 // now see what we got
993 if ( strLHS == "type" ) {
994 strMimeType = strRHS;
995 }
996 else if ( strLHS == "desc" ) {
997 strDesc = strRHS;
998 }
999 else if ( strLHS == "exts" ) {
1000 strExtensions = strRHS;
1001 }
1002 else {
1003 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
1004 strFileName.c_str(), nLine + 1, strLHS.c_str());
1005 }
1006
1007 if ( !entryEnded ) {
1008 if ( !nextFieldOnSameLine )
1009 pc = NULL;
1010 //else: don't reset it
1011
1012 // as we don't reset strMimeType, the next field in this entry
1013 // will be interpreted correctly.
1014
1015 continue;
1016 }
1017 }
1018
1019 // although it doesn't seem to be covered by RFCs, some programs
1020 // (notably Netscape) create their entries with several comma
1021 // separated extensions (RFC mention the spaces only)
1022 strExtensions.Replace(",", " ");
1023
1024 // also deal with the leading dot
1025 if ( !strExtensions.IsEmpty() && strExtensions[0] == '.' ) {
1026 strExtensions.erase(0, 1);
1027 }
1028
1029 int index = m_aTypes.Index(strMimeType);
1030 if ( index == wxNOT_FOUND ) {
1031 // add a new entry
1032 m_aTypes.Add(strMimeType);
1033 m_aEntries.Add(NULL);
1034 m_aExtensions.Add(strExtensions);
1035 m_aDescriptions.Add(strDesc);
1036 }
1037 else {
1038 // modify an existing one
1039 if ( !strDesc.IsEmpty() ) {
1040 m_aDescriptions[index] = strDesc; // replace old value
1041 }
1042 m_aExtensions[index] += strExtensions;
1043 }
1044
1045 // finished with this line
1046 pc = NULL;
1047 }
1048
1049 // check our data integriry
1050 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
1051 m_aTypes.Count() == m_aExtensions.Count() &&
1052 m_aTypes.Count() == m_aDescriptions.Count() );
1053 }
1054
1055 void wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName)
1056 {
1057 wxLogTrace("--- Parsing mailcap file '%s' ---", strFileName.c_str());
1058
1059 wxTextFile file(strFileName);
1060 if ( !file.Open() )
1061 return;
1062
1063 // see the comments near the end of function for the reason we need this
1064 wxArrayInt aEntryIndices;
1065
1066 size_t nLineCount = file.GetLineCount();
1067 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
1068 // now we're at the start of the line
1069 const char *pc = file[nLine].c_str();
1070
1071 // skip whitespace
1072 while ( isspace(*pc) )
1073 pc++;
1074
1075 // comment or empty string?
1076 if ( *pc == '#' || *pc == '\0' )
1077 continue;
1078
1079 // no, do parse
1080
1081 // what field are we currently in? The first 2 are fixed and there may
1082 // be an arbitrary number of other fields -- currently, we are not
1083 // interested in any of them, but we should parse them as well...
1084 enum
1085 {
1086 Field_Type,
1087 Field_OpenCmd,
1088 Field_Other
1089 } currentToken = Field_Type;
1090
1091 // the flags and field values on the current line
1092 bool needsterminal = FALSE,
1093 copiousoutput = FALSE;
1094 wxString strType,
1095 strOpenCmd,
1096 strPrintCmd,
1097 strTest,
1098 strDesc,
1099 curField; // accumulator
1100 for ( bool cont = TRUE; cont; pc++ ) {
1101 switch ( *pc ) {
1102 case '\\':
1103 // interpret the next character literally (notice that
1104 // backslash can be used for line continuation)
1105 if ( *++pc == '\0' ) {
1106 // fetch the next line.
1107
1108 // pc currently points to nowhere, but after the next
1109 // pc++ in the for line it will point to the beginning
1110 // of the next line in the file
1111 pc = file[++nLine].c_str() - 1;
1112 }
1113 else {
1114 // just a normal character
1115 curField += *pc;
1116 }
1117 break;
1118
1119 case '\0':
1120 cont = FALSE; // end of line reached, exit the loop
1121
1122 // fall through
1123
1124 case ';':
1125 // store this field and start looking for the next one
1126
1127 // trim whitespaces from both sides
1128 curField.Trim(TRUE).Trim(FALSE);
1129
1130 switch ( currentToken ) {
1131 case Field_Type:
1132 strType = curField;
1133 if ( strType.Find('/') == wxNOT_FOUND ) {
1134 // we interpret "type" as "type/*"
1135 strType += "/*";
1136 }
1137
1138 currentToken = Field_OpenCmd;
1139 break;
1140
1141 case Field_OpenCmd:
1142 strOpenCmd = curField;
1143
1144 currentToken = Field_Other;
1145 break;
1146
1147 case Field_Other:
1148 {
1149 // "good" mailcap entry?
1150 bool ok = TRUE;
1151
1152 // is this something of the form foo=bar?
1153 const char *pEq = strchr(curField, '=');
1154 if ( pEq != NULL ) {
1155 wxString lhs = curField.BeforeFirst('='),
1156 rhs = curField.AfterFirst('=');
1157
1158 lhs.Trim(TRUE); // from right
1159 rhs.Trim(FALSE); // from left
1160
1161 if ( lhs == "print" )
1162 strPrintCmd = rhs;
1163 else if ( lhs == "test" )
1164 strTest = rhs;
1165 else if ( lhs == "description" ) {
1166 // it might be quoted
1167 if ( rhs[0u] == '"' &&
1168 rhs.Last() == '"' ) {
1169 strDesc = wxString(rhs.c_str() + 1,
1170 rhs.Len() - 2);
1171 }
1172 else {
1173 strDesc = rhs;
1174 }
1175 }
1176 else if ( lhs == "compose" ||
1177 lhs == "composetyped" ||
1178 lhs == "notes" ||
1179 lhs == "edit" )
1180 ; // ignore
1181 else
1182 ok = FALSE;
1183
1184 }
1185 else {
1186 // no, it's a simple flag
1187 // TODO support the flags:
1188 // 1. create an xterm for 'needsterminal'
1189 // 2. append "| $PAGER" for 'copiousoutput'
1190 if ( curField == "needsterminal" )
1191 needsterminal = TRUE;
1192 else if ( curField == "copiousoutput" )
1193 copiousoutput = TRUE;
1194 else if ( curField == "textualnewlines" )
1195 ; // ignore
1196 else
1197 ok = FALSE;
1198 }
1199
1200 if ( !ok )
1201 {
1202 // don't flood the user with error messages
1203 // if we don't understand something in his
1204 // mailcap, but give them in debug mode
1205 // because this might be useful for the
1206 // programmer
1207 wxLogDebug
1208 (
1209 "Mailcap file %s, line %d: unknown "
1210 "field '%s' for the MIME type "
1211 "'%s' ignored.",
1212 strFileName.c_str(),
1213 nLine + 1,
1214 curField.c_str(),
1215 strType.c_str()
1216 );
1217 }
1218 }
1219
1220 // it already has this value
1221 //currentToken = Field_Other;
1222 break;
1223
1224 default:
1225 wxFAIL_MSG("unknown field type in mailcap");
1226 }
1227
1228 // next token starts immediately after ';'
1229 curField.Empty();
1230 break;
1231
1232 default:
1233 curField += *pc;
1234 }
1235 }
1236
1237 // check that we really read something reasonable
1238 if ( currentToken == Field_Type || currentToken == Field_OpenCmd ) {
1239 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
1240 "ignored."),
1241 strFileName.c_str(), nLine + 1);
1242 }
1243 else {
1244 MailCapEntry *entry = new MailCapEntry(strOpenCmd,
1245 strPrintCmd,
1246 strTest);
1247
1248 strType.MakeLower();
1249 int nIndex = m_aTypes.Index(strType);
1250 if ( nIndex == wxNOT_FOUND ) {
1251 // new file type
1252 m_aTypes.Add(strType);
1253
1254 m_aEntries.Add(entry);
1255 m_aExtensions.Add("");
1256 m_aDescriptions.Add(strDesc);
1257 }
1258 else {
1259 // modify the existing entry: the entry in one and the same file
1260 // are read in top-to-bottom order, i.e. the entries read first
1261 // should be tried before the entries below. However, the files
1262 // read later should override the settings in the files read
1263 // before, thus we Append() the new entry to the list if it has
1264 // already occured in _this_ file, but Prepend() it if it
1265 // occured in some of the previous ones.
1266 if ( aEntryIndices.Index(nIndex) == wxNOT_FOUND ) {
1267 // first time in this file
1268 aEntryIndices.Add(nIndex);
1269 entry->Prepend(m_aEntries[nIndex]);
1270 m_aEntries[nIndex] = entry;
1271 }
1272 else {
1273 // not the first time in _this_ file
1274 entry->Append(m_aEntries[nIndex]);
1275 }
1276
1277 if ( !strDesc.IsEmpty() ) {
1278 // @@ replace the old one - what else can we do??
1279 m_aDescriptions[nIndex] = strDesc;
1280 }
1281 }
1282 }
1283
1284 // check our data integriry
1285 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
1286 m_aTypes.Count() == m_aExtensions.Count() &&
1287 m_aTypes.Count() == m_aDescriptions.Count() );
1288 }
1289 }
1290
1291 #endif // OS type
1292
1293 #endif
1294 // __WIN16__