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(__APPLE__)
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 WXDLLEXPORT wxIcon
;
34 class WXDLLEXPORT wxFileTypeImpl
;
35 class WXDLLEXPORT 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,
49 #define wxICON_LARGE 0
50 #define wxICON_SMALL 1
53 TODO: would it be more convenient to have this class?
55 class WXDLLEXPORT wxMimeType : public wxString
58 // all string ctors here
60 wxString GetType() const { return BeforeFirst(_T('/')); }
61 wxString GetSubType() const { return AfterFirst(_T('/')); }
63 void SetSubType(const wxString& subtype)
65 *this = GetType() + _T('/') + subtype;
68 bool Matches(const wxMimeType& wildcard)
70 // implement using wxMimeTypesManager::IsOfType()
76 // ----------------------------------------------------------------------------
77 // wxFileTypeInfo: static container of information accessed via wxFileType.
79 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
80 // ----------------------------------------------------------------------------
82 class WXDLLEXPORT wxFileTypeInfo
87 wxFileTypeInfo(const wxChar
*mimeType
,
88 const wxChar
*openCmd
,
89 const wxChar
*printCmd
,
91 // the other parameters form a NULL terminated list of
95 // the array elements correspond to the parameters of the ctor above in
97 wxFileTypeInfo(const wxArrayString
& sArray
);
99 // invalid item - use this to terminate the array passed to
100 // wxMimeTypesManager::AddFallbacks
103 // test if this object can be used
104 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
108 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
110 m_iconFile
= iconFile
;
111 m_iconIndex
= iconIndex
;
113 // set the short desc
114 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
118 const wxString
& GetMimeType() const { return m_mimeType
; }
119 // get the open command
120 const wxString
& GetOpenCommand() const { return m_openCmd
; }
121 // get the print command
122 const wxString
& GetPrintCommand() const { return m_printCmd
; }
123 // get the short description (only used under Win32 so far)
124 const wxString
& GetShortDesc() const { return m_shortDesc
; }
125 // get the long, user visible description
126 const wxString
& GetDescription() const { return m_desc
; }
127 // get the array of all extensions
128 const wxArrayString
& GetExtensions() const { return m_exts
; }
129 int GetExtensionsCount() const {return m_exts
.GetCount(); }
131 const wxString
& GetIconFile() const { return m_iconFile
; }
132 int GetIconIndex() const { return m_iconIndex
; }
135 wxString m_mimeType
, // the MIME type in "type/subtype" form
136 m_openCmd
, // command to use for opening the file (%s allowed)
137 m_printCmd
, // command to use for printing the file (%s allowed)
138 m_shortDesc
, // a short string used in the registry
139 m_desc
; // a free form description of this file type
142 wxString m_iconFile
; // the file containing the icon
143 int m_iconIndex
; // icon index in this file
145 wxArrayString m_exts
; // the extensions which are mapped on this filetype
149 // the additional (except "open" and "print") command names and values
150 wxArrayString m_commandNames
,
155 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
157 // ----------------------------------------------------------------------------
158 // wxFileType: gives access to all information about the files of given type.
160 // This class holds information about a given "file type". File type is the
161 // same as MIME type under Unix, but under Windows it corresponds more to an
162 // extension than to MIME type (in fact, several extensions may correspond to a
163 // file type). This object may be created in many different ways and depending
164 // on how it was created some fields may be unknown so the return value of all
165 // the accessors *must* be checked!
166 // ----------------------------------------------------------------------------
168 class WXDLLEXPORT wxFileType
170 friend class WXDLLEXPORT wxMimeTypesManagerImpl
; // it has access to m_impl
173 // An object of this class must be passed to Get{Open|Print}Command. The
174 // default implementation is trivial and doesn't know anything at all about
175 // parameters, only filename and MIME type are used (so it's probably ok for
176 // Windows where %{param} is not used anyhow)
177 class MessageParameters
181 MessageParameters() { }
182 MessageParameters(const wxString
& filename
,
183 const wxString
& mimetype
= _T(""))
184 : m_filename(filename
), m_mimetype(mimetype
) { }
186 // accessors (called by GetOpenCommand)
188 const wxString
& GetFileName() const { return m_filename
; }
190 const wxString
& GetMimeType() const { return m_mimetype
; }
192 // override this function in derived class
193 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
194 { return wxEmptyString
; }
196 // virtual dtor as in any base class
197 virtual ~MessageParameters() { }
200 wxString m_filename
, m_mimetype
;
203 // ctor from static data
204 wxFileType(const wxFileTypeInfo
& ftInfo
);
206 // accessors: all of them return true if the corresponding information
207 // could be retrieved/found, false otherwise (and in this case all [out]
208 // parameters are unchanged)
209 // return the MIME type for this file type
210 bool GetMimeType(wxString
*mimeType
) const;
211 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
212 // fill passed in array with all extensions associated with this file
214 bool GetExtensions(wxArrayString
& extensions
);
215 // get the icon corresponding to this file type, the name of the file
216 // where the icon resides is return in iconfile if !NULL and its index
217 // in this file (Win-only) is in iconIndex
218 bool GetIcon(wxIcon
*icon
,
219 wxString
*iconFile
= NULL
,
220 int *iconIndex
= NULL
,
221 int iconSize
= wxICON_LARGE
) const;
222 // get a brief file type description ("*.txt" => "text document")
223 bool GetDescription(wxString
*desc
) const;
225 // get the command to be used to open/print the given file.
226 // get the command to execute the file of given type
227 bool GetOpenCommand(wxString
*openCmd
,
228 const MessageParameters
& params
) const;
229 // a simpler to use version of GetOpenCommand() -- it only takes the
230 // filename and returns an empty string on failure
231 wxString
GetOpenCommand(const wxString
& filename
) const;
232 // get the command to print the file of given type
233 bool GetPrintCommand(wxString
*printCmd
,
234 const MessageParameters
& params
) const;
237 // return the number of commands defined for this file type, 0 if none
238 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
239 const wxFileType::MessageParameters
& params
) const;
241 // set an arbitrary command, ask confirmation if it already exists and
242 // overwriteprompt is TRUE
243 bool SetCommand(const wxString
& cmd
, const wxString
& verb
,
244 bool overwriteprompt
= TRUE
);
246 bool SetDefaultIcon(const wxString
& cmd
= wxEmptyString
, int index
= 0);
249 // remove the association for this filetype from the system MIME database:
250 // notice that it will only work if the association is defined in the user
251 // file/registry part, we will never modify the system-wide settings
255 // expand a string in the format of GetOpenCommand (which may contain
256 // '%s' and '%t' format specificators for the file name and mime type
257 // and %{param} constructions).
258 static wxString
ExpandCommand(const wxString
& command
,
259 const MessageParameters
& params
);
261 // dtor (not virtual, shouldn't be derived from)
265 // default ctor is private because the user code never creates us
268 // no copy ctor/assignment operator
269 wxFileType(const wxFileType
&);
270 wxFileType
& operator=(const wxFileType
&);
272 // the static container of wxFileType data: if it's not NULL, it means that
273 // this object is used as fallback only
274 const wxFileTypeInfo
*m_info
;
276 // the object which implements the real stuff like reading and writing
277 // to/from system MIME database
278 wxFileTypeImpl
*m_impl
;
281 // ----------------------------------------------------------------------------
282 // wxMimeTypesManager: interface to system MIME database.
284 // This class accesses the information about all known MIME types and allows
285 // the application to retrieve information (including how to handle data of
286 // given type) about them.
287 // ----------------------------------------------------------------------------
289 class WXDLLEXPORT wxMimeTypesManager
292 // static helper functions
293 // -----------------------
295 // check if the given MIME type is the same as the other one: the
296 // second argument may contain wildcards ('*'), but not the first. If
297 // the types are equal or if the mimeType matches wildcard the function
298 // returns TRUE, otherwise it returns FALSE
299 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
302 wxMimeTypesManager();
304 // NB: the following 2 functions are for Unix only and don't do anything
307 // loads data from standard files according to the mailcap styles
308 // specified: this is a bitwise OR of wxMailcapStyle values
310 // use the extraDir parameter if you want to look for files in another
312 void Initialize(int mailcapStyle
= wxMAILCAP_ALL
,
313 const wxString
& extraDir
= wxEmptyString
);
315 // and this function clears all the data from the manager
318 // Database lookup: all functions return a pointer to wxFileType object
319 // whose methods may be used to query it for the information you're
320 // interested in. If the return value is !NULL, caller is responsible for
322 // get file type from file extension
323 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
324 // get file type from MIME type (in format <category>/<format>)
325 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
327 // other operations: return TRUE if there were no errors or FALSE if there
328 // were some unreckognized entries (the good entries are always read anyhow)
330 // FIXME: These ought to be private ??
332 // read in additional file (the standard ones are read automatically)
333 // in mailcap format (see mimetype.cpp for description)
335 // 'fallback' parameter may be set to TRUE to avoid overriding the
336 // settings from other, previously parsed, files by this one: normally,
337 // the files read most recently would override the older files, but with
338 // fallback == TRUE this won't happen
340 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
341 // read in additional file in mime.types format
342 bool ReadMimeTypes(const wxString
& filename
);
344 // enumerate all known MIME types
346 // returns the number of retrieved file types
347 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
349 // these functions can be used to provide default values for some of the
350 // MIME types inside the program itself (you may also use
351 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
352 // achieve the same goal, but this requires having this info in a file).
354 // The filetypes array should be terminated by either NULL entry or an
355 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
356 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
357 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
359 // create or remove associations
361 // create a new association using the fields of wxFileTypeInfo (at least
362 // the MIME type and the extension should be set)
363 // if the other fields are empty, the existing values should be left alone
364 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
367 bool Unassociate(wxFileType
*ft
) ;
369 // dtor (not virtual, shouldn't be derived from)
370 ~wxMimeTypesManager();
373 // no copy ctor/assignment operator
374 wxMimeTypesManager(const wxMimeTypesManager
&);
375 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
377 // the fallback info which is used if the information is not found in the
378 // real system database
379 wxArrayFileTypeInfo m_fallbacks
;
381 // the object working with the system MIME database
382 wxMimeTypesManagerImpl
*m_impl
;
384 // if m_impl is NULL, create one
387 friend class wxMimeTypeCmnModule
;
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 // the default mime manager for wxWindows programs
396 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
398 #endif // wxUSE_MIMETYPE