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