]> git.saurik.com Git - wxWidgets.git/blame - include/wx/mimetype.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[wxWidgets.git] / include / wx / mimetype.h
CommitLineData
b13d92d1
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/mimetype.h
3// Purpose: classes and functions to manage MIME types
4// Author: Vadim Zeitlin
5// Modified by:
c7ce8392 6// Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32
b13d92d1 7// Created: 23.09.98
b13d92d1 8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence (part of wxExtra library)
b13d92d1
VZ
10/////////////////////////////////////////////////////////////////////////////
11
a6c65e88
VZ
12#ifndef _WX_MIMETYPE_H_
13#define _WX_MIMETYPE_H_
b13d92d1 14
a6c65e88
VZ
15// ----------------------------------------------------------------------------
16// headers and such
17// ----------------------------------------------------------------------------
b13d92d1 18
ce4169a4
RR
19#include "wx/defs.h"
20
1e6feb95
VZ
21#if wxUSE_MIMETYPE
22
b13d92d1
VZ
23// the things we really need
24#include "wx/string.h"
ecf23aa6 25#include "wx/dynarray.h"
2b850ae1 26#include "wx/arrstr.h"
b13d92d1 27
d1f6e2cf
VS
28#include <stdarg.h>
29
a6c65e88 30// fwd decls
b5dbe15d
VS
31class WXDLLIMPEXP_FWD_BASE wxIconLocation;
32class WXDLLIMPEXP_FWD_BASE wxFileTypeImpl;
33class WXDLLIMPEXP_FWD_BASE wxMimeTypesManagerImpl;
a6c65e88 34
2b813b73
VZ
35// these constants define the MIME informations source under UNIX and are used
36// by wxMimeTypesManager::Initialize()
37enum wxMailcapStyle
38{
39 wxMAILCAP_STANDARD = 1,
40 wxMAILCAP_NETSCAPE = 2,
41 wxMAILCAP_KDE = 4,
42 wxMAILCAP_GNOME = 8,
43
44 wxMAILCAP_ALL = 15
45};
46
a6c65e88
VZ
47/*
48 TODO: would it be more convenient to have this class?
49
bddd7a8d 50class WXDLLIMPEXP_BASE wxMimeType : public wxString
a6c65e88
VZ
51{
52public:
53 // all string ctors here
54
9a83f860
VZ
55 wxString GetType() const { return BeforeFirst(wxT('/')); }
56 wxString GetSubType() const { return AfterFirst(wxT('/')); }
a6c65e88
VZ
57
58 void SetSubType(const wxString& subtype)
59 {
9a83f860 60 *this = GetType() + wxT('/') + subtype;
a6c65e88
VZ
61 }
62
63 bool Matches(const wxMimeType& wildcard)
64 {
65 // implement using wxMimeTypesManager::IsOfType()
66 }
67};
68
69*/
70
2b850ae1
RR
71// wxMimeTypeCommands stores the verbs defined for the given MIME type with
72// their values
73class WXDLLIMPEXP_BASE wxMimeTypeCommands
74{
75public:
76 wxMimeTypeCommands() {}
77
78 wxMimeTypeCommands(const wxArrayString& verbs,
79 const wxArrayString& commands)
80 : m_verbs(verbs),
81 m_commands(commands)
82 {
83 }
84
85 // add a new verb with the command or replace the old value
bde733b0 86 void AddOrReplaceVerb(const wxString& verb, const wxString& cmd);
2b850ae1
RR
87 void Add(const wxString& s)
88 {
89 m_verbs.Add(s.BeforeFirst(wxT('=')));
90 m_commands.Add(s.AfterFirst(wxT('=')));
91 }
92
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]; }
97
98 bool HasVerb(const wxString& verb) const
99 { return m_verbs.Index(verb) != wxNOT_FOUND; }
100
bde733b0
VZ
101 // returns empty string and wxNOT_FOUND in idx if no such verb
102 wxString GetCommandForVerb(const wxString& verb, size_t *idx = NULL) const;
2b850ae1
RR
103
104 // get a "verb=command" string
bde733b0 105 wxString GetVerbCmd(size_t n) const;
2b850ae1
RR
106
107private:
108 wxArrayString m_verbs;
109 wxArrayString m_commands;
110};
111
a6c65e88
VZ
112// ----------------------------------------------------------------------------
113// wxFileTypeInfo: static container of information accessed via wxFileType.
114//
115// This class is used with wxMimeTypesManager::AddFallbacks() and Associate()
116// ----------------------------------------------------------------------------
117
bddd7a8d 118class WXDLLIMPEXP_BASE wxFileTypeInfo
a6c65e88 119{
2523e9b7 120private:
d1f6e2cf
VS
121 void DoVarArgInit(const wxString& mimeType,
122 const wxString& openCmd,
123 const wxString& printCmd,
124 const wxString& desc,
125 va_list argptr);
126
278d7ab4
VS
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
132 // extensions
133 ...);
134
a6c65e88 135public:
278d7ab4
VS
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.
c5895661 139 struct CtorString
278d7ab4
VS
140 {
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) {}
de4983f3
VS
145 CtorString(const wxScopedCharBuffer& str) : m_str(str) {}
146 CtorString(const wxScopedWCharBuffer& str) : m_str(str) {}
278d7ab4
VS
147
148 operator const wxString*() const { return &m_str; }
149
150 wxString m_str;
151 };
152
a6c65e88 153 // ctors
2523e9b7 154
df53be12
VZ
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)
159 {
160 }
161
162 // Ctor allowing to specify the values of all fields at once:
163 //
2523e9b7
VS
164 // wxFileTypeInfo(const wxString& mimeType,
165 // const wxString& openCmd,
166 // const wxString& printCmd,
167 // const wxString& desc,
7c37eb10
VZ
168 // // the other parameters form a list of extensions for this
169 // // file type and should be terminated with wxNullPtr (not
170 // // just NULL!)
2523e9b7
VS
171 // ...);
172 WX_DEFINE_VARARG_FUNC_CTOR(wxFileTypeInfo,
278d7ab4
VS
173 4, (const CtorString&,
174 const CtorString&,
175 const CtorString&,
176 const CtorString&),
177 VarArgInit, VarArgInit)
2523e9b7
VS
178#ifdef __WATCOMC__
179 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
180 WX_VARARG_WATCOM_WORKAROUND_CTOR(
181 wxFileTypeInfo,
182 4, (const wxString&,
183 const wxString&,
184 const wxString&,
185 const wxString&),
278d7ab4
VS
186 (CtorString(f1),
187 CtorString(f2),
188 CtorString(f3),
189 CtorString(f4)));
59a14f69
VS
190 WX_VARARG_WATCOM_WORKAROUND_CTOR(
191 wxFileTypeInfo,
192 4, (const wxCStrData&,
193 const wxCStrData&,
194 const wxCStrData&,
195 const wxCStrData&),
278d7ab4
VS
196 (CtorString(f1),
197 CtorString(f2),
198 CtorString(f3),
199 CtorString(f4)));
59a14f69
VS
200 WX_VARARG_WATCOM_WORKAROUND_CTOR(
201 wxFileTypeInfo,
202 4, (const char*,
203 const char*,
204 const char*,
205 const char*),
278d7ab4
VS
206 (CtorString(f1),
207 CtorString(f2),
208 CtorString(f3),
209 CtorString(f4)));
59a14f69
VS
210 WX_VARARG_WATCOM_WORKAROUND_CTOR(
211 wxFileTypeInfo,
212 4, (const wchar_t*,
213 const wchar_t*,
214 const wchar_t*,
215 const wchar_t*),
278d7ab4
VS
216 (CtorString(f1),
217 CtorString(f2),
218 CtorString(f3),
219 CtorString(f4)));
2523e9b7 220#endif
a6c65e88 221
2b813b73
VZ
222 // the array elements correspond to the parameters of the ctor above in
223 // the same order
224 wxFileTypeInfo(const wxArrayString& sArray);
225
a6c65e88
VZ
226 // invalid item - use this to terminate the array passed to
227 // wxMimeTypesManager::AddFallbacks
228 wxFileTypeInfo() { }
229
230 // test if this object can be used
16cba29d 231 bool IsValid() const { return !m_mimeType.empty(); }
a6c65e88
VZ
232
233 // setters
df53be12
VZ
234 // set the open/print commands
235 void SetOpenCommand(const wxString& command) { m_openCmd = command; }
236 void SetPrintCommand(const wxString& command) { m_printCmd = command; }
237
238 // set the description
239 void SetDescription(const wxString& desc) { m_desc = desc; }
240
241 // add another extension corresponding to this file type
242 void AddExtension(const wxString& ext) { m_exts.push_back(ext); }
243
a6c65e88
VZ
244 // set the icon info
245 void SetIcon(const wxString& iconFile, int iconIndex = 0)
246 {
247 m_iconFile = iconFile;
248 m_iconIndex = iconIndex;
249 }
250 // set the short desc
251 void SetShortDesc(const wxString& shortDesc) { m_shortDesc = shortDesc; }
252
253 // accessors
254 // get the MIME type
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; }
03603400 266 size_t GetExtensionsCount() const {return m_exts.GetCount(); }
a6c65e88
VZ
267 // get the icon info
268 const wxString& GetIconFile() const { return m_iconFile; }
269 int GetIconIndex() const { return m_iconIndex; }
270
271private:
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
277
278 // icon stuff
279 wxString m_iconFile; // the file containing the icon
280 int m_iconIndex; // icon index in this file
281
282 wxArrayString m_exts; // the extensions which are mapped on this filetype
283
2b813b73 284
a6c65e88
VZ
285#if 0 // TODO
286 // the additional (except "open" and "print") command names and values
287 wxArrayString m_commandNames,
288 m_commandValues;
289#endif // 0
290};
291
4460b6c4
VS
292WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo,
293 WXDLLIMPEXP_BASE);
66806a0b 294
a6c65e88
VZ
295// ----------------------------------------------------------------------------
296// wxFileType: gives access to all information about the files of given type.
297//
b13d92d1
VZ
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!
a6c65e88
VZ
304// ----------------------------------------------------------------------------
305
bddd7a8d 306class WXDLLIMPEXP_BASE wxFileType
b13d92d1 307{
b5dbe15d 308friend class WXDLLIMPEXP_FWD_BASE wxMimeTypesManagerImpl; // it has access to m_impl
b13d92d1
VZ
309
310public:
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
316 {
317 public:
318 // ctors
319 MessageParameters() { }
0532a258 320 MessageParameters(const wxString& filename,
fda7962d 321 const wxString& mimetype = wxEmptyString)
b13d92d1
VZ
322 : m_filename(filename), m_mimetype(mimetype) { }
323
324 // accessors (called by GetOpenCommand)
325 // filename
326 const wxString& GetFileName() const { return m_filename; }
327 // mime type
328 const wxString& GetMimeType() const { return m_mimetype; }
329
330 // override this function in derived class
a6c65e88
VZ
331 virtual wxString GetParamValue(const wxString& WXUNUSED(name)) const
332 { return wxEmptyString; }
b13d92d1
VZ
333
334 // virtual dtor as in any base class
335 virtual ~MessageParameters() { }
336
337 protected:
338 wxString m_filename, m_mimetype;
339 };
340
a6c65e88
VZ
341 // ctor from static data
342 wxFileType(const wxFileTypeInfo& ftInfo);
343
b13d92d1
VZ
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;
4d2976ad 349 bool GetMimeTypes(wxArrayString& mimeTypes) const;
b13d92d1
VZ
350 // fill passed in array with all extensions associated with this file
351 // type
352 bool GetExtensions(wxArrayString& extensions);
da0766ab
VZ
353 // get the icon corresponding to this file type and of the given size
354 bool GetIcon(wxIconLocation *iconloc) const;
11d395f9
VZ
355 bool GetIcon(wxIconLocation *iconloc,
356 const MessageParameters& params) const;
b13d92d1
VZ
357 // get a brief file type description ("*.txt" => "text document")
358 bool GetDescription(wxString *desc) const;
359
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;
0532a258
VZ
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;
b13d92d1
VZ
367 // get the command to print the file of given type
368 bool GetPrintCommand(wxString *printCmd,
369 const MessageParameters& params) const;
370
c7ce8392
VZ
371
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;
375
2b813b73 376 // set an arbitrary command, ask confirmation if it already exists and
4e32eea1 377 // overwriteprompt is true
2b813b73 378 bool SetCommand(const wxString& cmd, const wxString& verb,
4e32eea1 379 bool overwriteprompt = true);
2b813b73
VZ
380
381 bool SetDefaultIcon(const wxString& cmd = wxEmptyString, int index = 0);
382
383
a6c65e88
VZ
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
c7ce8392
VZ
387 bool Unassociate();
388
b13d92d1
VZ
389 // operations
390 // expand a string in the format of GetOpenCommand (which may contain
d13b34d3 391 // '%s' and '%t' format specifiers for the file name and mime type
b13d92d1
VZ
392 // and %{param} constructions).
393 static wxString ExpandCommand(const wxString& command,
394 const MessageParameters& params);
395
396 // dtor (not virtual, shouldn't be derived from)
397 ~wxFileType();
398
399private:
400 // default ctor is private because the user code never creates us
401 wxFileType();
402
403 // no copy ctor/assignment operator
404 wxFileType(const wxFileType&);
405 wxFileType& operator=(const wxFileType&);
406
a6c65e88
VZ
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;
71f21f46 410
a6c65e88
VZ
411 // the object which implements the real stuff like reading and writing
412 // to/from system MIME database
413 wxFileTypeImpl *m_impl;
71f21f46
VZ
414};
415
2b850ae1
RR
416//----------------------------------------------------------------------------
417// wxMimeTypesManagerFactory
418//----------------------------------------------------------------------------
419
420class WXDLLIMPEXP_BASE wxMimeTypesManagerFactory
421{
422public:
423 wxMimeTypesManagerFactory() {}
424 virtual ~wxMimeTypesManagerFactory() {}
425
426 virtual wxMimeTypesManagerImpl *CreateMimeTypesManagerImpl();
427
a893b65e
VZ
428 static void Set( wxMimeTypesManagerFactory *factory );
429 static wxMimeTypesManagerFactory *Get();
03647350 430
2b850ae1
RR
431private:
432 static wxMimeTypesManagerFactory *m_factory;
433};
434
a6c65e88
VZ
435// ----------------------------------------------------------------------------
436// wxMimeTypesManager: interface to system MIME database.
437//
b13d92d1
VZ
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.
a6c65e88
VZ
441// ----------------------------------------------------------------------------
442
bddd7a8d 443class WXDLLIMPEXP_BASE wxMimeTypesManager
b13d92d1
VZ
444{
445public:
da61ab31
VZ
446 // static helper functions
447 // -----------------------
448
a6c65e88
VZ
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
4e32eea1 452 // returns true, otherwise it returns false
da61ab31
VZ
453 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
454
b13d92d1
VZ
455 // ctor
456 wxMimeTypesManager();
457
2b813b73
VZ
458 // NB: the following 2 functions are for Unix only and don't do anything
459 // elsewhere
460
461 // loads data from standard files according to the mailcap styles
462 // specified: this is a bitwise OR of wxMailcapStyle values
463 //
464 // use the extraDir parameter if you want to look for files in another
465 // directory
805f26b3 466 void Initialize(int mailcapStyle = wxMAILCAP_ALL,
2b813b73
VZ
467 const wxString& extraDir = wxEmptyString);
468
469 // and this function clears all the data from the manager
470 void ClearData();
471
b13d92d1
VZ
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
475 // deleting it.
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);
480
696e1ea0 481 // enumerate all known MIME types
1b986aef
VZ
482 //
483 // returns the number of retrieved file types
696e1ea0 484 size_t EnumAllFileTypes(wxArrayString& mimetypes);
1b986aef 485
71f21f46 486 // these functions can be used to provide default values for some of the
29886d1b 487 // MIME types inside the program itself
71f21f46 488 //
a6c65e88
VZ
489 // The filetypes array should be terminated by either NULL entry or an
490 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
8e124873 491 void AddFallbacks(const wxFileTypeInfo *filetypes);
a6c65e88
VZ
492 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
493
494 // create or remove associations
71f21f46 495
a6c65e88
VZ
496 // create a new association using the fields of wxFileTypeInfo (at least
497 // the MIME type and the extension should be set)
2b813b73 498 // if the other fields are empty, the existing values should be left alone
a6c65e88
VZ
499 wxFileType *Associate(const wxFileTypeInfo& ftInfo);
500
501 // undo Associate()
2b813b73 502 bool Unassociate(wxFileType *ft) ;
c7ce8392 503
b13d92d1
VZ
504 // dtor (not virtual, shouldn't be derived from)
505 ~wxMimeTypesManager();
506
507private:
508 // no copy ctor/assignment operator
509 wxMimeTypesManager(const wxMimeTypesManager&);
510 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
511
a6c65e88
VZ
512 // the fallback info which is used if the information is not found in the
513 // real system database
514 wxArrayFileTypeInfo m_fallbacks;
515
516 // the object working with the system MIME database
b13d92d1 517 wxMimeTypesManagerImpl *m_impl;
c7ce8392 518
ecf23aa6
VS
519 // if m_impl is NULL, create one
520 void EnsureImpl();
c7ce8392 521
66806a0b 522 friend class wxMimeTypeCmnModule;
b13d92d1
VZ
523};
524
ecf23aa6
VS
525
526// ----------------------------------------------------------------------------
527// global variables
528// ----------------------------------------------------------------------------
529
77ffb593 530// the default mime manager for wxWidgets programs
16cba29d 531extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager *) wxTheMimeTypesManager;
ecf23aa6 532
1e6feb95
VZ
533#endif // wxUSE_MIMETYPE
534
ce4169a4 535#endif
a6c65e88 536 //_WX_MIMETYPE_H_