1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/mimetype.cpp
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_MIMETYPE && wxUSE_FILE
21 #include "wx/unix/mimetype.h"
24 #include "wx/dynarray.h"
25 #include "wx/string.h"
32 #include "wx/confbase.h"
36 #include "wx/tokenzr.h"
37 #include "wx/iconloc.h"
38 #include "wx/filename.h"
40 #include "wx/apptrait.h"
42 // other standard headers
52 wxMimeTextFile(const wxString
& fname
)
59 wxFFile
file( m_fname
);
63 size_t size
= file
.Length();
64 wxCharBuffer
buffer( size
);
65 file
.Read( (void*) (const char*) buffer
, size
);
67 // Check for valid UTF-8 here?
68 wxString all
= wxString::FromUTF8( buffer
, size
);
70 wxStringTokenizer
tok( all
, "\n" );
71 while (tok
.HasMoreTokens())
73 wxString t
= tok
.GetNextToken();
75 if ((!!t
) && (t
.Find( "comment" ) != 0) && (t
.Find( "#" ) != 0) && (t
.Find( "generic" ) != 0))
81 unsigned int GetLineCount() const { return m_text
.GetCount(); }
82 wxString
&GetLine( unsigned int line
) { return m_text
[line
]; }
84 int pIndexOf(const wxString
& sSearch
,
85 bool bIncludeComments
= false,
88 wxString sTest
= sSearch
;
90 for(size_t i
= iStart
; i
< GetLineCount(); i
++)
92 wxString sLine
= GetLine(i
);
93 if(bIncludeComments
|| ! sLine
.StartsWith(wxT("#")))
95 if(sLine
.StartsWith(sTest
))
102 wxString
GetVerb(size_t i
)
104 if (i
> GetLineCount() )
105 return wxEmptyString
;
107 wxString sTmp
= GetLine(i
).BeforeFirst(wxT('='));
111 wxString
GetCmd(size_t i
)
113 if (i
> GetLineCount() )
114 return wxEmptyString
;
116 wxString sTmp
= GetLine(i
).AfterFirst(wxT('='));
121 wxArrayString m_text
;
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // MIME code tracing mask
130 #define TRACE_MIME wxT("mime")
133 // Read a XDG *.desktop file of type 'Application'
134 void wxMimeTypesManagerImpl::LoadXDGApp(const wxString
& filename
)
136 wxLogTrace(TRACE_MIME
, wxT("loading XDG file %s"), filename
.c_str());
138 wxMimeTextFile
file(filename
);
142 // Here, only type 'Application' should be considered.
143 int nIndex
= file
.pIndexOf( "Type=" );
144 if (nIndex
!= wxNOT_FOUND
&& file
.GetCmd(nIndex
) != "application")
147 // The hidden entry specifies a file to be ignored.
148 nIndex
= file
.pIndexOf( "Hidden=" );
149 if (nIndex
!= wxNOT_FOUND
&& file
.GetCmd(nIndex
) == "true")
152 // Semicolon separated list of mime types handled by the application.
153 nIndex
= file
.pIndexOf( wxT("MimeType=") );
154 if (nIndex
== wxNOT_FOUND
)
156 wxString mimetypes
= file
.GetCmd (nIndex
);
158 // Name of the application
160 nIndex
= wxNOT_FOUND
;
161 #if wxUSE_INTL // try "Name[locale name]" first
162 wxLocale
*locale
= wxGetLocale();
164 nIndex
= file
.pIndexOf(_T("Name[")+locale
->GetName()+_T("]="));
166 if(nIndex
== wxNOT_FOUND
)
167 nIndex
= file
.pIndexOf( wxT("Name=") );
168 if(nIndex
!= wxNOT_FOUND
)
169 nameapp
= file
.GetCmd(nIndex
);
171 // Icon of the application.
172 wxString nameicon
, namemini
;
173 nIndex
= wxNOT_FOUND
;
174 #if wxUSE_INTL // try "Icon[locale name]" first
176 nIndex
= file
.pIndexOf(_T("Icon[")+locale
->GetName()+_T("]="));
178 if(nIndex
== wxNOT_FOUND
)
179 nIndex
= file
.pIndexOf( wxT("Icon=") );
180 if(nIndex
!= wxNOT_FOUND
) {
181 nameicon
= wxString(wxT("--icon ")) + file
.GetCmd(nIndex
);
182 namemini
= wxString(wxT("--miniicon ")) + file
.GetCmd(nIndex
);
185 // Replace some of the field code in the 'Exec' entry.
186 // TODO: deal with %d, %D, %n, %N, %k and %v (but last one is deprecated)
187 nIndex
= file
.pIndexOf( wxT("Exec=") );
188 if (nIndex
== wxNOT_FOUND
)
190 wxString sCmd
= file
.GetCmd(nIndex
);
191 // we expect %f; others including %F and %U and %u are possible
192 sCmd
.Replace(wxT("%F"), wxT("%f"));
193 sCmd
.Replace(wxT("%U"), wxT("%f"));
194 sCmd
.Replace(wxT("%u"), wxT("%f"));
195 if (0 == sCmd
.Replace ( wxT("%f"), wxT("%s") ))
196 sCmd
= sCmd
+ wxT(" %s");
197 sCmd
.Replace(wxT("%c"), nameapp
);
198 sCmd
.Replace(wxT("%i"), nameicon
);
199 sCmd
.Replace(wxT("%m"), namemini
);
201 wxStringTokenizer
tokenizer(mimetypes
, _T(";"));
202 while(tokenizer
.HasMoreTokens()) {
203 wxString mimetype
= tokenizer
.GetNextToken().Lower();
204 nIndex
= m_aTypes
.Index(mimetype
);
205 if(nIndex
!= wxNOT_FOUND
) { // is this a known MIME type?
206 wxMimeTypeCommands
* entry
= m_aEntries
[nIndex
];
207 entry
->AddOrReplaceVerb(wxT("open"), sCmd
);
212 void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString
& dirname
)
214 // Don't complain if we don't have permissions to read - it confuses users
217 if(! wxDir::Exists(dirname
))
220 if ( !dir
.IsOpened() )
224 // Look into .desktop files
225 bool cont
= dir
.GetFirst(&filename
, _T("*.desktop"), wxDIR_FILES
);
228 wxFileName
p(dirname
, filename
);
229 LoadXDGApp( p
.GetFullPath() );
230 cont
= dir
.GetNext(&filename
);
234 // RR: I'm not sure this makes any sense. On my system we'll just
235 // scan the YAST2 and other useless directories
237 // Look recursively into subdirs
238 cont
= dir
.GetFirst(&filename
, wxEmptyString
, wxDIR_DIRS
);
241 wxFileName
p(dirname
, wxEmptyString
);
242 p
.AppendDir(filename
);
243 LoadXDGAppsFilesFromDir( p
.GetPath() );
244 cont
= dir
.GetNext(&filename
);
250 void wxMimeTypesManagerImpl::LoadXDGGlobs(const wxString
& filename
)
252 wxLogTrace(TRACE_MIME
, wxT("loading XDG globs file from %s"), filename
.c_str());
254 wxMimeTextFile
file(filename
);
259 for (i
= 0; i
< file
.GetLineCount(); i
++)
261 wxStringTokenizer
tok( file
.GetLine(i
), ":" );
262 wxString mime
= tok
.GetNextToken();
263 wxString ext
= tok
.GetNextToken();
268 AddToMimeData(mime
, wxEmptyString
, NULL
, exts
, wxEmptyString
, true );
272 // ----------------------------------------------------------------------------
273 // wxFileTypeImpl (Unix)
274 // ----------------------------------------------------------------------------
276 wxString
wxFileTypeImpl::GetExpandedCommand(const wxString
& verb
, const wxFileType::MessageParameters
& params
) const
280 while ( (i
< m_index
.GetCount() ) && sTmp
.empty() )
282 sTmp
= m_manager
->GetCommand( verb
, m_index
[i
] );
286 return wxFileType::ExpandCommand(sTmp
, params
);
289 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
293 while ( (i
< m_index
.GetCount() ) && sTmp
.empty() )
295 sTmp
= m_manager
->m_aIcons
[m_index
[i
]];
304 iconLoc
->SetFileName(sTmp
);
310 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
313 size_t nCount
= m_index
.GetCount();
314 for (size_t i
= 0; i
< nCount
; i
++)
315 mimeTypes
.Add(m_manager
->m_aTypes
[m_index
[i
]]);
320 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
321 wxArrayString
*commands
,
322 const wxFileType::MessageParameters
& params
) const
324 wxString vrb
, cmd
, sTmp
;
326 wxMimeTypeCommands
* sPairs
;
328 // verbs and commands have been cleared already in mimecmn.cpp...
329 // if we find no entries in the exact match, try the inexact match
330 for (size_t n
= 0; ((count
== 0) && (n
< m_index
.GetCount())); n
++)
332 // list of verb = command pairs for this mimetype
333 sPairs
= m_manager
->m_aEntries
[m_index
[n
]];
335 for ( i
= 0; i
< sPairs
->GetCount(); i
++ )
337 vrb
= sPairs
->GetVerb(i
);
338 // some gnome entries have "." inside
339 vrb
= vrb
.AfterLast(wxT('.'));
340 cmd
= sPairs
->GetCmd(i
);
343 cmd
= wxFileType::ExpandCommand(cmd
, params
);
345 if ( vrb
.IsSameAs(wxT("open")))
348 verbs
->Insert(vrb
, 0u);
350 commands
->Insert(cmd
, 0u);
366 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
368 const wxString strExtensions
= m_manager
->GetExtension(m_index
[0]);
371 // one extension in the space or comma-delimited list
373 wxString::const_iterator end
= strExtensions
.end();
374 for ( wxString::const_iterator p
= strExtensions
.begin(); /* nothing */; ++p
)
376 if ( p
== end
|| *p
== wxT(' ') || *p
== wxT(',') )
378 if ( !strExt
.empty() )
380 extensions
.Add(strExt
);
383 //else: repeated spaces
384 // (shouldn't happen, but it's not that important if it does happen)
389 else if ( *p
== wxT('.') )
391 // remove the dot from extension (but only if it's the first char)
392 if ( !strExt
.empty() )
396 //else: no, don't append it
407 // set an arbitrary command:
408 // could adjust the code to ask confirmation if it already exists and
409 // overwriteprompt is true, but this is currently ignored as *Associate* has
410 // no overwrite prompt
412 wxFileTypeImpl::SetCommand(const wxString
& cmd
,
413 const wxString
& verb
,
414 bool WXUNUSED(overwriteprompt
))
416 wxArrayString strExtensions
;
417 wxString strDesc
, strIcon
;
419 wxArrayString strTypes
;
420 GetMimeTypes(strTypes
);
421 if ( strTypes
.IsEmpty() )
424 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands();
425 entry
->Add(verb
+ wxT("=") + cmd
+ wxT(" %s "));
428 size_t nCount
= strTypes
.GetCount();
429 for ( size_t i
= 0; i
< nCount
; i
++ )
431 if ( m_manager
->DoAssociation
440 // DoAssociation() took ownership of entry, don't delete it below
451 // ignore index on the grounds that we only have one icon in a Unix file
452 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& strIcon
, int WXUNUSED(index
))
457 wxArrayString strExtensions
;
460 wxArrayString strTypes
;
461 GetMimeTypes(strTypes
);
462 if ( strTypes
.IsEmpty() )
465 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands();
467 size_t nCount
= strTypes
.GetCount();
468 for ( size_t i
= 0; i
< nCount
; i
++ )
470 if ( m_manager
->DoAssociation
479 // we don't need to free entry now, DoAssociation() took ownership
491 // ----------------------------------------------------------------------------
492 // wxMimeTypesManagerImpl (Unix)
493 // ----------------------------------------------------------------------------
495 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
497 m_initialized
= false;
500 void wxMimeTypesManagerImpl::InitIfNeeded()
502 if ( !m_initialized
)
504 // set the flag first to prevent recursion
505 m_initialized
= true;
507 wxString wm
= wxTheApp
->GetTraits()->GetDesktopEnvironment();
509 if (wm
== wxT("KDE"))
510 Initialize( wxMAILCAP_KDE
);
511 else if (wm
== wxT("GNOME"))
512 Initialize( wxMAILCAP_GNOME
);
520 // read system and user mailcaps and other files
521 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles
,
522 const wxString
& sExtraDir
)
525 // XDG tables are never installed on OpenVMS
529 // Read MIME type - extension associations
530 LoadXDGGlobs( "/usr/share/mime/globs" );
531 LoadXDGGlobs( "/usr/local/share/mime/globs" );
533 // Load desktop files for XDG, and then override them with the defaults.
534 // We will override them one desktop file at a time, rather
535 // than one mime type at a time, but it should be a reasonable
538 wxString xdgDataHome
= wxGetenv("XDG_DATA_HOME");
539 if ( xdgDataHome
.empty() )
540 xdgDataHome
= wxGetHomeDir() + "/.local/share";
541 wxString xdgDataDirs
= wxGetenv("XDG_DATA_DIRS");
542 if ( xdgDataDirs
.empty() )
544 xdgDataDirs
= "/usr/local/share:/usr/share";
545 if (mailcapStyles
& wxMAILCAP_GNOME
)
546 xdgDataDirs
+= ":/usr/share/gnome:/opt/gnome/share";
547 if (mailcapStyles
& wxMAILCAP_KDE
)
548 xdgDataDirs
+= ":/usr/share/kde3:/opt/kde3/share";
550 if ( !sExtraDir
.empty() )
553 xdgDataDirs
+= sExtraDir
;
557 wxStringTokenizer
tokenizer(xdgDataDirs
, ":");
558 while ( tokenizer
.HasMoreTokens() )
560 wxString p
= tokenizer
.GetNextToken();
563 dirs
.insert(dirs
.begin(), xdgDataHome
);
565 wxString defaultsList
;
567 for (i
= 0; i
< dirs
.GetCount(); i
++)
569 wxString f
= dirs
[i
];
570 if (f
.Last() != '/') f
+= '/';
571 f
+= "applications/defaults.list";
579 // Load application files and associate them to corresponding mime types.
580 size_t nDirs
= dirs
.GetCount();
581 for (size_t nDir
= 0; nDir
< nDirs
; nDir
++)
583 wxString dirStr
= dirs
[nDir
];
584 if (dirStr
.Last() != '/') dirStr
+= '/';
585 dirStr
+= "applications";
586 LoadXDGAppsFilesFromDir(dirStr
);
589 if (!defaultsList
.IsEmpty())
591 wxArrayString deskTopFilesSeen
;
593 wxMimeTextFile
textfile(defaultsList
);
594 if ( textfile
.Open() )
596 int nIndex
= textfile
.pIndexOf( wxT("[Default Applications]") );
597 if (nIndex
!= wxNOT_FOUND
)
599 for (i
= nIndex
+1; i
< textfile
.GetLineCount(); i
++)
601 if (textfile
.GetLine(i
).Find(wxT("=")) != wxNOT_FOUND
)
603 wxString mimeType
= textfile
.GetVerb(i
);
604 wxString desktopFile
= textfile
.GetCmd(i
);
606 if (deskTopFilesSeen
.Index(desktopFile
) == wxNOT_FOUND
)
608 deskTopFilesSeen
.Add(desktopFile
);
610 for (j
= 0; j
< dirs
.GetCount(); j
++)
612 wxString desktopPath
= dirs
[j
];
613 if (desktopPath
.Last() != '/') desktopPath
+= '/';
614 desktopPath
+= "applications/";
615 desktopPath
+= desktopFile
;
617 if (wxFileExists(desktopPath
))
618 LoadXDGApp(desktopPath
);
629 // clear data so you can read another group of WM files
630 void wxMimeTypesManagerImpl::ClearData()
634 m_aExtensions
.Clear();
635 m_aDescriptions
.Clear();
637 WX_CLEAR_ARRAY(m_aEntries
);
641 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
646 wxFileType
* wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
650 wxString strType
= ftInfo
.GetMimeType();
651 wxString strDesc
= ftInfo
.GetDescription();
652 wxString strIcon
= ftInfo
.GetIconFile();
654 wxMimeTypeCommands
*entry
= new wxMimeTypeCommands();
656 if ( ! ftInfo
.GetOpenCommand().empty())
657 entry
->Add(wxT("open=") + ftInfo
.GetOpenCommand() + wxT(" %s "));
658 if ( ! ftInfo
.GetPrintCommand().empty())
659 entry
->Add(wxT("print=") + ftInfo
.GetPrintCommand() + wxT(" %s "));
661 // now find where these extensions are in the data store and remove them
662 wxArrayString sA_Exts
= ftInfo
.GetExtensions();
663 wxString sExt
, sExtStore
;
665 size_t nExtCount
= sA_Exts
.GetCount();
666 for (i
=0; i
< nExtCount
; i
++)
668 sExt
= sA_Exts
.Item(i
);
670 // clean up to just a space before and after
671 sExt
.Trim().Trim(false);
672 sExt
= wxT(' ') + sExt
+ wxT(' ');
673 size_t nCount
= m_aExtensions
.GetCount();
674 for (nIndex
= 0; nIndex
< nCount
; nIndex
++)
676 sExtStore
= m_aExtensions
.Item(nIndex
);
677 if (sExtStore
.Replace(sExt
, wxT(" ") ) > 0)
678 m_aExtensions
.Item(nIndex
) = sExtStore
;
682 if ( !DoAssociation(strType
, strIcon
, entry
, sA_Exts
, strDesc
) )
685 return GetFileTypeFromMimeType(strType
);
688 bool wxMimeTypesManagerImpl::DoAssociation(const wxString
& strType
,
689 const wxString
& strIcon
,
690 wxMimeTypeCommands
*entry
,
691 const wxArrayString
& strExtensions
,
692 const wxString
& strDesc
)
694 int nIndex
= AddToMimeData(strType
, strIcon
, entry
, strExtensions
, strDesc
, true);
696 if ( nIndex
== wxNOT_FOUND
)
702 int wxMimeTypesManagerImpl::AddToMimeData(const wxString
& strType
,
703 const wxString
& strIcon
,
704 wxMimeTypeCommands
*entry
,
705 const wxArrayString
& strExtensions
,
706 const wxString
& strDesc
,
707 bool replaceExisting
)
711 // ensure mimetype is always lower case
712 wxString mimeType
= strType
.Lower();
714 // is this a known MIME type?
715 int nIndex
= m_aTypes
.Index(mimeType
);
716 if ( nIndex
== wxNOT_FOUND
)
719 m_aTypes
.Add(mimeType
);
720 m_aIcons
.Add(strIcon
);
721 m_aEntries
.Add(entry
? entry
: new wxMimeTypeCommands
);
723 // change nIndex so we can use it below to add the extensions
724 m_aExtensions
.Add(wxEmptyString
);
725 nIndex
= m_aExtensions
.size() - 1;
727 m_aDescriptions
.Add(strDesc
);
729 else // yes, we already have it
731 if ( replaceExisting
)
733 // if new description change it
734 if ( !strDesc
.empty())
735 m_aDescriptions
[nIndex
] = strDesc
;
737 // if new icon change it
738 if ( !strIcon
.empty())
739 m_aIcons
[nIndex
] = strIcon
;
743 delete m_aEntries
[nIndex
];
744 m_aEntries
[nIndex
] = entry
;
747 else // add data we don't already have ...
749 // if new description add only if none
750 if ( m_aDescriptions
[nIndex
].empty() )
751 m_aDescriptions
[nIndex
] = strDesc
;
753 // if new icon and no existing icon
754 if ( m_aIcons
[nIndex
].empty() )
755 m_aIcons
[nIndex
] = strIcon
;
757 // add any new entries...
760 wxMimeTypeCommands
*entryOld
= m_aEntries
[nIndex
];
762 size_t count
= entry
->GetCount();
763 for ( size_t i
= 0; i
< count
; i
++ )
765 const wxString
& verb
= entry
->GetVerb(i
);
766 if ( !entryOld
->HasVerb(verb
) )
768 entryOld
->AddOrReplaceVerb(verb
, entry
->GetCmd(i
));
772 // as we don't store it anywhere, it won't be deleted later as
773 // usual -- do it immediately instead
779 // always add the extensions to this mimetype
780 wxString
& exts
= m_aExtensions
[nIndex
];
782 // add all extensions we don't have yet
784 size_t count
= strExtensions
.GetCount();
785 for ( size_t i
= 0; i
< count
; i
++ )
787 ext
= strExtensions
[i
];
790 if ( exts
.Find(ext
) == wxNOT_FOUND
)
796 // check data integrity
797 wxASSERT( m_aTypes
.GetCount() == m_aEntries
.GetCount() &&
798 m_aTypes
.GetCount() == m_aExtensions
.GetCount() &&
799 m_aTypes
.GetCount() == m_aIcons
.GetCount() &&
800 m_aTypes
.GetCount() == m_aDescriptions
.GetCount() );
805 wxFileType
* wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
812 size_t count
= m_aExtensions
.GetCount();
813 for ( size_t n
= 0; n
< count
; n
++ )
815 wxStringTokenizer
tk(m_aExtensions
[n
], wxT(' '));
817 while ( tk
.HasMoreTokens() )
819 // consider extensions as not being case-sensitive
820 if ( tk
.GetNextToken().IsSameAs(ext
, false /* no case */) )
823 wxFileType
*fileType
= new wxFileType
;
824 fileType
->m_impl
->Init(this, n
);
834 wxFileType
* wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
838 wxFileType
* fileType
= NULL
;
839 // mime types are not case-sensitive
840 wxString
mimetype(mimeType
);
841 mimetype
.MakeLower();
843 // first look for an exact match
844 int index
= m_aTypes
.Index(mimetype
);
846 if ( index
!= wxNOT_FOUND
)
848 fileType
= new wxFileType
;
849 fileType
->m_impl
->Init(this, index
);
852 // then try to find "text/*" as match for "text/plain" (for example)
853 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
854 // the whole string - ok.
857 wxString strCategory
= mimetype
.BeforeFirst(wxT('/'));
859 size_t nCount
= m_aTypes
.GetCount();
860 for ( size_t n
= 0; n
< nCount
; n
++ )
862 if ( (m_aTypes
[n
].BeforeFirst(wxT('/')) == strCategory
) &&
863 m_aTypes
[n
].AfterFirst(wxT('/')) == wxT("*") )
870 if ( index
!= wxNOT_FOUND
)
872 // don't throw away fileType that was already found
874 fileType
= new wxFileType
;
875 fileType
->m_impl
->Init(this, index
);
881 wxString
wxMimeTypesManagerImpl::GetCommand(const wxString
& verb
, size_t nIndex
) const
883 wxString command
, testcmd
, sV
, sTmp
;
884 sV
= verb
+ wxT("=");
886 // list of verb = command pairs for this mimetype
887 wxMimeTypeCommands
* sPairs
= m_aEntries
[nIndex
];
890 size_t nCount
= sPairs
->GetCount();
891 for ( i
= 0; i
< nCount
; i
++ )
893 sTmp
= sPairs
->GetVerbCmd (i
);
894 if ( sTmp
.Contains(sV
) )
895 command
= sTmp
.AfterFirst(wxT('='));
901 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo
& filetype
)
906 const wxArrayString
& exts
= filetype
.GetExtensions();
907 size_t nExts
= exts
.GetCount();
908 for ( size_t nExt
= 0; nExt
< nExts
; nExt
++ )
911 extensions
+= wxT(' ');
913 extensions
+= exts
[nExt
];
916 AddMimeTypeInfo(filetype
.GetMimeType(),
918 filetype
.GetDescription());
921 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString
& strMimeType
,
922 const wxString
& strExtensions
,
923 const wxString
& strDesc
)
925 // reading mailcap may find image/* , while
926 // reading mime.types finds image/gif and no match is made
927 // this means all the get functions don't work fix this
929 wxString sTmp
= strExtensions
;
932 sTmp
.Trim().Trim(false);
934 while (!sTmp
.empty())
936 sExts
.Add(sTmp
.AfterLast(wxT(' ')));
937 sTmp
= sTmp
.BeforeLast(wxT(' '));
940 AddToMimeData(strMimeType
, strIcon
, NULL
, sExts
, strDesc
, true);
943 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
949 size_t count
= m_aTypes
.GetCount();
950 for ( size_t n
= 0; n
< count
; n
++ )
952 // don't return template types from here (i.e. anything containg '*')
953 const wxString
&type
= m_aTypes
[n
];
954 if ( type
.Find(wxT('*')) == wxNOT_FOUND
)
960 return mimetypes
.GetCount();
963 // ----------------------------------------------------------------------------
964 // writing to MIME type files
965 // ----------------------------------------------------------------------------
967 bool wxMimeTypesManagerImpl::Unassociate(wxFileType
*ft
)
971 wxArrayString sMimeTypes
;
972 ft
->GetMimeTypes(sMimeTypes
);
975 size_t nCount
= sMimeTypes
.GetCount();
976 for (i
= 0; i
< nCount
; i
++)
978 const wxString
&sMime
= sMimeTypes
.Item(i
);
979 int nIndex
= m_aTypes
.Index(sMime
);
980 if ( nIndex
== wxNOT_FOUND
)
982 // error if we get here ??
987 m_aTypes
.RemoveAt(nIndex
);
988 m_aEntries
.RemoveAt(nIndex
);
989 m_aExtensions
.RemoveAt(nIndex
);
990 m_aDescriptions
.RemoveAt(nIndex
);
991 m_aIcons
.RemoveAt(nIndex
);
994 // check data integrity
995 wxASSERT( m_aTypes
.GetCount() == m_aEntries
.GetCount() &&
996 m_aTypes
.GetCount() == m_aExtensions
.GetCount() &&
997 m_aTypes
.GetCount() == m_aIcons
.GetCount() &&
998 m_aTypes
.GetCount() == m_aDescriptions
.GetCount() );
1004 // wxUSE_MIMETYPE && wxUSE_FILE