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