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