]>
Commit | Line | Data |
---|---|---|
1cd04aa2 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/mimetype.cpp | |
3 | // Purpose: classes and functions to manage MIME types | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 01.21.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: Adopted from msw port --(c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license (part of wxExtra library) | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #define INCL_DOS | |
13 | #define INCL_GPI | |
14 | #define INCL_WIN | |
15 | #include <os2.h> | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/string.h" | |
19 | #if wxUSE_GUI | |
20 | #include "wx/icon.h" | |
21 | #endif | |
22 | #endif //WX_PRECOMP | |
23 | ||
24 | #include "wx/log.h" | |
25 | #include "wx/file.h" | |
26 | #include "wx/intl.h" | |
27 | #include "wx/dynarray.h" | |
28 | #include "wx/confbase.h" | |
29 | ||
30 | #include "wx/os2/mimetype.h" | |
31 | ||
32 | // other standard headers | |
33 | #include <ctype.h> | |
34 | ||
35 | // in case we're compiling in non-GUI mode | |
36 | class WXDLLEXPORT wxIcon; | |
37 | ||
38 | // These classes use Windows registry to retrieve the required information. | |
39 | // | |
40 | // Keys used (not all of them are documented, so it might actually stop working | |
41 | // in futur versions of Windows...): | |
42 | // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME | |
43 | // types, each key has a string value "Extension" which gives (dot preceded) | |
44 | // extension for the files of this MIME type. | |
45 | // | |
46 | // 2. "HKCR\.ext" contains | |
47 | // a) unnamed value containing the "filetype" | |
48 | // b) value "Content Type" containing the MIME type | |
49 | // | |
50 | // 3. "HKCR\filetype" contains | |
51 | // a) unnamed value containing the description | |
52 | // b) subkey "DefaultIcon" with single unnamed value giving the icon index in | |
53 | // an icon file | |
54 | // c) shell\open\command and shell\open\print subkeys containing the commands | |
55 | // to open/print the file (the positional parameters are introduced by %1, | |
56 | // %2, ... in these strings, we change them to %s ourselves) | |
57 | ||
58 | // although I don't know of any official documentation which mentions this | |
59 | // location, uses it, so it isn't likely to change | |
60 | static const wxChar *MIME_DATABASE_KEY = wxT("MIME\\Database\\Content Type\\"); | |
61 | ||
62 | wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const | |
63 | { | |
64 | // TODO: OS/2 doesn't have a registry but uses Prf | |
65 | /* | |
66 | // suppress possible error messages | |
67 | wxLogNull nolog; | |
68 | wxString strKey; | |
69 | ||
70 | if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() ) | |
71 | strKey = m_ext; | |
72 | if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() ) | |
73 | strKey = m_strFileType; | |
74 | ||
75 | if ( !strKey ) | |
76 | { | |
77 | // no info | |
78 | return wxEmptyString; | |
79 | } | |
80 | ||
81 | strKey << wxT("\\shell\\") << verb; | |
82 | wxRegKey key(wxRegKey::HKCR, strKey + _T("\\command")); | |
83 | wxString command; | |
84 | if ( key.Open() ) { | |
85 | // it's the default value of the key | |
86 | if ( key.QueryValue(wxT(""), command) ) { | |
87 | // transform it from '%1' to '%s' style format string (now also | |
88 | // test for %L - apparently MS started using it as well for the | |
89 | // same purpose) | |
90 | ||
91 | // NB: we don't make any attempt to verify that the string is valid, | |
92 | // i.e. doesn't contain %2, or second %1 or .... But we do make | |
93 | // sure that we return a string with _exactly_ one '%s'! | |
94 | bool foundFilename = FALSE; | |
95 | size_t len = command.Len(); | |
96 | for ( size_t n = 0; (n < len) && !foundFilename; n++ ) { | |
97 | if ( command[n] == wxT('%') && | |
98 | (n + 1 < len) && | |
99 | (command[n + 1] == wxT('1') || | |
100 | command[n + 1] == wxT('L')) ) { | |
101 | // replace it with '%s' | |
102 | command[n + 1] = wxT('s'); | |
103 | ||
104 | foundFilename = TRUE; | |
105 | } | |
106 | } | |
107 | ||
108 | #if wxUSE_DDE | |
109 | // look whether we must issue some DDE requests to the application | |
110 | // (and not just launch it) | |
111 | strKey += _T("\\DDEExec"); | |
112 | wxRegKey keyDDE(wxRegKey::HKCR, strKey); | |
113 | if ( keyDDE.Open() ) { | |
114 | wxString ddeCommand, ddeServer, ddeTopic; | |
115 | keyDDE.QueryValue(_T(""), ddeCommand); | |
116 | ddeCommand.Replace(_T("%1"), _T("%s")); | |
117 | ||
118 | wxRegKey(wxRegKey::HKCR, strKey + _T("\\Application")). | |
119 | QueryValue(_T(""), ddeServer); | |
120 | wxRegKey(wxRegKey::HKCR, strKey + _T("\\Topic")). | |
121 | QueryValue(_T(""), ddeTopic); | |
122 | ||
123 | // HACK: we use a special feature of wxExecute which exists | |
124 | // just because we need it here: it will establish DDE | |
125 | // conversation with the program it just launched | |
126 | command.Prepend(_T("WX_DDE#")); | |
127 | command << _T('#') << ddeServer | |
128 | << _T('#') << ddeTopic | |
129 | << _T('#') << ddeCommand; | |
130 | } | |
131 | else | |
132 | #endif // wxUSE_DDE | |
133 | if ( !foundFilename ) { | |
134 | // we didn't find any '%1' - the application doesn't know which | |
135 | // file to open (note that we only do it if there is no DDEExec | |
136 | // subkey) | |
137 | // | |
138 | // HACK: append the filename at the end, hope that it will do | |
139 | command << wxT(" %s"); | |
140 | } | |
141 | } | |
142 | } | |
143 | //else: no such file type or no value, will return empty string | |
144 | ||
145 | return command; | |
146 | */ | |
147 | return wxEmptyString; | |
148 | } | |
149 | ||
150 | bool | |
151 | wxFileTypeImpl::GetOpenCommand(wxString *openCmd, | |
152 | const wxFileType::MessageParameters& params) | |
153 | const | |
154 | { | |
155 | wxString cmd; | |
156 | if ( m_info ) { | |
157 | cmd = m_info->GetOpenCommand(); | |
158 | } | |
159 | else { | |
160 | cmd = GetCommand(wxT("open")); | |
161 | } | |
162 | ||
163 | *openCmd = wxFileType::ExpandCommand(cmd, params); | |
164 | ||
165 | return !openCmd->IsEmpty(); | |
166 | } | |
167 | ||
168 | bool | |
169 | wxFileTypeImpl::GetPrintCommand(wxString *printCmd, | |
170 | const wxFileType::MessageParameters& params) | |
171 | const | |
172 | { | |
173 | wxString cmd; | |
174 | if ( m_info ) { | |
175 | cmd = m_info->GetPrintCommand(); | |
176 | } | |
177 | else { | |
178 | cmd = GetCommand(wxT("print")); | |
179 | } | |
180 | ||
181 | *printCmd = wxFileType::ExpandCommand(cmd, params); | |
182 | ||
183 | return !printCmd->IsEmpty(); | |
184 | } | |
185 | ||
186 | // TODO this function is half implemented | |
187 | bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) | |
188 | { | |
189 | if ( m_info ) { | |
190 | extensions = m_info->GetExtensions(); | |
191 | ||
192 | return TRUE; | |
193 | } | |
194 | else if ( m_ext.IsEmpty() ) { | |
195 | // the only way to get the list of extensions from the file type is to | |
196 | // scan through all extensions in the registry - too slow... | |
197 | return FALSE; | |
198 | } | |
199 | else { | |
200 | extensions.Empty(); | |
201 | extensions.Add(m_ext); | |
202 | ||
203 | // it's a lie too, we don't return _all_ extensions... | |
204 | return TRUE; | |
205 | } | |
206 | } | |
207 | ||
208 | bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const | |
209 | { | |
210 | if ( m_info ) { | |
211 | // we already have it | |
212 | *mimeType = m_info->GetMimeType(); | |
213 | ||
214 | return TRUE; | |
215 | } | |
216 | ||
217 | // suppress possible error messages | |
218 | wxLogNull nolog; | |
219 | // TODO: substitue reg key stuff (maybe make a Prf class for OS/2??) | |
220 | /* | |
221 | wxRegKey key(wxRegKey::HKCR, wxT(".") + m_ext); | |
222 | if ( key.Open() && key.QueryValue(wxT("Content Type"), *mimeType) ) { | |
223 | return TRUE; | |
224 | } | |
225 | else { | |
226 | return FALSE; | |
227 | } | |
228 | */ | |
229 | return FALSE; | |
230 | } | |
231 | ||
232 | bool wxFileTypeImpl::GetIcon(wxIcon *icon) const | |
233 | { | |
234 | #if wxUSE_GUI | |
235 | if ( m_info ) { | |
236 | // we don't have icons in the fallback resources | |
237 | return FALSE; | |
238 | } | |
239 | ||
240 | wxString strIconKey; | |
241 | strIconKey << m_strFileType << wxT("\\DefaultIcon"); | |
242 | ||
243 | // suppress possible error messages | |
244 | wxLogNull nolog; | |
245 | //TODO: | |
246 | /* | |
247 | wxRegKey key(wxRegKey::HKCR, strIconKey); | |
248 | ||
249 | if ( key.Open() ) { | |
250 | wxString strIcon; | |
251 | // it's the default value of the key | |
252 | if ( key.QueryValue(wxT(""), strIcon) ) { | |
253 | // the format is the following: <full path to file>, <icon index> | |
254 | // NB: icon index may be negative as well as positive and the full | |
255 | // path may contain the environment variables inside '%' | |
256 | wxString strFullPath = strIcon.BeforeLast(wxT(',')), | |
257 | strIndex = strIcon.AfterLast(wxT(',')); | |
258 | ||
259 | // index may be omitted, in which case BeforeLast(',') is empty and | |
260 | // AfterLast(',') is the whole string | |
261 | if ( strFullPath.IsEmpty() ) { | |
262 | strFullPath = strIndex; | |
263 | strIndex = wxT("0"); | |
264 | } | |
265 | ||
266 | wxString strExpPath = wxExpandEnvVars(strFullPath); | |
267 | int nIndex = wxAtoi(strIndex); | |
268 | ||
269 | HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex); | |
270 | switch ( (int)hIcon ) { | |
271 | case 0: // means no icons were found | |
272 | case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/... | |
273 | wxLogDebug(wxT("incorrect registry entry '%s': no such icon."), | |
274 | key.GetName().c_str()); | |
275 | break; | |
276 | ||
277 | default: | |
278 | icon->SetHICON((WXHICON)hIcon); | |
279 | return TRUE; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | // no such file type or no value or incorrect icon entry | |
285 | */ | |
286 | #endif // wxUSE_GUI | |
287 | return FALSE; | |
288 | } | |
289 | ||
290 | bool wxFileTypeImpl::GetDescription(wxString *desc) const | |
291 | { | |
292 | if ( m_info ) { | |
293 | // we already have it | |
294 | *desc = m_info->GetDescription(); | |
295 | ||
296 | return TRUE; | |
297 | } | |
298 | ||
299 | // suppress possible error messages | |
300 | wxLogNull nolog; | |
301 | // TODO: | |
302 | /* | |
303 | wxRegKey key(wxRegKey::HKCR, m_strFileType); | |
304 | ||
305 | if ( key.Open() ) { | |
306 | // it's the default value of the key | |
307 | if ( key.QueryValue(wxT(""), *desc) ) { | |
308 | return TRUE; | |
309 | } | |
310 | } | |
311 | */ | |
312 | return FALSE; | |
313 | } | |
314 | ||
315 | // extension -> file type | |
316 | wxFileType * | |
317 | wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) | |
318 | { | |
319 | // add the leading point if necessary | |
320 | wxString str; | |
a4372af6 | 321 | if ( ext[(size_t) 0] != wxT('.') ) { |
1cd04aa2 DW |
322 | str = wxT('.'); |
323 | } | |
324 | str << ext; | |
325 | ||
326 | // suppress possible error messages | |
327 | wxLogNull nolog; | |
328 | ||
329 | bool knownExtension = FALSE; | |
330 | ||
331 | wxString strFileType; | |
332 | // TODO: | |
333 | /* | |
334 | wxRegKey key(wxRegKey::HKCR, str); | |
335 | if ( key.Open() ) { | |
336 | // it's the default value of the key | |
337 | if ( key.QueryValue(wxT(""), strFileType) ) { | |
338 | // create the new wxFileType object | |
339 | wxFileType *fileType = new wxFileType; | |
340 | fileType->m_impl->Init(strFileType, ext); | |
341 | ||
342 | return fileType; | |
343 | } | |
344 | else { | |
345 | // this extension doesn't have a filetype, but it's known to the | |
346 | // system and may be has some other useful keys (open command or | |
347 | // content-type), so still return a file type object for it | |
348 | knownExtension = TRUE; | |
349 | } | |
350 | } | |
351 | */ | |
352 | // check the fallbacks | |
353 | // TODO linear search is potentially slow, perhaps we should use a sorted | |
354 | // array? | |
355 | size_t count = m_fallbacks.GetCount(); | |
356 | for ( size_t n = 0; n < count; n++ ) { | |
357 | if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { | |
358 | wxFileType *fileType = new wxFileType; | |
359 | fileType->m_impl->Init(m_fallbacks[n]); | |
360 | ||
361 | return fileType; | |
362 | } | |
363 | } | |
364 | ||
365 | if ( knownExtension ) | |
366 | { | |
367 | wxFileType *fileType = new wxFileType; | |
368 | fileType->m_impl->Init(wxEmptyString, ext); | |
369 | ||
370 | return fileType; | |
371 | } | |
372 | else | |
373 | { | |
374 | // unknown extension | |
375 | return NULL; | |
376 | } | |
377 | } | |
378 | ||
379 | // MIME type -> extension -> file type | |
380 | wxFileType * | |
381 | wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) | |
382 | { | |
383 | wxString strKey = MIME_DATABASE_KEY; | |
384 | strKey << mimeType; | |
385 | ||
386 | // suppress possible error messages | |
387 | wxLogNull nolog; | |
388 | ||
389 | wxString ext; | |
390 | // TODO: | |
391 | /* | |
392 | wxRegKey key(wxRegKey::HKCR, strKey); | |
393 | if ( key.Open() ) { | |
394 | if ( key.QueryValue(wxT("Extension"), ext) ) { | |
395 | return GetFileTypeFromExtension(ext); | |
396 | } | |
397 | } | |
398 | ||
399 | // check the fallbacks | |
400 | // TODO linear search is potentially slow, perhaps we should use a sorted | |
401 | // array? | |
402 | size_t count = m_fallbacks.GetCount(); | |
403 | for ( size_t n = 0; n < count; n++ ) { | |
404 | if ( wxMimeTypesManager::IsOfType(mimeType, | |
405 | m_fallbacks[n].GetMimeType()) ) { | |
406 | wxFileType *fileType = new wxFileType; | |
407 | fileType->m_impl->Init(m_fallbacks[n]); | |
408 | ||
409 | return fileType; | |
410 | } | |
411 | } | |
412 | */ | |
413 | // unknown MIME type | |
414 | return NULL; | |
415 | } | |
416 | ||
417 | size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) | |
418 | { | |
419 | // enumerate all keys under MIME_DATABASE_KEY | |
420 | // TODO: | |
421 | /* | |
422 | wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); | |
423 | ||
424 | wxString type; | |
425 | long cookie; | |
426 | bool cont = key.GetFirstKey(type, cookie); | |
427 | while ( cont ) | |
428 | { | |
429 | mimetypes.Add(type); | |
430 | ||
431 | cont = key.GetNextKey(type, cookie); | |
432 | } | |
433 | ||
434 | return mimetypes.GetCount(); | |
435 | */ | |
436 | return 0; | |
437 | } | |
438 |