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
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_MIMETYPE_H_
13 #define _WX_MIMETYPE_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
23 // the things we really need
24 #include "wx/string.h"
25 #include "wx/dynarray.h"
26 #include "wx/arrstr.h"
31 class WXDLLIMPEXP_FWD_BASE wxIconLocation
;
32 class WXDLLIMPEXP_FWD_BASE wxFileTypeImpl
;
33 class WXDLLIMPEXP_FWD_BASE wxMimeTypesManagerImpl
;
35 // these constants define the MIME informations source under UNIX and are used
36 // by wxMimeTypesManager::Initialize()
39 wxMAILCAP_STANDARD
= 1,
40 wxMAILCAP_NETSCAPE
= 2,
48 TODO: would it be more convenient to have this class?
50 class WXDLLIMPEXP_BASE wxMimeType : public wxString
53 // all string ctors here
55 wxString GetType() const { return BeforeFirst(wxT('/')); }
56 wxString GetSubType() const { return AfterFirst(wxT('/')); }
58 void SetSubType(const wxString& subtype)
60 *this = GetType() + wxT('/') + subtype;
63 bool Matches(const wxMimeType& wildcard)
65 // implement using wxMimeTypesManager::IsOfType()
71 // wxMimeTypeCommands stores the verbs defined for the given MIME type with
73 class WXDLLIMPEXP_BASE wxMimeTypeCommands
76 wxMimeTypeCommands() {}
78 wxMimeTypeCommands(const wxArrayString
& verbs
,
79 const wxArrayString
& commands
)
85 // add a new verb with the command or replace the old value
86 void AddOrReplaceVerb(const wxString
& verb
, const wxString
& cmd
);
87 void Add(const wxString
& s
)
89 m_verbs
.Add(s
.BeforeFirst(wxT('=')));
90 m_commands
.Add(s
.AfterFirst(wxT('=')));
93 // access the commands
94 size_t GetCount() const { return m_verbs
.GetCount(); }
95 const wxString
& GetVerb(size_t n
) const { return m_verbs
[n
]; }
96 const wxString
& GetCmd(size_t n
) const { return m_commands
[n
]; }
98 bool HasVerb(const wxString
& verb
) const
99 { return m_verbs
.Index(verb
) != wxNOT_FOUND
; }
101 // returns empty string and wxNOT_FOUND in idx if no such verb
102 wxString
GetCommandForVerb(const wxString
& verb
, size_t *idx
= NULL
) const;
104 // get a "verb=command" string
105 wxString
GetVerbCmd(size_t n
) const;
108 wxArrayString m_verbs
;
109 wxArrayString m_commands
;
112 // ----------------------------------------------------------------------------
113 // wxFileTypeInfo: static container of information accessed via wxFileType.
115 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
116 // ----------------------------------------------------------------------------
118 class WXDLLIMPEXP_BASE wxFileTypeInfo
121 void DoVarArgInit(const wxString
& mimeType
,
122 const wxString
& openCmd
,
123 const wxString
& printCmd
,
124 const wxString
& desc
,
127 void VarArgInit(const wxString
*mimeType
,
128 const wxString
*openCmd
,
129 const wxString
*printCmd
,
130 const wxString
*desc
,
131 // the other parameters form a NULL terminated list of
136 // NB: This is a helper to get implicit conversion of variadic ctor's
137 // fixed arguments into something that can be passed to VarArgInit().
138 // Do not use, it's used by the ctor only.
141 CtorString(const char *str
) : m_str(str
) {}
142 CtorString(const wchar_t *str
) : m_str(str
) {}
143 CtorString(const wxString
& str
) : m_str(str
) {}
144 CtorString(const wxCStrData
& str
) : m_str(str
) {}
145 CtorString(const wxScopedCharBuffer
& str
) : m_str(str
) {}
146 CtorString(const wxScopedWCharBuffer
& str
) : m_str(str
) {}
148 operator const wxString
*() const { return &m_str
; }
155 // Ctor specifying just the MIME type (which is mandatory), the other
156 // fields can be set later if needed.
157 wxFileTypeInfo(const wxString
& mimeType
)
158 : m_mimeType(mimeType
)
162 // Ctor allowing to specify the values of all fields at once:
164 // wxFileTypeInfo(const wxString& mimeType,
165 // const wxString& openCmd,
166 // const wxString& printCmd,
167 // const wxString& desc,
168 // // the other parameters form a list of extensions for this
169 // // file type and should be terminated with wxNullPtr (not
172 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo
,
173 4, (const CtorString
&,
177 VarArgInit
, VarArgInit
)
179 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
180 WX_VARARG_WATCOM_WORKAROUND_CTOR(
190 WX_VARARG_WATCOM_WORKAROUND_CTOR(
192 4, (const wxCStrData
&,
200 WX_VARARG_WATCOM_WORKAROUND_CTOR(
210 WX_VARARG_WATCOM_WORKAROUND_CTOR(
222 // the array elements correspond to the parameters of the ctor above in
224 wxFileTypeInfo(const wxArrayString
& sArray
);
226 // invalid item - use this to terminate the array passed to
227 // wxMimeTypesManager::AddFallbacks
230 // test if this object can be used
231 bool IsValid() const { return !m_mimeType
.empty(); }
234 // set the open/print commands
235 void SetOpenCommand(const wxString
& command
) { m_openCmd
= command
; }
236 void SetPrintCommand(const wxString
& command
) { m_printCmd
= command
; }
238 // set the description
239 void SetDescription(const wxString
& desc
) { m_desc
= desc
; }
241 // add another extension corresponding to this file type
242 void AddExtension(const wxString
& ext
) { m_exts
.push_back(ext
); }
245 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
247 m_iconFile
= iconFile
;
248 m_iconIndex
= iconIndex
;
250 // set the short desc
251 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
255 const wxString
& GetMimeType() const { return m_mimeType
; }
256 // get the open command
257 const wxString
& GetOpenCommand() const { return m_openCmd
; }
258 // get the print command
259 const wxString
& GetPrintCommand() const { return m_printCmd
; }
260 // get the short description (only used under Win32 so far)
261 const wxString
& GetShortDesc() const { return m_shortDesc
; }
262 // get the long, user visible description
263 const wxString
& GetDescription() const { return m_desc
; }
264 // get the array of all extensions
265 const wxArrayString
& GetExtensions() const { return m_exts
; }
266 size_t GetExtensionsCount() const {return m_exts
.GetCount(); }
268 const wxString
& GetIconFile() const { return m_iconFile
; }
269 int GetIconIndex() const { return m_iconIndex
; }
272 wxString m_mimeType
, // the MIME type in "type/subtype" form
273 m_openCmd
, // command to use for opening the file (%s allowed)
274 m_printCmd
, // command to use for printing the file (%s allowed)
275 m_shortDesc
, // a short string used in the registry
276 m_desc
; // a free form description of this file type
279 wxString m_iconFile
; // the file containing the icon
280 int m_iconIndex
; // icon index in this file
282 wxArrayString m_exts
; // the extensions which are mapped on this filetype
286 // the additional (except "open" and "print") command names and values
287 wxArrayString m_commandNames
,
292 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
,
295 // ----------------------------------------------------------------------------
296 // wxFileType: gives access to all information about the files of given type.
298 // This class holds information about a given "file type". File type is the
299 // same as MIME type under Unix, but under Windows it corresponds more to an
300 // extension than to MIME type (in fact, several extensions may correspond to a
301 // file type). This object may be created in many different ways and depending
302 // on how it was created some fields may be unknown so the return value of all
303 // the accessors *must* be checked!
304 // ----------------------------------------------------------------------------
306 class WXDLLIMPEXP_BASE wxFileType
308 friend class WXDLLIMPEXP_FWD_BASE wxMimeTypesManagerImpl
; // it has access to m_impl
311 // An object of this class must be passed to Get{Open|Print}Command. The
312 // default implementation is trivial and doesn't know anything at all about
313 // parameters, only filename and MIME type are used (so it's probably ok for
314 // Windows where %{param} is not used anyhow)
315 class MessageParameters
319 MessageParameters() { }
320 MessageParameters(const wxString
& filename
,
321 const wxString
& mimetype
= wxEmptyString
)
322 : m_filename(filename
), m_mimetype(mimetype
) { }
324 // accessors (called by GetOpenCommand)
326 const wxString
& GetFileName() const { return m_filename
; }
328 const wxString
& GetMimeType() const { return m_mimetype
; }
330 // override this function in derived class
331 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
332 { return wxEmptyString
; }
334 // virtual dtor as in any base class
335 virtual ~MessageParameters() { }
338 wxString m_filename
, m_mimetype
;
341 // ctor from static data
342 wxFileType(const wxFileTypeInfo
& ftInfo
);
344 // accessors: all of them return true if the corresponding information
345 // could be retrieved/found, false otherwise (and in this case all [out]
346 // parameters are unchanged)
347 // return the MIME type for this file type
348 bool GetMimeType(wxString
*mimeType
) const;
349 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
350 // fill passed in array with all extensions associated with this file
352 bool GetExtensions(wxArrayString
& extensions
);
353 // get the icon corresponding to this file type and of the given size
354 bool GetIcon(wxIconLocation
*iconloc
) const;
355 bool GetIcon(wxIconLocation
*iconloc
,
356 const MessageParameters
& params
) const;
357 // get a brief file type description ("*.txt" => "text document")
358 bool GetDescription(wxString
*desc
) const;
360 // get the command to be used to open/print the given file.
361 // get the command to execute the file of given type
362 bool GetOpenCommand(wxString
*openCmd
,
363 const MessageParameters
& params
) const;
364 // a simpler to use version of GetOpenCommand() -- it only takes the
365 // filename and returns an empty string on failure
366 wxString
GetOpenCommand(const wxString
& filename
) const;
367 // get the command to print the file of given type
368 bool GetPrintCommand(wxString
*printCmd
,
369 const MessageParameters
& params
) const;
372 // return the number of commands defined for this file type, 0 if none
373 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
374 const wxFileType::MessageParameters
& params
) const;
376 // set an arbitrary command, ask confirmation if it already exists and
377 // overwriteprompt is true
378 bool SetCommand(const wxString
& cmd
, const wxString
& verb
,
379 bool overwriteprompt
= true);
381 bool SetDefaultIcon(const wxString
& cmd
= wxEmptyString
, int index
= 0);
384 // remove the association for this filetype from the system MIME database:
385 // notice that it will only work if the association is defined in the user
386 // file/registry part, we will never modify the system-wide settings
390 // expand a string in the format of GetOpenCommand (which may contain
391 // '%s' and '%t' format specifiers for the file name and mime type
392 // and %{param} constructions).
393 static wxString
ExpandCommand(const wxString
& command
,
394 const MessageParameters
& params
);
396 // dtor (not virtual, shouldn't be derived from)
400 // default ctor is private because the user code never creates us
403 // no copy ctor/assignment operator
404 wxFileType(const wxFileType
&);
405 wxFileType
& operator=(const wxFileType
&);
407 // the static container of wxFileType data: if it's not NULL, it means that
408 // this object is used as fallback only
409 const wxFileTypeInfo
*m_info
;
411 // the object which implements the real stuff like reading and writing
412 // to/from system MIME database
413 wxFileTypeImpl
*m_impl
;
416 //----------------------------------------------------------------------------
417 // wxMimeTypesManagerFactory
418 //----------------------------------------------------------------------------
420 class WXDLLIMPEXP_BASE wxMimeTypesManagerFactory
423 wxMimeTypesManagerFactory() {}
424 virtual ~wxMimeTypesManagerFactory() {}
426 virtual wxMimeTypesManagerImpl
*CreateMimeTypesManagerImpl();
428 static void Set( wxMimeTypesManagerFactory
*factory
);
429 static wxMimeTypesManagerFactory
*Get();
432 static wxMimeTypesManagerFactory
*m_factory
;
435 // ----------------------------------------------------------------------------
436 // wxMimeTypesManager: interface to system MIME database.
438 // This class accesses the information about all known MIME types and allows
439 // the application to retrieve information (including how to handle data of
440 // given type) about them.
441 // ----------------------------------------------------------------------------
443 class WXDLLIMPEXP_BASE wxMimeTypesManager
446 // static helper functions
447 // -----------------------
449 // check if the given MIME type is the same as the other one: the
450 // second argument may contain wildcards ('*'), but not the first. If
451 // the types are equal or if the mimeType matches wildcard the function
452 // returns true, otherwise it returns false
453 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
456 wxMimeTypesManager();
458 // NB: the following 2 functions are for Unix only and don't do anything
461 // loads data from standard files according to the mailcap styles
462 // specified: this is a bitwise OR of wxMailcapStyle values
464 // use the extraDir parameter if you want to look for files in another
466 void Initialize(int mailcapStyle
= wxMAILCAP_ALL
,
467 const wxString
& extraDir
= wxEmptyString
);
469 // and this function clears all the data from the manager
472 // Database lookup: all functions return a pointer to wxFileType object
473 // whose methods may be used to query it for the information you're
474 // interested in. If the return value is !NULL, caller is responsible for
476 // get file type from file extension
477 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
478 // get file type from MIME type (in format <category>/<format>)
479 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
481 // enumerate all known MIME types
483 // returns the number of retrieved file types
484 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
486 // these functions can be used to provide default values for some of the
487 // MIME types inside the program itself
489 // The filetypes array should be terminated by either NULL entry or an
490 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
491 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
492 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
494 // create or remove associations
496 // create a new association using the fields of wxFileTypeInfo (at least
497 // the MIME type and the extension should be set)
498 // if the other fields are empty, the existing values should be left alone
499 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
502 bool Unassociate(wxFileType
*ft
) ;
504 // dtor (not virtual, shouldn't be derived from)
505 ~wxMimeTypesManager();
508 // no copy ctor/assignment operator
509 wxMimeTypesManager(const wxMimeTypesManager
&);
510 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
512 // the fallback info which is used if the information is not found in the
513 // real system database
514 wxArrayFileTypeInfo m_fallbacks
;
516 // the object working with the system MIME database
517 wxMimeTypesManagerImpl
*m_impl
;
519 // if m_impl is NULL, create one
522 friend class wxMimeTypeCmnModule
;
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 // the default mime manager for wxWidgets programs
531 extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager
*) wxTheMimeTypesManager
;
533 #endif // wxUSE_MIMETYPE