]> git.saurik.com Git - wxWidgets.git/blob - include/wx/mimetype.h
regenerated after bakefile change to always create shared-ld-sh under Darwin
[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
28 // fwd decls
29 class WXDLLIMPEXP_BASE wxIconLocation;
30 class WXDLLIMPEXP_BASE wxFileTypeImpl;
31 class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl;
32
33 // these constants define the MIME informations source under UNIX and are used
34 // by wxMimeTypesManager::Initialize()
35 enum 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
45 /*
46 TODO: would it be more convenient to have this class?
47
48 class WXDLLIMPEXP_BASE wxMimeType : public wxString
49 {
50 public:
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
75 class WXDLLIMPEXP_BASE wxFileTypeInfo
76 {
77 public:
78 // ctors
79 // a normal item
80 wxFileTypeInfo(const wxChar *mimeType,
81 const wxChar *openCmd,
82 const wxChar *printCmd,
83 const wxChar *desc,
84 // the other parameters form a NULL terminated list of
85 // extensions
86 ...);
87
88 // the array elements correspond to the parameters of the ctor above in
89 // the same order
90 wxFileTypeInfo(const wxArrayString& sArray);
91
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
97 bool IsValid() const { return !m_mimeType.empty(); }
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; }
122 int GetExtensionsCount() const {return m_exts.GetCount(); }
123 // get the icon info
124 const wxString& GetIconFile() const { return m_iconFile; }
125 int GetIconIndex() const { return m_iconIndex; }
126
127 private:
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
140
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
148 WX_DECLARE_USER_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo,
149 WXDLLIMPEXP_BASE);
150
151 // ----------------------------------------------------------------------------
152 // wxFileType: gives access to all information about the files of given type.
153 //
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!
160 // ----------------------------------------------------------------------------
161
162 class WXDLLIMPEXP_BASE wxFileType
163 {
164 friend class WXDLLIMPEXP_BASE wxMimeTypesManagerImpl; // it has access to m_impl
165
166 public:
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() { }
176 MessageParameters(const wxString& filename,
177 const wxString& mimetype = wxEmptyString)
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
187 virtual wxString GetParamValue(const wxString& WXUNUSED(name)) const
188 { return wxEmptyString; }
189
190 // virtual dtor as in any base class
191 virtual ~MessageParameters() { }
192
193 protected:
194 wxString m_filename, m_mimetype;
195 };
196
197 // ctor from static data
198 wxFileType(const wxFileTypeInfo& ftInfo);
199
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;
205 bool GetMimeTypes(wxArrayString& mimeTypes) const;
206 // fill passed in array with all extensions associated with this file
207 // type
208 bool GetExtensions(wxArrayString& extensions);
209 // get the icon corresponding to this file type and of the given size
210 bool GetIcon(wxIconLocation *iconloc) const;
211 bool GetIcon(wxIconLocation *iconloc,
212 const MessageParameters& params) const;
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;
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;
223 // get the command to print the file of given type
224 bool GetPrintCommand(wxString *printCmd,
225 const MessageParameters& params) const;
226
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
232 // set an arbitrary command, ask confirmation if it already exists and
233 // overwriteprompt is true
234 bool SetCommand(const wxString& cmd, const wxString& verb,
235 bool overwriteprompt = true);
236
237 bool SetDefaultIcon(const wxString& cmd = wxEmptyString, int index = 0);
238
239
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
243 bool Unassociate();
244
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
255 private:
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
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;
266
267 // the object which implements the real stuff like reading and writing
268 // to/from system MIME database
269 wxFileTypeImpl *m_impl;
270 };
271
272 // ----------------------------------------------------------------------------
273 // wxMimeTypesManager: interface to system MIME database.
274 //
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.
278 // ----------------------------------------------------------------------------
279
280 class WXDLLIMPEXP_BASE wxMimeTypesManager
281 {
282 public:
283 // static helper functions
284 // -----------------------
285
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
289 // returns true, otherwise it returns false
290 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
291
292 // ctor
293 wxMimeTypesManager();
294
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
303 void Initialize(int mailcapStyle = wxMAILCAP_ALL,
304 const wxString& extraDir = wxEmptyString);
305
306 // and this function clears all the data from the manager
307 void ClearData();
308
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
318 // other operations: return true if there were no errors or false if there
319 // were some unrecognized entries (the good entries are always read anyhow)
320 //
321 // FIXME: These ought to be private ??
322
323 // read in additional file (the standard ones are read automatically)
324 // in mailcap format (see mimetype.cpp for description)
325 //
326 // 'fallback' parameter may be set to true to avoid overriding the
327 // settings from other, previously parsed, files by this one: normally,
328 // the files read most recently would override the older files, but with
329 // fallback == true this won't happen
330
331 bool ReadMailcap(const wxString& filename, bool fallback = false);
332 // read in additional file in mime.types format
333 bool ReadMimeTypes(const wxString& filename);
334
335 // enumerate all known MIME types
336 //
337 // returns the number of retrieved file types
338 size_t EnumAllFileTypes(wxArrayString& mimetypes);
339
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
342 // ReadMailcap(filenameWithDefaultTypes, true /* use as fallback */) to
343 // achieve the same goal, but this requires having this info in a file).
344 //
345 // The filetypes array should be terminated by either NULL entry or an
346 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
347 void AddFallbacks(const wxFileTypeInfo *filetypes);
348 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
349
350 // create or remove associations
351
352 // create a new association using the fields of wxFileTypeInfo (at least
353 // the MIME type and the extension should be set)
354 // if the other fields are empty, the existing values should be left alone
355 wxFileType *Associate(const wxFileTypeInfo& ftInfo);
356
357 // undo Associate()
358 bool Unassociate(wxFileType *ft) ;
359
360 // dtor (not virtual, shouldn't be derived from)
361 ~wxMimeTypesManager();
362
363 private:
364 // no copy ctor/assignment operator
365 wxMimeTypesManager(const wxMimeTypesManager&);
366 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
367
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
373 wxMimeTypesManagerImpl *m_impl;
374
375 // if m_impl is NULL, create one
376 void EnsureImpl();
377
378 friend class wxMimeTypeCmnModule;
379 };
380
381
382 // ----------------------------------------------------------------------------
383 // global variables
384 // ----------------------------------------------------------------------------
385
386 // the default mime manager for wxWidgets programs
387 extern WXDLLIMPEXP_DATA_BASE(wxMimeTypesManager *) wxTheMimeTypesManager;
388
389 #endif // wxUSE_MIMETYPE
390
391 #endif
392 //_WX_MIMETYPE_H_