1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
6 // Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_MIMETYPE_H_
14 #define _WX_MIMETYPE_H_
17 #pragma interface "mimetypebase.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
26 // the things we really need
27 #include "wx/string.h"
28 #include "wx/dynarray.h"
31 class WXDLLEXPORT wxIcon
;
32 class WXDLLEXPORT wxFileTypeImpl
;
33 class WXDLLEXPORT wxMimeTypesManagerImpl
;
36 TODO: would it be more convenient to have this class?
38 class WXDLLEXPORT wxMimeType : public wxString
41 // all string ctors here
43 wxString GetType() const { return BeforeFirst(_T('/')); }
44 wxString GetSubType() const { return AfterFirst(_T('/')); }
46 void SetSubType(const wxString& subtype)
48 *this = GetType() + _T('/') + subtype;
51 bool Matches(const wxMimeType& wildcard)
53 // implement using wxMimeTypesManager::IsOfType()
59 // ----------------------------------------------------------------------------
60 // wxFileTypeInfo: static container of information accessed via wxFileType.
62 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
63 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxFileTypeInfo
70 wxFileTypeInfo(const char *mimeType
,
74 // the other parameters form a NULL terminated list of
78 // invalid item - use this to terminate the array passed to
79 // wxMimeTypesManager::AddFallbacks
82 // test if this object can be used
83 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
87 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
89 m_iconFile
= iconFile
;
90 m_iconIndex
= iconIndex
;
93 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
97 const wxString
& GetMimeType() const { return m_mimeType
; }
98 // get the open command
99 const wxString
& GetOpenCommand() const { return m_openCmd
; }
100 // get the print command
101 const wxString
& GetPrintCommand() const { return m_printCmd
; }
102 // get the short description (only used under Win32 so far)
103 const wxString
& GetShortDesc() const { return m_shortDesc
; }
104 // get the long, user visible description
105 const wxString
& GetDescription() const { return m_desc
; }
106 // get the array of all extensions
107 const wxArrayString
& GetExtensions() const { return m_exts
; }
109 const wxString
& GetIconFile() const { return m_iconFile
; }
110 int GetIconIndex() const { return m_iconIndex
; }
113 wxString m_mimeType
, // the MIME type in "type/subtype" form
114 m_openCmd
, // command to use for opening the file (%s allowed)
115 m_printCmd
, // command to use for printing the file (%s allowed)
116 m_shortDesc
, // a short string used in the registry
117 m_desc
; // a free form description of this file type
120 wxString m_iconFile
; // the file containing the icon
121 int m_iconIndex
; // icon index in this file
123 wxArrayString m_exts
; // the extensions which are mapped on this filetype
126 // the additional (except "open" and "print") command names and values
127 wxArrayString m_commandNames
,
132 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
134 // ----------------------------------------------------------------------------
135 // wxFileType: gives access to all information about the files of given type.
137 // This class holds information about a given "file type". File type is the
138 // same as MIME type under Unix, but under Windows it corresponds more to an
139 // extension than to MIME type (in fact, several extensions may correspond to a
140 // file type). This object may be created in many different ways and depending
141 // on how it was created some fields may be unknown so the return value of all
142 // the accessors *must* be checked!
143 // ----------------------------------------------------------------------------
145 class WXDLLEXPORT wxFileType
147 friend class WXDLLEXPORT wxMimeTypesManagerImpl
; // it has access to m_impl
150 // An object of this class must be passed to Get{Open|Print}Command. The
151 // default implementation is trivial and doesn't know anything at all about
152 // parameters, only filename and MIME type are used (so it's probably ok for
153 // Windows where %{param} is not used anyhow)
154 class MessageParameters
158 MessageParameters() { }
159 MessageParameters(const wxString
& filename
, const wxString
& mimetype
)
160 : m_filename(filename
), m_mimetype(mimetype
) { }
162 // accessors (called by GetOpenCommand)
164 const wxString
& GetFileName() const { return m_filename
; }
166 const wxString
& GetMimeType() const { return m_mimetype
; }
168 // override this function in derived class
169 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
170 { return wxEmptyString
; }
172 // virtual dtor as in any base class
173 virtual ~MessageParameters() { }
176 wxString m_filename
, m_mimetype
;
179 // ctor from static data
180 wxFileType(const wxFileTypeInfo
& ftInfo
);
182 // accessors: all of them return true if the corresponding information
183 // could be retrieved/found, false otherwise (and in this case all [out]
184 // parameters are unchanged)
185 // return the MIME type for this file type
186 bool GetMimeType(wxString
*mimeType
) const;
187 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
188 // fill passed in array with all extensions associated with this file
190 bool GetExtensions(wxArrayString
& extensions
);
191 // get the icon corresponding to this file type, the name of the file
192 // where the icon resides is return in iconfile if !NULL and its index
193 // in this file (Win-only) is in iconIndex
194 bool GetIcon(wxIcon
*icon
,
195 wxString
*iconFile
= NULL
,
196 int *iconIndex
= NULL
) const;
197 // get a brief file type description ("*.txt" => "text document")
198 bool GetDescription(wxString
*desc
) const;
200 // get the command to be used to open/print the given file.
201 // get the command to execute the file of given type
202 bool GetOpenCommand(wxString
*openCmd
,
203 const MessageParameters
& params
) const;
204 // get the command to print the file of given type
205 bool GetPrintCommand(wxString
*printCmd
,
206 const MessageParameters
& params
) const;
209 // return the number of commands defined for this file type, 0 if none
210 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
211 const wxFileType::MessageParameters
& params
) const;
213 // remove the association for this filetype from the system MIME database:
214 // notice that it will only work if the association is defined in the user
215 // file/registry part, we will never modify the system-wide settings
219 // expand a string in the format of GetOpenCommand (which may contain
220 // '%s' and '%t' format specificators for the file name and mime type
221 // and %{param} constructions).
222 static wxString
ExpandCommand(const wxString
& command
,
223 const MessageParameters
& params
);
225 // dtor (not virtual, shouldn't be derived from)
229 // default ctor is private because the user code never creates us
232 // no copy ctor/assignment operator
233 wxFileType(const wxFileType
&);
234 wxFileType
& operator=(const wxFileType
&);
236 // the static container of wxFileType data: if it's not NULL, it means that
237 // this object is used as fallback only
238 const wxFileTypeInfo
*m_info
;
240 // the object which implements the real stuff like reading and writing
241 // to/from system MIME database
242 wxFileTypeImpl
*m_impl
;
245 // ----------------------------------------------------------------------------
246 // wxMimeTypesManager: interface to system MIME database.
248 // This class accesses the information about all known MIME types and allows
249 // the application to retrieve information (including how to handle data of
250 // given type) about them.
251 // ----------------------------------------------------------------------------
253 class WXDLLEXPORT wxMimeTypesManager
256 // static helper functions
257 // -----------------------
259 // check if the given MIME type is the same as the other one: the
260 // second argument may contain wildcards ('*'), but not the first. If
261 // the types are equal or if the mimeType matches wildcard the function
262 // returns TRUE, otherwise it returns FALSE
263 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
266 wxMimeTypesManager();
268 // Database lookup: all functions return a pointer to wxFileType object
269 // whose methods may be used to query it for the information you're
270 // interested in. If the return value is !NULL, caller is responsible for
272 // get file type from file extension
273 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
274 // get file type from MIME type (in format <category>/<format>)
275 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
277 // other operations: return TRUE if there were no errors or FALSE if there
278 // were some unreckognized entries (the good entries are always read anyhow)
279 // read in additional file (the standard ones are read automatically)
280 // in mailcap format (see mimetype.cpp for description)
282 // 'fallback' parameter may be set to TRUE to avoid overriding the
283 // settings from other, previously parsed, files by this one: normally,
284 // the files read most recently would override the older files, but with
285 // fallback == TRUE this won't happen
286 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
287 // read in additional file in mime.types format
288 bool ReadMimeTypes(const wxString
& filename
);
290 // enumerate all known MIME types
292 // returns the number of retrieved file types
293 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
295 // these functions can be used to provide default values for some of the
296 // MIME types inside the program itself (you may also use
297 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
298 // achieve the same goal, but this requires having this info in a file).
300 // The filetypes array should be terminated by either NULL entry or an
301 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
302 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
303 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
305 // create or remove associations
307 // create a new association using the fields of wxFileTypeInfo (at least
308 // the MIME type and the extension should be set)
309 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
312 bool Unassociate(wxFileType
*ft
) { return ft
->Unassociate(); }
314 // dtor (not virtual, shouldn't be derived from)
315 ~wxMimeTypesManager();
318 // no copy ctor/assignment operator
319 wxMimeTypesManager(const wxMimeTypesManager
&);
320 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
322 // the fallback info which is used if the information is not found in the
323 // real system database
324 wxArrayFileTypeInfo m_fallbacks
;
326 // the object working with the system MIME database
327 wxMimeTypesManagerImpl
*m_impl
;
329 // if m_impl is NULL, create one
332 friend class wxMimeTypeCmnModule
;
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 // the default mime manager for wxWindows programs
341 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
346 /* vi: set cin tw=80 ts=4 sw=4: */