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 class wxMimeTypeCmnModule
;
35 // This class holds information about a given "file type". File type is the
36 // same as MIME type under Unix, but under Windows it corresponds more to an
37 // extension than to MIME type (in fact, several extensions may correspond to a
38 // file type). This object may be created in many different ways and depending
39 // on how it was created some fields may be unknown so the return value of all
40 // the accessors *must* be checked!
41 class WXDLLEXPORT wxFileType
43 friend class WXDLLEXPORT wxMimeTypesManagerImpl
; // it has access to m_impl
46 // An object of this class must be passed to Get{Open|Print}Command. The
47 // default implementation is trivial and doesn't know anything at all about
48 // parameters, only filename and MIME type are used (so it's probably ok for
49 // Windows where %{param} is not used anyhow)
50 class MessageParameters
54 MessageParameters() { }
55 MessageParameters(const wxString
& filename
, const wxString
& mimetype
)
56 : m_filename(filename
), m_mimetype(mimetype
) { }
58 // accessors (called by GetOpenCommand)
60 const wxString
& GetFileName() const { return m_filename
; }
62 const wxString
& GetMimeType() const { return m_mimetype
; }
64 // override this function in derived class
65 virtual wxString
GetParamValue(const wxString
& WXUNUSED(paramName
)) const
68 // virtual dtor as in any base class
69 virtual ~MessageParameters() { }
72 wxString m_filename
, m_mimetype
;
75 // accessors: all of them return true if the corresponding information
76 // could be retrieved/found, false otherwise (and in this case all [out]
77 // parameters are unchanged)
78 // return the MIME type for this file type
79 bool GetMimeType(wxString
*mimeType
) const;
80 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
81 // fill passed in array with all extensions associated with this file
83 bool GetExtensions(wxArrayString
& extensions
);
84 // get the icon corresponding to this file type
85 bool GetIcon(wxIcon
*icon
) const;
86 // get a brief file type description ("*.txt" => "text document")
87 bool GetDescription(wxString
*desc
) const;
89 // get the command to be used to open/print the given file.
90 // get the command to execute the file of given type
91 bool GetOpenCommand(wxString
*openCmd
,
92 const MessageParameters
& params
) const;
93 // get the command to print the file of given type
94 bool GetPrintCommand(wxString
*printCmd
,
95 const MessageParameters
& params
) const;
98 // expand a string in the format of GetOpenCommand (which may contain
99 // '%s' and '%t' format specificators for the file name and mime type
100 // and %{param} constructions).
101 static wxString
ExpandCommand(const wxString
& command
,
102 const MessageParameters
& params
);
104 // dtor (not virtual, shouldn't be derived from)
108 // default ctor is private because the user code never creates us
111 // no copy ctor/assignment operator
112 wxFileType(const wxFileType
&);
113 wxFileType
& operator=(const wxFileType
&);
115 wxFileTypeImpl
*m_impl
;
118 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
119 // just as the container for the wxFileType data.
120 class WXDLLEXPORT wxFileTypeInfo
125 wxFileTypeInfo(const char *mimeType
,
127 const char *printCmd
,
129 // the other parameters form a NULL terminated list of
133 // invalid item - use this to terminate the array passed to
134 // wxMimeTypesManager::AddFallbacks
137 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
141 const wxString
& GetMimeType() const { return m_mimeType
; }
142 // get the open command
143 const wxString
& GetOpenCommand() const { return m_openCmd
; }
144 // get the print command
145 const wxString
& GetPrintCommand() const { return m_printCmd
; }
146 // get the description
147 const wxString
& GetDescription() const { return m_desc
; }
148 // get the array of all extensions
149 const wxArrayString
& GetExtensions() const { return m_exts
; }
152 wxString m_mimeType
, // the MIME type in "type/subtype" form
153 m_openCmd
, // command to use for opening the file (%s allowed)
154 m_printCmd
, // command to use for printing the file (%s allowed)
155 m_desc
; // a free form description of this file type
157 wxArrayString m_exts
; // the extensions which are mapped on this filetype
160 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
163 // This class accesses the information about all known MIME types and allows
164 // the application to retrieve information (including how to handle data of
165 // given type) about them.
167 // NB: currently it doesn't support modifying MIME database (read-only access).
168 class WXDLLEXPORT wxMimeTypesManager
171 // static helper functions
172 // -----------------------
174 // check if the given MIME type is the same as the other one: the second
175 // argument may contain wildcards ('*'), but not the first. If the
176 // types are equal or if the mimeType matches wildcard the function
177 // returns TRUE, otherwise it returns FALSE
178 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
181 wxMimeTypesManager();
183 // Database lookup: all functions return a pointer to wxFileType object
184 // whose methods may be used to query it for the information you're
185 // interested in. If the return value is !NULL, caller is responsible for
187 // get file type from file extension
188 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
189 // get file type from MIME type (in format <category>/<format>)
190 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
192 // other operations: return TRUE if there were no errors or FALSE if there
193 // were some unreckognized entries (the good entries are always read anyhow)
194 // read in additional file (the standard ones are read automatically)
195 // in mailcap format (see mimetype.cpp for description)
197 // 'fallback' parameter may be set to TRUE to avoid overriding the
198 // settings from other, previously parsed, files by this one: normally,
199 // the files read most recently would override the older files, but with
200 // fallback == TRUE this won't happen
201 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
202 // read in additional file in mime.types format
203 bool ReadMimeTypes(const wxString
& filename
);
205 // enumerate all known MIME types
207 // returns the number of retrieved file types
208 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
210 // these functions can be used to provide default values for some of the
211 // MIME types inside the program itself (you may also use
212 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
213 // achieve the same goal, but this requires having this info in a file).
215 // It isn't possible (currently) to provide fallback icons using this
218 // The filetypes array should be terminated by a NULL entry
219 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
221 // dtor (not virtual, shouldn't be derived from)
222 ~wxMimeTypesManager();
225 // no copy ctor/assignment operator
226 wxMimeTypesManager(const wxMimeTypesManager
&);
227 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
229 wxMimeTypesManagerImpl
*m_impl
;
231 // if m_impl is NULL, create one
234 friend class wxMimeTypeCmnModule
;
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 // the default mime manager for wxWindows programs
243 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
251 /* vi: set cin tw=80 ts=4 sw=4: */