]> git.saurik.com Git - wxWidgets.git/blob - include/wx/mimetype.h
Patch 1708290
[wxWidgets.git] / include / wx / mimetype.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/mimetype.h
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32
7 // Created: 23.09.98
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_MIMETYPE_H_
14 #define _WX_MIMETYPE_H_
15
16 // ----------------------------------------------------------------------------
17 // headers and such
18 // ----------------------------------------------------------------------------
19
20 #include "wx/defs.h"
21
22 #if wxUSE_MIMETYPE
23
24 // the things we really need
25 #include "wx/string.h"
26 #include "wx/dynarray.h"
27 #include "wx/arrstr.h"
28
29 // fwd decls
30 class WXDLLIMPEXP_BASE wxIconLocation;
31 class WXDLLIMPEXP_BASE wxFileTypeImpl;
32 class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl;
33
34 // these constants define the MIME informations source under UNIX and are used
35 // by wxMimeTypesManager::Initialize()
36 enum wxMailcapStyle
37 {
38 wxMAILCAP_STANDARD = 1,
39 wxMAILCAP_NETSCAPE = 2,
40 wxMAILCAP_KDE = 4,
41 wxMAILCAP_GNOME = 8,
42
43 wxMAILCAP_ALL = 15
44 };
45
46 /*
47 TODO: would it be more convenient to have this class?
48
49 class WXDLLIMPEXP_BASE wxMimeType : public wxString
50 {
51 public:
52 // all string ctors here
53
54 wxString GetType() const { return BeforeFirst(_T('/')); }
55 wxString GetSubType() const { return AfterFirst(_T('/')); }
56
57 void SetSubType(const wxString& subtype)
58 {
59 *this = GetType() + _T('/') + subtype;
60 }
61
62 bool Matches(const wxMimeType& wildcard)
63 {
64 // implement using wxMimeTypesManager::IsOfType()
65 }
66 };
67
68 */
69
70 // wxMimeTypeCommands stores the verbs defined for the given MIME type with
71 // their values
72 class WXDLLIMPEXP_BASE wxMimeTypeCommands
73 {
74 public:
75 wxMimeTypeCommands() {}
76
77 wxMimeTypeCommands(const wxArrayString& verbs,
78 const wxArrayString& commands)
79 : m_verbs(verbs),
80 m_commands(commands)
81 {
82 }
83
84 // add a new verb with the command or replace the old value
85 void AddOrReplaceVerb(const wxString& verb, const wxString& cmd);
86 void Add(const wxString& s)
87 {
88 m_verbs.Add(s.BeforeFirst(wxT('=')));
89 m_commands.Add(s.AfterFirst(wxT('=')));
90 }
91
92 // access the commands
93 size_t GetCount() const { return m_verbs.GetCount(); }
94 const wxString& GetVerb(size_t n) const { return m_verbs[n]; }
95 const wxString& GetCmd(size_t n) const { return m_commands[n]; }
96
97 bool HasVerb(const wxString& verb) const
98 { return m_verbs.Index(verb) != wxNOT_FOUND; }
99
100 // returns empty string and wxNOT_FOUND in idx if no such verb
101 wxString GetCommandForVerb(const wxString& verb, size_t *idx = NULL) const;
102
103 // get a "verb=command" string
104 wxString GetVerbCmd(size_t n) const;
105
106 private:
107 wxArrayString m_verbs;
108 wxArrayString m_commands;
109 };
110
111 // ----------------------------------------------------------------------------
112 // wxFileTypeInfo: static container of information accessed via wxFileType.
113 //
114 // This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
115 // ----------------------------------------------------------------------------
116
117 class WXDLLIMPEXP_BASE wxFileTypeInfo
118 {
119 private:
120 void VarArgInit(const wxString& mimeType,
121 const wxString& openCmd,
122 const wxString& printCmd,
123 const wxString& desc,
124 // the other parameters form a NULL terminated list of
125 // extensions
126 ...);
127 public:
128 // ctors
129 // a normal item
130
131 // wxFileTypeInfo(const wxString& mimeType,
132 // const wxString& openCmd,
133 // const wxString& printCmd,
134 // const wxString& desc,
135 // // the other parameters form a NULL terminated list of
136 // // extensions
137 // ...);
138 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo,
139 4, (const wxString&, const wxString&,
140 const wxString&, const wxString&),
141 VarArgInit)
142 #ifdef __WATCOMC__
143 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
144 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo,
145 4, (const char*, const char*,
146 const char*, const char*),
147 VarArgInit)
148 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo,
149 4, (const wchar_t*, const wchar_t*,
150 const wchar_t*, const wchar_t*),
151 VarArgInit)
152 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo,
153 4, (const wxCStrData&, const wxCStrData&,
154 const wxCStrData&, const wxCStrData&),
155 VarArgInit)
156 #endif
157
158 // the array elements correspond to the parameters of the ctor above in
159 // the same order
160 wxFileTypeInfo(const wxArrayString& sArray);
161
162 // invalid item - use this to terminate the array passed to
163 // wxMimeTypesManager::AddFallbacks
164 wxFileTypeInfo() { }
165
166 // test if this object can be used
167 bool IsValid() const { return !m_mimeType.empty(); }
168
169 // setters
170 // set the icon info
171 void SetIcon(const wxString& iconFile, int iconIndex = 0)
172 {
173 m_iconFile = iconFile;
174 m_iconIndex = iconIndex;
175 }
176 // set the short desc
177 void SetShortDesc(const wxString& shortDesc) { m_shortDesc = shortDesc; }
178
179 // accessors
180 // get the MIME type
181 const wxString& GetMimeType() const { return m_mimeType; }
182 // get the open command
183 const wxString& GetOpenCommand() const { return m_openCmd; }
184 // get the print command
185 const wxString& GetPrintCommand() const { return m_printCmd; }
186 // get the short description (only used under Win32 so far)
187 const wxString& GetShortDesc() const { return m_shortDesc; }
188 // get the long, user visible description
189 const wxString& GetDescription() const { return m_desc; }
190 // get the array of all extensions
191 const wxArrayString& GetExtensions() const { return m_exts; }
192 size_t GetExtensionsCount() const {return m_exts.GetCount(); }
193 // get the icon info
194 const wxString& GetIconFile() const { return m_iconFile; }
195 int GetIconIndex() const { return m_iconIndex; }
196
197 private:
198 wxString m_mimeType, // the MIME type in "type/subtype" form
199 m_openCmd, // command to use for opening the file (%s allowed)
200 m_printCmd, // command to use for printing the file (%s allowed)
201 m_shortDesc, // a short string used in the registry
202 m_desc; // a free form description of this file type
203
204 // icon stuff
205 wxString m_iconFile; // the file containing the icon
206 int m_iconIndex; // icon index in this file
207
208 wxArrayString m_exts; // the extensions which are mapped on this filetype
209
210
211 #if 0 // TODO
212 // the additional (except "open" and "print") command names and values
213 wxArrayString m_commandNames,
214 m_commandValues;
215 #endif // 0
216 };
217
218 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo,
219 WXDLLIMPEXP_BASE);
220
221 // ----------------------------------------------------------------------------
222 // wxFileType: gives access to all information about the files of given type.
223 //
224 // This class holds information about a given "file type". File type is the
225 // same as MIME type under Unix, but under Windows it corresponds more to an
226 // extension than to MIME type (in fact, several extensions may correspond to a
227 // file type). This object may be created in many different ways and depending
228 // on how it was created some fields may be unknown so the return value of all
229 // the accessors *must* be checked!
230 // ----------------------------------------------------------------------------
231
232 class WXDLLIMPEXP_BASE wxFileType
233 {
234 friend class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl; // it has access to m_impl
235
236 public:
237 // An object of this class must be passed to Get{Open|Print}Command. The
238 // default implementation is trivial and doesn't know anything at all about
239 // parameters, only filename and MIME type are used (so it's probably ok for
240 // Windows where %{param} is not used anyhow)
241 class MessageParameters
242 {
243 public:
244 // ctors
245 MessageParameters() { }
246 MessageParameters(const wxString& filename,
247 const wxString& mimetype = wxEmptyString)
248 : m_filename(filename), m_mimetype(mimetype) { }
249
250 // accessors (called by GetOpenCommand)
251 // filename
252 const wxString& GetFileName() const { return m_filename; }
253 // mime type
254 const wxString& GetMimeType() const { return m_mimetype; }
255
256 // override this function in derived class
257 virtual wxString GetParamValue(const wxString& WXUNUSED(name)) const
258 { return wxEmptyString; }
259
260 // virtual dtor as in any base class
261 virtual ~MessageParameters() { }
262
263 protected:
264 wxString m_filename, m_mimetype;
265 };
266
267 // ctor from static data
268 wxFileType(const wxFileTypeInfo& ftInfo);
269
270 // accessors: all of them return true if the corresponding information
271 // could be retrieved/found, false otherwise (and in this case all [out]
272 // parameters are unchanged)
273 // return the MIME type for this file type
274 bool GetMimeType(wxString *mimeType) const;
275 bool GetMimeTypes(wxArrayString& mimeTypes) const;
276 // fill passed in array with all extensions associated with this file
277 // type
278 bool GetExtensions(wxArrayString& extensions);
279 // get the icon corresponding to this file type and of the given size
280 bool GetIcon(wxIconLocation *iconloc) const;
281 bool GetIcon(wxIconLocation *iconloc,
282 const MessageParameters& params) const;
283 // get a brief file type description ("*.txt" => "text document")
284 bool GetDescription(wxString *desc) const;
285
286 // get the command to be used to open/print the given file.
287 // get the command to execute the file of given type
288 bool GetOpenCommand(wxString *openCmd,
289 const MessageParameters& params) const;
290 // a simpler to use version of GetOpenCommand() -- it only takes the
291 // filename and returns an empty string on failure
292 wxString GetOpenCommand(const wxString& filename) const;
293 // get the command to print the file of given type
294 bool GetPrintCommand(wxString *printCmd,
295 const MessageParameters& params) const;
296
297
298 // return the number of commands defined for this file type, 0 if none
299 size_t GetAllCommands(wxArrayString *verbs, wxArrayString *commands,
300 const wxFileType::MessageParameters& params) const;
301
302 // set an arbitrary command, ask confirmation if it already exists and
303 // overwriteprompt is true
304 bool SetCommand(const wxString& cmd, const wxString& verb,
305 bool overwriteprompt = true);
306
307 bool SetDefaultIcon(const wxString& cmd = wxEmptyString, int index = 0);
308
309
310 // remove the association for this filetype from the system MIME database:
311 // notice that it will only work if the association is defined in the user
312 // file/registry part, we will never modify the system-wide settings
313 bool Unassociate();
314
315 // operations
316 // expand a string in the format of GetOpenCommand (which may contain
317 // '%s' and '%t' format specificators for the file name and mime type
318 // and %{param} constructions).
319 static wxString ExpandCommand(const wxString& command,
320 const MessageParameters& params);
321
322 // dtor (not virtual, shouldn't be derived from)
323 ~wxFileType();
324
325 private:
326 // default ctor is private because the user code never creates us
327 wxFileType();
328
329 // no copy ctor/assignment operator
330 wxFileType(const wxFileType&);
331 wxFileType& operator=(const wxFileType&);
332
333 // the static container of wxFileType data: if it's not NULL, it means that
334 // this object is used as fallback only
335 const wxFileTypeInfo *m_info;
336
337 // the object which implements the real stuff like reading and writing
338 // to/from system MIME database
339 wxFileTypeImpl *m_impl;
340 };
341
342 //----------------------------------------------------------------------------
343 // wxMimeTypesManagerFactory
344 //----------------------------------------------------------------------------
345
346 class WXDLLIMPEXP_BASE wxMimeTypesManagerFactory
347 {
348 public:
349 wxMimeTypesManagerFactory() {}
350 virtual ~wxMimeTypesManagerFactory() {}
351
352 virtual wxMimeTypesManagerImpl *CreateMimeTypesManagerImpl();
353
354 static void Set( wxMimeTypesManagerFactory *factory );
355 static wxMimeTypesManagerFactory *Get();
356
357 private:
358 static wxMimeTypesManagerFactory *m_factory;
359 };
360
361 // ----------------------------------------------------------------------------
362 // wxMimeTypesManager: interface to system MIME database.
363 //
364 // This class accesses the information about all known MIME types and allows
365 // the application to retrieve information (including how to handle data of
366 // given type) about them.
367 // ----------------------------------------------------------------------------
368
369 class WXDLLIMPEXP_BASE wxMimeTypesManager
370 {
371 public:
372 // static helper functions
373 // -----------------------
374
375 // check if the given MIME type is the same as the other one: the
376 // second argument may contain wildcards ('*'), but not the first. If
377 // the types are equal or if the mimeType matches wildcard the function
378 // returns true, otherwise it returns false
379 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
380
381 // ctor
382 wxMimeTypesManager();
383
384 // NB: the following 2 functions are for Unix only and don't do anything
385 // elsewhere
386
387 // loads data from standard files according to the mailcap styles
388 // specified: this is a bitwise OR of wxMailcapStyle values
389 //
390 // use the extraDir parameter if you want to look for files in another
391 // directory
392 void Initialize(int mailcapStyle = wxMAILCAP_ALL,
393 const wxString& extraDir = wxEmptyString);
394
395 // and this function clears all the data from the manager
396 void ClearData();
397
398 // Database lookup: all functions return a pointer to wxFileType object
399 // whose methods may be used to query it for the information you're
400 // interested in. If the return value is !NULL, caller is responsible for
401 // deleting it.
402 // get file type from file extension
403 wxFileType *GetFileTypeFromExtension(const wxString& ext);
404 // get file type from MIME type (in format <category>/<format>)
405 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
406
407 // other operations: return true if there were no errors or false if there
408 // were some unrecognized entries (the good entries are always read anyhow)
409 //
410 // FIXME: These ought to be private ??
411
412 // read in additional file (the standard ones are read automatically)
413 // in mailcap format (see mimetype.cpp for description)
414 //
415 // 'fallback' parameter may be set to true to avoid overriding the
416 // settings from other, previously parsed, files by this one: normally,
417 // the files read most recently would override the older files, but with
418 // fallback == true this won't happen
419
420 bool ReadMailcap(const wxString& filename, bool fallback = false);
421 // read in additional file in mime.types format
422 bool ReadMimeTypes(const wxString& filename);
423
424 // enumerate all known MIME types
425 //
426 // returns the number of retrieved file types
427 size_t EnumAllFileTypes(wxArrayString& mimetypes);
428
429 // these functions can be used to provide default values for some of the
430 // MIME types inside the program itself (you may also use
431 // ReadMailcap(filenameWithDefaultTypes, true /* use as fallback */) to
432 // achieve the same goal, but this requires having this info in a file).
433 //
434 // The filetypes array should be terminated by either NULL entry or an
435 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
436 void AddFallbacks(const wxFileTypeInfo *filetypes);
437 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
438
439 // create or remove associations
440
441 // create a new association using the fields of wxFileTypeInfo (at least
442 // the MIME type and the extension should be set)
443 // if the other fields are empty, the existing values should be left alone
444 wxFileType *Associate(const wxFileTypeInfo& ftInfo);
445
446 // undo Associate()
447 bool Unassociate(wxFileType *ft) ;
448
449 // dtor (not virtual, shouldn't be derived from)
450 ~wxMimeTypesManager();
451
452 private:
453 // no copy ctor/assignment operator
454 wxMimeTypesManager(const wxMimeTypesManager&);
455 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
456
457 // the fallback info which is used if the information is not found in the
458 // real system database
459 wxArrayFileTypeInfo m_fallbacks;
460
461 // the object working with the system MIME database
462 wxMimeTypesManagerImpl *m_impl;
463
464 // if m_impl is NULL, create one
465 void EnsureImpl();
466
467 friend class wxMimeTypeCmnModule;
468 };
469
470
471 // ----------------------------------------------------------------------------
472 // global variables
473 // ----------------------------------------------------------------------------
474
475 // the default mime manager for wxWidgets programs
476 extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager *) wxTheMimeTypesManager;
477
478 #endif // wxUSE_MIMETYPE
479
480 #endif
481 //_WX_MIMETYPE_H_