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