]> git.saurik.com Git - wxWidgets.git/blob - include/wx/mimetype.h
changing the number of items in wxHtmlListBox should flush the cache
[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 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "mimetypebase.h"
18 #endif // __GNUG__
19
20 // ----------------------------------------------------------------------------
21 // headers and such
22 // ----------------------------------------------------------------------------
23
24 #include "wx/defs.h"
25
26 #if wxUSE_MIMETYPE
27
28 // the things we really need
29 #include "wx/string.h"
30 #include "wx/dynarray.h"
31
32 // fwd decls
33 #if defined(__WXMSW__)
34 class WXDLLEXPORT wxIconLocation;
35 #else
36 class WXDLLEXPORT wxIconLocationBase;
37 typedef wxIconLocationBase wxIconLocation;
38 #endif //defined(__WXMSW__)
39 class WXDLLEXPORT wxFileTypeImpl;
40 class WXDLLEXPORT wxMimeTypesManagerImpl;
41
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
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
89 wxFileTypeInfo(const wxChar *mimeType,
90 const wxChar *openCmd,
91 const wxChar *printCmd,
92 const wxChar *desc,
93 // the other parameters form a NULL terminated list of
94 // extensions
95 ...);
96
97 // the array elements correspond to the parameters of the ctor above in
98 // the same order
99 wxFileTypeInfo(const wxArrayString& sArray);
100
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; }
131 int GetExtensionsCount() const {return m_exts.GetCount(); }
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
149
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);
158
159 // ----------------------------------------------------------------------------
160 // wxFileType: gives access to all information about the files of given type.
161 //
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!
168 // ----------------------------------------------------------------------------
169
170 class WXDLLEXPORT wxFileType
171 {
172 friend class WXDLLEXPORT wxMimeTypesManagerImpl; // it has access to m_impl
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() { }
184 MessageParameters(const wxString& filename,
185 const wxString& mimetype = _T(""))
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
195 virtual wxString GetParamValue(const wxString& WXUNUSED(name)) const
196 { return wxEmptyString; }
197
198 // virtual dtor as in any base class
199 virtual ~MessageParameters() { }
200
201 protected:
202 wxString m_filename, m_mimetype;
203 };
204
205 // ctor from static data
206 wxFileType(const wxFileTypeInfo& ftInfo);
207
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;
213 bool GetMimeTypes(wxArrayString& mimeTypes) const;
214 // fill passed in array with all extensions associated with this file
215 // type
216 bool GetExtensions(wxArrayString& extensions);
217 // get the icon corresponding to this file type and of the given size
218 bool GetIcon(wxIconLocation *iconloc) const;
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;
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;
229 // get the command to print the file of given type
230 bool GetPrintCommand(wxString *printCmd,
231 const MessageParameters& params) const;
232
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
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
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
249 bool Unassociate();
250
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
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;
272
273 // the object which implements the real stuff like reading and writing
274 // to/from system MIME database
275 wxFileTypeImpl *m_impl;
276 };
277
278 // ----------------------------------------------------------------------------
279 // wxMimeTypesManager: interface to system MIME database.
280 //
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.
284 // ----------------------------------------------------------------------------
285
286 class WXDLLEXPORT wxMimeTypesManager
287 {
288 public:
289 // static helper functions
290 // -----------------------
291
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
295 // returns TRUE, otherwise it returns FALSE
296 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
297
298 // ctor
299 wxMimeTypesManager();
300
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
309 void Initialize(int mailcapStyle = wxMAILCAP_ALL,
310 const wxString& extraDir = wxEmptyString);
311
312 // and this function clears all the data from the manager
313 void ClearData();
314
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
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)
326 //
327 // FIXME: These ought to be private ??
328
329 // read in additional file (the standard ones are read automatically)
330 // in mailcap format (see mimetype.cpp for description)
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
336
337 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
338 // read in additional file in mime.types format
339 bool ReadMimeTypes(const wxString& filename);
340
341 // enumerate all known MIME types
342 //
343 // returns the number of retrieved file types
344 size_t EnumAllFileTypes(wxArrayString& mimetypes);
345
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 //
351 // The filetypes array should be terminated by either NULL entry or an
352 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
353 void AddFallbacks(const wxFileTypeInfo *filetypes);
354 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
355
356 // create or remove associations
357
358 // create a new association using the fields of wxFileTypeInfo (at least
359 // the MIME type and the extension should be set)
360 // if the other fields are empty, the existing values should be left alone
361 wxFileType *Associate(const wxFileTypeInfo& ftInfo);
362
363 // undo Associate()
364 bool Unassociate(wxFileType *ft) ;
365
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
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
379 wxMimeTypesManagerImpl *m_impl;
380
381 // if m_impl is NULL, create one
382 void EnsureImpl();
383
384 friend class wxMimeTypeCmnModule;
385 };
386
387
388 // ----------------------------------------------------------------------------
389 // global variables
390 // ----------------------------------------------------------------------------
391
392 // the default mime manager for wxWindows programs
393 WXDLLEXPORT_DATA(extern wxMimeTypesManager *) wxTheMimeTypesManager;
394
395 #endif // wxUSE_MIMETYPE
396
397 #endif
398 //_WX_MIMETYPE_H_