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