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