]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: mimetype.h | |
e54c96f1 | 3 | // Purpose: interface of wxMimeTypesManager |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxMimeTypesManager | |
7c913512 | 11 | |
23324ae1 FM |
12 | This class allows the application to retrieve the information about all known |
13 | MIME types from a system-specific location and the filename extensions to the | |
14 | MIME types and vice versa. After initialization the functions | |
f369c7c2 | 15 | GetFileTypeFromMimeType() and GetFileTypeFromExtension() |
ba1d7a6c FM |
16 | may be called: they will return a wxFileType object which may be further |
17 | queried for file description, icon and other attributes. | |
18 | ||
19 | Under Windows, the MIME type information is queried from registry. | |
20 | Under Linux and Unix, it is queried from the XDG data directories. | |
7c913512 | 21 | |
f369c7c2 | 22 | Currently, wxMimeTypesManager is limited to reading MIME type information. |
7c913512 | 23 | |
ba1d7a6c | 24 | The application should not construct its own manager: it should use the |
f369c7c2 | 25 | object pointer ::wxTheMimeTypesManger. |
7c913512 | 26 | |
ba1d7a6c FM |
27 | |
28 | @section mimetypemanager_helpers Helper functions | |
29 | ||
30 | All of these functions are static (i.e. don't need a wxMimeTypesManager object | |
31 | to call them) and provide some useful operations for string representations of | |
32 | MIME types. Their usage is recommended instead of directly working with MIME | |
33 | types using wxString functions. | |
34 | ||
35 | - wxMimeTypesManager::IsOfType() | |
36 | ||
37 | ||
38 | @section mimetypemanager_ctor Constructor and destructor | |
39 | ||
40 | NB: You won't normally need to use more than one wxMimeTypesManager object | |
41 | in a program. | |
42 | ||
43 | - wxMimeTypesManager::wxMimeTypesManager() | |
44 | - wxMimeTypesManager::~wxMimeTypesManager() | |
45 | ||
46 | ||
47 | @section mimetypemanager_query Query database | |
48 | ||
49 | These functions are the heart of this class: they allow to find a file type | |
50 | object from either file extension or MIME type. | |
51 | If the function is successful, it returns a pointer to the wxFileType object | |
52 | which must be deleted by the caller, otherwise NULL will be returned. | |
53 | ||
54 | - wxMimeTypesManager::GetFileTypeFromMimeType() | |
55 | - wxMimeTypesManager::GetFileTypeFromExtension() | |
56 | ||
57 | ||
58 | @section mimetypemanager_init Initialization functions | |
59 | ||
60 | Unix: These functions may be used to load additional files (except for the | |
61 | default ones which are loaded automatically) containing MIME information in | |
62 | either mailcap(5) or mime.types(5) format. | |
63 | ||
64 | - wxMimeTypesManager::ReadMailcap() | |
65 | - wxMimeTypesManager::ReadMimeTypes() | |
66 | - wxMimeTypesManager::AddFallbacks() | |
67 | ||
68 | ||
69 | ||
23324ae1 FM |
70 | @library{wxbase} |
71 | @category{misc} | |
7c913512 | 72 | |
e54c96f1 | 73 | @see wxFileType |
23324ae1 | 74 | */ |
7c913512 | 75 | class wxMimeTypesManager |
23324ae1 FM |
76 | { |
77 | public: | |
78 | /** | |
f369c7c2 | 79 | Constructor puts the object in the "working" state. |
23324ae1 FM |
80 | */ |
81 | wxMimeTypesManager(); | |
82 | ||
83 | /** | |
84 | Destructor is not virtual, so this class should not be derived from. | |
85 | */ | |
86 | ~wxMimeTypesManager(); | |
87 | ||
88 | /** | |
89 | This function may be used to provide hard-wired fallbacks for the MIME types | |
90 | and extensions that might not be present in the system MIME database. | |
23324ae1 FM |
91 | Please see the typetest sample for an example of using it. |
92 | */ | |
4cc4bfaf | 93 | void AddFallbacks(const wxFileTypeInfo* fallbacks); |
23324ae1 | 94 | |
23324ae1 FM |
95 | /** |
96 | Gather information about the files with given extension and return the | |
f369c7c2 | 97 | corresponding wxFileType object or @NULL if the extension is unknown. |
ba1d7a6c | 98 | |
4cc4bfaf | 99 | The @a extension parameter may have, or not, the leading dot, if it has it, |
23324ae1 FM |
100 | it is stripped automatically. It must not however be empty. |
101 | */ | |
102 | wxFileType* GetFileTypeFromExtension(const wxString& extension); | |
103 | ||
104 | /** | |
105 | Gather information about the files with given MIME type and return the | |
f369c7c2 | 106 | corresponding wxFileType object or @NULL if the MIME type is unknown. |
23324ae1 FM |
107 | */ |
108 | wxFileType* GetFileTypeFromMimeType(const wxString& mimeType); | |
109 | ||
23324ae1 FM |
110 | |
111 | /** | |
ba1d7a6c FM |
112 | This function returns @true if either the given @a mimeType is exactly |
113 | the same as @a wildcard or if it has the same category and the subtype of | |
4cc4bfaf FM |
114 | @a wildcard is '*'. Note that the '*' wildcard is not allowed in |
115 | @a mimeType itself. | |
ba1d7a6c | 116 | |
23324ae1 FM |
117 | The comparison don by this function is case insensitive so it is not |
118 | necessary to convert the strings to the same case before calling it. | |
119 | */ | |
f369c7c2 | 120 | static bool IsOfType(const wxString& mimeType, const wxString& wildcard); |
23324ae1 FM |
121 | }; |
122 | ||
123 | ||
f369c7c2 RR |
124 | /** |
125 | The global wxMimeTypesManager instance. | |
126 | */ | |
127 | wxMimeTypesManager* wxTheMimeTypesManager; | |
128 | ||
e54c96f1 | 129 | |
23324ae1 FM |
130 | /** |
131 | @class wxFileType | |
7c913512 | 132 | |
ba1d7a6c FM |
133 | This class holds information about a given @e file type. |
134 | ||
135 | File type is the same as MIME type under Unix, but under Windows it corresponds | |
136 | more to an extension than to MIME type (in fact, several extensions may | |
137 | correspond to a file type). | |
138 | ||
139 | This object may be created in several different ways: the program might know the | |
140 | file extension and wish to find out the corresponding MIME type or, conversely, it | |
23324ae1 FM |
141 | might want to find the right extension for the file to which it writes the |
142 | contents of given MIME type. Depending on how it was created some fields may be | |
143 | unknown so the return value of all the accessors @b must be checked: @false | |
144 | will be returned if the corresponding information couldn't be found. | |
7c913512 | 145 | |
23324ae1 | 146 | The objects of this class are never created by the application code but are |
7c913512 | 147 | returned by wxMimeTypesManager::GetFileTypeFromMimeType and |
23324ae1 FM |
148 | wxMimeTypesManager::GetFileTypeFromExtension methods. |
149 | But it is your responsibility to delete the returned pointer when you're done | |
150 | with it! | |
7c913512 | 151 | |
23324ae1 FM |
152 | A brief reminder about what the MIME types are (see the RFC 1341 for more |
153 | information): basically, it is just a pair category/type (for example, | |
154 | "text/plain") where the category is a basic indication of what a file is. | |
155 | Examples of categories are "application", "image", "text", "binary", and | |
156 | type is a precise definition of the document format: "plain" in the example | |
157 | above means just ASCII text without any formatting, while "text/html" is the | |
158 | HTML document source. | |
7c913512 | 159 | |
23324ae1 FM |
160 | A MIME type may have one or more associated extensions: "text/plain" will |
161 | typically correspond to the extension ".txt", but may as well be associated with | |
162 | ".ini" or ".conf". | |
7c913512 | 163 | |
ba1d7a6c FM |
164 | |
165 | @section filetype_example MessageParameters class | |
166 | ||
167 | One of the most common usages of MIME is to encode an e-mail message. | |
168 | The MIME type of the encoded message is an example of a message parameter. | |
169 | These parameters are found in the message headers ("Content-XXX"). | |
170 | ||
171 | At the very least, they must specify the MIME type and the version of MIME | |
172 | used, but almost always they provide additional information about the message | |
173 | such as the original file name or the charset (for the text documents). | |
174 | These parameters may be useful to the program used to open, edit, view or | |
175 | print the message, so, for example, an e-mail client program will have to | |
176 | pass them to this program. Because wxFileType itself can not know about | |
177 | these parameters, it uses MessageParameters class to query them. | |
178 | ||
179 | The default implementation only requires the caller to provide the file name | |
180 | (always used by the program to be called - it must know which file to open) | |
181 | and the MIME type and supposes that there are no other parameters. | |
182 | ||
183 | If you wish to supply additional parameters, you must derive your own class | |
184 | from MessageParameters and override GetParamValue() function, for example: | |
185 | ||
186 | @code | |
187 | // provide the message parameters for the MIME type manager | |
188 | class MailMessageParameters : public wxFileType::MessageParameters | |
189 | { | |
190 | public: | |
191 | MailMessageParameters(const wxString& filename, | |
192 | const wxString& mimetype) | |
193 | : wxFileType::MessageParameters(filename, mimetype) | |
194 | { | |
195 | } | |
196 | ||
197 | virtual wxString GetParamValue(const wxString& name) const | |
198 | { | |
199 | // parameter names are not case-sensitive | |
200 | if ( name.CmpNoCase("charset") == 0 ) | |
201 | return "US-ASCII"; | |
202 | else | |
203 | return wxFileType::MessageParameters::GetParamValue(name); | |
204 | } | |
205 | }; | |
206 | @endcode | |
207 | ||
208 | Now you only need to create an object of this class and pass it to, for example, | |
209 | GetOpenCommand like this: | |
210 | ||
211 | @code | |
212 | wxString command; | |
213 | if ( filetype->GetOpenCommand(&command, | |
214 | MailMessageParameters("foo.txt", "text/plain")) ) | |
215 | { | |
216 | // the full command for opening the text documents is in 'command' | |
217 | // (it might be "notepad foo.txt" under Windows or "cat foo.txt" under Unix) | |
218 | } | |
219 | else | |
220 | { | |
221 | // we don't know how to handle such files... | |
222 | } | |
223 | @endcode | |
224 | ||
225 | Windows: As only the file name is used by the program associated with the | |
226 | given extension anyhow (but no other message parameters), there is no need | |
227 | to ever derive from MessageParameters class for a Windows-only program. | |
228 | ||
229 | ||
23324ae1 | 230 | @library{wxbase} |
ba1d7a6c | 231 | @category{misc} |
7c913512 | 232 | |
e54c96f1 | 233 | @see wxMimeTypesManager |
23324ae1 | 234 | */ |
7c913512 | 235 | class wxFileType |
23324ae1 | 236 | { |
8067ee11 | 237 | private: |
23324ae1 FM |
238 | /** |
239 | The default constructor is private because you should never create objects of | |
240 | this type: they are only returned by wxMimeTypesManager methods. | |
241 | */ | |
242 | wxFileType(); | |
243 | ||
8067ee11 FM |
244 | public: |
245 | /** | |
246 | Copy ctor. | |
247 | */ | |
248 | wxFileType(const wxFileTypeInfo& ftInfo); | |
249 | ||
23324ae1 FM |
250 | /** |
251 | The destructor of this class is not virtual, so it should not be derived from. | |
252 | */ | |
253 | ~wxFileType(); | |
254 | ||
255 | /** | |
256 | This function is primarily intended for GetOpenCommand and GetPrintCommand | |
ba1d7a6c FM |
257 | usage but may be also used by the application directly if, for example, you |
258 | want to use some non-default command to open the file. | |
3c4f71cc | 259 | |
ba1d7a6c FM |
260 | The function replaces all occurrences of: |
261 | - %s with the full file name | |
262 | - %t with the MIME type | |
263 | - %{param} with the value of the parameter @e param | |
23324ae1 | 264 | using the MessageParameters object you pass to it. |
ba1d7a6c | 265 | |
23324ae1 FM |
266 | If there is no '%s' in the command string (and the string is not empty), it is |
267 | assumed that the command reads the data on stdin and so the effect is the same | |
268 | as " %s" were appended to the string. | |
ba1d7a6c | 269 | |
23324ae1 FM |
270 | Unlike all other functions of this class, there is no error return for this |
271 | function. | |
272 | */ | |
273 | static wxString ExpandCommand(const wxString& command, | |
274 | MessageParameters& params); | |
275 | ||
276 | /** | |
4cc4bfaf | 277 | If the function returns @true, the string pointed to by @a desc is filled |
23324ae1 FM |
278 | with a brief description for this file type: for example, "text document" for |
279 | the "text/plain" MIME type. | |
280 | */ | |
adaaa686 | 281 | bool GetDescription(wxString* desc) const; |
23324ae1 FM |
282 | |
283 | /** | |
4cc4bfaf | 284 | If the function returns @true, the array @a extensions is filled |
23324ae1 | 285 | with all extensions associated with this file type: for example, it may |
ba1d7a6c FM |
286 | contain the following two elements for the MIME type "text/html" |
287 | (notice the absence of the leading dot): "html" and "htm". | |
288 | ||
23324ae1 | 289 | @b Windows: This function is currently not implemented: there is no |
ba1d7a6c FM |
290 | (efficient) way to retrieve associated extensions from the given MIME type |
291 | on this platform, so it will only return @true if the wxFileType object was | |
292 | created by wxMimeTypesManager::GetFileTypeFromExtension function in the | |
293 | first place. | |
23324ae1 FM |
294 | */ |
295 | bool GetExtensions(wxArrayString& extensions); | |
296 | ||
297 | /** | |
298 | If the function returns @true, the @c iconLoc is filled with the | |
ba1d7a6c FM |
299 | location of the icon for this MIME type. |
300 | A wxIcon may be created from @a iconLoc later. | |
301 | ||
23324ae1 FM |
302 | @b Windows: The function returns the icon shown by Explorer for the files of |
303 | the specified type. | |
ba1d7a6c | 304 | |
23324ae1 | 305 | @b Mac: This function is not implemented and always returns @false. |
ba1d7a6c | 306 | |
23324ae1 FM |
307 | @b Unix: MIME manager gathers information about icons from GNOME |
308 | and KDE settings and thus GetIcon's success depends on availability | |
309 | of these desktop environments. | |
310 | */ | |
adaaa686 | 311 | bool GetIcon(wxIconLocation* iconLoc) const; |
23324ae1 FM |
312 | |
313 | /** | |
4cc4bfaf | 314 | If the function returns @true, the string pointed to by @a mimeType is filled |
23324ae1 FM |
315 | with full MIME type specification for this file type: for example, "text/plain". |
316 | */ | |
adaaa686 | 317 | bool GetMimeType(wxString* mimeType) const; |
23324ae1 FM |
318 | |
319 | /** | |
ba1d7a6c FM |
320 | Same as GetMimeType() but returns array of MIME types. |
321 | ||
322 | This array will contain only one item in most cases but sometimes, | |
323 | notably under Unix with KDE, may contain more MIME types. | |
324 | This happens when one file extension is mapped to different MIME types | |
325 | by KDE, mailcap and mime.types. | |
23324ae1 | 326 | */ |
43c48e1e | 327 | bool GetMimeTypes(wxArrayString& mimeTypes) const; |
23324ae1 FM |
328 | |
329 | //@{ | |
330 | /** | |
331 | With the first version of this method, if the @true is returned, the | |
4cc4bfaf | 332 | string pointed to by @a command is filled with the command which must be |
ba1d7a6c FM |
333 | executed (see wxExecute()) in order to open the file of the given type. |
334 | ||
335 | In this case, the name of the file as well as any other parameters | |
336 | is retrieved from MessageParameters() class. | |
337 | ||
23324ae1 FM |
338 | In the second case, only the filename is specified and the command to be used |
339 | to open this kind of file is returned directly. An empty string is returned to | |
340 | indicate that an error occurred (typically meaning that there is no standard way | |
341 | to open this kind of files). | |
342 | */ | |
ba1d7a6c | 343 | bool GetOpenCommand(wxString* command, MessageParameters& params); |
7c913512 | 344 | wxString GetOpenCommand(const wxString& filename); |
23324ae1 FM |
345 | //@} |
346 | ||
347 | /** | |
4cc4bfaf | 348 | If the function returns @true, the string pointed to by @a command is filled |
ba1d7a6c FM |
349 | with the command which must be executed (see wxExecute()) in order to |
350 | print the file of the given type. | |
23324ae1 | 351 | |
ba1d7a6c | 352 | The name of the file is retrieved from the MessageParameters class. |
23324ae1 | 353 | */ |
43c48e1e FM |
354 | bool GetPrintCommand(wxString* command, |
355 | const MessageParameters& params) const; | |
23324ae1 | 356 | }; |
e54c96f1 | 357 |