added wxFileType::GetMimeTypes
[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 bool GetMimeTypes(wxArrayString& mimeTypes) const;
79 // fill passed in array with all extensions associated with this file
80 // type
81 bool GetExtensions(wxArrayString& extensions);
82 // get the icon corresponding to this file type
83 bool GetIcon(wxIcon *icon) const;
84 // get a brief file type description ("*.txt" => "text document")
85 bool GetDescription(wxString *desc) const;
86
87 // get the command to be used to open/print the given file.
88 // get the command to execute the file of given type
89 bool GetOpenCommand(wxString *openCmd,
90 const MessageParameters& params) const;
91 // get the command to print the file of given type
92 bool GetPrintCommand(wxString *printCmd,
93 const MessageParameters& params) const;
94
95 // operations
96 // expand a string in the format of GetOpenCommand (which may contain
97 // '%s' and '%t' format specificators for the file name and mime type
98 // and %{param} constructions).
99 static wxString ExpandCommand(const wxString& command,
100 const MessageParameters& params);
101
102 // dtor (not virtual, shouldn't be derived from)
103 ~wxFileType();
104
105 private:
106 // default ctor is private because the user code never creates us
107 wxFileType();
108
109 // no copy ctor/assignment operator
110 wxFileType(const wxFileType&);
111 wxFileType& operator=(const wxFileType&);
112
113 wxFileTypeImpl *m_impl;
114 };
115
116 // This class is only used wuth wxMimeTypesManager::AddFallbacks() and is meant
117 // just as the container for the wxFileType data.
118 class WXDLLEXPORT wxFileTypeInfo
119 {
120 public:
121 // ctors
122 // a normal item
123 wxFileTypeInfo(const char *mimeType,
124 const char *openCmd,
125 const char *printCmd,
126 const char *desc,
127 // the other parameters form a NULL terminated list of
128 // extensions
129 ...);
130
131 // invalid item - use this to terminate the array passed to
132 // wxMimeTypesManager::AddFallbacks
133 wxFileTypeInfo() { }
134
135 bool IsValid() const { return !m_mimeType.IsEmpty(); }
136
137 // accessors
138 // get the MIME type
139 const wxString& GetMimeType() const { return m_mimeType; }
140 // get the open command
141 const wxString& GetOpenCommand() const { return m_openCmd; }
142 // get the print command
143 const wxString& GetPrintCommand() const { return m_printCmd; }
144 // get the description
145 const wxString& GetDescription() const { return m_desc; }
146 // get the array of all extensions
147 const wxArrayString& GetExtensions() const { return m_exts; }
148
149 private:
150 wxString m_mimeType, // the MIME type in "type/subtype" form
151 m_openCmd, // command to use for opening the file (%s allowed)
152 m_printCmd, // command to use for printing the file (%s allowed)
153 m_desc; // a free form description of this file type
154
155 wxArrayString m_exts; // the extensions which are mapped on this filetype
156 };
157
158 WX_DECLARE_EXPORTED_OBJARRAY(wxFileTypeInfo, wxArrayFileTypeInfo);
159
160
161 // This class accesses the information about all known MIME types and allows
162 // the application to retrieve information (including how to handle data of
163 // given type) about them.
164 //
165 // NB: currently it doesn't support modifying MIME database (read-only access).
166 class WXDLLEXPORT wxMimeTypesManager
167 {
168 public:
169 // static helper functions
170 // -----------------------
171
172 // check if the given MIME type is the same as the other one: the second
173 // argument may contain wildcards ('*'), but not the first. If the
174 // types are equal or if the mimeType matches wildcard the function
175 // returns TRUE, otherwise it returns FALSE
176 static bool IsOfType(const wxString& mimeType, const wxString& wildcard);
177
178 // ctor
179 wxMimeTypesManager();
180
181 // Database lookup: all functions return a pointer to wxFileType object
182 // whose methods may be used to query it for the information you're
183 // interested in. If the return value is !NULL, caller is responsible for
184 // deleting it.
185 // get file type from file extension
186 wxFileType *GetFileTypeFromExtension(const wxString& ext);
187 // get file type from MIME type (in format <category>/<format>)
188 wxFileType *GetFileTypeFromMimeType(const wxString& mimeType);
189
190 // other operations: return TRUE if there were no errors or FALSE if there
191 // were some unreckognized entries (the good entries are always read anyhow)
192 // read in additional file (the standard ones are read automatically)
193 // in mailcap format (see mimetype.cpp for description)
194 //
195 // 'fallback' parameter may be set to TRUE to avoid overriding the
196 // settings from other, previously parsed, files by this one: normally,
197 // the files read most recently would override the older files, but with
198 // fallback == TRUE this won't happen
199 bool ReadMailcap(const wxString& filename, bool fallback = FALSE);
200 // read in additional file in mime.types format
201 bool ReadMimeTypes(const wxString& filename);
202
203 // enumerate all known MIME types
204 //
205 // returns the number of retrieved file types
206 size_t EnumAllFileTypes(wxArrayString& mimetypes);
207
208 // these functions can be used to provide default values for some of the
209 // MIME types inside the program itself (you may also use
210 // ReadMailcap(filenameWithDefaultTypes, TRUE /* use as fallback */) to
211 // achieve the same goal, but this requires having this info in a file).
212 //
213 // It isn't possible (currently) to provide fallback icons using this
214 // function.
215 //
216 // The filetypes array should be terminated by a NULL entry
217 void AddFallbacks(const wxFileTypeInfo *filetypes);
218
219 // dtor (not virtual, shouldn't be derived from)
220 ~wxMimeTypesManager();
221
222 private:
223 // no copy ctor/assignment operator
224 wxMimeTypesManager(const wxMimeTypesManager&);
225 wxMimeTypesManager& operator=(const wxMimeTypesManager&);
226
227 wxMimeTypesManagerImpl *m_impl;
228
229 // if m_impl is NULL, create one
230 void EnsureImpl();
231 };
232
233
234 // ----------------------------------------------------------------------------
235 // global variables
236 // ----------------------------------------------------------------------------
237
238 // the default mime manager for wxWindows programs
239 WXDLLEXPORT_DATA(extern wxMimeTypesManager *) wxTheMimeTypesManager;
240
241 #endif
242 // wxUSE_FILE
243
244 #endif
245 //_MIMETYPE_H
246
247 /* vi: set cin tw=80 ts=4 sw=4: */