]> git.saurik.com Git - wxWidgets.git/blob - src/common/mimetype.cpp
GNOME/KDE integration for wxMimeTypeManager
[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 #elif defined(__UNIX__)
47 #include "wx/ffile.h"
48 #include "wx/textfile.h"
49 #include "wx/dir.h"
50 #include "wx/utils.h"
51 #endif // OS
52
53 #include "wx/mimetype.h"
54
55 // other standard headers
56 #include <ctype.h>
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // implementation classes, platform dependent
63 #ifdef __WXMSW__
64
65 // These classes use Windows registry to retrieve the required information.
66 //
67 // Keys used (not all of them are documented, so it might actually stop working
68 // in futur versions of Windows...):
69 // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME
70 // types, each key has a string value "Extension" which gives (dot preceded)
71 // extension for the files of this MIME type.
72 //
73 // 2. "HKCR\.ext" contains
74 // a) unnamed value containing the "filetype"
75 // b) value "Content Type" containing the MIME type
76 //
77 // 3. "HKCR\filetype" contains
78 // a) unnamed value containing the description
79 // b) subkey "DefaultIcon" with single unnamed value giving the icon index in
80 // an icon file
81 // c) shell\open\command and shell\open\print subkeys containing the commands
82 // to open/print the file (the positional parameters are introduced by %1,
83 // %2, ... in these strings, we change them to %s ourselves)
84
85 // although I don't know of any official documentation which mentions this
86 // location, uses it, so it isn't likely to change
87 static const wxChar *MIME_DATABASE_KEY = wxT("MIME\\Database\\Content Type\\");
88
89 class wxFileTypeImpl
90 {
91 public:
92 // ctor
93 wxFileTypeImpl() { m_info = NULL; }
94
95 // one of these Init() function must be called (ctor can't take any
96 // arguments because it's common)
97
98 // initialize us with our file type name and extension - in this case
99 // we will read all other data from the registry
100 void Init(const wxString& strFileType, const wxString& ext)
101 { m_strFileType = strFileType; m_ext = ext; }
102
103 // initialize us with a wxFileTypeInfo object - it contains all the
104 // data
105 void Init(const wxFileTypeInfo& info)
106 { m_info = &info; }
107
108 // implement accessor functions
109 bool GetExtensions(wxArrayString& extensions);
110 bool GetMimeType(wxString *mimeType) const;
111 bool GetIcon(wxIcon *icon) const;
112 bool GetDescription(wxString *desc) const;
113 bool GetOpenCommand(wxString *openCmd,
114 const wxFileType::MessageParameters& params) const;
115 bool GetPrintCommand(wxString *printCmd,
116 const wxFileType::MessageParameters& params) const;
117
118 private:
119 // helper function: reads the command corresponding to the specified verb
120 // from the registry (returns an empty string if not found)
121 wxString GetCommand(const wxChar *verb) const;
122
123 // we use either m_info or read the data from the registry if m_info == NULL
124 const wxFileTypeInfo *m_info;
125 wxString m_strFileType, // may be empty
126 m_ext;
127 };
128
129 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo);
130 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
132
133 class wxMimeTypesManagerImpl
134 {
135 public:
136 // nothing to do here, we don't load any data but just go and fetch it from
137 // the registry when asked for
138 wxMimeTypesManagerImpl() { }
139
140 // implement containing class functions
141 wxFileType *GetFileTypeFromExtension(const wxString& ext);
142 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
143
144 size_t EnumAllFileTypes(wxArrayString& mimetypes);
145
146 // this are NOPs under Windows
147 bool ReadMailcap(const wxString& filename, bool fallback = TRUE)
148 { return TRUE; }
149 bool ReadMimeTypes(const wxString& filename)
150 { return TRUE; }
151
152 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
153
154 private:
155 wxArrayFileTypeInfo m_fallbacks;
156 };
157
158 #elif defined( __WXMAC__ )
159
160 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo);
161 #include "wx/arrimpl.cpp"
162 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
163
164 class wxMimeTypesManagerImpl
165 {
166 public :
167 wxMimeTypesManagerImpl() { }
168
169 // implement containing class functions
170 wxFileType *GetFileTypeFromExtension(const wxString& ext);
171 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
172
173 size_t EnumAllFileTypes(wxArrayString& mimetypes);
174
175 // this are NOPs under MacOS
176 bool ReadMailcap(const wxString& filename, bool fallback = TRUE) { return TRUE; }
177 bool ReadMimeTypes(const wxString& filename) { return TRUE; }
178
179 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
180
181 private:
182 wxArrayFileTypeInfo m_fallbacks;
183 };
184
185 class wxFileTypeImpl
186 {
187 public:
188 // initialize us with our file type name
189 void SetFileType(const wxString& strFileType)
190 { m_strFileType = strFileType; }
191 void SetExt(const wxString& ext)
192 { m_ext = ext; }
193
194 // implement accessor functions
195 bool GetExtensions(wxArrayString& extensions);
196 bool GetMimeType(wxString *mimeType) const;
197 bool GetIcon(wxIcon *icon) const;
198 bool GetDescription(wxString *desc) const;
199 bool GetOpenCommand(wxString *openCmd,
200 const wxFileType::MessageParameters&) const
201 { return GetCommand(openCmd, "open"); }
202 bool GetPrintCommand(wxString *printCmd,
203 const wxFileType::MessageParameters&) const
204 { return GetCommand(printCmd, "print"); }
205
206 private:
207 // helper function
208 bool GetCommand(wxString *command, const char *verb) const;
209
210 wxString m_strFileType, m_ext;
211 };
212
213 #else // Unix
214
215 // this class uses both mailcap and mime.types to gather information about file
216 // types.
217 //
218 // The information about mailcap file was extracted from metamail(1) sources and
219 // documentation.
220 //
221 // Format of mailcap file: spaces are ignored, each line is either a comment
222 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
223 // A backslash can be used to quote semicolons and newlines (and, in fact,
224 // anything else including itself).
225 //
226 // The first field is always the MIME type in the form of type/subtype (see RFC
227 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
228 // "type" which means the same as "type/*", although I'm not sure whether this
229 // is standard.
230 //
231 // The second field is always the command to run. It is subject to
232 // parameter/filename expansion described below.
233 //
234 // All the following fields are optional and may not be present at all. If
235 // they're present they may appear in any order, although each of them should
236 // appear only once. The optional fields are the following:
237 // * notes=xxx is an uninterpreted string which is silently ignored
238 // * test=xxx is the command to be used to determine whether this mailcap line
239 // applies to our data or not. The RHS of this field goes through the
240 // parameter/filename expansion (as the 2nd field) and the resulting string
241 // is executed. The line applies only if the command succeeds, i.e. returns 0
242 // exit code.
243 // * print=xxx is the command to be used to print (and not view) the data of
244 // this type (parameter/filename expansion is done here too)
245 // * edit=xxx is the command to open/edit the data of this type
246 // * needsterminal means that a new console must be created for the viewer
247 // * copiousoutput means that the viewer doesn't interact with the user but
248 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
249 // good example), thus it might be a good idea to use some kind of paging
250 // mechanism.
251 // * textualnewlines means not to perform CR/LF translation (not honored)
252 // * compose and composetyped fields are used to determine the program to be
253 // called to create a new message pert in the specified format (unused).
254 //
255 // Parameter/filename xpansion:
256 // * %s is replaced with the (full) file name
257 // * %t is replaced with MIME type/subtype of the entry
258 // * for multipart type only %n is replaced with the nnumber of parts and %F is
259 // replaced by an array of (content-type, temporary file name) pairs for all
260 // message parts (TODO)
261 // * %{parameter} is replaced with the value of parameter taken from
262 // Content-type header line of the message.
263 //
264 // FIXME any docs with real descriptions of these files??
265 //
266 // There are 2 possible formats for mime.types file, one entry per line (used
267 // for global mime.types) and "expanded" format where an entry takes multiple
268 // lines (used for users mime.types).
269 //
270 // For both formats spaces are ignored and lines starting with a '#' are
271 // comments. Each record has one of two following forms:
272 // a) for "brief" format:
273 // <mime type> <space separated list of extensions>
274 // b) for "expanded" format:
275 // type=<mime type> \ desc="<description>" \ exts="ext"
276 //
277 // We try to autodetect the format of mime.types: if a non-comment line starts
278 // with "type=" we assume the second format, otherwise the first one.
279
280 // there may be more than one entry for one and the same mime type, to
281 // choose the right one we have to run the command specified in the test
282 // field on our data.
283 class MailCapEntry
284 {
285 public:
286 // ctor
287 MailCapEntry(const wxString& openCmd,
288 const wxString& printCmd,
289 const wxString& testCmd)
290 : m_openCmd(openCmd), m_printCmd(printCmd), m_testCmd(testCmd)
291 {
292 m_next = NULL;
293 }
294
295 // accessors
296 const wxString& GetOpenCmd() const { return m_openCmd; }
297 const wxString& GetPrintCmd() const { return m_printCmd; }
298 const wxString& GetTestCmd() const { return m_testCmd; }
299
300 MailCapEntry *GetNext() const { return m_next; }
301
302 // operations
303 // prepend this element to the list
304 void Prepend(MailCapEntry *next) { m_next = next; }
305 // insert into the list at given position
306 void Insert(MailCapEntry *next, size_t pos)
307 {
308 // FIXME slooow...
309 MailCapEntry *cur;
310 size_t n = 0;
311 for ( cur = next; cur != NULL; cur = cur->m_next, n++ ) {
312 if ( n == pos )
313 break;
314 }
315
316 wxASSERT_MSG( n == pos, wxT("invalid position in MailCapEntry::Insert") );
317
318 m_next = cur->m_next;
319 cur->m_next = this;
320 }
321 // append this element to the list
322 void Append(MailCapEntry *next)
323 {
324 wxCHECK_RET( next != NULL, wxT("Append()ing to what?") );
325
326 // FIXME slooow...
327 MailCapEntry *cur;
328 for ( cur = next; cur->m_next != NULL; cur = cur->m_next )
329 ;
330
331 cur->m_next = this;
332
333 wxASSERT_MSG( !m_next, wxT("Append()ing element already in the list?") );
334 }
335
336 private:
337 wxString m_openCmd, // command to use to open/view the file
338 m_printCmd, // print
339 m_testCmd; // only apply this entry if test yields
340 // true (i.e. the command returns 0)
341
342 MailCapEntry *m_next; // in the linked list
343 };
344
345 WX_DEFINE_ARRAY(MailCapEntry *, ArrayTypeEntries);
346
347 // the base class which may be used to find an icon for the MIME type
348 class wxMimeTypeIconHandler
349 {
350 public:
351 virtual bool GetIcon(const wxString& mimetype, wxIcon *icon) = 0;
352 };
353
354 WX_DEFINE_ARRAY(wxMimeTypeIconHandler *, ArrayIconHandlers);
355
356 // the icon handler which uses GNOME MIME database
357 class wxGNOMEIconHandler : public wxMimeTypeIconHandler
358 {
359 public:
360 virtual bool GetIcon(const wxString& mimetype, wxIcon *icon);
361
362 private:
363 void Init();
364 void LoadIconsFromKeyFile(const wxString& filename);
365 void LoadKeyFilesFromDir(const wxString& dirbase);
366
367 static bool m_inited;
368
369 static wxSortedArrayString ms_mimetypes;
370 static wxArrayString ms_icons;
371 };
372
373 // the icon handler which uses KDE MIME database
374 class wxKDEIconHandler : public wxMimeTypeIconHandler
375 {
376 public:
377 virtual bool GetIcon(const wxString& mimetype, wxIcon *icon);
378
379 private:
380 void LoadLinksForMimeSubtype(const wxString& dirbase,
381 const wxString& subdir,
382 const wxString& filename);
383 void LoadLinksForMimeType(const wxString& dirbase,
384 const wxString& subdir);
385 void LoadLinkFilesFromDir(const wxString& dirbase);
386 void Init();
387
388 static bool m_inited;
389
390 static wxSortedArrayString ms_mimetypes;
391 static wxArrayString ms_icons;
392 };
393
394 // this is the real wxMimeTypesManager for Unix
395 class wxMimeTypesManagerImpl
396 {
397 friend class wxFileTypeImpl; // give it access to m_aXXX variables
398
399 public:
400 // ctor loads all info into memory for quicker access later on
401 // TODO it would be nice to load them all, but parse on demand only...
402 wxMimeTypesManagerImpl();
403
404 // implement containing class functions
405 wxFileType *GetFileTypeFromExtension(const wxString& ext);
406 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
407
408 size_t EnumAllFileTypes(wxArrayString& mimetypes);
409
410 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
411 bool ReadMimeTypes(const wxString& filename);
412
413 void AddFallback(const wxFileTypeInfo& filetype);
414
415 // add information about the given mimetype
416 void AddMimeTypeInfo(const wxString& mimetype,
417 const wxString& extensions,
418 const wxString& description);
419 void AddMailcapInfo(const wxString& strType,
420 const wxString& strOpenCmd,
421 const wxString& strPrintCmd,
422 const wxString& strTest,
423 const wxString& strDesc);
424
425 // accessors
426 // get the string containing space separated extensions for the given
427 // file type
428 wxString GetExtension(size_t index) { return m_aExtensions[index]; }
429
430 // get the array of icon handlers
431 static ArrayIconHandlers& GetIconHandlers();
432
433 private:
434 wxArrayString m_aTypes, // MIME types
435 m_aDescriptions, // descriptions (just some text)
436 m_aExtensions; // space separated list of extensions
437 ArrayTypeEntries m_aEntries; // commands and tests for this file type
438
439 // head of the linked list of the icon handlers
440 static ArrayIconHandlers ms_iconHandlers;
441 };
442
443 class wxFileTypeImpl
444 {
445 public:
446 // initialization functions
447 void Init(wxMimeTypesManagerImpl *manager, size_t index)
448 { m_manager = manager; m_index = index; }
449
450 // accessors
451 bool GetExtensions(wxArrayString& extensions);
452 bool GetMimeType(wxString *mimeType) const
453 { *mimeType = m_manager->m_aTypes[m_index]; return TRUE; }
454 bool GetIcon(wxIcon *icon) const;
455 bool GetDescription(wxString *desc) const
456 { *desc = m_manager->m_aDescriptions[m_index]; return TRUE; }
457
458 bool GetOpenCommand(wxString *openCmd,
459 const wxFileType::MessageParameters& params) const
460 {
461 return GetExpandedCommand(openCmd, params, TRUE);
462 }
463
464 bool GetPrintCommand(wxString *printCmd,
465 const wxFileType::MessageParameters& params) const
466 {
467 return GetExpandedCommand(printCmd, params, FALSE);
468 }
469
470 private:
471 // get the entry which passes the test (may return NULL)
472 MailCapEntry *GetEntry(const wxFileType::MessageParameters& params) const;
473
474 // choose the correct entry to use and expand the command
475 bool GetExpandedCommand(wxString *expandedCmd,
476 const wxFileType::MessageParameters& params,
477 bool open) const;
478
479 wxMimeTypesManagerImpl *m_manager;
480 size_t m_index; // in the wxMimeTypesManagerImpl arrays
481 };
482
483 #endif // OS type
484
485 // ============================================================================
486 // common classes
487 // ============================================================================
488
489 // ----------------------------------------------------------------------------
490 // wxFileTypeInfo
491 // ----------------------------------------------------------------------------
492
493 wxFileTypeInfo::wxFileTypeInfo(const char *mimeType,
494 const char *openCmd,
495 const char *printCmd,
496 const char *desc,
497 ...)
498 : m_mimeType(mimeType),
499 m_openCmd(openCmd),
500 m_printCmd(printCmd),
501 m_desc(desc)
502 {
503 va_list argptr;
504 va_start(argptr, desc);
505
506 for ( ;; )
507 {
508 const char *ext = va_arg(argptr, const char *);
509 if ( !ext )
510 {
511 // NULL terminates the list
512 break;
513 }
514
515 m_exts.Add(ext);
516 }
517
518 va_end(argptr);
519 }
520
521 // ============================================================================
522 // implementation of the wrapper classes
523 // ============================================================================
524
525 // ----------------------------------------------------------------------------
526 // wxFileType
527 // ----------------------------------------------------------------------------
528
529 wxString wxFileType::ExpandCommand(const wxString& command,
530 const wxFileType::MessageParameters& params)
531 {
532 bool hasFilename = FALSE;
533
534 wxString str;
535 for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) {
536 if ( *pc == wxT('%') ) {
537 switch ( *++pc ) {
538 case wxT('s'):
539 // '%s' expands into file name (quoted because it might
540 // contain spaces) - except if there are already quotes
541 // there because otherwise some programs may get confused
542 // by double double quotes
543 #if 0
544 if ( *(pc - 2) == wxT('"') )
545 str << params.GetFileName();
546 else
547 str << wxT('"') << params.GetFileName() << wxT('"');
548 #endif
549 str << params.GetFileName();
550 hasFilename = TRUE;
551 break;
552
553 case wxT('t'):
554 // '%t' expands into MIME type (quote it too just to be
555 // consistent)
556 str << wxT('\'') << params.GetMimeType() << wxT('\'');
557 break;
558
559 case wxT('{'):
560 {
561 const wxChar *pEnd = wxStrchr(pc, wxT('}'));
562 if ( pEnd == NULL ) {
563 wxString mimetype;
564 wxLogWarning(_("Unmatched '{' in an entry for "
565 "mime type %s."),
566 params.GetMimeType().c_str());
567 str << wxT("%{");
568 }
569 else {
570 wxString param(pc + 1, pEnd - pc - 1);
571 str << wxT('\'') << params.GetParamValue(param) << wxT('\'');
572 pc = pEnd;
573 }
574 }
575 break;
576
577 case wxT('n'):
578 case wxT('F'):
579 // TODO %n is the number of parts, %F is an array containing
580 // the names of temp files these parts were written to
581 // and their mime types.
582 break;
583
584 default:
585 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
586 *pc, command.c_str());
587 str << *pc;
588 }
589 }
590 else {
591 str << *pc;
592 }
593 }
594
595 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
596 // the program will accept the data on stdin: so give it to it!
597 if ( !hasFilename && !str.IsEmpty() ) {
598 str << wxT(" < '") << params.GetFileName() << wxT('\'');
599 }
600
601 return str;
602 }
603
604 wxFileType::wxFileType()
605 {
606 m_impl = new wxFileTypeImpl;
607 }
608
609 wxFileType::~wxFileType()
610 {
611 delete m_impl;
612 }
613
614 bool wxFileType::GetExtensions(wxArrayString& extensions)
615 {
616 return m_impl->GetExtensions(extensions);
617 }
618
619 bool wxFileType::GetMimeType(wxString *mimeType) const
620 {
621 return m_impl->GetMimeType(mimeType);
622 }
623
624 bool wxFileType::GetIcon(wxIcon *icon) const
625 {
626 return m_impl->GetIcon(icon);
627 }
628
629 bool wxFileType::GetDescription(wxString *desc) const
630 {
631 return m_impl->GetDescription(desc);
632 }
633
634 bool
635 wxFileType::GetOpenCommand(wxString *openCmd,
636 const wxFileType::MessageParameters& params) const
637 {
638 return m_impl->GetOpenCommand(openCmd, params);
639 }
640
641 bool
642 wxFileType::GetPrintCommand(wxString *printCmd,
643 const wxFileType::MessageParameters& params) const
644 {
645 return m_impl->GetPrintCommand(printCmd, params);
646 }
647
648 // ----------------------------------------------------------------------------
649 // wxMimeTypesManager
650 // ----------------------------------------------------------------------------
651
652 bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
653 const wxString& wildcard)
654 {
655 wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND,
656 wxT("first MIME type can't contain wildcards") );
657
658 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
659 if ( wildcard.BeforeFirst(wxT('/')).IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) )
660 {
661 wxString strSubtype = wildcard.AfterFirst(wxT('/'));
662
663 if ( strSubtype == wxT("*") ||
664 strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) )
665 {
666 // matches (either exactly or it's a wildcard)
667 return TRUE;
668 }
669 }
670
671 return FALSE;
672 }
673
674 wxMimeTypesManager::wxMimeTypesManager()
675 {
676 m_impl = new wxMimeTypesManagerImpl;
677 }
678
679 wxMimeTypesManager::~wxMimeTypesManager()
680 {
681 delete m_impl;
682 }
683
684 wxFileType *
685 wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
686 {
687 return m_impl->GetFileTypeFromExtension(ext);
688 }
689
690 wxFileType *
691 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
692 {
693 return m_impl->GetFileTypeFromMimeType(mimeType);
694 }
695
696 bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
697 {
698 return m_impl->ReadMailcap(filename, fallback);
699 }
700
701 bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
702 {
703 return m_impl->ReadMimeTypes(filename);
704 }
705
706 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
707 {
708 for ( const wxFileTypeInfo *ft = filetypes; ft->IsValid(); ft++ ) {
709 m_impl->AddFallback(*ft);
710 }
711 }
712
713 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
714 {
715 return m_impl->EnumAllFileTypes(mimetypes);
716 }
717
718 // ============================================================================
719 // real (OS specific) implementation
720 // ============================================================================
721
722 #ifdef __WXMSW__
723
724 wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const
725 {
726 // suppress possible error messages
727 wxLogNull nolog;
728 wxString strKey;
729
730 if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() )
731 strKey = m_ext;
732 if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() )
733 strKey = m_strFileType;
734
735 if ( !strKey )
736 {
737 // no info
738 return wxEmptyString;
739 }
740
741 strKey << wxT("\\shell\\") << verb << wxT("\\command");
742 wxRegKey key(wxRegKey::HKCR, strKey);
743 wxString command;
744 if ( key.Open() ) {
745 // it's the default value of the key
746 if ( key.QueryValue(wxT(""), command) ) {
747 // transform it from '%1' to '%s' style format string
748
749 // NB: we don't make any attempt to verify that the string is valid,
750 // i.e. doesn't contain %2, or second %1 or .... But we do make
751 // sure that we return a string with _exactly_ one '%s'!
752 bool foundFilename = FALSE;
753 size_t len = command.Len();
754 for ( size_t n = 0; (n < len) && !foundFilename; n++ ) {
755 if ( command[n] == wxT('%') &&
756 (n + 1 < len) && command[n + 1] == wxT('1') ) {
757 // replace it with '%s'
758 command[n + 1] = wxT('s');
759
760 foundFilename = TRUE;
761 }
762 }
763
764 if ( !foundFilename ) {
765 // we didn't find any '%1'!
766 // HACK: append the filename at the end, hope that it will do
767 command << wxT(" %s");
768 }
769 }
770 }
771 //else: no such file type or no value, will return empty string
772
773 return command;
774 }
775
776 bool
777 wxFileTypeImpl::GetOpenCommand(wxString *openCmd,
778 const wxFileType::MessageParameters& params)
779 const
780 {
781 wxString cmd;
782 if ( m_info ) {
783 cmd = m_info->GetOpenCommand();
784 }
785 else {
786 cmd = GetCommand(wxT("open"));
787 }
788
789 *openCmd = wxFileType::ExpandCommand(cmd, params);
790
791 return !openCmd->IsEmpty();
792 }
793
794 bool
795 wxFileTypeImpl::GetPrintCommand(wxString *printCmd,
796 const wxFileType::MessageParameters& params)
797 const
798 {
799 wxString cmd;
800 if ( m_info ) {
801 cmd = m_info->GetPrintCommand();
802 }
803 else {
804 cmd = GetCommand(wxT("print"));
805 }
806
807 *printCmd = wxFileType::ExpandCommand(cmd, params);
808
809 return !printCmd->IsEmpty();
810 }
811
812 // TODO this function is half implemented
813 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
814 {
815 if ( m_info ) {
816 extensions = m_info->GetExtensions();
817
818 return TRUE;
819 }
820 else if ( m_ext.IsEmpty() ) {
821 // the only way to get the list of extensions from the file type is to
822 // scan through all extensions in the registry - too slow...
823 return FALSE;
824 }
825 else {
826 extensions.Empty();
827 extensions.Add(m_ext);
828
829 // it's a lie too, we don't return _all_ extensions...
830 return TRUE;
831 }
832 }
833
834 bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
835 {
836 if ( m_info ) {
837 // we already have it
838 *mimeType = m_info->GetMimeType();
839
840 return TRUE;
841 }
842
843 // suppress possible error messages
844 wxLogNull nolog;
845 wxRegKey key(wxRegKey::HKCR, wxT(".") + m_ext);
846 if ( key.Open() && key.QueryValue(wxT("Content Type"), *mimeType) ) {
847 return TRUE;
848 }
849 else {
850 return FALSE;
851 }
852 }
853
854 bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
855 {
856 #if wxUSE_GUI
857 if ( m_info ) {
858 // we don't have icons in the fallback resources
859 return FALSE;
860 }
861
862 wxString strIconKey;
863 strIconKey << m_strFileType << wxT("\\DefaultIcon");
864
865 // suppress possible error messages
866 wxLogNull nolog;
867 wxRegKey key(wxRegKey::HKCR, strIconKey);
868
869 if ( key.Open() ) {
870 wxString strIcon;
871 // it's the default value of the key
872 if ( key.QueryValue(wxT(""), strIcon) ) {
873 // the format is the following: <full path to file>, <icon index>
874 // NB: icon index may be negative as well as positive and the full
875 // path may contain the environment variables inside '%'
876 wxString strFullPath = strIcon.BeforeLast(wxT(',')),
877 strIndex = strIcon.AfterLast(wxT(','));
878
879 // index may be omitted, in which case BeforeLast(',') is empty and
880 // AfterLast(',') is the whole string
881 if ( strFullPath.IsEmpty() ) {
882 strFullPath = strIndex;
883 strIndex = wxT("0");
884 }
885
886 wxString strExpPath = wxExpandEnvVars(strFullPath);
887 int nIndex = wxAtoi(strIndex);
888
889 HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex);
890 switch ( (int)hIcon ) {
891 case 0: // means no icons were found
892 case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/...
893 wxLogDebug(wxT("incorrect registry entry '%s': no such icon."),
894 key.GetName().c_str());
895 break;
896
897 default:
898 icon->SetHICON((WXHICON)hIcon);
899 return TRUE;
900 }
901 }
902 }
903
904 // no such file type or no value or incorrect icon entry
905 #endif // wxUSE_GUI
906
907 return FALSE;
908 }
909
910 bool wxFileTypeImpl::GetDescription(wxString *desc) const
911 {
912 if ( m_info ) {
913 // we already have it
914 *desc = m_info->GetDescription();
915
916 return TRUE;
917 }
918
919 // suppress possible error messages
920 wxLogNull nolog;
921 wxRegKey key(wxRegKey::HKCR, m_strFileType);
922
923 if ( key.Open() ) {
924 // it's the default value of the key
925 if ( key.QueryValue(wxT(""), *desc) ) {
926 return TRUE;
927 }
928 }
929
930 return FALSE;
931 }
932
933 // extension -> file type
934 wxFileType *
935 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
936 {
937 // add the leading point if necessary
938 wxString str;
939 if ( ext[0u] != wxT('.') ) {
940 str = wxT('.');
941 }
942 str << ext;
943
944 // suppress possible error messages
945 wxLogNull nolog;
946
947 bool knownExtension = FALSE;
948
949 wxString strFileType;
950 wxRegKey key(wxRegKey::HKCR, str);
951 if ( key.Open() ) {
952 // it's the default value of the key
953 if ( key.QueryValue(wxT(""), strFileType) ) {
954 // create the new wxFileType object
955 wxFileType *fileType = new wxFileType;
956 fileType->m_impl->Init(strFileType, ext);
957
958 return fileType;
959 }
960 else {
961 // this extension doesn't have a filetype, but it's known to the
962 // system and may be has some other useful keys (open command or
963 // content-type), so still return a file type object for it
964 knownExtension = TRUE;
965 }
966 }
967
968 // check the fallbacks
969 // TODO linear search is potentially slow, perhaps we should use a sorted
970 // array?
971 size_t count = m_fallbacks.GetCount();
972 for ( size_t n = 0; n < count; n++ ) {
973 if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) {
974 wxFileType *fileType = new wxFileType;
975 fileType->m_impl->Init(m_fallbacks[n]);
976
977 return fileType;
978 }
979 }
980
981 if ( knownExtension )
982 {
983 wxFileType *fileType = new wxFileType;
984 fileType->m_impl->Init(wxEmptyString, ext);
985
986 return fileType;
987 }
988 else
989 {
990 // unknown extension
991 return NULL;
992 }
993 }
994
995 // MIME type -> extension -> file type
996 wxFileType *
997 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
998 {
999 wxString strKey = MIME_DATABASE_KEY;
1000 strKey << mimeType;
1001
1002 // suppress possible error messages
1003 wxLogNull nolog;
1004
1005 wxString ext;
1006 wxRegKey key(wxRegKey::HKCR, strKey);
1007 if ( key.Open() ) {
1008 if ( key.QueryValue(wxT("Extension"), ext) ) {
1009 return GetFileTypeFromExtension(ext);
1010 }
1011 }
1012
1013 // check the fallbacks
1014 // TODO linear search is potentially slow, perhaps we should use a sorted
1015 // array?
1016 size_t count = m_fallbacks.GetCount();
1017 for ( size_t n = 0; n < count; n++ ) {
1018 if ( wxMimeTypesManager::IsOfType(mimeType,
1019 m_fallbacks[n].GetMimeType()) ) {
1020 wxFileType *fileType = new wxFileType;
1021 fileType->m_impl->Init(m_fallbacks[n]);
1022
1023 return fileType;
1024 }
1025 }
1026
1027 // unknown MIME type
1028 return NULL;
1029 }
1030
1031 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
1032 {
1033 // enumerate all keys under MIME_DATABASE_KEY
1034 wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY);
1035
1036 wxString type;
1037 long cookie;
1038 bool cont = key.GetFirstKey(type, cookie);
1039 while ( cont )
1040 {
1041 mimetypes.Add(type);
1042
1043 cont = key.GetNextKey(type, cookie);
1044 }
1045
1046 return mimetypes.GetCount();
1047 }
1048
1049 #elif defined ( __WXMAC__ )
1050
1051 bool wxFileTypeImpl::GetCommand(wxString *command, const char *verb) const
1052 {
1053 return FALSE;
1054 }
1055
1056 // @@ this function is half implemented
1057 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
1058 {
1059 return FALSE;
1060 }
1061
1062 bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
1063 {
1064 if ( m_strFileType.Length() > 0 )
1065 {
1066 *mimeType = m_strFileType ;
1067 return TRUE ;
1068 }
1069 else
1070 return FALSE;
1071 }
1072
1073 bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
1074 {
1075 // no such file type or no value or incorrect icon entry
1076 return FALSE;
1077 }
1078
1079 bool wxFileTypeImpl::GetDescription(wxString *desc) const
1080 {
1081 return FALSE;
1082 }
1083
1084 // extension -> file type
1085 wxFileType *
1086 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& e)
1087 {
1088 wxString ext = e ;
1089 ext = ext.Lower() ;
1090 if ( ext == "txt" )
1091 {
1092 wxFileType *fileType = new wxFileType;
1093 fileType->m_impl->SetFileType("text/text");
1094 fileType->m_impl->SetExt(ext);
1095 return fileType;
1096 }
1097 else if ( ext == "htm" || ext == "html" )
1098 {
1099 wxFileType *fileType = new wxFileType;
1100 fileType->m_impl->SetFileType("text/html");
1101 fileType->m_impl->SetExt(ext);
1102 return fileType;
1103 }
1104 else if ( ext == "gif" )
1105 {
1106 wxFileType *fileType = new wxFileType;
1107 fileType->m_impl->SetFileType("image/gif");
1108 fileType->m_impl->SetExt(ext);
1109 return fileType;
1110 }
1111 else if ( ext == "png" )
1112 {
1113 wxFileType *fileType = new wxFileType;
1114 fileType->m_impl->SetFileType("image/png");
1115 fileType->m_impl->SetExt(ext);
1116 return fileType;
1117 }
1118 else if ( ext == "jpg" || ext == "jpeg" )
1119 {
1120 wxFileType *fileType = new wxFileType;
1121 fileType->m_impl->SetFileType("image/jpeg");
1122 fileType->m_impl->SetExt(ext);
1123 return fileType;
1124 }
1125 else if ( ext == "bmp" )
1126 {
1127 wxFileType *fileType = new wxFileType;
1128 fileType->m_impl->SetFileType("image/bmp");
1129 fileType->m_impl->SetExt(ext);
1130 return fileType;
1131 }
1132 else if ( ext == "tif" || ext == "tiff" )
1133 {
1134 wxFileType *fileType = new wxFileType;
1135 fileType->m_impl->SetFileType("image/tiff");
1136 fileType->m_impl->SetExt(ext);
1137 return fileType;
1138 }
1139 else if ( ext == "xpm" )
1140 {
1141 wxFileType *fileType = new wxFileType;
1142 fileType->m_impl->SetFileType("image/xpm");
1143 fileType->m_impl->SetExt(ext);
1144 return fileType;
1145 }
1146 else if ( ext == "xbm" )
1147 {
1148 wxFileType *fileType = new wxFileType;
1149 fileType->m_impl->SetFileType("image/xbm");
1150 fileType->m_impl->SetExt(ext);
1151 return fileType;
1152 }
1153
1154 // unknown extension
1155 return NULL;
1156 }
1157
1158 // MIME type -> extension -> file type
1159 wxFileType *
1160 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
1161 {
1162 return NULL;
1163 }
1164
1165 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
1166 {
1167 wxFAIL_MSG( _T("TODO") ); // VZ: don't know anything about this for Mac
1168
1169 return 0;
1170 }
1171
1172 #else // Unix
1173
1174 // ============================================================================
1175 // Unix implementation
1176 // ============================================================================
1177
1178 // ----------------------------------------------------------------------------
1179 // various statics
1180 // ----------------------------------------------------------------------------
1181
1182 static wxGNOMEIconHandler gs_iconHandlerGNOME;
1183 static wxKDEIconHandler gs_iconHandlerKDE;
1184
1185 bool wxGNOMEIconHandler::m_inited = FALSE;
1186 wxSortedArrayString wxGNOMEIconHandler::ms_mimetypes;
1187 wxArrayString wxGNOMEIconHandler::ms_icons;
1188
1189 bool wxKDEIconHandler::m_inited = FALSE;
1190 wxSortedArrayString wxKDEIconHandler::ms_mimetypes;
1191 wxArrayString wxKDEIconHandler::ms_icons;
1192
1193 ArrayIconHandlers wxMimeTypesManagerImpl::ms_iconHandlers;
1194
1195 // ----------------------------------------------------------------------------
1196 // wxGNOMEIconHandler
1197 // ----------------------------------------------------------------------------
1198
1199 // GNOME stores the info we're interested in in several locations:
1200 // 1. xxx.keys files under /usr/share/mime-info
1201 // 2. xxx.keys files under ~/.gnome/mime-info
1202 //
1203 // The format of xxx.keys file is the following:
1204 //
1205 // mimetype/subtype:
1206 // field=value
1207 //
1208 // with blank lines separating the entries and indented lines starting with
1209 // TABs. We're interested in the field icon-filename whose value is the path
1210 // containing the icon.
1211
1212 void wxGNOMEIconHandler::LoadIconsFromKeyFile(const wxString& filename)
1213 {
1214 wxTextFile textfile(filename);
1215 if ( !textfile.Open() )
1216 return;
1217
1218 // values for the entry being parsed
1219 wxString curMimeType, curIconFile;
1220
1221 const wxChar *pc;
1222 size_t nLineCount = textfile.GetLineCount();
1223 for ( size_t nLine = 0; ; nLine++ )
1224 {
1225 if ( nLine < nLineCount )
1226 {
1227 pc = textfile[nLine].c_str();
1228 if ( *pc == _T('#') )
1229 {
1230 // skip comments
1231 continue;
1232 }
1233 }
1234 else
1235 {
1236 // so that we will fall into the "if" below
1237 pc = NULL;
1238 }
1239
1240 if ( !pc || !*pc )
1241 {
1242 // end of the entry
1243 if ( !!curMimeType && !!curIconFile )
1244 {
1245 // do we already know this mimetype?
1246 int i = ms_mimetypes.Index(curMimeType);
1247 if ( i == wxNOT_FOUND )
1248 {
1249 // add a new entry
1250 size_t n = ms_mimetypes.Add(curMimeType);
1251 ms_icons.Insert(curIconFile, n);
1252 }
1253 else
1254 {
1255 // replace the existing one (this means that the directories
1256 // should be searched in order of increased priority!)
1257 ms_icons[(size_t)i] = curIconFile;
1258 }
1259 }
1260
1261 if ( !pc )
1262 {
1263 // the end - this can only happen if nLine == nLineCount
1264 break;
1265 }
1266
1267 curIconFile.Empty();
1268
1269 continue;
1270 }
1271
1272 // what do we have here?
1273 if ( *pc == _T('\t') )
1274 {
1275 // this is a field=value ling
1276 pc++; // skip leading TAB
1277
1278 static const int lenField = 13; // strlen("icon-filename")
1279 if ( wxStrncmp(pc, _T("icon-filename"), lenField) == 0 )
1280 {
1281 // skip '=' which follows and take everything left until the end
1282 // of line
1283 curIconFile = pc + lenField + 1;
1284 }
1285 //else: some other field, we don't care
1286 }
1287 else
1288 {
1289 // this is the start of the new section
1290 curMimeType.Empty();
1291
1292 while ( *pc != _T(':') && *pc != _T('\0') )
1293 {
1294 curMimeType += *pc++;
1295 }
1296
1297 if ( !*pc )
1298 {
1299 // we reached the end of line without finding the colon,
1300 // something is wrong - ignore this line completely
1301 wxLogDebug(_T("Unreckognized line %d in file '%s' ignored"),
1302 nLine + 1, filename.c_str());
1303
1304 break;
1305 }
1306 }
1307 }
1308 }
1309
1310 void wxGNOMEIconHandler::LoadKeyFilesFromDir(const wxString& dirbase)
1311 {
1312 wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase),
1313 _T("base directory shouldn't end with a slash") );
1314
1315 wxString dirname = dirbase;
1316 dirname << _T("/mime-info");
1317
1318 if ( !wxDir::Exists(dirname) )
1319 return;
1320
1321 wxDir dir(dirname);
1322 if ( !dir.IsOpened() )
1323 return;
1324
1325 // we will concatenate it with filename to get the full path below
1326 dirname += _T('/');
1327
1328 wxString filename;
1329 bool cont = dir.GetFirst(&filename, _T("*.keys"), wxDIR_FILES);
1330 while ( cont )
1331 {
1332 LoadIconsFromKeyFile(dirname + filename);
1333
1334 cont = dir.GetNext(&filename);
1335 }
1336 }
1337
1338 void wxGNOMEIconHandler::Init()
1339 {
1340 wxArrayString dirs;
1341 dirs.Add(_T("/usr/share"));
1342 dirs.Add(wxGetHomeDir() + _T("/.gnome"));
1343
1344 size_t nDirs = dirs.GetCount();
1345 for ( size_t nDir = 0; nDir < nDirs; nDir++ )
1346 {
1347 LoadKeyFilesFromDir(dirs[nDir]);
1348 }
1349
1350 m_inited = TRUE;
1351 }
1352
1353 bool wxGNOMEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon)
1354 {
1355 if ( !m_inited )
1356 {
1357 Init();
1358 }
1359
1360 int index = ms_mimetypes.Index(mimetype);
1361 if ( index == wxNOT_FOUND )
1362 return FALSE;
1363
1364 wxString iconname = ms_icons[(size_t)index];
1365
1366 #if wxUSE_GUI
1367 *icon = wxIcon(iconname);
1368 #else
1369 // helpful for testing in console mode
1370 wxLogDebug(_T("Found GNOME icon for '%s': '%s'\n"),
1371 mimetype.c_str(), iconname.c_str());
1372 #endif
1373
1374 return TRUE;
1375 }
1376
1377 // ----------------------------------------------------------------------------
1378 // wxKDEIconHandler
1379 // ----------------------------------------------------------------------------
1380
1381 // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype
1382 // may be found in either of the following locations
1383 //
1384 // 1. /usr/share/mimelnk/mimetype/subtype.kdelnk
1385 // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk
1386 //
1387 // The format of a .kdelnk file is almost the same as the one used by
1388 // wxFileConfig, i.e. there are groups, comments and entries. The icon is the
1389 // value for the entry "Type"
1390
1391 void wxKDEIconHandler::LoadLinksForMimeSubtype(const wxString& dirbase,
1392 const wxString& subdir,
1393 const wxString& filename)
1394 {
1395 wxFFile file(dirbase + filename);
1396 if ( !file.IsOpened() )
1397 return;
1398
1399 // these files are small, slurp the entire file at once
1400 wxString text;
1401 if ( !file.ReadAll(&text) )
1402 return;
1403
1404 int pos = text.Find(_T("Icon="));
1405 if ( pos == wxNOT_FOUND )
1406 {
1407 // no icon info
1408 return;
1409 }
1410
1411 wxString icon;
1412
1413 const wxChar *pc = text.c_str() + pos + 5; // 5 == strlen("Icon=")
1414 while ( *pc && *pc != _T('\n') )
1415 {
1416 icon += *pc++;
1417 }
1418
1419 if ( !!icon )
1420 {
1421 // don't check that the file actually exists - would be too slow
1422 icon.Prepend(_T("/usr/share/icons/"));
1423
1424 // construct mimetype from the directory name and the basename of the
1425 // file (it always has .kdelnk extension)
1426 wxString mimetype;
1427 mimetype << subdir << _T('/') << filename.BeforeLast(_T('.'));
1428
1429 // do we already have this MIME type?
1430 int i = ms_mimetypes.Index(mimetype);
1431 if ( i == wxNOT_FOUND )
1432 {
1433 // add it
1434 size_t n = ms_mimetypes.Add(mimetype);
1435 ms_icons.Insert(icon, n);
1436 }
1437 else
1438 {
1439 // replace the old value
1440 ms_icons[(size_t)i] = icon;
1441 }
1442 }
1443 }
1444
1445 void wxKDEIconHandler::LoadLinksForMimeType(const wxString& dirbase,
1446 const wxString& subdir)
1447 {
1448 wxString dirname = dirbase;
1449 dirname += subdir;
1450 wxDir dir(dirname);
1451 if ( !dir.IsOpened() )
1452 return;
1453
1454 dirname += _T('/');
1455
1456 wxString filename;
1457 bool cont = dir.GetFirst(&filename, _T("*.kdelnk"), wxDIR_FILES);
1458 while ( cont )
1459 {
1460 LoadLinksForMimeSubtype(dirname, subdir, filename);
1461
1462 cont = dir.GetNext(&filename);
1463 }
1464 }
1465
1466 void wxKDEIconHandler::LoadLinkFilesFromDir(const wxString& dirbase)
1467 {
1468 wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase),
1469 _T("base directory shouldn't end with a slash") );
1470
1471 wxString dirname = dirbase;
1472 dirname << _T("/mimelnk");
1473
1474 if ( !wxDir::Exists(dirname) )
1475 return;
1476
1477 wxDir dir(dirname);
1478 if ( !dir.IsOpened() )
1479 return;
1480
1481 // we will concatenate it with dir name to get the full path below
1482 dirname += _T('/');
1483
1484 wxString subdir;
1485 bool cont = dir.GetFirst(&subdir, wxEmptyString, wxDIR_DIRS);
1486 while ( cont )
1487 {
1488 LoadLinksForMimeType(dirname, subdir);
1489
1490 cont = dir.GetNext(&subdir);
1491 }
1492 }
1493
1494 void wxKDEIconHandler::Init()
1495 {
1496 wxArrayString dirs;
1497 dirs.Add(_T("/usr/share"));
1498 dirs.Add(wxGetHomeDir() + _T("/.kde/share"));
1499
1500 size_t nDirs = dirs.GetCount();
1501 for ( size_t nDir = 0; nDir < nDirs; nDir++ )
1502 {
1503 LoadLinkFilesFromDir(dirs[nDir]);
1504 }
1505
1506 m_inited = TRUE;
1507 }
1508
1509 bool wxKDEIconHandler::GetIcon(const wxString& mimetype, wxIcon *icon)
1510 {
1511 if ( !m_inited )
1512 {
1513 Init();
1514 }
1515
1516 int index = ms_mimetypes.Index(mimetype);
1517 if ( index == wxNOT_FOUND )
1518 return FALSE;
1519
1520 wxString iconname = ms_icons[(size_t)index];
1521
1522 #if wxUSE_GUI
1523 *icon = wxIcon(iconname);
1524 #else
1525 // helpful for testing in console mode
1526 wxLogDebug(_T("Found KDE icon for '%s': '%s'\n"),
1527 mimetype.c_str(), iconname.c_str());
1528 #endif
1529
1530 return TRUE;
1531 }
1532
1533 // ----------------------------------------------------------------------------
1534 // wxFileTypeImpl (Unix)
1535 // ----------------------------------------------------------------------------
1536
1537 MailCapEntry *
1538 wxFileTypeImpl::GetEntry(const wxFileType::MessageParameters& params) const
1539 {
1540 wxString command;
1541 MailCapEntry *entry = m_manager->m_aEntries[m_index];
1542 while ( entry != NULL ) {
1543 // notice that an empty command would always succeed (it's ok)
1544 command = wxFileType::ExpandCommand(entry->GetTestCmd(), params);
1545
1546 if ( command.IsEmpty() || (wxSystem(command) == 0) ) {
1547 // ok, passed
1548 wxLogTrace(wxT("Test '%s' for mime type '%s' succeeded."),
1549 command.c_str(), params.GetMimeType().c_str());
1550 break;
1551 }
1552 else {
1553 wxLogTrace(wxT("Test '%s' for mime type '%s' failed."),
1554 command.c_str(), params.GetMimeType().c_str());
1555 }
1556
1557 entry = entry->GetNext();
1558 }
1559
1560 return entry;
1561 }
1562
1563 bool wxFileTypeImpl::GetIcon(wxIcon *icon) const
1564 {
1565 wxString mimetype;
1566 (void)GetMimeType(&mimetype);
1567
1568 ArrayIconHandlers& handlers = m_manager->GetIconHandlers();
1569 size_t count = handlers.GetCount();
1570 for ( size_t n = 0; n < count; n++ )
1571 {
1572 if ( handlers[n]->GetIcon(mimetype, icon) )
1573 return TRUE;
1574 }
1575
1576 return FALSE;
1577 }
1578
1579 bool
1580 wxFileTypeImpl::GetExpandedCommand(wxString *expandedCmd,
1581 const wxFileType::MessageParameters& params,
1582 bool open) const
1583 {
1584 MailCapEntry *entry = GetEntry(params);
1585 if ( entry == NULL ) {
1586 // all tests failed...
1587 return FALSE;
1588 }
1589
1590 wxString cmd = open ? entry->GetOpenCmd() : entry->GetPrintCmd();
1591 if ( cmd.IsEmpty() ) {
1592 // may happen, especially for "print"
1593 return FALSE;
1594 }
1595
1596 *expandedCmd = wxFileType::ExpandCommand(cmd, params);
1597 return TRUE;
1598 }
1599
1600 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
1601 {
1602 wxString strExtensions = m_manager->GetExtension(m_index);
1603 extensions.Empty();
1604
1605 // one extension in the space or comma delimitid list
1606 wxString strExt;
1607 for ( const wxChar *p = strExtensions; ; p++ ) {
1608 if ( *p == wxT(' ') || *p == wxT(',') || *p == wxT('\0') ) {
1609 if ( !strExt.IsEmpty() ) {
1610 extensions.Add(strExt);
1611 strExt.Empty();
1612 }
1613 //else: repeated spaces (shouldn't happen, but it's not that
1614 // important if it does happen)
1615
1616 if ( *p == wxT('\0') )
1617 break;
1618 }
1619 else if ( *p == wxT('.') ) {
1620 // remove the dot from extension (but only if it's the first char)
1621 if ( !strExt.IsEmpty() ) {
1622 strExt += wxT('.');
1623 }
1624 //else: no, don't append it
1625 }
1626 else {
1627 strExt += *p;
1628 }
1629 }
1630
1631 return TRUE;
1632 }
1633
1634 // ----------------------------------------------------------------------------
1635 // wxMimeTypesManagerImpl (Unix)
1636 // ----------------------------------------------------------------------------
1637
1638 /* static */
1639 ArrayIconHandlers& wxMimeTypesManagerImpl::GetIconHandlers()
1640 {
1641 if ( ms_iconHandlers.GetCount() == 0 )
1642 {
1643 ms_iconHandlers.Add(&gs_iconHandlerGNOME);
1644 ms_iconHandlers.Add(&gs_iconHandlerKDE);
1645 }
1646
1647 return ms_iconHandlers;
1648 }
1649
1650 // read system and user mailcaps (TODO implement mime.types support)
1651 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1652 {
1653 // directories where we look for mailcap and mime.types by default
1654 // (taken from metamail(1) sources)
1655 static const wxChar *aStandardLocations[] =
1656 {
1657 wxT("/etc"),
1658 wxT("/usr/etc"),
1659 wxT("/usr/local/etc"),
1660 wxT("/etc/mail"),
1661 wxT("/usr/public/lib")
1662 };
1663
1664 // first read the system wide file(s)
1665 for ( size_t n = 0; n < WXSIZEOF(aStandardLocations); n++ ) {
1666 wxString dir = aStandardLocations[n];
1667
1668 wxString file = dir + wxT("/mailcap");
1669 if ( wxFile::Exists(file) ) {
1670 ReadMailcap(file);
1671 }
1672
1673 file = dir + wxT("/mime.types");
1674 if ( wxFile::Exists(file) ) {
1675 ReadMimeTypes(file);
1676 }
1677 }
1678
1679 wxString strHome = wxGetenv(wxT("HOME"));
1680
1681 // and now the users mailcap
1682 wxString strUserMailcap = strHome + wxT("/.mailcap");
1683 if ( wxFile::Exists(strUserMailcap) ) {
1684 ReadMailcap(strUserMailcap);
1685 }
1686
1687 // read the users mime.types
1688 wxString strUserMimeTypes = strHome + wxT("/.mime.types");
1689 if ( wxFile::Exists(strUserMimeTypes) ) {
1690 ReadMimeTypes(strUserMimeTypes);
1691 }
1692 }
1693
1694 wxFileType *
1695 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
1696 {
1697 size_t count = m_aExtensions.GetCount();
1698 for ( size_t n = 0; n < count; n++ ) {
1699 wxString extensions = m_aExtensions[n];
1700 while ( !extensions.IsEmpty() ) {
1701 wxString field = extensions.BeforeFirst(wxT(' '));
1702 extensions = extensions.AfterFirst(wxT(' '));
1703
1704 // consider extensions as not being case-sensitive
1705 if ( field.IsSameAs(ext, FALSE /* no case */) ) {
1706 // found
1707 wxFileType *fileType = new wxFileType;
1708 fileType->m_impl->Init(this, n);
1709
1710 return fileType;
1711 }
1712 }
1713 }
1714
1715 // not found
1716 return NULL;
1717 }
1718
1719 wxFileType *
1720 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
1721 {
1722 // mime types are not case-sensitive
1723 wxString mimetype(mimeType);
1724 mimetype.MakeLower();
1725
1726 // first look for an exact match
1727 int index = m_aTypes.Index(mimetype);
1728 if ( index == wxNOT_FOUND ) {
1729 // then try to find "text/*" as match for "text/plain" (for example)
1730 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
1731 // the whole string - ok.
1732 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
1733
1734 size_t nCount = m_aTypes.Count();
1735 for ( size_t n = 0; n < nCount; n++ ) {
1736 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
1737 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") ) {
1738 index = n;
1739 break;
1740 }
1741 }
1742 }
1743
1744 if ( index != wxNOT_FOUND ) {
1745 wxFileType *fileType = new wxFileType;
1746 fileType->m_impl->Init(this, index);
1747
1748 return fileType;
1749 }
1750 else {
1751 // not found...
1752 return NULL;
1753 }
1754 }
1755
1756 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
1757 {
1758 wxString extensions;
1759 const wxArrayString& exts = filetype.GetExtensions();
1760 size_t nExts = exts.GetCount();
1761 for ( size_t nExt = 0; nExt < nExts; nExt++ ) {
1762 if ( nExt > 0 ) {
1763 extensions += wxT(' ');
1764 }
1765 extensions += exts[nExt];
1766 }
1767
1768 AddMimeTypeInfo(filetype.GetMimeType(),
1769 extensions,
1770 filetype.GetDescription());
1771
1772 AddMailcapInfo(filetype.GetMimeType(),
1773 filetype.GetOpenCommand(),
1774 filetype.GetPrintCommand(),
1775 wxT(""),
1776 filetype.GetDescription());
1777 }
1778
1779 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
1780 const wxString& strExtensions,
1781 const wxString& strDesc)
1782 {
1783 int index = m_aTypes.Index(strMimeType);
1784 if ( index == wxNOT_FOUND ) {
1785 // add a new entry
1786 m_aTypes.Add(strMimeType);
1787 m_aEntries.Add(NULL);
1788 m_aExtensions.Add(strExtensions);
1789 m_aDescriptions.Add(strDesc);
1790 }
1791 else {
1792 // modify an existing one
1793 if ( !strDesc.IsEmpty() ) {
1794 m_aDescriptions[index] = strDesc; // replace old value
1795 }
1796 m_aExtensions[index] += ' ' + strExtensions;
1797 }
1798 }
1799
1800 void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType,
1801 const wxString& strOpenCmd,
1802 const wxString& strPrintCmd,
1803 const wxString& strTest,
1804 const wxString& strDesc)
1805 {
1806 MailCapEntry *entry = new MailCapEntry(strOpenCmd, strPrintCmd, strTest);
1807
1808 int nIndex = m_aTypes.Index(strType);
1809 if ( nIndex == wxNOT_FOUND ) {
1810 // new file type
1811 m_aTypes.Add(strType);
1812
1813 m_aEntries.Add(entry);
1814 m_aExtensions.Add(wxT(""));
1815 m_aDescriptions.Add(strDesc);
1816 }
1817 else {
1818 // always append the entry in the tail of the list - info added with
1819 // this function can only come from AddFallbacks()
1820 MailCapEntry *entryOld = m_aEntries[nIndex];
1821 if ( entryOld )
1822 entry->Append(entryOld);
1823 else
1824 m_aEntries[nIndex] = entry;
1825 }
1826 }
1827
1828 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
1829 {
1830 wxLogTrace(wxT("--- Parsing mime.types file '%s' ---"), strFileName.c_str());
1831
1832 wxTextFile file(strFileName);
1833 if ( !file.Open() )
1834 return FALSE;
1835
1836 // the information we extract
1837 wxString strMimeType, strDesc, strExtensions;
1838
1839 size_t nLineCount = file.GetLineCount();
1840 const wxChar *pc = NULL;
1841 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
1842 if ( pc == NULL ) {
1843 // now we're at the start of the line
1844 pc = file[nLine].c_str();
1845 }
1846 else {
1847 // we didn't finish with the previous line yet
1848 nLine--;
1849 }
1850
1851 // skip whitespace
1852 while ( wxIsspace(*pc) )
1853 pc++;
1854
1855 // comment or blank line?
1856 if ( *pc == wxT('#') || !*pc ) {
1857 // skip the whole line
1858 pc = NULL;
1859 continue;
1860 }
1861
1862 // detect file format
1863 const wxChar *pEqualSign = wxStrchr(pc, wxT('='));
1864 if ( pEqualSign == NULL ) {
1865 // brief format
1866 // ------------
1867
1868 // first field is mime type
1869 for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != wxT('\0'); pc++ ) {
1870 strMimeType += *pc;
1871 }
1872
1873 // skip whitespace
1874 while ( wxIsspace(*pc) )
1875 pc++;
1876
1877 // take all the rest of the string
1878 strExtensions = pc;
1879
1880 // no description...
1881 strDesc.Empty();
1882 }
1883 else {
1884 // expanded format
1885 // ---------------
1886
1887 // the string on the left of '=' is the field name
1888 wxString strLHS(pc, pEqualSign - pc);
1889
1890 // eat whitespace
1891 for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ )
1892 ;
1893
1894 const wxChar *pEnd;
1895 if ( *pc == wxT('"') ) {
1896 // the string is quoted and ends at the matching quote
1897 pEnd = wxStrchr(++pc, wxT('"'));
1898 if ( pEnd == NULL ) {
1899 wxLogWarning(_("Mime.types file %s, line %d: unterminated "
1900 "quoted string."),
1901 strFileName.c_str(), nLine + 1);
1902 }
1903 }
1904 else {
1905 // unquoted string ends at the first space
1906 for ( pEnd = pc; !wxIsspace(*pEnd); pEnd++ )
1907 ;
1908 }
1909
1910 // now we have the RHS (field value)
1911 wxString strRHS(pc, pEnd - pc);
1912
1913 // check what follows this entry
1914 if ( *pEnd == wxT('"') ) {
1915 // skip this quote
1916 pEnd++;
1917 }
1918
1919 for ( pc = pEnd; wxIsspace(*pc); pc++ )
1920 ;
1921
1922 // if there is something left, it may be either a '\\' to continue
1923 // the line or the next field of the same entry
1924 bool entryEnded = *pc == wxT('\0'),
1925 nextFieldOnSameLine = FALSE;
1926 if ( !entryEnded ) {
1927 nextFieldOnSameLine = ((*pc != wxT('\\')) || (pc[1] != wxT('\0')));
1928 }
1929
1930 // now see what we got
1931 if ( strLHS == wxT("type") ) {
1932 strMimeType = strRHS;
1933 }
1934 else if ( strLHS == wxT("desc") ) {
1935 strDesc = strRHS;
1936 }
1937 else if ( strLHS == wxT("exts") ) {
1938 strExtensions = strRHS;
1939 }
1940 else {
1941 wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
1942 strFileName.c_str(), nLine + 1, strLHS.c_str());
1943 }
1944
1945 if ( !entryEnded ) {
1946 if ( !nextFieldOnSameLine )
1947 pc = NULL;
1948 //else: don't reset it
1949
1950 // as we don't reset strMimeType, the next field in this entry
1951 // will be interpreted correctly.
1952
1953 continue;
1954 }
1955 }
1956
1957 // although it doesn't seem to be covered by RFCs, some programs
1958 // (notably Netscape) create their entries with several comma
1959 // separated extensions (RFC mention the spaces only)
1960 strExtensions.Replace(wxT(","), wxT(" "));
1961
1962 // also deal with the leading dot
1963 if ( !strExtensions.IsEmpty() && strExtensions[0u] == wxT('.') )
1964 {
1965 strExtensions.erase(0, 1);
1966 }
1967
1968 AddMimeTypeInfo(strMimeType, strExtensions, strDesc);
1969
1970 // finished with this line
1971 pc = NULL;
1972 }
1973
1974 // check our data integriry
1975 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
1976 m_aTypes.Count() == m_aExtensions.Count() &&
1977 m_aTypes.Count() == m_aDescriptions.Count() );
1978
1979 return TRUE;
1980 }
1981
1982 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
1983 bool fallback)
1984 {
1985 wxLogTrace(wxT("--- Parsing mailcap file '%s' ---"), strFileName.c_str());
1986
1987 wxTextFile file(strFileName);
1988 if ( !file.Open() )
1989 return FALSE;
1990
1991 // see the comments near the end of function for the reason we need these
1992 // variables (search for the next occurence of them)
1993 // indices of MIME types (in m_aTypes) we already found in this file
1994 wxArrayInt aEntryIndices;
1995 // aLastIndices[n] is the index of last element in
1996 // m_aEntries[aEntryIndices[n]] from this file
1997 wxArrayInt aLastIndices;
1998
1999 size_t nLineCount = file.GetLineCount();
2000 for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
2001 // now we're at the start of the line
2002 const wxChar *pc = file[nLine].c_str();
2003
2004 // skip whitespace
2005 while ( wxIsspace(*pc) )
2006 pc++;
2007
2008 // comment or empty string?
2009 if ( *pc == wxT('#') || *pc == wxT('\0') )
2010 continue;
2011
2012 // no, do parse
2013
2014 // what field are we currently in? The first 2 are fixed and there may
2015 // be an arbitrary number of other fields -- currently, we are not
2016 // interested in any of them, but we should parse them as well...
2017 enum
2018 {
2019 Field_Type,
2020 Field_OpenCmd,
2021 Field_Other
2022 } currentToken = Field_Type;
2023
2024 // the flags and field values on the current line
2025 bool needsterminal = FALSE,
2026 copiousoutput = FALSE;
2027 wxString strType,
2028 strOpenCmd,
2029 strPrintCmd,
2030 strTest,
2031 strDesc,
2032 curField; // accumulator
2033 for ( bool cont = TRUE; cont; pc++ ) {
2034 switch ( *pc ) {
2035 case wxT('\\'):
2036 // interpret the next character literally (notice that
2037 // backslash can be used for line continuation)
2038 if ( *++pc == wxT('\0') ) {
2039 // fetch the next line.
2040
2041 // pc currently points to nowhere, but after the next
2042 // pc++ in the for line it will point to the beginning
2043 // of the next line in the file
2044 pc = file[++nLine].c_str() - 1;
2045 }
2046 else {
2047 // just a normal character
2048 curField += *pc;
2049 }
2050 break;
2051
2052 case wxT('\0'):
2053 cont = FALSE; // end of line reached, exit the loop
2054
2055 // fall through
2056
2057 case wxT(';'):
2058 // store this field and start looking for the next one
2059
2060 // trim whitespaces from both sides
2061 curField.Trim(TRUE).Trim(FALSE);
2062
2063 switch ( currentToken ) {
2064 case Field_Type:
2065 strType = curField;
2066 if ( strType.Find(wxT('/')) == wxNOT_FOUND ) {
2067 // we interpret "type" as "type/*"
2068 strType += wxT("/*");
2069 }
2070
2071 currentToken = Field_OpenCmd;
2072 break;
2073
2074 case Field_OpenCmd:
2075 strOpenCmd = curField;
2076
2077 currentToken = Field_Other;
2078 break;
2079
2080 case Field_Other:
2081 {
2082 // "good" mailcap entry?
2083 bool ok = TRUE;
2084
2085 // is this something of the form foo=bar?
2086 const wxChar *pEq = wxStrchr(curField, wxT('='));
2087 if ( pEq != NULL ) {
2088 wxString lhs = curField.BeforeFirst(wxT('=')),
2089 rhs = curField.AfterFirst(wxT('='));
2090
2091 lhs.Trim(TRUE); // from right
2092 rhs.Trim(FALSE); // from left
2093
2094 if ( lhs == wxT("print") )
2095 strPrintCmd = rhs;
2096 else if ( lhs == wxT("test") )
2097 strTest = rhs;
2098 else if ( lhs == wxT("description") ) {
2099 // it might be quoted
2100 if ( rhs[0u] == wxT('"') &&
2101 rhs.Last() == wxT('"') ) {
2102 strDesc = wxString(rhs.c_str() + 1,
2103 rhs.Len() - 2);
2104 }
2105 else {
2106 strDesc = rhs;
2107 }
2108 }
2109 else if ( lhs == wxT("compose") ||
2110 lhs == wxT("composetyped") ||
2111 lhs == wxT("notes") ||
2112 lhs == wxT("edit") )
2113 ; // ignore
2114 else
2115 ok = FALSE;
2116
2117 }
2118 else {
2119 // no, it's a simple flag
2120 // TODO support the flags:
2121 // 1. create an xterm for 'needsterminal'
2122 // 2. append "| $PAGER" for 'copiousoutput'
2123 if ( curField == wxT("needsterminal") )
2124 needsterminal = TRUE;
2125 else if ( curField == wxT("copiousoutput") )
2126 copiousoutput = TRUE;
2127 else if ( curField == wxT("textualnewlines") )
2128 ; // ignore
2129 else
2130 ok = FALSE;
2131 }
2132
2133 if ( !ok )
2134 {
2135 // we don't understand this field, but
2136 // Netscape stores info in it, so don't warn
2137 // about it
2138 if ( curField.Left(16u) != "x-mozilla-flags=" )
2139 {
2140 // don't flood the user with error
2141 // messages if we don't understand
2142 // something in his mailcap, but give
2143 // them in debug mode because this might
2144 // be useful for the programmer
2145 wxLogDebug
2146 (
2147 wxT("Mailcap file %s, line %d: "
2148 "unknown field '%s' for the "
2149 "MIME type '%s' ignored."),
2150 strFileName.c_str(),
2151 nLine + 1,
2152 curField.c_str(),
2153 strType.c_str()
2154 );
2155 }
2156 }
2157 }
2158
2159 // it already has this value
2160 //currentToken = Field_Other;
2161 break;
2162
2163 default:
2164 wxFAIL_MSG(wxT("unknown field type in mailcap"));
2165 }
2166
2167 // next token starts immediately after ';'
2168 curField.Empty();
2169 break;
2170
2171 default:
2172 curField += *pc;
2173 }
2174 }
2175
2176 // check that we really read something reasonable
2177 if ( currentToken == Field_Type || currentToken == Field_OpenCmd ) {
2178 wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
2179 "ignored."),
2180 strFileName.c_str(), nLine + 1);
2181 }
2182 else {
2183 MailCapEntry *entry = new MailCapEntry(strOpenCmd,
2184 strPrintCmd,
2185 strTest);
2186
2187 // NB: because of complications below (we must get entries priority
2188 // right), we can't use AddMailcapInfo() here, unfortunately.
2189 strType.MakeLower();
2190 int nIndex = m_aTypes.Index(strType);
2191 if ( nIndex == wxNOT_FOUND ) {
2192 // new file type
2193 m_aTypes.Add(strType);
2194
2195 m_aEntries.Add(entry);
2196 m_aExtensions.Add(wxT(""));
2197 m_aDescriptions.Add(strDesc);
2198 }
2199 else {
2200 // modify the existing entry: the entries in one and the same
2201 // file are read in top-to-bottom order, i.e. the entries read
2202 // first should be tried before the entries below. However,
2203 // the files read later should override the settings in the
2204 // files read before (except if fallback is TRUE), thus we
2205 // Insert() the new entry to the list if it has already
2206 // occured in _this_ file, but Prepend() it if it occured in
2207 // some of the previous ones and Append() to it in the
2208 // fallback case
2209
2210 if ( fallback ) {
2211 // 'fallback' parameter prevents the entries from this
2212 // file from overriding the other ones - always append
2213 MailCapEntry *entryOld = m_aEntries[nIndex];
2214 if ( entryOld )
2215 entry->Append(entryOld);
2216 else
2217 m_aEntries[nIndex] = entry;
2218 }
2219 else {
2220 int entryIndex = aEntryIndices.Index(nIndex);
2221 if ( entryIndex == wxNOT_FOUND ) {
2222 // first time in this file
2223 aEntryIndices.Add(nIndex);
2224 aLastIndices.Add(0);
2225
2226 entry->Prepend(m_aEntries[nIndex]);
2227 m_aEntries[nIndex] = entry;
2228 }
2229 else {
2230 // not the first time in _this_ file
2231 size_t nEntryIndex = (size_t)entryIndex;
2232 MailCapEntry *entryOld = m_aEntries[nIndex];
2233 if ( entryOld )
2234 entry->Insert(entryOld, aLastIndices[nEntryIndex]);
2235 else
2236 m_aEntries[nIndex] = entry;
2237
2238 // the indices were shifted by 1
2239 aLastIndices[nEntryIndex]++;
2240 }
2241 }
2242
2243 if ( !strDesc.IsEmpty() ) {
2244 // replace the old one - what else can we do??
2245 m_aDescriptions[nIndex] = strDesc;
2246 }
2247 }
2248 }
2249
2250 // check our data integriry
2251 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
2252 m_aTypes.Count() == m_aExtensions.Count() &&
2253 m_aTypes.Count() == m_aDescriptions.Count() );
2254 }
2255
2256 return TRUE;
2257 }
2258
2259 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
2260 {
2261 mimetypes.Empty();
2262
2263 wxString type;
2264 size_t count = m_aTypes.GetCount();
2265 for ( size_t n = 0; n < count; n++ )
2266 {
2267 // don't return template types from here (i.e. anything containg '*')
2268 type = m_aTypes[n];
2269 if ( type.Find(_T('*')) == wxNOT_FOUND )
2270 {
2271 mimetypes.Add(type);
2272 }
2273 }
2274
2275 return mimetypes.GetCount();
2276 }
2277
2278 #endif
2279 // OS type
2280
2281 #endif
2282 // wxUSE_FILE && wxUSE_TEXTFILE
2283
2284 #endif
2285 // __WIN16__