]> git.saurik.com Git - wxWidgets.git/blob - include/wx/mimetype.h
refresh cache on size/margins change; expanded cache to contain N elements, not just one
[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 class WXDLLEXPORT wxIcon;
34 class WXDLLEXPORT wxFileTypeImpl;
35 class WXDLLEXPORT wxMimeTypesManagerImpl;
36
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
49 #define wxICON_LARGE 0
50 #define wxICON_SMALL 1
51
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
87 wxFileTypeInfo(const wxChar *mimeType,
88 const wxChar *openCmd,
89 const wxChar *printCmd,
90 const wxChar *desc,
91 // the other parameters form a NULL terminated list of
92 // extensions
93 ...);
94
95 // the array elements correspond to the parameters of the ctor above in
96 // the same order
97 wxFileTypeInfo(const wxArrayString& sArray);
98
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; }
129 int GetExtensionsCount() const {return m_exts.GetCount(); }
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
147
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);
156
157 // ----------------------------------------------------------------------------
158 // wxFileType: gives access to all information about the files of given type.
159 //
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!
166 // ----------------------------------------------------------------------------
167
168 class WXDLLEXPORT wxFileType
169 {
170 friend class WXDLLEXPORT wxMimeTypesManagerImpl; // it has access to m_impl
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() { }
182 MessageParameters(const wxString& filename,
183 const wxString& mimetype = _T(""))
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
193 virtual wxString GetParamValue(const wxString& WXUNUSED(name)) const
194 { return wxEmptyString; }
195
196 // virtual dtor as in any base class
197 virtual ~MessageParameters() { }
198
199 protected:
200 wxString m_filename, m_mimetype;
201 };
202
203 // ctor from static data
204 wxFileType(const wxFileTypeInfo& ftInfo);
205
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;
211 bool GetMimeTypes(wxArrayString& mimeTypes) const;
212 // fill passed in array with all extensions associated with this file
213 // type
214 bool GetExtensions(wxArrayString& extensions);
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,
220 int *iconIndex = NULL,
221 int iconSize = wxICON_LARGE) const;
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;
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;
232 // get the command to print the file of given type
233 bool GetPrintCommand(wxString *printCmd,
234 const MessageParameters& params) const;
235
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
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
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
252 bool Unassociate();
253
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
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;
275
276 // the object which implements the real stuff like reading and writing
277 // to/from system MIME database
278 wxFileTypeImpl *m_impl;
279 };
280
281 // ----------------------------------------------------------------------------
282 // wxMimeTypesManager: interface to system MIME database.
283 //
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.
287 // ----------------------------------------------------------------------------
288
289 class WXDLLEXPORT wxMimeTypesManager
290 {
291 public:
292 // static helper functions
293 // -----------------------
294
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
298 // returns TRUE, otherwise it returns FALSE
299 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
300
301 // ctor
302 wxMimeTypesManager();
303
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
312 void Initialize(int mailcapStyle = wxMAILCAP_ALL,
313 const wxString& extraDir = wxEmptyString);
314
315 // and this function clears all the data from the manager
316 void ClearData();
317
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
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)
329 //
330 // FIXME: These ought to be private ??
331
332 // read in additional file (the standard ones are read automatically)
333 // in mailcap format (see mimetype.cpp for description)
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
339
340 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
341 // read in additional file in mime.types format
342 bool ReadMimeTypes(const wxString& filename);
343
344 // enumerate all known MIME types
345 //
346 // returns the number of retrieved file types
347 size_t EnumAllFileTypes(wxArrayString& mimetypes);
348
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 //
354 // The filetypes array should be terminated by either NULL entry or an
355 // invalid wxFileTypeInfo (i.e. the one created with default ctor)
356 void AddFallbacks(const wxFileTypeInfo *filetypes);
357 void AddFallback(const wxFileTypeInfo& ft) { m_fallbacks.Add(ft); }
358
359 // create or remove associations
360
361 // create a new association using the fields of wxFileTypeInfo (at least
362 // the MIME type and the extension should be set)
363 // if the other fields are empty, the existing values should be left alone
364 wxFileType *Associate(const wxFileTypeInfo& ftInfo);
365
366 // undo Associate()
367 bool Unassociate(wxFileType *ft) ;
368
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
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
382 wxMimeTypesManagerImpl *m_impl;
383
384 // if m_impl is NULL, create one
385 void EnsureImpl();
386
387 friend class wxMimeTypeCmnModule;
388 };
389
390
391 // ----------------------------------------------------------------------------
392 // global variables
393 // ----------------------------------------------------------------------------
394
395 // the default mime manager for wxWindows programs
396 WXDLLEXPORT_DATA(extern wxMimeTypesManager *) wxTheMimeTypesManager;
397
398 #endif // wxUSE_MIMETYPE
399
400 #endif
401 //_WX_MIMETYPE_H_