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