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 /////////////////////////////////////////////////////////////////////////////
18 class wxMimeTypesManagerImpl
;
24 // the things we really need
25 #include "wx/string.h"
27 // This class holds information about a given "file type". File type is the
28 // same as MIME type under Unix, but under Windows it corresponds more to an
29 // extension than to MIME type (in fact, several extensions may correspond to a
30 // file type). This object may be created in many different ways and depending
31 // on how it was created some fields may be unknown so the return value of all
32 // the accessors *must* be checked!
33 class WXDLLEXPORT wxFileType
36 friend class wxMimeTypesManagerImpl
; // it has access to m_impl
38 friend wxMimeTypesManagerImpl
; // it has access to m_impl
42 // An object of this class must be passed to Get{Open|Print}Command. The
43 // default implementation is trivial and doesn't know anything at all about
44 // parameters, only filename and MIME type are used (so it's probably ok for
45 // Windows where %{param} is not used anyhow)
46 class MessageParameters
50 MessageParameters() { }
51 MessageParameters(const wxString
& filename
, const wxString
& mimetype
)
52 : m_filename(filename
), m_mimetype(mimetype
) { }
54 // accessors (called by GetOpenCommand)
56 const wxString
& GetFileName() const { return m_filename
; }
58 const wxString
& GetMimeType() const { return m_mimetype
; }
60 // override this function in derived class
61 virtual wxString
GetParamValue(const wxString
& WXUNUSED(paramName
)) const
64 // virtual dtor as in any base class
65 virtual ~MessageParameters() { }
68 wxString m_filename
, m_mimetype
;
71 // accessors: all of them return true if the corresponding information
72 // could be retrieved/found, false otherwise (and in this case all [out]
73 // parameters are unchanged)
74 // return the MIME type for this file type
75 bool GetMimeType(wxString
*mimeType
) const;
76 // fill passed in array with all extensions associated with this file
78 bool GetExtensions(wxArrayString
& extensions
);
79 // get the icon corresponding to this file type
80 bool GetIcon(wxIcon
*icon
) const;
81 // get a brief file type description ("*.txt" => "text document")
82 bool GetDescription(wxString
*desc
) const;
84 // get the command to be used to open/print the given file.
85 // get the command to execute the file of given type
86 bool GetOpenCommand(wxString
*openCmd
,
87 const MessageParameters
& params
) const;
88 // get the command to print the file of given type
89 bool GetPrintCommand(wxString
*printCmd
,
90 const MessageParameters
& params
) const;
93 // expand a string in the format of GetOpenCommand (which may contain
94 // '%s' and '%t' format specificators for the file name and mime type
95 // and %{param} constructions).
96 static wxString
ExpandCommand(const wxString
& command
,
97 const MessageParameters
& params
);
99 // dtor (not virtual, shouldn't be derived from)
103 // default ctor is private because the user code never creates us
106 // no copy ctor/assignment operator
107 wxFileType(const wxFileType
&);
108 wxFileType
& operator=(const wxFileType
&);
110 wxFileTypeImpl
*m_impl
;
113 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
114 // just as the container for the wxFileType data.
115 class WXDLLEXPORT wxFileTypeInfo
120 wxFileTypeInfo(const char *mimeType
,
122 const char *printCmd
,
124 // the other parameters form a NULL terminated list of
128 // invalid item - use this to terminate the array passed to
129 // wxMimeTypesManager::AddFallbacks
132 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
136 const wxString
& GetMimeType() const { return m_mimeType
; }
137 // get the open command
138 const wxString
& GetOpenCommand() const { return m_openCmd
; }
139 // get the print command
140 const wxString
& GetPrintCommand() const { return m_printCmd
; }
141 // get the description
142 const wxString
& GetDescription() const { return m_desc
; }
143 // get the array of all extensions
144 const wxArrayString
& GetExtensions() const { return m_exts
; }
147 wxString m_mimeType
, // the MIME type in "type/subtype" form
148 m_openCmd
, // command to use for opening the file (%s allowed)
149 m_printCmd
, // command to use for printing the file (%s allowed)
150 m_desc
; // a free form description of this file type
152 wxArrayString m_exts
; // the extensions which are mapped on this filetype
155 // This class accesses the information about all known MIME types and allows
156 // the application to retrieve information (including how to handle data of
157 // given type) about them.
159 // NB: currently it doesn't support modifying MIME database (read-only access).
160 class WXDLLEXPORT wxMimeTypesManager
163 // static helper functions
164 // -----------------------
166 // check if the given MIME type is the same as the other one: the second
167 // argument may contain wildcards ('*'), but not the first. If the
168 // types are equal or if the mimeType matches wildcard the function
169 // returns TRUE, otherwise it returns FALSE
170 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
173 wxMimeTypesManager();
175 // Database lookup: all functions return a pointer to wxFileType object
176 // whose methods may be used to query it for the information you're
177 // interested in. If the return value is !NULL, caller is responsible for
179 // get file type from file extension
180 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
181 // get file type from MIME type (in format <category>/<format>)
182 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
184 // other operations: return TRUE if there were no errors or FALSE if there
185 // were some unreckognized entries (the good entries are always read anyhow)
186 // read in additional file (the standard ones are read automatically)
187 // in mailcap format (see mimetype.cpp for description)
189 // 'fallback' parameter may be set to TRUE to avoid overriding the
190 // settings from other, previously parsed, files by this one: normally,
191 // the files read most recently would override the older files, but with
192 // fallback == TRUE this won't happen
193 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
194 // read in additional file in mime.types format
195 bool ReadMimeTypes(const wxString
& filename
);
197 // these functions can be used to provide default values for some of the
198 // MIME types inside the program itself (you may also use
199 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
200 // achieve the same goal, but this requires having this info in a file).
202 // It isn't possible (currently) to provide fallback icons using this
205 // The filetypes array should be terminated by a NULL entry
206 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
208 // dtor (not virtual, shouldn't be derived from)
209 ~wxMimeTypesManager();
212 // no copy ctor/assignment operator
213 wxMimeTypesManager(const wxMimeTypesManager
&);
214 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
216 wxMimeTypesManagerImpl
*m_impl
;
225 /* vi: set cin tw=80 ts=4 sw=4: */