1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "mimetypebase.h"
23 class wxMimeTypesManagerImpl
;
29 // the things we really need
30 #include "wx/string.h"
31 #include "wx/dynarray.h"
33 // This class holds information about a given "file type". File type is the
34 // same as MIME type under Unix, but under Windows it corresponds more to an
35 // extension than to MIME type (in fact, several extensions may correspond to a
36 // file type). This object may be created in many different ways and depending
37 // on how it was created some fields may be unknown so the return value of all
38 // the accessors *must* be checked!
39 class WXDLLEXPORT wxFileType
41 friend class WXDLLEXPORT wxMimeTypesManagerImpl
; // it has access to m_impl
44 // An object of this class must be passed to Get{Open|Print}Command. The
45 // default implementation is trivial and doesn't know anything at all about
46 // parameters, only filename and MIME type are used (so it's probably ok for
47 // Windows where %{param} is not used anyhow)
48 class MessageParameters
52 MessageParameters() { }
53 MessageParameters(const wxString
& filename
, const wxString
& mimetype
)
54 : m_filename(filename
), m_mimetype(mimetype
) { }
56 // accessors (called by GetOpenCommand)
58 const wxString
& GetFileName() const { return m_filename
; }
60 const wxString
& GetMimeType() const { return m_mimetype
; }
62 // override this function in derived class
63 virtual wxString
GetParamValue(const wxString
& WXUNUSED(paramName
)) const
66 // virtual dtor as in any base class
67 virtual ~MessageParameters() { }
70 wxString m_filename
, m_mimetype
;
73 // accessors: all of them return true if the corresponding information
74 // could be retrieved/found, false otherwise (and in this case all [out]
75 // parameters are unchanged)
76 // return the MIME type for this file type
77 bool GetMimeType(wxString
*mimeType
) const;
78 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
79 // fill passed in array with all extensions associated with this file
81 bool GetExtensions(wxArrayString
& extensions
);
82 // get the icon corresponding to this file type
83 bool GetIcon(wxIcon
*icon
) const;
84 // get a brief file type description ("*.txt" => "text document")
85 bool GetDescription(wxString
*desc
) const;
87 // get the command to be used to open/print the given file.
88 // get the command to execute the file of given type
89 bool GetOpenCommand(wxString
*openCmd
,
90 const MessageParameters
& params
) const;
91 // get the command to print the file of given type
92 bool GetPrintCommand(wxString
*printCmd
,
93 const MessageParameters
& params
) const;
96 // expand a string in the format of GetOpenCommand (which may contain
97 // '%s' and '%t' format specificators for the file name and mime type
98 // and %{param} constructions).
99 static wxString
ExpandCommand(const wxString
& command
,
100 const MessageParameters
& params
);
102 // dtor (not virtual, shouldn't be derived from)
106 // default ctor is private because the user code never creates us
109 // no copy ctor/assignment operator
110 wxFileType(const wxFileType
&);
111 wxFileType
& operator=(const wxFileType
&);
113 wxFileTypeImpl
*m_impl
;
116 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
117 // just as the container for the wxFileType data.
118 class WXDLLEXPORT wxFileTypeInfo
123 wxFileTypeInfo(const char *mimeType
,
125 const char *printCmd
,
127 // the other parameters form a NULL terminated list of
131 // invalid item - use this to terminate the array passed to
132 // wxMimeTypesManager::AddFallbacks
135 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
139 const wxString
& GetMimeType() const { return m_mimeType
; }
140 // get the open command
141 const wxString
& GetOpenCommand() const { return m_openCmd
; }
142 // get the print command
143 const wxString
& GetPrintCommand() const { return m_printCmd
; }
144 // get the description
145 const wxString
& GetDescription() const { return m_desc
; }
146 // get the array of all extensions
147 const wxArrayString
& GetExtensions() const { return m_exts
; }
150 wxString m_mimeType
, // the MIME type in "type/subtype" form
151 m_openCmd
, // command to use for opening the file (%s allowed)
152 m_printCmd
, // command to use for printing the file (%s allowed)
153 m_desc
; // a free form description of this file type
155 wxArrayString m_exts
; // the extensions which are mapped on this filetype
158 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
161 // This class accesses the information about all known MIME types and allows
162 // the application to retrieve information (including how to handle data of
163 // given type) about them.
165 // NB: currently it doesn't support modifying MIME database (read-only access).
166 class WXDLLEXPORT wxMimeTypesManager
169 // static helper functions
170 // -----------------------
172 // check if the given MIME type is the same as the other one: the second
173 // argument may contain wildcards ('*'), but not the first. If the
174 // types are equal or if the mimeType matches wildcard the function
175 // returns TRUE, otherwise it returns FALSE
176 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
179 wxMimeTypesManager();
181 // Database lookup: all functions return a pointer to wxFileType object
182 // whose methods may be used to query it for the information you're
183 // interested in. If the return value is !NULL, caller is responsible for
185 // get file type from file extension
186 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
187 // get file type from MIME type (in format <category>/<format>)
188 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
190 // other operations: return TRUE if there were no errors or FALSE if there
191 // were some unreckognized entries (the good entries are always read anyhow)
192 // read in additional file (the standard ones are read automatically)
193 // in mailcap format (see mimetype.cpp for description)
195 // 'fallback' parameter may be set to TRUE to avoid overriding the
196 // settings from other, previously parsed, files by this one: normally,
197 // the files read most recently would override the older files, but with
198 // fallback == TRUE this won't happen
199 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
200 // read in additional file in mime.types format
201 bool ReadMimeTypes(const wxString
& filename
);
203 // enumerate all known MIME types
205 // returns the number of retrieved file types
206 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
208 // these functions can be used to provide default values for some of the
209 // MIME types inside the program itself (you may also use
210 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
211 // achieve the same goal, but this requires having this info in a file).
213 // It isn't possible (currently) to provide fallback icons using this
216 // The filetypes array should be terminated by a NULL entry
217 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
219 // dtor (not virtual, shouldn't be derived from)
220 ~wxMimeTypesManager();
223 // no copy ctor/assignment operator
224 wxMimeTypesManager(const wxMimeTypesManager
&);
225 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
227 wxMimeTypesManagerImpl
*m_impl
;
229 // if m_impl is NULL, create one
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 // the default mime manager for wxWindows programs
239 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
247 /* vi: set cin tw=80 ts=4 sw=4: */