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