1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/mimetype.h
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 license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _MIMETYPE_IMPL_H
13 #define _MIMETYPE_IMPL_H
16 #pragma interface "mimetype.h"
19 #include "wx/mimetype.h"
21 #if (wxUSE_FILE && wxUSE_TEXTFILE)
23 class wxMimeTypeCommands
;
25 WX_DEFINE_ARRAY(wxMimeTypeCommands
*, wxMimeCommandsArray
);
27 // this is the real wxMimeTypesManager for Unix
28 class WXDLLEXPORT wxMimeTypesManagerImpl
32 wxMimeTypesManagerImpl();
33 ~wxMimeTypesManagerImpl();
35 // load all data into memory - done when it is needed for the first time
36 void Initialize(int mailcapStyles
= wxMAILCAP_STANDARD
,
37 const wxString
& extraDir
= wxEmptyString
);
39 // and delete the data here
42 // implement containing class functions
43 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
44 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
46 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
48 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
49 bool ReadMimeTypes(const wxString
& filename
);
51 void AddFallback(const wxFileTypeInfo
& filetype
);
53 // add information about the given mimetype
54 void AddMimeTypeInfo(const wxString
& mimetype
,
55 const wxString
& extensions
,
56 const wxString
& description
);
57 void AddMailcapInfo(const wxString
& strType
,
58 const wxString
& strOpenCmd
,
59 const wxString
& strPrintCmd
,
60 const wxString
& strTest
,
61 const wxString
& strDesc
);
63 // add a new record to the user .mailcap/.mime.types files
64 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
66 bool Unassociate(wxFileType
*ft
);
69 // get the string containing space separated extensions for the given
71 wxString
GetExtension(size_t index
) { return m_aExtensions
[index
]; }
76 if ( !m_initialized
) {
77 // set the flag first to prevent recursion
83 wxArrayString m_aTypes
, // MIME types
84 m_aDescriptions
, // descriptions (just some text)
85 m_aExtensions
, // space separated list of extensions
86 m_aIcons
; // Icon filenames
88 // verb=command pairs for this file type
89 wxMimeCommandsArray m_aEntries
;
91 // are we initialized?
94 // keep track of the files we had already loaded (this is a bitwise OR of
95 // wxMailcapStyle values)
96 int m_mailcapStylesInited
;
98 wxString
GetCommand(const wxString
&verb
, size_t nIndex
) const;
101 void LoadGnomeDataFromKeyFile(const wxString
& filename
);
102 void LoadGnomeMimeTypesFromMimeFile(const wxString
& filename
);
103 void LoadGnomeMimeFilesFromDir(const wxString
& dirbase
);
104 void GetGnomeMimeInfo(const wxString
& sExtraDir
);
107 bool CheckGnomeDirsExist();
108 bool WriteGnomeKeyFile(int index
, bool delete_index
);
109 bool WriteGnomeMimeFile(int index
, bool delete_index
);
112 void LoadKDELinksForMimeSubtype(const wxString
& dirbase
,
113 const wxString
& subdir
,
114 const wxString
& filename
,
115 const wxArrayString
& icondirs
);
116 void LoadKDELinksForMimeType(const wxString
& dirbase
,
117 const wxString
& subdir
,
118 const wxArrayString
& icondirs
);
119 void LoadKDELinkFilesFromDir(const wxString
& dirbase
,
120 const wxArrayString
& icondirs
);
121 void GetKDEMimeInfo(const wxString
& sExtraDir
);
124 bool WriteKDEMimeFile(int index
, bool delete_index
);
125 bool CheckKDEDirsExist(const wxString
& sOK
, const wxString
& sTest
);
127 //read write Netscape and MetaMail formats
128 void GetMimeInfo (const wxString
& sExtraDir
);
129 bool WriteToMailCap (int index
, bool delete_index
);
130 bool WriteToMimeTypes (int index
, bool delete_index
);
131 bool WriteToNSMimeTypes (int index
, bool delete_index
);
133 // ReadMailcap() helper
134 bool ProcessOtherMailcapField(struct MailcapLineData
& data
,
135 const wxString
& curField
);
137 // functions used to do associations
139 int AddToMimeData(const wxString
& strType
,
140 const wxString
& strIcon
,
141 wxMimeTypeCommands
*entry
,
142 const wxArrayString
& strExtensions
,
143 const wxString
& strDesc
,
144 bool replaceExisting
= TRUE
);
146 bool DoAssociation(const wxString
& strType
,
147 const wxString
& strIcon
,
148 wxMimeTypeCommands
*entry
,
149 const wxArrayString
& strExtensions
,
150 const wxString
& strDesc
);
152 bool WriteMimeInfo(int nIndex
, bool delete_mime
);
154 // give it access to m_aXXX variables
155 friend class WXDLLEXPORT wxFileTypeImpl
;
160 class WXDLLEXPORT wxFileTypeImpl
163 // initialization functions
164 // this is used to construct a list of mimetypes which match;
165 // if built with GetFileTypeFromMimetype index 0 has the exact match and
166 // index 1 the type / * match
167 // if built with GetFileTypeFromExtension, index 0 has the mimetype for
168 // the first extension found, index 1 for the second and so on
170 void Init(wxMimeTypesManagerImpl
*manager
, size_t index
)
171 { m_manager
= manager
; m_index
.Add(index
); }
174 bool GetExtensions(wxArrayString
& extensions
);
175 bool GetMimeType(wxString
*mimeType
) const
176 { *mimeType
= m_manager
->m_aTypes
[m_index
[0]]; return TRUE
; }
177 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
178 bool GetIcon(wxIcon
*icon
, wxString
*iconFile
= NULL
,
179 int *iconIndex
= NULL
) const;
181 bool GetDescription(wxString
*desc
) const
182 { *desc
= m_manager
->m_aDescriptions
[m_index
[0]]; return TRUE
; }
184 bool GetOpenCommand(wxString
*openCmd
,
185 const wxFileType::MessageParameters
& params
) const
187 *openCmd
= GetExpandedCommand(wxT("open"), params
);
188 return (! openCmd
-> IsEmpty() );
191 bool GetPrintCommand(wxString
*printCmd
,
192 const wxFileType::MessageParameters
& params
) const
194 *printCmd
= GetExpandedCommand(wxT("print"), params
);
195 return (! printCmd
-> IsEmpty() );
198 // return the number of commands defined for this file type, 0 if none
199 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
200 const wxFileType::MessageParameters
& params
) const;
203 // remove the record for this file type
204 // probably a mistake to come here, use wxMimeTypesManager.Unassociate (ft) instead
205 bool Unassociate(wxFileType
*ft
)
207 return m_manager
->Unassociate(ft
);
210 // set an arbitrary command, ask confirmation if it already exists and
211 // overwriteprompt is TRUE
212 bool SetCommand(const wxString
& cmd
, const wxString
& verb
, bool overwriteprompt
= TRUE
);
213 bool SetDefaultIcon(const wxString
& strIcon
= wxEmptyString
, int index
= 0);
217 GetExpandedCommand(const wxString
& verb
,
218 const wxFileType::MessageParameters
& params
) const;
220 wxMimeTypesManagerImpl
*m_manager
;
221 wxArrayInt m_index
; // in the wxMimeTypesManagerImpl arrays