1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 licence (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/msw/mimetype.h"
24 #include "wx/dynarray.h"
25 #include "wx/string.h"
31 #include "wx/msgdlg.h"
36 #include "wx/iconloc.h"
37 #include "wx/confbase.h"
40 #include "wx/msw/registry.h"
41 #include "wx/msw/private.h"
44 // other standard headers
47 // in case we're compiling in non-GUI mode
48 class WXDLLIMPEXP_FWD_CORE wxIcon
;
50 // These classes use Windows registry to retrieve the required information.
52 // Keys used (not all of them are documented, so it might actually stop working
53 // in future versions of Windows...):
54 // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME
55 // types, each key has a string value "Extension" which gives (dot preceded)
56 // extension for the files of this MIME type.
58 // 2. "HKCR\.ext" contains
59 // a) unnamed value containing the "filetype"
60 // b) value "Content Type" containing the MIME type
62 // 3. "HKCR\filetype" contains
63 // a) unnamed value containing the description
64 // b) subkey "DefaultIcon" with single unnamed value giving the icon index in
66 // c) shell\open\command and shell\open\print subkeys containing the commands
67 // to open/print the file (the positional parameters are introduced by %1,
68 // %2, ... in these strings, we change them to %s ourselves)
70 // although I don't know of any official documentation which mentions this
71 // location, uses it, so it isn't likely to change
72 static const wxChar
*MIME_DATABASE_KEY
= wxT("MIME\\Database\\Content Type\\");
74 // this function replaces Microsoft %1 with Unix-like %s
75 static bool CanonicalizeParams(wxString
& command
)
77 // transform it from '%1' to '%s' style format string (now also test for %L
78 // as apparently MS started using it as well for the same purpose)
80 // NB: we don't make any attempt to verify that the string is valid, i.e.
81 // doesn't contain %2, or second %1 or .... But we do make sure that we
82 // return a string with _exactly_ one '%s'!
83 bool foundFilename
= false;
84 size_t len
= command
.length();
85 for ( size_t n
= 0; (n
< len
) && !foundFilename
; n
++ )
87 if ( command
[n
] == wxT('%') &&
89 (command
[n
+ 1] == wxT('1') || command
[n
+ 1] == wxT('L')) )
91 // replace it with '%s'
92 command
[n
+ 1] = wxT('s');
101 void wxFileTypeImpl::Init(const wxString
& strFileType
, const wxString
& ext
)
103 // VZ: does it? (FIXME)
104 wxCHECK_RET( !ext
.empty(), wxT("needs an extension") );
106 if ( ext
[0u] != wxT('.') ) {
111 m_strFileType
= strFileType
;
112 if ( !strFileType
) {
113 m_strFileType
= m_ext
.AfterFirst('.') + wxT("_auto_file");
117 wxString
wxFileTypeImpl::GetVerbPath(const wxString
& verb
) const
120 path
<< m_strFileType
<< wxT("\\shell\\") << verb
<< wxT("\\command");
124 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
125 wxArrayString
*commands
,
126 const wxFileType::MessageParameters
& params
) const
128 wxCHECK_MSG( !m_ext
.empty(), 0, wxT("GetAllCommands() needs an extension") );
130 if ( m_strFileType
.empty() )
132 // get it from the registry
133 wxFileTypeImpl
*self
= wxConstCast(this, wxFileTypeImpl
);
134 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
135 if ( !rkey
.Exists() || !rkey
.QueryValue(wxEmptyString
, self
->m_strFileType
) )
137 wxLogDebug(wxT("Can't get the filetype for extension '%s'."),
144 // enum all subkeys of HKCR\filetype\shell
146 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ wxT("\\shell"));
149 bool ok
= rkey
.GetFirstKey(verb
, dummy
);
152 wxString command
= wxFileType::ExpandCommand(GetCommand(verb
), params
);
154 // we want the open bverb to eb always the first
156 if ( verb
.CmpNoCase(wxT("open")) == 0 )
159 verbs
->Insert(verb
, 0);
161 commands
->Insert(command
, 0);
163 else // anything else than "open"
168 commands
->Add(command
);
173 ok
= rkey
.GetNextKey(verb
, dummy
);
179 // ----------------------------------------------------------------------------
180 // modify the registry database
181 // ----------------------------------------------------------------------------
183 bool wxFileTypeImpl::EnsureExtKeyExists()
185 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
186 if ( !rkey
.Exists() )
188 if ( !rkey
.Create() || !rkey
.SetValue(wxEmptyString
, m_strFileType
) )
190 wxLogError(_("Failed to create registry entry for '%s' files."),
199 // ----------------------------------------------------------------------------
200 // get the command to use
201 // ----------------------------------------------------------------------------
203 static wxString
wxFileTypeImplGetCurVer(const wxString
& progId
)
205 wxRegKey
key(wxRegKey::HKCR
, progId
+ wxT("\\CurVer"));
209 if (key
.QueryValue(wxEmptyString
, value
))
215 wxString
wxFileTypeImpl::GetCommand(const wxString
& verb
) const
217 // suppress possible error messages
222 wxRegKey
explorerKey(wxRegKey::HKCU
, wxT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\") + m_ext
);
223 if (explorerKey
.Exists())
225 if (explorerKey
.Open(wxRegKey::Read
))
227 if (explorerKey
.QueryValue(wxT("Progid"), strKey
))
229 strKey
= wxFileTypeImplGetCurVer(strKey
);
235 if (!strKey
&& wxRegKey(wxRegKey::HKCR
, m_ext
+ wxT("\\shell")).Exists())
238 if ( !strKey
&& !m_strFileType
.empty())
240 wxString fileType
= wxFileTypeImplGetCurVer(m_strFileType
);
241 if (wxRegKey(wxRegKey::HKCR
, fileType
+ wxT("\\shell")).Exists())
248 return wxEmptyString
;
251 strKey
<< wxT("\\shell\\") << verb
;
252 wxRegKey
key(wxRegKey::HKCR
, strKey
+ wxT("\\command"));
254 if ( key
.Open(wxRegKey::Read
) ) {
255 // it's the default value of the key
256 if ( key
.QueryValue(wxEmptyString
, command
) ) {
257 bool foundFilename
= CanonicalizeParams(command
);
260 // look whether we must issue some DDE requests to the application
261 // (and not just launch it)
262 strKey
+= wxT("\\DDEExec");
263 wxRegKey
keyDDE(wxRegKey::HKCR
, strKey
);
264 if ( keyDDE
.Open(wxRegKey::Read
) ) {
265 wxString ddeCommand
, ddeServer
, ddeTopic
;
266 keyDDE
.QueryValue(wxEmptyString
, ddeCommand
);
267 ddeCommand
.Replace(wxT("%1"), wxT("%s"));
269 wxRegKey
keyServer(wxRegKey::HKCR
, strKey
+ wxT("\\Application"));
270 keyServer
.QueryValue(wxEmptyString
, ddeServer
);
271 wxRegKey
keyTopic(wxRegKey::HKCR
, strKey
+ wxT("\\Topic"));
272 keyTopic
.QueryValue(wxEmptyString
, ddeTopic
);
274 if (ddeTopic
.empty())
275 ddeTopic
= wxT("System");
277 // HACK: we use a special feature of wxExecute which exists
278 // just because we need it here: it will establish DDE
279 // conversation with the program it just launched
280 command
.Prepend(wxT("WX_DDE#"));
281 command
<< wxT('#') << ddeServer
282 << wxT('#') << ddeTopic
283 << wxT('#') << ddeCommand
;
287 if ( !foundFilename
)
289 // we didn't find any '%1' - the application doesn't know which
290 // file to open (note that we only do it if there is no DDEExec
293 // HACK: append the filename at the end, hope that it will do
294 command
<< wxT(" %s");
298 //else: no such file type or no value, will return empty string
304 wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
305 const wxFileType::MessageParameters
& params
)
308 wxString cmd
= GetCommand(wxT("open"));
310 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
312 return !openCmd
->empty();
316 wxFileTypeImpl::GetPrintCommand(wxString
*printCmd
,
317 const wxFileType::MessageParameters
& params
)
320 wxString cmd
= GetCommand(wxT("print"));
322 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
324 return !printCmd
->empty();
327 // ----------------------------------------------------------------------------
328 // getting other stuff
329 // ----------------------------------------------------------------------------
331 // TODO this function is half implemented
332 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
334 if ( m_ext
.empty() ) {
335 // the only way to get the list of extensions from the file type is to
336 // scan through all extensions in the registry - too slow...
341 extensions
.Add(m_ext
);
343 // it's a lie too, we don't return _all_ extensions...
348 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
350 // suppress possible error messages
352 wxRegKey
key(wxRegKey::HKCR
, m_ext
);
354 return key
.Open(wxRegKey::Read
) &&
355 key
.QueryValue(wxT("Content Type"), *mimeType
);
358 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
362 if ( !GetMimeType(&s
) )
373 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
376 strIconKey
<< m_strFileType
<< wxT("\\DefaultIcon");
378 // suppress possible error messages
380 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
382 if ( key
.Open(wxRegKey::Read
) ) {
384 // it's the default value of the key
385 if ( key
.QueryValue(wxEmptyString
, strIcon
) ) {
386 // the format is the following: <full path to file>, <icon index>
387 // NB: icon index may be negative as well as positive and the full
388 // path may contain the environment variables inside '%'
389 wxString strFullPath
= strIcon
.BeforeLast(wxT(',')),
390 strIndex
= strIcon
.AfterLast(wxT(','));
392 // index may be omitted, in which case BeforeLast(',') is empty and
393 // AfterLast(',') is the whole string
394 if ( strFullPath
.empty() ) {
395 strFullPath
= strIndex
;
401 iconLoc
->SetFileName(wxExpandEnvVars(strFullPath
));
403 iconLoc
->SetIndex(wxAtoi(strIndex
));
410 // no such file type or no value or incorrect icon entry
414 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
416 // suppress possible error messages
418 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
420 if ( key
.Open(wxRegKey::Read
) ) {
421 // it's the default value of the key
422 if ( key
.QueryValue(wxEmptyString
, *desc
) ) {
432 wxMimeTypesManagerImpl::CreateFileType(const wxString
& filetype
, const wxString
& ext
)
434 wxFileType
*fileType
= new wxFileType
;
435 fileType
->m_impl
->Init(filetype
, ext
);
439 // extension -> file type
441 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
443 // add the leading point if necessary
445 if ( ext
[0u] != wxT('.') ) {
450 // suppress possible error messages
453 bool knownExtension
= false;
455 wxString strFileType
;
456 wxRegKey
key(wxRegKey::HKCR
, str
);
457 if ( key
.Open(wxRegKey::Read
) ) {
458 // it's the default value of the key
459 if ( key
.QueryValue(wxEmptyString
, strFileType
) ) {
460 // create the new wxFileType object
461 return CreateFileType(strFileType
, ext
);
464 // this extension doesn't have a filetype, but it's known to the
465 // system and may be has some other useful keys (open command or
466 // content-type), so still return a file type object for it
467 knownExtension
= true;
471 if ( !knownExtension
)
477 return CreateFileType(wxEmptyString
, ext
);
482 wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext)
484 wxFileType *fileType = GetFileTypeFromExtension(ext);
487 fileType = CreateFileType(wxEmptyString, ext);
494 // MIME type -> extension -> file type
496 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
498 wxString strKey
= MIME_DATABASE_KEY
;
501 // suppress possible error messages
505 wxRegKey
key(wxRegKey::HKCR
, strKey
);
506 if ( key
.Open(wxRegKey::Read
) ) {
507 if ( key
.QueryValue(wxT("Extension"), ext
) ) {
508 return GetFileTypeFromExtension(ext
);
516 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
518 // enumerate all keys under MIME_DATABASE_KEY
519 wxRegKey
key(wxRegKey::HKCR
, MIME_DATABASE_KEY
);
523 bool cont
= key
.GetFirstKey(type
, cookie
);
528 cont
= key
.GetNextKey(type
, cookie
);
531 return mimetypes
.GetCount();
534 // ----------------------------------------------------------------------------
535 // create a new association
536 // ----------------------------------------------------------------------------
538 wxFileType
*wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
540 wxCHECK_MSG( !ftInfo
.GetExtensions().empty(), NULL
,
541 wxT("Associate() needs extension") );
544 size_t iExtCount
= 0;
548 wxString ext
= ftInfo
.GetExtensions()[iExtCount
];
550 wxCHECK_MSG( !ext
.empty(), NULL
,
551 wxT("Associate() needs non empty extension") );
553 if ( ext
[0u] != wxT('.') )
554 extWithDot
= wxT('.');
557 // start by setting the HKCR\\.ext entries
558 // default is filetype; content type is mimetype
559 const wxString
& filetypeOrig
= ftInfo
.GetShortDesc();
561 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
564 // create the mapping from the extension to the filetype
569 if ( filetypeOrig
.empty() )
571 // make it up from the extension
572 filetype
<< extWithDot
.c_str() + 1 << wxT("_file");
576 // just use the provided one
577 filetype
= filetypeOrig
;
580 key
.SetValue(wxEmptyString
, filetype
);
585 // key already exists, maybe we want to change it ??
586 if (!filetypeOrig
.empty())
588 filetype
= filetypeOrig
;
589 key
.SetValue(wxEmptyString
, filetype
);
593 key
.QueryValue(wxEmptyString
, filetype
);
597 // now set a mimetypeif we have it, but ignore it if none
598 const wxString
& mimetype
= ftInfo
.GetMimeType();
599 if ( !mimetype
.empty() )
602 ok
= key
.SetValue(wxT("Content Type"), mimetype
);
606 // create the MIME key
607 wxString strKey
= MIME_DATABASE_KEY
;
609 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
610 ok
= keyMIME
.Create();
614 // and provide a back link to the extension
615 keyMIME
.SetValue(wxT("Extension"), extWithDot
);
621 // now make other extensions have the same filetype
623 for (iExtCount
=1; iExtCount
< ftInfo
.GetExtensionsCount(); iExtCount
++ )
625 ext
= ftInfo
.GetExtensions()[iExtCount
];
626 if ( ext
[0u] != wxT('.') )
627 extWithDot
= wxT('.');
630 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
631 if ( !key
.Exists() ) key
.Create();
632 key
.SetValue(wxEmptyString
, filetype
);
634 // now set any mimetypes we may have, but ignore it if none
635 const wxString
& mimetype
= ftInfo
.GetMimeType();
636 if ( !mimetype
.empty() )
639 ok
= key
.SetValue(wxT("Content Type"), mimetype
);
643 // create the MIME key
644 wxString strKey
= MIME_DATABASE_KEY
;
646 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
647 ok
= keyMIME
.Create();
651 // and provide a back link to the extension
652 keyMIME
.SetValue(wxT("Extension"), extWithDot
);
657 } // end of for loop; all extensions now point to HKCR\.ext\Default
659 // create the filetype key itself (it will be empty for now, but
660 // SetCommand(), SetDefaultIcon() &c will use it later)
661 wxRegKey
keyFT(wxRegKey::HKCR
, filetype
);
664 wxFileType
*ft
= CreateFileType(filetype
, extWithDot
);
668 if (! ftInfo
.GetOpenCommand ().empty() ) ft
->SetCommand (ftInfo
.GetOpenCommand (), wxT("open" ) );
669 if (! ftInfo
.GetPrintCommand().empty() ) ft
->SetCommand (ftInfo
.GetPrintCommand(), wxT("print" ) );
670 // chris: I don't like the ->m_impl-> here FIX this ??
671 if (! ftInfo
.GetDescription ().empty() ) ft
->m_impl
->SetDescription (ftInfo
.GetDescription ()) ;
672 if (! ftInfo
.GetIconFile().empty() ) ft
->SetDefaultIcon (ftInfo
.GetIconFile(), ftInfo
.GetIconIndex() );
679 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
,
680 const wxString
& verb
,
681 bool WXUNUSED(overwriteprompt
))
683 wxCHECK_MSG( !m_ext
.empty() && !verb
.empty(), false,
684 wxT("SetCommand() needs an extension and a verb") );
686 if ( !EnsureExtKeyExists() )
689 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
691 if ( rkey
.Exists() && overwriteprompt
)
695 rkey
.QueryValue(wxEmptyString
, old
);
699 _("Do you want to overwrite the command used to %s "
700 "files with extension \"%s\" ?\nCurrent value is \n%s, "
701 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
706 _("Confirm registry update"),
707 wxYES_NO
| wxICON_QUESTION
717 // 1. translate '%s' to '%1' instead of always adding it
718 // 2. create DDEExec value if needed (undo GetCommand)
719 return rkey
.Create() && rkey
.SetValue(wxEmptyString
, cmd
+ wxT(" \"%1\"") );
723 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
725 wxCHECK_MSG( !m_ext.empty(), false, wxT("SetMimeType() needs extension") );
727 if ( !EnsureExtKeyExists() )
730 // VZ: is this really useful? (FIXME)
734 // make up a default value for it
736 wxFileName::SplitPath(GetCommand(wxT("open")), NULL, &cmd, NULL);
737 mimeType << wxT("application/x-") << cmd;
741 mimeType = mimeTypeOrig;
744 wxRegKey rkey(wxRegKey::HKCR, m_ext);
745 return rkey.Create() && rkey.SetValue(wxT("Content Type"), mimeType);
749 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& cmd
, int index
)
751 wxCHECK_MSG( !m_ext
.empty(), false, wxT("SetDefaultIcon() needs extension") );
752 wxCHECK_MSG( !m_strFileType
.empty(), false, wxT("File key not found") );
753 // the next line fails on a SMBshare, I think because it is case mangled
754 // wxCHECK_MSG( !wxFileExists(cmd), false, wxT("Icon file not found.") );
756 if ( !EnsureExtKeyExists() )
759 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ wxT("\\DefaultIcon"));
761 return rkey
.Create() &&
762 rkey
.SetValue(wxEmptyString
,
763 wxString::Format(wxT("%s,%d"), cmd
.c_str(), index
));
766 bool wxFileTypeImpl::SetDescription (const wxString
& desc
)
768 wxCHECK_MSG( !m_strFileType
.empty(), false, wxT("File key not found") );
769 wxCHECK_MSG( !desc
.empty(), false, wxT("No file description supplied") );
771 if ( !EnsureExtKeyExists() )
774 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
);
776 return rkey
.Create() &&
777 rkey
.SetValue(wxEmptyString
, desc
);
780 // ----------------------------------------------------------------------------
781 // remove file association
782 // ----------------------------------------------------------------------------
784 bool wxFileTypeImpl::Unassociate()
787 if ( !RemoveOpenCommand() )
789 if ( !RemoveDefaultIcon() )
791 if ( !RemoveMimeType() )
793 if ( !RemoveDescription() )
797 //this might hold other keys, eg some have CSLID keys
800 // delete the root key
801 wxRegKey key(wxRegKey::HKCR, m_ext);
803 result = key.DeleteSelf();
809 bool wxFileTypeImpl::RemoveOpenCommand()
811 return RemoveCommand(wxT("open"));
814 bool wxFileTypeImpl::RemoveCommand(const wxString
& verb
)
816 wxCHECK_MSG( !m_ext
.empty() && !verb
.empty(), false,
817 wxT("RemoveCommand() needs an extension and a verb") );
819 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
821 // if the key already doesn't exist, it's a success
822 return !rkey
.Exists() || rkey
.DeleteSelf();
825 bool wxFileTypeImpl::RemoveMimeType()
827 wxCHECK_MSG( !m_ext
.empty(), false, wxT("RemoveMimeType() needs extension") );
829 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
830 return !rkey
.Exists() || rkey
.DeleteSelf();
833 bool wxFileTypeImpl::RemoveDefaultIcon()
835 wxCHECK_MSG( !m_ext
.empty(), false,
836 wxT("RemoveDefaultIcon() needs extension") );
838 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
+ wxT("\\DefaultIcon"));
839 return !rkey
.Exists() || rkey
.DeleteSelf();
842 bool wxFileTypeImpl::RemoveDescription()
844 wxCHECK_MSG( !m_ext
.empty(), false,
845 wxT("RemoveDescription() needs extension") );
847 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
);
848 return !rkey
.Exists() || rkey
.DeleteSelf();
851 #endif // wxUSE_MIMETYPE