split mimetype.h/.cpp into platform specific code
[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 // Created: 23.09.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _MIMETYPE_H
13 #define _MIMETYPE_H
14
15 #ifdef __GNUG__
16 #pragma interface "mimetypebase.h"
17 #endif
18
19
20 // fwd decls
21 class wxIcon;
22 class wxFileTypeImpl;
23 class wxMimeTypesManagerImpl;
24
25 #include "wx/defs.h"
26
27 #if wxUSE_FILE
28
29 // the things we really need
30 #include "wx/string.h"
31 #include "wx/dynarray.h"
32
33 // This class holds information about a given "file type". File type is the
34 // same as MIME type under Unix, but under Windows it corresponds more to an
35 // extension than to MIME type (in fact, several extensions may correspond to a
36 // file type). This object may be created in many different ways and depending
37 // on how it was created some fields may be unknown so the return value of all
38 // the accessors *must* be checked!
39 class WXDLLEXPORT wxFileType
40 {
41 friend class WXDLLEXPORT wxMimeTypesManagerImpl; // it has access to m_impl
42
43 public:
44 // An object of this class must be passed to Get{Open|Print}Command. The
45 // default implementation is trivial and doesn't know anything at all about
46 // parameters, only filename and MIME type are used (so it's probably ok for
47 // Windows where %{param} is not used anyhow)
48 class MessageParameters
49 {
50 public:
51 // ctors
52 MessageParameters() { }
53 MessageParameters(const wxString& filename, const wxString& mimetype)
54 : m_filename(filename), m_mimetype(mimetype) { }
55
56 // accessors (called by GetOpenCommand)
57 // filename
58 const wxString& GetFileName() const { return m_filename; }
59 // mime type
60 const wxString& GetMimeType() const { return m_mimetype; }
61
62 // override this function in derived class
63 virtual wxString GetParamValue(const wxString& WXUNUSED(paramName)) const
64 { return ""; }
65
66 // virtual dtor as in any base class
67 virtual ~MessageParameters() { }
68
69 protected:
70 wxString m_filename, m_mimetype;
71 };
72
73 // accessors: all of them return true if the corresponding information
74 // could be retrieved/found, false otherwise (and in this case all [out]
75 // parameters are unchanged)
76 // return the MIME type for this file type
77 bool GetMimeType(wxString *mimeType) const;
78 // fill passed in array with all extensions associated with this file
79 // type
80 bool GetExtensions(wxArrayString& extensions);
81 // get the icon corresponding to this file type
82 bool GetIcon(wxIcon *icon) const;
83 // get a brief file type description ("*.txt" => "text document")
84 bool GetDescription(wxString *desc) const;
85
86 // get the command to be used to open/print the given file.
87 // get the command to execute the file of given type
88 bool GetOpenCommand(wxString *openCmd,
89 const MessageParameters& params) const;
90 // get the command to print the file of given type
91 bool GetPrintCommand(wxString *printCmd,
92 const MessageParameters& params) const;
93
94 // operations
95 // expand a string in the format of GetOpenCommand (which may contain
96 // '%s' and '%t' format specificators for the file name and mime type
97 // and %{param} constructions).
98 static wxString ExpandCommand(const wxString& command,
99 const MessageParameters& params);
100
101 // dtor (not virtual, shouldn't be derived from)
102 ~wxFileType();
103
104 private:
105 // default ctor is private because the user code never creates us
106 wxFileType();
107
108 // no copy ctor/assignment operator
109 wxFileType(const wxFileType&);
110 wxFileType& operator=(const wxFileType&);
111
112 wxFileTypeImpl *m_impl;
113 };
114
115 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
116 // just as the container for the wxFileType data.
117 class WXDLLEXPORT wxFileTypeInfo
118 {
119 public:
120 // ctors
121 // a normal item
122 wxFileTypeInfo(const char *mimeType,
123 const char *openCmd,
124 const char *printCmd,
125 const char *desc,
126 // the other parameters form a NULL terminated list of
127 // extensions
128 ...);
129
130 // invalid item - use this to terminate the array passed to
131 // wxMimeTypesManager::AddFallbacks
132 wxFileTypeInfo() { }
133
134 bool IsValid() const { return !m_mimeType.IsEmpty(); }
135
136 // accessors
137 // get the MIME type
138 const wxString& GetMimeType() const { return m_mimeType; }
139 // get the open command
140 const wxString& GetOpenCommand() const { return m_openCmd; }
141 // get the print command
142 const wxString& GetPrintCommand() const { return m_printCmd; }
143 // get the description
144 const wxString& GetDescription() const { return m_desc; }
145 // get the array of all extensions
146 const wxArrayString& GetExtensions() const { return m_exts; }
147
148 private:
149 wxString m_mimeType, // the MIME type in "type/subtype" form
150 m_openCmd, // command to use for opening the file (%s allowed)
151 m_printCmd, // command to use for printing the file (%s allowed)
152 m_desc; // a free form description of this file type
153
154 wxArrayString m_exts; // the extensions which are mapped on this filetype
155 };
156
157 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo);
158
159
160 // This class accesses the information about all known MIME types and allows
161 // the application to retrieve information (including how to handle data of
162 // given type) about them.
163 //
164 // NB: currently it doesn't support modifying MIME database (read-only access).
165 class WXDLLEXPORT wxMimeTypesManager
166 {
167 public:
168 // static helper functions
169 // -----------------------
170
171 // check if the given MIME type is the same as the other one: the second
172 // argument may contain wildcards ('*'), but not the first. If the
173 // types are equal or if the mimeType matches wildcard the function
174 // returns TRUE, otherwise it returns FALSE
175 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
176
177 // ctor
178 wxMimeTypesManager();
179
180 // Database lookup: all functions return a pointer to wxFileType object
181 // whose methods may be used to query it for the information you're
182 // interested in. If the return value is !NULL, caller is responsible for
183 // deleting it.
184 // get file type from file extension
185 wxFileType *GetFileTypeFromExtension(const wxString& ext);
186 // get file type from MIME type (in format <category>/<format>)
187 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
188
189 // other operations: return TRUE if there were no errors or FALSE if there
190 // were some unreckognized entries (the good entries are always read anyhow)
191 // read in additional file (the standard ones are read automatically)
192 // in mailcap format (see mimetype.cpp for description)
193 //
194 // 'fallback' parameter may be set to TRUE to avoid overriding the
195 // settings from other, previously parsed, files by this one: normally,
196 // the files read most recently would override the older files, but with
197 // fallback == TRUE this won't happen
198 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
199 // read in additional file in mime.types format
200 bool ReadMimeTypes(const wxString& filename);
201
202 // enumerate all known MIME types
203 //
204 // returns the number of retrieved file types
205 size_t EnumAllFileTypes(wxArrayString& mimetypes);
206
207 // these functions can be used to provide default values for some of the
208 // MIME types inside the program itself (you may also use
209 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
210 // achieve the same goal, but this requires having this info in a file).
211 //
212 // It isn't possible (currently) to provide fallback icons using this
213 // function.
214 //
215 // The filetypes array should be terminated by a NULL entry
216 void AddFallbacks(const wxFileTypeInfo *filetypes);
217
218 // dtor (not virtual, shouldn't be derived from)
219 ~wxMimeTypesManager();
220
221 private:
222 // no copy ctor/assignment operator
223 wxMimeTypesManager(const wxMimeTypesManager&);
224 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
225
226 wxMimeTypesManagerImpl *m_impl;
227
228 // if m_impl is NULL, create one
229 void EnsureImpl();
230 };
231
232
233 // ----------------------------------------------------------------------------
234 // global variables
235 // ----------------------------------------------------------------------------
236
237 // the default mime manager for wxWindows programs
238 WXDLLEXPORT_DATA(extern wxMimeTypesManager *) wxTheMimeTypesManager;
239
240 #endif
241 // wxUSE_FILE
242
243 #endif
244 //_MIMETYPE_H
245
246 /* vi: set cin tw=80 ts=4 sw=4: */