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