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 #if defined(__WXMSW__)
34 class WXDLLEXPORT wxIconLocation
;
36 class WXDLLEXPORT wxIconLocationBase
;
37 typedef wxIconLocationBase wxIconLocation
;
38 #endif //defined(__WXMSW__)
39 class WXDLLEXPORT wxFileTypeImpl
;
40 class WXDLLEXPORT wxMimeTypesManagerImpl
;
42 // these constants define the MIME informations source under UNIX and are used
43 // by wxMimeTypesManager::Initialize()
46 wxMAILCAP_STANDARD
= 1,
47 wxMAILCAP_NETSCAPE
= 2,
55 TODO: would it be more convenient to have this class?
57 class WXDLLEXPORT wxMimeType : public wxString
60 // all string ctors here
62 wxString GetType() const { return BeforeFirst(_T('/')); }
63 wxString GetSubType() const { return AfterFirst(_T('/')); }
65 void SetSubType(const wxString& subtype)
67 *this = GetType() + _T('/') + subtype;
70 bool Matches(const wxMimeType& wildcard)
72 // implement using wxMimeTypesManager::IsOfType()
78 // ----------------------------------------------------------------------------
79 // wxFileTypeInfo: static container of information accessed via wxFileType.
81 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
82 // ----------------------------------------------------------------------------
84 class WXDLLEXPORT wxFileTypeInfo
89 wxFileTypeInfo(const wxChar
*mimeType
,
90 const wxChar
*openCmd
,
91 const wxChar
*printCmd
,
93 // the other parameters form a NULL terminated list of
97 // the array elements correspond to the parameters of the ctor above in
99 wxFileTypeInfo(const wxArrayString
& sArray
);
101 // invalid item - use this to terminate the array passed to
102 // wxMimeTypesManager::AddFallbacks
105 // test if this object can be used
106 bool IsValid() const { return !m_mimeType
.IsEmpty(); }
110 void SetIcon(const wxString
& iconFile
, int iconIndex
= 0)
112 m_iconFile
= iconFile
;
113 m_iconIndex
= iconIndex
;
115 // set the short desc
116 void SetShortDesc(const wxString
& shortDesc
) { m_shortDesc
= shortDesc
; }
120 const wxString
& GetMimeType() const { return m_mimeType
; }
121 // get the open command
122 const wxString
& GetOpenCommand() const { return m_openCmd
; }
123 // get the print command
124 const wxString
& GetPrintCommand() const { return m_printCmd
; }
125 // get the short description (only used under Win32 so far)
126 const wxString
& GetShortDesc() const { return m_shortDesc
; }
127 // get the long, user visible description
128 const wxString
& GetDescription() const { return m_desc
; }
129 // get the array of all extensions
130 const wxArrayString
& GetExtensions() const { return m_exts
; }
131 int GetExtensionsCount() const {return m_exts
.GetCount(); }
133 const wxString
& GetIconFile() const { return m_iconFile
; }
134 int GetIconIndex() const { return m_iconIndex
; }
137 wxString m_mimeType
, // the MIME type in "type/subtype" form
138 m_openCmd
, // command to use for opening the file (%s allowed)
139 m_printCmd
, // command to use for printing the file (%s allowed)
140 m_shortDesc
, // a short string used in the registry
141 m_desc
; // a free form description of this file type
144 wxString m_iconFile
; // the file containing the icon
145 int m_iconIndex
; // icon index in this file
147 wxArrayString m_exts
; // the extensions which are mapped on this filetype
151 // the additional (except "open" and "print") command names and values
152 wxArrayString m_commandNames
,
157 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo
, wxArrayFileTypeInfo
);
159 // ----------------------------------------------------------------------------
160 // wxFileType: gives access to all information about the files of given type.
162 // This class holds information about a given "file type". File type is the
163 // same as MIME type under Unix, but under Windows it corresponds more to an
164 // extension than to MIME type (in fact, several extensions may correspond to a
165 // file type). This object may be created in many different ways and depending
166 // on how it was created some fields may be unknown so the return value of all
167 // the accessors *must* be checked!
168 // ----------------------------------------------------------------------------
170 class WXDLLEXPORT wxFileType
172 friend class WXDLLEXPORT wxMimeTypesManagerImpl
; // it has access to m_impl
175 // An object of this class must be passed to Get{Open|Print}Command. The
176 // default implementation is trivial and doesn't know anything at all about
177 // parameters, only filename and MIME type are used (so it's probably ok for
178 // Windows where %{param} is not used anyhow)
179 class MessageParameters
183 MessageParameters() { }
184 MessageParameters(const wxString
& filename
,
185 const wxString
& mimetype
= _T(""))
186 : m_filename(filename
), m_mimetype(mimetype
) { }
188 // accessors (called by GetOpenCommand)
190 const wxString
& GetFileName() const { return m_filename
; }
192 const wxString
& GetMimeType() const { return m_mimetype
; }
194 // override this function in derived class
195 virtual wxString
GetParamValue(const wxString
& WXUNUSED(name
)) const
196 { return wxEmptyString
; }
198 // virtual dtor as in any base class
199 virtual ~MessageParameters() { }
202 wxString m_filename
, m_mimetype
;
205 // ctor from static data
206 wxFileType(const wxFileTypeInfo
& ftInfo
);
208 // accessors: all of them return true if the corresponding information
209 // could be retrieved/found, false otherwise (and in this case all [out]
210 // parameters are unchanged)
211 // return the MIME type for this file type
212 bool GetMimeType(wxString
*mimeType
) const;
213 bool GetMimeTypes(wxArrayString
& mimeTypes
) const;
214 // fill passed in array with all extensions associated with this file
216 bool GetExtensions(wxArrayString
& extensions
);
217 // get the icon corresponding to this file type and of the given size
218 bool GetIcon(wxIconLocation
*iconloc
) const;
219 // get a brief file type description ("*.txt" => "text document")
220 bool GetDescription(wxString
*desc
) const;
222 // get the command to be used to open/print the given file.
223 // get the command to execute the file of given type
224 bool GetOpenCommand(wxString
*openCmd
,
225 const MessageParameters
& params
) const;
226 // a simpler to use version of GetOpenCommand() -- it only takes the
227 // filename and returns an empty string on failure
228 wxString
GetOpenCommand(const wxString
& filename
) const;
229 // get the command to print the file of given type
230 bool GetPrintCommand(wxString
*printCmd
,
231 const MessageParameters
& params
) const;
234 // return the number of commands defined for this file type, 0 if none
235 size_t GetAllCommands(wxArrayString
*verbs
, wxArrayString
*commands
,
236 const wxFileType::MessageParameters
& params
) const;
238 // set an arbitrary command, ask confirmation if it already exists and
239 // overwriteprompt is TRUE
240 bool SetCommand(const wxString
& cmd
, const wxString
& verb
,
241 bool overwriteprompt
= TRUE
);
243 bool SetDefaultIcon(const wxString
& cmd
= wxEmptyString
, int index
= 0);
246 // remove the association for this filetype from the system MIME database:
247 // notice that it will only work if the association is defined in the user
248 // file/registry part, we will never modify the system-wide settings
252 // expand a string in the format of GetOpenCommand (which may contain
253 // '%s' and '%t' format specificators for the file name and mime type
254 // and %{param} constructions).
255 static wxString
ExpandCommand(const wxString
& command
,
256 const MessageParameters
& params
);
258 // dtor (not virtual, shouldn't be derived from)
262 // default ctor is private because the user code never creates us
265 // no copy ctor/assignment operator
266 wxFileType(const wxFileType
&);
267 wxFileType
& operator=(const wxFileType
&);
269 // the static container of wxFileType data: if it's not NULL, it means that
270 // this object is used as fallback only
271 const wxFileTypeInfo
*m_info
;
273 // the object which implements the real stuff like reading and writing
274 // to/from system MIME database
275 wxFileTypeImpl
*m_impl
;
278 // ----------------------------------------------------------------------------
279 // wxMimeTypesManager: interface to system MIME database.
281 // This class accesses the information about all known MIME types and allows
282 // the application to retrieve information (including how to handle data of
283 // given type) about them.
284 // ----------------------------------------------------------------------------
286 class WXDLLEXPORT wxMimeTypesManager
289 // static helper functions
290 // -----------------------
292 // check if the given MIME type is the same as the other one: the
293 // second argument may contain wildcards ('*'), but not the first. If
294 // the types are equal or if the mimeType matches wildcard the function
295 // returns TRUE, otherwise it returns FALSE
296 static bool IsOfType(const wxString
& mimeType
, const wxString
& wildcard
);
299 wxMimeTypesManager();
301 // NB: the following 2 functions are for Unix only and don't do anything
304 // loads data from standard files according to the mailcap styles
305 // specified: this is a bitwise OR of wxMailcapStyle values
307 // use the extraDir parameter if you want to look for files in another
309 void Initialize(int mailcapStyle
= wxMAILCAP_ALL
,
310 const wxString
& extraDir
= wxEmptyString
);
312 // and this function clears all the data from the manager
315 // Database lookup: all functions return a pointer to wxFileType object
316 // whose methods may be used to query it for the information you're
317 // interested in. If the return value is !NULL, caller is responsible for
319 // get file type from file extension
320 wxFileType
*GetFileTypeFromExtension(const wxString
& ext
);
321 // get file type from MIME type (in format <category>/<format>)
322 wxFileType
*GetFileTypeFromMimeType(const wxString
& mimeType
);
324 // other operations: return TRUE if there were no errors or FALSE if there
325 // were some unreckognized entries (the good entries are always read anyhow)
327 // FIXME: These ought to be private ??
329 // read in additional file (the standard ones are read automatically)
330 // in mailcap format (see mimetype.cpp for description)
332 // 'fallback' parameter may be set to TRUE to avoid overriding the
333 // settings from other, previously parsed, files by this one: normally,
334 // the files read most recently would override the older files, but with
335 // fallback == TRUE this won't happen
337 bool ReadMailcap(const wxString
& filename
, bool fallback
= FALSE
);
338 // read in additional file in mime.types format
339 bool ReadMimeTypes(const wxString
& filename
);
341 // enumerate all known MIME types
343 // returns the number of retrieved file types
344 size_t EnumAllFileTypes(wxArrayString
& mimetypes
);
346 // these functions can be used to provide default values for some of the
347 // MIME types inside the program itself (you may also use
348 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
349 // achieve the same goal, but this requires having this info in a file).
351 // The filetypes array should be terminated by either NULL entry or an
352 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
353 void AddFallbacks(const wxFileTypeInfo
*filetypes
);
354 void AddFallback(const wxFileTypeInfo
& ft
) { m_fallbacks
.Add(ft
); }
356 // create or remove associations
358 // create a new association using the fields of wxFileTypeInfo (at least
359 // the MIME type and the extension should be set)
360 // if the other fields are empty, the existing values should be left alone
361 wxFileType
*Associate(const wxFileTypeInfo
& ftInfo
);
364 bool Unassociate(wxFileType
*ft
) ;
366 // dtor (not virtual, shouldn't be derived from)
367 ~wxMimeTypesManager();
370 // no copy ctor/assignment operator
371 wxMimeTypesManager(const wxMimeTypesManager
&);
372 wxMimeTypesManager
& operator=(const wxMimeTypesManager
&);
374 // the fallback info which is used if the information is not found in the
375 // real system database
376 wxArrayFileTypeInfo m_fallbacks
;
378 // the object working with the system MIME database
379 wxMimeTypesManagerImpl
*m_impl
;
381 // if m_impl is NULL, create one
384 friend class wxMimeTypeCmnModule
;
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 // the default mime manager for wxWindows programs
393 WXDLLEXPORT_DATA(extern wxMimeTypesManager
*) wxTheMimeTypesManager
;
395 #endif // wxUSE_MIMETYPE