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 // fill passed in array with all extensions associated with this file
80 bool GetExtensions(wxArrayString
& extensions
);
81 // get the icon corresponding to this file type
82 bool GetIcon(wxIcon
*icon
) const;
83 // get a brief file type description ("*.txt" => "text document")
84 bool GetDescription(wxString
*desc
) const;
86 // get the command to be used to open/print the given file.
87 // get the command to execute the file of given type
88 bool GetOpenCommand(wxString
*openCmd
,
89 const MessageParameters
& params
) const;
90 // get the command to print the file of given type
91 bool GetPrintCommand(wxString
*printCmd
,
92 const MessageParameters
& params
) const;
95 // expand a string in the format of GetOpenCommand (which may contain
96 // '%s' and '%t' format specificators for the file name and mime type
97 // and %{param} constructions).
98 static wxString
ExpandCommand(const wxString
& command
,
99 const MessageParameters
& params
);
101 // dtor (not virtual, shouldn't be derived from)
105 // default ctor is private because the user code never creates us
108 // no copy ctor/assignment operator
109 wxFileType(const wxFileType
&);
110 wxFileType
& operator=(const wxFileType
&);
112 wxFileTypeImpl
*m_impl
;
115 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
116 // just as the container for the wxFileType data.
117 class WXDLLEXPORT wxFileTypeInfo
122 wxFileTypeInfo(const char *mimeType
,
124 const char *printCmd
,
126 // the other parameters form a NULL terminated list of
130 // invalid item - use this to terminate the array passed to
131 // wxMimeTypesManager::AddFallbacks
134 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
138 const wxString
& GetMimeType() const { return m_mimeType
; }
139 // get the open command
140 const wxString
& GetOpenCommand() const { return m_openCmd
; }
141 // get the print command
142 const wxString
& GetPrintCommand() const { return m_printCmd
; }
143 // get the description
144 const wxString
& GetDescription() const { return m_desc
; }
145 // get the array of all extensions
146 const wxArrayString
& GetExtensions() const { return m_exts
; }
149 wxString m_mimeType
, // the MIME type in "type/subtype" form
150 m_openCmd
, // command to use for opening the file (%s allowed)
151 m_printCmd
, // command to use for printing the file (%s allowed)
152 m_desc
; // a free form description of this file type
154 wxArrayString m_exts
; // the extensions which are mapped on this filetype
157 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
160 // This class accesses the information about all known MIME types and allows
161 // the application to retrieve information (including how to handle data of
162 // given type) about them.
164 // NB: currently it doesn't support modifying MIME database (read-only access).
165 class WXDLLEXPORT wxMimeTypesManager
168 // static helper functions
169 // -----------------------
171 // check if the given MIME type is the same as the other one: the second
172 // argument may contain wildcards ('*'), but not the first. If the
173 // types are equal or if the mimeType matches wildcard the function
174 // returns TRUE, otherwise it returns FALSE
175 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
178 wxMimeTypesManager();
180 // Database lookup: all functions return a pointer to wxFileType object
181 // whose methods may be used to query it for the information you're
182 // interested in. If the return value is !NULL, caller is responsible for
184 // get file type from file extension
185 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
186 // get file type from MIME type (in format <category>/<format>)
187 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
189 // other operations: return TRUE if there were no errors or FALSE if there
190 // were some unreckognized entries (the good entries are always read anyhow)
191 // read in additional file (the standard ones are read automatically)
192 // in mailcap format (see mimetype.cpp for description)
194 // 'fallback' parameter may be set to TRUE to avoid overriding the
195 // settings from other, previously parsed, files by this one: normally,
196 // the files read most recently would override the older files, but with
197 // fallback == TRUE this won't happen
198 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
199 // read in additional file in mime.types format
200 bool ReadMimeTypes(const wxString
& filename
);
202 // enumerate all known MIME types
204 // returns the number of retrieved file types
205 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
207 // these functions can be used to provide default values for some of the
208 // MIME types inside the program itself (you may also use
209 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
210 // achieve the same goal, but this requires having this info in a file).
212 // It isn't possible (currently) to provide fallback icons using this
215 // The filetypes array should be terminated by a NULL entry
216 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
218 // dtor (not virtual, shouldn't be derived from)
219 ~wxMimeTypesManager();
222 // no copy ctor/assignment operator
223 wxMimeTypesManager(const wxMimeTypesManager
&);
224 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
226 wxMimeTypesManagerImpl
*m_impl
;
228 // if m_impl is NULL, create one
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 // the default mime manager for wxWindows programs
238 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
246 /* vi: set cin tw=80 ts=4 sw=4: */