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 licence (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_MIMETYPE_H_
14 #define _WX_MIMETYPE_H_
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
24 // the things we really need
25 #include "wx/string.h"
26 #include "wx/dynarray.h"
29 class WXDLLIMPEXP_BASE wxIconLocation
;
30 class WXDLLIMPEXP_BASE wxFileTypeImpl
;
31 class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl
;
33 // these constants define the MIME informations source under UNIX and are used
34 // by wxMimeTypesManager::Initialize()
37 wxMAILCAP_STANDARD
= 1,
38 wxMAILCAP_NETSCAPE
= 2,
46 TODO: would it be more convenient to have this class?
48 class WXDLLIMPEXP_BASE wxMimeType : public wxString
51 // all string ctors here
53 wxString GetType() const { return BeforeFirst(_T('/')); }
54 wxString GetSubType() const { return AfterFirst(_T('/')); }
56 void SetSubType(const wxString& subtype)
58 *this = GetType() + _T('/') + subtype;
61 bool Matches(const wxMimeType& wildcard)
63 // implement using wxMimeTypesManager::IsOfType()
69 // ----------------------------------------------------------------------------
70 // wxFileTypeInfo: static container of information accessed via wxFileType.
72 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
73 // ----------------------------------------------------------------------------
75 class WXDLLIMPEXP_BASE wxFileTypeInfo
80 wxFileTypeInfo(const wxChar
*mimeType
,
81 const wxChar
*openCmd
,
82 const wxChar
*printCmd
,
84 // the other parameters form a NULL terminated list of
88 // the array elements correspond to the parameters of the ctor above in
90 wxFileTypeInfo(const wxArrayString
& sArray
);
92 // invalid item - use this to terminate the array passed to
93 // wxMimeTypesManager::AddFallbacks
96 // test if this object can be used
97 bool IsValid() const { return !m_mimeType
.empty(); }
101 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
103 m_iconFile
= iconFile
;
104 m_iconIndex
= iconIndex
;
106 // set the short desc
107 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
111 const wxString
& GetMimeType() const { return m_mimeType
; }
112 // get the open command
113 const wxString
& GetOpenCommand() const { return m_openCmd
; }
114 // get the print command
115 const wxString
& GetPrintCommand() const { return m_printCmd
; }
116 // get the short description (only used under Win32 so far)
117 const wxString
& GetShortDesc() const { return m_shortDesc
; }
118 // get the long, user visible description
119 const wxString
& GetDescription() const { return m_desc
; }
120 // get the array of all extensions
121 const wxArrayString
& GetExtensions() const { return m_exts
; }
122 size_t GetExtensionsCount() const {return m_exts
.GetCount(); }
124 const wxString
& GetIconFile() const { return m_iconFile
; }
125 int GetIconIndex() const { return m_iconIndex
; }
128 wxString m_mimeType
, // the MIME type in "type/subtype" form
129 m_openCmd
, // command to use for opening the file (%s allowed)
130 m_printCmd
, // command to use for printing the file (%s allowed)
131 m_shortDesc
, // a short string used in the registry
132 m_desc
; // a free form description of this file type
135 wxString m_iconFile
; // the file containing the icon
136 int m_iconIndex
; // icon index in this file
138 wxArrayString m_exts
; // the extensions which are mapped on this filetype
142 // the additional (except "open" and "print") command names and values
143 wxArrayString m_commandNames
,
148 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
,
151 // ----------------------------------------------------------------------------
152 // wxFileType: gives access to all information about the files of given type.
154 // This class holds information about a given "file type". File type is the
155 // same as MIME type under Unix, but under Windows it corresponds more to an
156 // extension than to MIME type (in fact, several extensions may correspond to a
157 // file type). This object may be created in many different ways and depending
158 // on how it was created some fields may be unknown so the return value of all
159 // the accessors *must* be checked!
160 // ----------------------------------------------------------------------------
162 class WXDLLIMPEXP_BASE wxFileType
164 friend class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl
; // it has access to m_impl
167 // An object of this class must be passed to Get{Open|Print}Command. The
168 // default implementation is trivial and doesn't know anything at all about
169 // parameters, only filename and MIME type are used (so it's probably ok for
170 // Windows where %{param} is not used anyhow)
171 class MessageParameters
175 MessageParameters() { }
176 MessageParameters(const wxString
& filename
,
177 const wxString
& mimetype
= wxEmptyString
)
178 : m_filename(filename
), m_mimetype(mimetype
) { }
180 // accessors (called by GetOpenCommand)
182 const wxString
& GetFileName() const { return m_filename
; }
184 const wxString
& GetMimeType() const { return m_mimetype
; }
186 // override this function in derived class
187 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
188 { return wxEmptyString
; }
190 // virtual dtor as in any base class
191 virtual ~MessageParameters() { }
194 wxString m_filename
, m_mimetype
;
197 // ctor from static data
198 wxFileType(const wxFileTypeInfo
& ftInfo
);
200 // accessors: all of them return true if the corresponding information
201 // could be retrieved/found, false otherwise (and in this case all [out]
202 // parameters are unchanged)
203 // return the MIME type for this file type
204 bool GetMimeType(wxString
*mimeType
) const;
205 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
206 // fill passed in array with all extensions associated with this file
208 bool GetExtensions(wxArrayString
& extensions
);
209 // get the icon corresponding to this file type and of the given size
210 bool GetIcon(wxIconLocation
*iconloc
) const;
211 bool GetIcon(wxIconLocation
*iconloc
,
212 const MessageParameters
& params
) const;
213 // get a brief file type description ("*.txt" => "text document")
214 bool GetDescription(wxString
*desc
) const;
216 // get the command to be used to open/print the given file.
217 // get the command to execute the file of given type
218 bool GetOpenCommand(wxString
*openCmd
,
219 const MessageParameters
& params
) const;
220 // a simpler to use version of GetOpenCommand() -- it only takes the
221 // filename and returns an empty string on failure
222 wxString
GetOpenCommand(const wxString
& filename
) const;
223 // get the command to print the file of given type
224 bool GetPrintCommand(wxString
*printCmd
,
225 const MessageParameters
& params
) const;
228 // return the number of commands defined for this file type, 0 if none
229 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
230 const wxFileType::MessageParameters
& params
) const;
232 // set an arbitrary command, ask confirmation if it already exists and
233 // overwriteprompt is true
234 bool SetCommand(const wxString
& cmd
, const wxString
& verb
,
235 bool overwriteprompt
= true);
237 bool SetDefaultIcon(const wxString
& cmd
= wxEmptyString
, int index
= 0);
240 // remove the association for this filetype from the system MIME database:
241 // notice that it will only work if the association is defined in the user
242 // file/registry part, we will never modify the system-wide settings
246 // expand a string in the format of GetOpenCommand (which may contain
247 // '%s' and '%t' format specificators for the file name and mime type
248 // and %{param} constructions).
249 static wxString
ExpandCommand(const wxString
& command
,
250 const MessageParameters
& params
);
252 // dtor (not virtual, shouldn't be derived from)
256 // default ctor is private because the user code never creates us
259 // no copy ctor/assignment operator
260 wxFileType(const wxFileType
&);
261 wxFileType
& operator=(const wxFileType
&);
263 // the static container of wxFileType data: if it's not NULL, it means that
264 // this object is used as fallback only
265 const wxFileTypeInfo
*m_info
;
267 // the object which implements the real stuff like reading and writing
268 // to/from system MIME database
269 wxFileTypeImpl
*m_impl
;
272 // ----------------------------------------------------------------------------
273 // wxMimeTypesManager: interface to system MIME database.
275 // This class accesses the information about all known MIME types and allows
276 // the application to retrieve information (including how to handle data of
277 // given type) about them.
278 // ----------------------------------------------------------------------------
280 class WXDLLIMPEXP_BASE wxMimeTypesManager
283 // static helper functions
284 // -----------------------
286 // check if the given MIME type is the same as the other one: the
287 // second argument may contain wildcards ('*'), but not the first. If
288 // the types are equal or if the mimeType matches wildcard the function
289 // returns true, otherwise it returns false
290 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
293 wxMimeTypesManager();
295 // NB: the following 2 functions are for Unix only and don't do anything
298 // loads data from standard files according to the mailcap styles
299 // specified: this is a bitwise OR of wxMailcapStyle values
301 // use the extraDir parameter if you want to look for files in another
303 void Initialize(int mailcapStyle
= wxMAILCAP_ALL
,
304 const wxString
& extraDir
= wxEmptyString
);
306 // and this function clears all the data from the manager
309 // Database lookup: all functions return a pointer to wxFileType object
310 // whose methods may be used to query it for the information you're
311 // interested in. If the return value is !NULL, caller is responsible for
313 // get file type from file extension
314 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
315 // get file type from MIME type (in format <category>/<format>)
316 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
318 // other operations: return true if there were no errors or false if there
319 // were some unrecognized entries (the good entries are always read anyhow)
321 // FIXME: These ought to be private ??
323 // read in additional file (the standard ones are read automatically)
324 // in mailcap format (see mimetype.cpp for description)
326 // 'fallback' parameter may be set to true to avoid overriding the
327 // settings from other, previously parsed, files by this one: normally,
328 // the files read most recently would override the older files, but with
329 // fallback == true this won't happen
331 bool ReadMailcap(const wxString
& filename
, bool fallback
= false);
332 // read in additional file in mime.types format
333 bool ReadMimeTypes(const wxString
& filename
);
335 // enumerate all known MIME types
337 // returns the number of retrieved file types
338 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
340 // these functions can be used to provide default values for some of the
341 // MIME types inside the program itself (you may also use
342 // ReadMailcap(filenameWithDefaultTypes, true /* use as fallback */) to
343 // achieve the same goal, but this requires having this info in a file).
345 // The filetypes array should be terminated by either NULL entry or an
346 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
347 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
348 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
350 // create or remove associations
352 // create a new association using the fields of wxFileTypeInfo (at least
353 // the MIME type and the extension should be set)
354 // if the other fields are empty, the existing values should be left alone
355 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
358 bool Unassociate(wxFileType
*ft
) ;
360 // dtor (not virtual, shouldn't be derived from)
361 ~wxMimeTypesManager();
364 // no copy ctor/assignment operator
365 wxMimeTypesManager(const wxMimeTypesManager
&);
366 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
368 // the fallback info which is used if the information is not found in the
369 // real system database
370 wxArrayFileTypeInfo m_fallbacks
;
372 // the object working with the system MIME database
373 wxMimeTypesManagerImpl
*m_impl
;
375 // if m_impl is NULL, create one
378 friend class wxMimeTypeCmnModule
;
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 // the default mime manager for wxWidgets programs
387 extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager
*) wxTheMimeTypesManager
;
389 #endif // wxUSE_MIMETYPE