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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "mimetypebase.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
28 // the things we really need
29 #include "wx/string.h"
30 #include "wx/dynarray.h"
33 class WXDLLIMPEXP_BASE wxIconLocation
;
34 class WXDLLIMPEXP_BASE wxFileTypeImpl
;
35 class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl
;
37 // these constants define the MIME informations source under UNIX and are used
38 // by wxMimeTypesManager::Initialize()
41 wxMAILCAP_STANDARD
= 1,
42 wxMAILCAP_NETSCAPE
= 2,
50 TODO: would it be more convenient to have this class?
52 class WXDLLIMPEXP_BASE wxMimeType : public wxString
55 // all string ctors here
57 wxString GetType() const { return BeforeFirst(_T('/')); }
58 wxString GetSubType() const { return AfterFirst(_T('/')); }
60 void SetSubType(const wxString& subtype)
62 *this = GetType() + _T('/') + subtype;
65 bool Matches(const wxMimeType& wildcard)
67 // implement using wxMimeTypesManager::IsOfType()
73 // ----------------------------------------------------------------------------
74 // wxFileTypeInfo: static container of information accessed via wxFileType.
76 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
77 // ----------------------------------------------------------------------------
79 class WXDLLIMPEXP_BASE wxFileTypeInfo
84 wxFileTypeInfo(const wxChar
*mimeType
,
85 const wxChar
*openCmd
,
86 const wxChar
*printCmd
,
88 // the other parameters form a NULL terminated list of
92 // the array elements correspond to the parameters of the ctor above in
94 wxFileTypeInfo(const wxArrayString
& sArray
);
96 // invalid item - use this to terminate the array passed to
97 // wxMimeTypesManager::AddFallbacks
100 // test if this object can be used
101 bool IsValid() const { return !m_mimeType
.empty(); }
105 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
107 m_iconFile
= iconFile
;
108 m_iconIndex
= iconIndex
;
110 // set the short desc
111 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
115 const wxString
& GetMimeType() const { return m_mimeType
; }
116 // get the open command
117 const wxString
& GetOpenCommand() const { return m_openCmd
; }
118 // get the print command
119 const wxString
& GetPrintCommand() const { return m_printCmd
; }
120 // get the short description (only used under Win32 so far)
121 const wxString
& GetShortDesc() const { return m_shortDesc
; }
122 // get the long, user visible description
123 const wxString
& GetDescription() const { return m_desc
; }
124 // get the array of all extensions
125 const wxArrayString
& GetExtensions() const { return m_exts
; }
126 int GetExtensionsCount() const {return m_exts
.GetCount(); }
128 const wxString
& GetIconFile() const { return m_iconFile
; }
129 int GetIconIndex() const { return m_iconIndex
; }
132 wxString m_mimeType
, // the MIME type in "type/subtype" form
133 m_openCmd
, // command to use for opening the file (%s allowed)
134 m_printCmd
, // command to use for printing the file (%s allowed)
135 m_shortDesc
, // a short string used in the registry
136 m_desc
; // a free form description of this file type
139 wxString m_iconFile
; // the file containing the icon
140 int m_iconIndex
; // icon index in this file
142 wxArrayString m_exts
; // the extensions which are mapped on this filetype
146 // the additional (except "open" and "print") command names and values
147 wxArrayString m_commandNames
,
152 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
,
155 // ----------------------------------------------------------------------------
156 // wxFileType: gives access to all information about the files of given type.
158 // This class holds information about a given "file type". File type is the
159 // same as MIME type under Unix, but under Windows it corresponds more to an
160 // extension than to MIME type (in fact, several extensions may correspond to a
161 // file type). This object may be created in many different ways and depending
162 // on how it was created some fields may be unknown so the return value of all
163 // the accessors *must* be checked!
164 // ----------------------------------------------------------------------------
166 class WXDLLIMPEXP_BASE wxFileType
168 friend class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl
; // it has access to m_impl
171 // An object of this class must be passed to Get{Open|Print}Command. The
172 // default implementation is trivial and doesn't know anything at all about
173 // parameters, only filename and MIME type are used (so it's probably ok for
174 // Windows where %{param} is not used anyhow)
175 class MessageParameters
179 MessageParameters() { }
180 MessageParameters(const wxString
& filename
,
181 const wxString
& mimetype
= wxEmptyString
)
182 : m_filename(filename
), m_mimetype(mimetype
) { }
184 // accessors (called by GetOpenCommand)
186 const wxString
& GetFileName() const { return m_filename
; }
188 const wxString
& GetMimeType() const { return m_mimetype
; }
190 // override this function in derived class
191 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
192 { return wxEmptyString
; }
194 // virtual dtor as in any base class
195 virtual ~MessageParameters() { }
198 wxString m_filename
, m_mimetype
;
201 // ctor from static data
202 wxFileType(const wxFileTypeInfo
& ftInfo
);
204 // accessors: all of them return true if the corresponding information
205 // could be retrieved/found, false otherwise (and in this case all [out]
206 // parameters are unchanged)
207 // return the MIME type for this file type
208 bool GetMimeType(wxString
*mimeType
) const;
209 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
210 // fill passed in array with all extensions associated with this file
212 bool GetExtensions(wxArrayString
& extensions
);
213 // get the icon corresponding to this file type and of the given size
214 bool GetIcon(wxIconLocation
*iconloc
) const;
215 bool GetIcon(wxIconLocation
*iconloc
,
216 const MessageParameters
& params
) const;
217 // get a brief file type description ("*.txt" => "text document")
218 bool GetDescription(wxString
*desc
) const;
220 // get the command to be used to open/print the given file.
221 // get the command to execute the file of given type
222 bool GetOpenCommand(wxString
*openCmd
,
223 const MessageParameters
& params
) const;
224 // a simpler to use version of GetOpenCommand() -- it only takes the
225 // filename and returns an empty string on failure
226 wxString
GetOpenCommand(const wxString
& filename
) const;
227 // get the command to print the file of given type
228 bool GetPrintCommand(wxString
*printCmd
,
229 const MessageParameters
& params
) const;
232 // return the number of commands defined for this file type, 0 if none
233 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
234 const wxFileType::MessageParameters
& params
) const;
236 // set an arbitrary command, ask confirmation if it already exists and
237 // overwriteprompt is true
238 bool SetCommand(const wxString
& cmd
, const wxString
& verb
,
239 bool overwriteprompt
= true);
241 bool SetDefaultIcon(const wxString
& cmd
= wxEmptyString
, int index
= 0);
244 // remove the association for this filetype from the system MIME database:
245 // notice that it will only work if the association is defined in the user
246 // file/registry part, we will never modify the system-wide settings
250 // expand a string in the format of GetOpenCommand (which may contain
251 // '%s' and '%t' format specificators for the file name and mime type
252 // and %{param} constructions).
253 static wxString
ExpandCommand(const wxString
& command
,
254 const MessageParameters
& params
);
256 // dtor (not virtual, shouldn't be derived from)
260 // default ctor is private because the user code never creates us
263 // no copy ctor/assignment operator
264 wxFileType(const wxFileType
&);
265 wxFileType
& operator=(const wxFileType
&);
267 // the static container of wxFileType data: if it's not NULL, it means that
268 // this object is used as fallback only
269 const wxFileTypeInfo
*m_info
;
271 // the object which implements the real stuff like reading and writing
272 // to/from system MIME database
273 wxFileTypeImpl
*m_impl
;
276 // ----------------------------------------------------------------------------
277 // wxMimeTypesManager: interface to system MIME database.
279 // This class accesses the information about all known MIME types and allows
280 // the application to retrieve information (including how to handle data of
281 // given type) about them.
282 // ----------------------------------------------------------------------------
284 class WXDLLIMPEXP_BASE wxMimeTypesManager
287 // static helper functions
288 // -----------------------
290 // check if the given MIME type is the same as the other one: the
291 // second argument may contain wildcards ('*'), but not the first. If
292 // the types are equal or if the mimeType matches wildcard the function
293 // returns true, otherwise it returns false
294 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
297 wxMimeTypesManager();
299 // NB: the following 2 functions are for Unix only and don't do anything
302 // loads data from standard files according to the mailcap styles
303 // specified: this is a bitwise OR of wxMailcapStyle values
305 // use the extraDir parameter if you want to look for files in another
307 void Initialize(int mailcapStyle
= wxMAILCAP_ALL
,
308 const wxString
& extraDir
= wxEmptyString
);
310 // and this function clears all the data from the manager
313 // Database lookup: all functions return a pointer to wxFileType object
314 // whose methods may be used to query it for the information you're
315 // interested in. If the return value is !NULL, caller is responsible for
317 // get file type from file extension
318 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
319 // get file type from MIME type (in format <category>/<format>)
320 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
322 // other operations: return true if there were no errors or false if there
323 // were some unreckognized entries (the good entries are always read anyhow)
325 // FIXME: These ought to be private ??
327 // read in additional file (the standard ones are read automatically)
328 // in mailcap format (see mimetype.cpp for description)
330 // 'fallback' parameter may be set to true to avoid overriding the
331 // settings from other, previously parsed, files by this one: normally,
332 // the files read most recently would override the older files, but with
333 // fallback == true this won't happen
335 bool ReadMailcap(const wxString
& filename
, bool fallback
= false);
336 // read in additional file in mime.types format
337 bool ReadMimeTypes(const wxString
& filename
);
339 // enumerate all known MIME types
341 // returns the number of retrieved file types
342 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
344 // these functions can be used to provide default values for some of the
345 // MIME types inside the program itself (you may also use
346 // ReadMailcap(filenameWithDefaultTypes, true /* use as fallback */) to
347 // achieve the same goal, but this requires having this info in a file).
349 // The filetypes array should be terminated by either NULL entry or an
350 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
351 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
352 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
354 // create or remove associations
356 // create a new association using the fields of wxFileTypeInfo (at least
357 // the MIME type and the extension should be set)
358 // if the other fields are empty, the existing values should be left alone
359 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
362 bool Unassociate(wxFileType
*ft
) ;
364 // dtor (not virtual, shouldn't be derived from)
365 ~wxMimeTypesManager();
368 // no copy ctor/assignment operator
369 wxMimeTypesManager(const wxMimeTypesManager
&);
370 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
372 // the fallback info which is used if the information is not found in the
373 // real system database
374 wxArrayFileTypeInfo m_fallbacks
;
376 // the object working with the system MIME database
377 wxMimeTypesManagerImpl
*m_impl
;
379 // if m_impl is NULL, create one
382 friend class wxMimeTypeCmnModule
;
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 // the default mime manager for wxWidgets programs
391 extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager
*) wxTheMimeTypesManager
;
393 #endif // wxUSE_MIMETYPE