]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/mimetype.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/mimetype.cpp
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mimetype.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 // Doesn't compile in WIN16 mode
27 #include "wx/string.h"
36 #include "wx/dynarray.h"
37 #include "wx/confbase.h"
40 #include "wx/msw/registry.h"
44 #include "wx/msw/mimetype.h"
46 // other standard headers
49 // in case we're compiling in non-GUI mode
50 class WXDLLEXPORT wxIcon
;
52 // These classes use Windows registry to retrieve the required information.
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.
60 // 2. "HKCR\.ext" contains
61 // a) unnamed value containing the "filetype"
62 // b) value "Content Type" containing the MIME type
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
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)
72 // although I don't know of any official documentation which mentions this
73 // location, uses it, so it isn't likely to change
74 static const wxChar
*MIME_DATABASE_KEY
= wxT("MIME\\Database\\Content Type\\");
76 wxString
wxFileTypeImpl::GetCommand(const wxChar
*verb
) const
78 // suppress possible error messages
82 if ( wxRegKey(wxRegKey::HKCR
, m_ext
+ _T("\\shell")).Exists() )
84 if ( wxRegKey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell")).Exists() )
85 strKey
= m_strFileType
;
93 strKey
<< wxT("\\shell\\") << verb
;
94 wxRegKey
key(wxRegKey::HKCR
, strKey
+ _T("\\command"));
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
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('%') &&
111 (command
[n
+ 1] == wxT('1') ||
112 command
[n
+ 1] == wxT('L')) ) {
113 // replace it with '%s'
114 command
[n
+ 1] = wxT('s');
116 foundFilename
= TRUE
;
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"));
130 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Application")).
131 QueryValue(_T(""), ddeServer
);
132 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Topic")).
133 QueryValue(_T(""), ddeTopic
);
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
;
145 if ( !foundFilename
) {
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
150 // HACK: append the filename at the end, hope that it will do
151 command
<< wxT(" %s");
155 //else: no such file type or no value, will return empty string
161 wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
162 const wxFileType::MessageParameters
& params
)
167 cmd
= m_info
->GetOpenCommand();
170 cmd
= GetCommand(wxT("open"));
173 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
175 return !openCmd
->IsEmpty();
179 wxFileTypeImpl::GetPrintCommand(wxString
*printCmd
,
180 const wxFileType::MessageParameters
& params
)
185 cmd
= m_info
->GetPrintCommand();
188 cmd
= GetCommand(wxT("print"));
191 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
193 return !printCmd
->IsEmpty();
196 // TODO this function is half implemented
197 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
200 extensions
= m_info
->GetExtensions();
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...
211 extensions
.Add(m_ext
);
213 // it's a lie too, we don't return _all_ extensions...
218 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
221 // we already have it
222 *mimeType
= m_info
->GetMimeType();
227 // suppress possible error messages
229 wxRegKey
key(wxRegKey::HKCR
, wxT(".") + m_ext
);
231 return key
.Open() && key
.QueryValue(wxT("Content Type"), *mimeType
);
235 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
239 if ( !GetMimeType(&s
) )
250 bool wxFileTypeImpl::GetIcon(wxIcon
*icon
) const
254 // we don't have icons in the fallback resources
259 strIconKey
<< m_strFileType
<< wxT("\\DefaultIcon");
261 // suppress possible error messages
263 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
267 // it's the default value of the key
268 if ( key
.QueryValue(wxT(""), strIcon
) ) {
269 // the format is the following: <full path to file>, <icon index>
270 // NB: icon index may be negative as well as positive and the full
271 // path may contain the environment variables inside '%'
272 wxString strFullPath
= strIcon
.BeforeLast(wxT(',')),
273 strIndex
= strIcon
.AfterLast(wxT(','));
275 // index may be omitted, in which case BeforeLast(',') is empty and
276 // AfterLast(',') is the whole string
277 if ( strFullPath
.IsEmpty() ) {
278 strFullPath
= strIndex
;
282 wxString strExpPath
= wxExpandEnvVars(strFullPath
);
283 int nIndex
= wxAtoi(strIndex
);
285 HICON hIcon
= ExtractIcon(GetModuleHandle(NULL
), strExpPath
, nIndex
);
286 switch ( (int)hIcon
) {
287 case 0: // means no icons were found
288 case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/...
289 wxLogDebug(wxT("incorrect registry entry '%s': no such icon."),
290 key
.GetName().c_str());
294 icon
->SetHICON((WXHICON
)hIcon
);
300 // no such file type or no value or incorrect icon entry
306 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
309 // we already have it
310 *desc
= m_info
->GetDescription();
315 // suppress possible error messages
317 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
320 // it's the default value of the key
321 if ( key
.QueryValue(wxT(""), *desc
) ) {
329 // extension -> file type
331 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
333 // add the leading point if necessary
335 if ( ext
[0u] != wxT('.') ) {
340 // suppress possible error messages
343 bool knownExtension
= FALSE
;
345 wxString strFileType
;
346 wxRegKey
key(wxRegKey::HKCR
, str
);
348 // it's the default value of the key
349 if ( key
.QueryValue(wxT(""), strFileType
) ) {
350 // create the new wxFileType object
351 wxFileType
*fileType
= new wxFileType
;
352 fileType
->m_impl
->Init(strFileType
, ext
);
357 // this extension doesn't have a filetype, but it's known to the
358 // system and may be has some other useful keys (open command or
359 // content-type), so still return a file type object for it
360 knownExtension
= TRUE
;
364 // check the fallbacks
365 // TODO linear search is potentially slow, perhaps we should use a sorted
367 size_t count
= m_fallbacks
.GetCount();
368 for ( size_t n
= 0; n
< count
; n
++ ) {
369 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
370 wxFileType
*fileType
= new wxFileType
;
371 fileType
->m_impl
->Init(m_fallbacks
[n
]);
377 if ( !knownExtension
)
383 wxFileType
*fileType
= new wxFileType
;
384 fileType
->m_impl
->Init(wxEmptyString
, ext
);
389 // MIME type -> extension -> file type
391 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
393 wxString strKey
= MIME_DATABASE_KEY
;
396 // suppress possible error messages
400 wxRegKey
key(wxRegKey::HKCR
, strKey
);
402 if ( key
.QueryValue(wxT("Extension"), ext
) ) {
403 return GetFileTypeFromExtension(ext
);
407 // check the fallbacks
408 // TODO linear search is potentially slow, perhaps we should use a sorted
410 size_t count
= m_fallbacks
.GetCount();
411 for ( size_t n
= 0; n
< count
; n
++ ) {
412 if ( wxMimeTypesManager::IsOfType(mimeType
,
413 m_fallbacks
[n
].GetMimeType()) ) {
414 wxFileType
*fileType
= new wxFileType
;
415 fileType
->m_impl
->Init(m_fallbacks
[n
]);
425 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
427 // enumerate all keys under MIME_DATABASE_KEY
428 wxRegKey
key(wxRegKey::HKCR
, MIME_DATABASE_KEY
);
432 bool cont
= key
.GetFirstKey(type
, cookie
);
437 cont
= key
.GetNextKey(type
, cookie
);
440 return mimetypes
.GetCount();