]>
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 licence (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mimetype.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 // Doesn't compile in WIN16 mode
29 #include "wx/string.h"
32 #include "wx/msgdlg.h"
38 #include "wx/iconloc.h"
40 #include "wx/dynarray.h"
41 #include "wx/confbase.h"
44 #include "wx/msw/registry.h"
45 #include "wx/msw/private.h"
48 #include "wx/msw/mimetype.h"
50 // other standard headers
53 // in case we're compiling in non-GUI mode
54 class WXDLLEXPORT wxIcon
;
56 // These classes use Windows registry to retrieve the required information.
58 // Keys used (not all of them are documented, so it might actually stop working
59 // in future versions of Windows...):
60 // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME
61 // types, each key has a string value "Extension" which gives (dot preceded)
62 // extension for the files of this MIME type.
64 // 2. "HKCR\.ext" contains
65 // a) unnamed value containing the "filetype"
66 // b) value "Content Type" containing the MIME type
68 // 3. "HKCR\filetype" contains
69 // a) unnamed value containing the description
70 // b) subkey "DefaultIcon" with single unnamed value giving the icon index in
72 // c) shell\open\command and shell\open\print subkeys containing the commands
73 // to open/print the file (the positional parameters are introduced by %1,
74 // %2, ... in these strings, we change them to %s ourselves)
76 // although I don't know of any official documentation which mentions this
77 // location, uses it, so it isn't likely to change
78 static const wxChar
*MIME_DATABASE_KEY
= wxT("MIME\\Database\\Content Type\\");
80 void wxFileTypeImpl::Init(const wxString
& strFileType
, const wxString
& ext
)
82 // VZ: does it? (FIXME)
83 wxCHECK_RET( !ext
.IsEmpty(), _T("needs an extension") );
85 if ( ext
[0u] != wxT('.') ) {
90 m_strFileType
= strFileType
;
92 m_strFileType
= m_ext
.AfterFirst('.') + _T("_auto_file");
96 wxString
wxFileTypeImpl::GetVerbPath(const wxString
& verb
) const
99 path
<< m_strFileType
<< _T("\\shell\\") << verb
<< _T("\\command");
103 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
104 wxArrayString
*commands
,
105 const wxFileType::MessageParameters
& params
) const
107 wxCHECK_MSG( !m_ext
.IsEmpty(), 0, _T("GetAllCommands() needs an extension") );
109 if ( m_strFileType
.IsEmpty() )
111 // get it from the registry
112 wxFileTypeImpl
*self
= wxConstCast(this, wxFileTypeImpl
);
113 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
114 if ( !rkey
.Exists() || !rkey
.QueryValue(wxEmptyString
, self
->m_strFileType
) )
116 wxLogDebug(_T("Can't get the filetype for extension '%s'."),
123 // enum all subkeys of HKCR\filetype\shell
125 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell"));
128 bool ok
= rkey
.GetFirstKey(verb
, dummy
);
131 wxString command
= wxFileType::ExpandCommand(GetCommand(verb
), params
);
133 // we want the open bverb to eb always the first
135 if ( verb
.CmpNoCase(_T("open")) == 0 )
138 verbs
->Insert(verb
, 0);
140 commands
->Insert(command
, 0);
142 else // anything else than "open"
147 commands
->Add(command
);
152 ok
= rkey
.GetNextKey(verb
, dummy
);
158 // ----------------------------------------------------------------------------
159 // modify the registry database
160 // ----------------------------------------------------------------------------
162 bool wxFileTypeImpl::EnsureExtKeyExists()
164 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
165 if ( !rkey
.Exists() )
167 if ( !rkey
.Create() || !rkey
.SetValue(wxEmptyString
, m_strFileType
) )
169 wxLogError(_("Failed to create registry entry for '%s' files."),
178 // ----------------------------------------------------------------------------
179 // get the command to use
180 // ----------------------------------------------------------------------------
182 wxString
wxFileTypeImpl::GetCommand(const wxChar
*verb
) const
184 // suppress possible error messages
188 if ( wxRegKey(wxRegKey::HKCR
, m_ext
+ _T("\\shell")).Exists() )
190 if ( wxRegKey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell")).Exists() )
191 strKey
= m_strFileType
;
196 return wxEmptyString
;
199 strKey
<< wxT("\\shell\\") << verb
;
200 wxRegKey
key(wxRegKey::HKCR
, strKey
+ _T("\\command"));
203 // it's the default value of the key
204 if ( key
.QueryValue(wxEmptyString
, command
) ) {
205 // transform it from '%1' to '%s' style format string (now also
206 // test for %L - apparently MS started using it as well for the
209 // NB: we don't make any attempt to verify that the string is valid,
210 // i.e. doesn't contain %2, or second %1 or .... But we do make
211 // sure that we return a string with _exactly_ one '%s'!
212 bool foundFilename
= FALSE
;
213 size_t len
= command
.Len();
214 for ( size_t n
= 0; (n
< len
) && !foundFilename
; n
++ ) {
215 if ( command
[n
] == wxT('%') &&
217 (command
[n
+ 1] == wxT('1') ||
218 command
[n
+ 1] == wxT('L')) ) {
219 // replace it with '%s'
220 command
[n
+ 1] = wxT('s');
222 foundFilename
= TRUE
;
227 // look whether we must issue some DDE requests to the application
228 // (and not just launch it)
229 strKey
+= _T("\\DDEExec");
230 wxRegKey
keyDDE(wxRegKey::HKCR
, strKey
);
231 if ( keyDDE
.Open() ) {
232 wxString ddeCommand
, ddeServer
, ddeTopic
;
233 keyDDE
.QueryValue(wxEmptyString
, ddeCommand
);
234 ddeCommand
.Replace(_T("%1"), _T("%s"));
236 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Application")).
237 QueryValue(wxEmptyString
, ddeServer
);
238 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Topic")).
239 QueryValue(wxEmptyString
, ddeTopic
);
241 if (ddeTopic
.IsEmpty())
242 ddeTopic
= wxT("System");
244 // HACK: we use a special feature of wxExecute which exists
245 // just because we need it here: it will establish DDE
246 // conversation with the program it just launched
247 command
.Prepend(_T("WX_DDE#"));
248 command
<< _T('#') << ddeServer
249 << _T('#') << ddeTopic
250 << _T('#') << ddeCommand
;
254 if ( !foundFilename
) {
255 // we didn't find any '%1' - the application doesn't know which
256 // file to open (note that we only do it if there is no DDEExec
259 // HACK: append the filename at the end, hope that it will do
260 command
<< wxT(" %s");
264 //else: no such file type or no value, will return empty string
270 wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
271 const wxFileType::MessageParameters
& params
)
274 wxString cmd
= GetCommand(wxT("open"));
276 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
278 return !openCmd
->IsEmpty();
282 wxFileTypeImpl::GetPrintCommand(wxString
*printCmd
,
283 const wxFileType::MessageParameters
& params
)
286 wxString cmd
= GetCommand(wxT("print"));
288 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
290 return !printCmd
->IsEmpty();
293 // ----------------------------------------------------------------------------
294 // getting other stuff
295 // ----------------------------------------------------------------------------
297 // TODO this function is half implemented
298 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
300 if ( m_ext
.IsEmpty() ) {
301 // the only way to get the list of extensions from the file type is to
302 // scan through all extensions in the registry - too slow...
307 extensions
.Add(m_ext
);
309 // it's a lie too, we don't return _all_ extensions...
314 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
316 // suppress possible error messages
318 wxRegKey
key(wxRegKey::HKCR
, m_ext
);
320 return key
.Open() && key
.QueryValue(wxT("Content Type"), *mimeType
);
323 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
327 if ( !GetMimeType(&s
) )
338 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
341 strIconKey
<< m_strFileType
<< wxT("\\DefaultIcon");
343 // suppress possible error messages
345 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
349 // it's the default value of the key
350 if ( key
.QueryValue(wxEmptyString
, strIcon
) ) {
351 // the format is the following: <full path to file>, <icon index>
352 // NB: icon index may be negative as well as positive and the full
353 // path may contain the environment variables inside '%'
354 wxString strFullPath
= strIcon
.BeforeLast(wxT(',')),
355 strIndex
= strIcon
.AfterLast(wxT(','));
357 // index may be omitted, in which case BeforeLast(',') is empty and
358 // AfterLast(',') is the whole string
359 if ( strFullPath
.IsEmpty() ) {
360 strFullPath
= strIndex
;
366 iconLoc
->SetFileName(wxExpandEnvVars(strFullPath
));
368 iconLoc
->SetIndex(wxAtoi(strIndex
));
375 // no such file type or no value or incorrect icon entry
379 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
381 // suppress possible error messages
383 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
386 // it's the default value of the key
387 if ( key
.QueryValue(wxEmptyString
, *desc
) ) {
397 wxMimeTypesManagerImpl::CreateFileType(const wxString
& filetype
, const wxString
& ext
)
399 wxFileType
*fileType
= new wxFileType
;
400 fileType
->m_impl
->Init(filetype
, ext
);
404 // extension -> file type
406 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
408 // add the leading point if necessary
410 if ( ext
[0u] != wxT('.') ) {
415 // suppress possible error messages
418 bool knownExtension
= FALSE
;
420 wxString strFileType
;
421 wxRegKey
key(wxRegKey::HKCR
, str
);
423 // it's the default value of the key
424 if ( key
.QueryValue(wxEmptyString
, strFileType
) ) {
425 // create the new wxFileType object
426 return CreateFileType(strFileType
, ext
);
429 // this extension doesn't have a filetype, but it's known to the
430 // system and may be has some other useful keys (open command or
431 // content-type), so still return a file type object for it
432 knownExtension
= TRUE
;
436 if ( !knownExtension
)
442 return CreateFileType(wxEmptyString
, ext
);
447 wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext)
449 wxFileType *fileType = GetFileTypeFromExtension(ext);
452 fileType = CreateFileType(wxEmptyString, ext);
459 // MIME type -> extension -> file type
461 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
463 wxString strKey
= MIME_DATABASE_KEY
;
466 // suppress possible error messages
470 wxRegKey
key(wxRegKey::HKCR
, strKey
);
472 if ( key
.QueryValue(wxT("Extension"), ext
) ) {
473 return GetFileTypeFromExtension(ext
);
481 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
483 // enumerate all keys under MIME_DATABASE_KEY
484 wxRegKey
key(wxRegKey::HKCR
, MIME_DATABASE_KEY
);
488 bool cont
= key
.GetFirstKey(type
, cookie
);
493 cont
= key
.GetNextKey(type
, cookie
);
496 return mimetypes
.GetCount();
499 // ----------------------------------------------------------------------------
500 // create a new association
501 // ----------------------------------------------------------------------------
503 wxFileType
*wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
505 wxCHECK_MSG( !ftInfo
.GetExtensions().IsEmpty(), NULL
,
506 _T("Associate() needs extension") );
513 wxString ext
= ftInfo
.GetExtensions()[iExtCount
];
515 wxCHECK_MSG( !ext
.empty(), NULL
,
516 _T("Associate() needs non empty extension") );
518 if ( ext
[0u] != _T('.') )
519 extWithDot
= _T('.');
522 // start by setting the HKCR\\.ext entries
523 // default is filetype; content type is mimetype
524 const wxString
& filetypeOrig
= ftInfo
.GetShortDesc();
526 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
529 // create the mapping from the extension to the filetype
534 if ( filetypeOrig
.empty() )
536 // make it up from the extension
537 filetype
<< extWithDot
.c_str() + 1 << _T("_file");
541 // just use the provided one
542 filetype
= filetypeOrig
;
545 ok
= key
.SetValue(wxEmptyString
, filetype
);
550 // key already exists, maybe we want to change it ??
551 if (!filetypeOrig
.empty())
553 filetype
= filetypeOrig
;
554 ok
= key
.SetValue(wxEmptyString
, filetype
);
558 ok
= key
.QueryValue(wxEmptyString
, filetype
);
561 // now set a mimetypeif we have it, but ignore it if none
562 const wxString
& mimetype
= ftInfo
.GetMimeType();
563 if ( !mimetype
.empty() )
566 ok
= key
.SetValue(_T("Content Type"), mimetype
);
570 // create the MIME key
571 wxString strKey
= MIME_DATABASE_KEY
;
573 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
574 ok
= keyMIME
.Create();
578 // and provide a back link to the extension
579 ok
= keyMIME
.SetValue(_T("Extension"), extWithDot
);
585 // now make other extensions have the same filetype
587 for (iExtCount
=1; iExtCount
< ftInfo
.GetExtensionsCount(); iExtCount
++ )
589 ext
= ftInfo
.GetExtensions()[iExtCount
];
590 if ( ext
[0u] != _T('.') )
591 extWithDot
= _T('.');
594 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
595 if ( !key
.Exists() ) ok
= key
.Create();
596 ok
= key
.SetValue(wxEmptyString
, filetype
);
598 // now set any mimetypes we may have, but ignore it if none
599 const wxString
& mimetype
= ftInfo
.GetMimeType();
600 if ( !mimetype
.empty() )
603 ok
= key
.SetValue(_T("Content Type"), mimetype
);
607 // create the MIME key
608 wxString strKey
= MIME_DATABASE_KEY
;
610 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
611 ok
= keyMIME
.Create();
615 // and provide a back link to the extension
616 ok
= keyMIME
.SetValue(_T("Extension"), extWithDot
);
622 } // end of for loop; all extensions now point to HKCR\.ext\Default
624 // create the filetype key itself (it will be empty for now, but
625 // SetCommand(), SetDefaultIcon() &c will use it later)
626 wxRegKey
keyFT(wxRegKey::HKCR
, filetype
);
629 wxFileType
*ft
= NULL
;
630 ft
= CreateFileType(filetype
, extWithDot
);
634 if (! ftInfo
.GetOpenCommand ().IsEmpty() ) ft
->SetCommand (ftInfo
.GetOpenCommand (), wxT("open" ) );
635 if (! ftInfo
.GetPrintCommand().IsEmpty() ) ft
->SetCommand (ftInfo
.GetPrintCommand(), wxT("print" ) );
636 // chris: I don't like the ->m_impl-> here FIX this ??
637 if (! ftInfo
.GetDescription ().IsEmpty() ) ft
->m_impl
->SetDescription (ftInfo
.GetDescription ()) ;
638 if (! ftInfo
.GetIconFile().IsEmpty() ) ft
->SetDefaultIcon (ftInfo
.GetIconFile(), ftInfo
.GetIconIndex() );
644 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
,
645 const wxString
& verb
,
646 bool WXUNUSED(overwriteprompt
))
648 wxCHECK_MSG( !m_ext
.IsEmpty() && !verb
.IsEmpty(), FALSE
,
649 _T("SetCommand() needs an extension and a verb") );
651 if ( !EnsureExtKeyExists() )
654 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
656 if ( rkey
.Exists() && overwriteprompt
)
660 rkey
.QueryValue(wxEmptyString
, old
);
664 _("Do you want to overwrite the command used to %s "
665 "files with extension \"%s\" ?\nCurrent value is \n%s, "
666 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
671 _("Confirm registry update"),
672 wxYES_NO
| wxICON_QUESTION
682 // 1. translate '%s' to '%1' instead of always adding it
683 // 2. create DDEExec value if needed (undo GetCommand)
684 return rkey
.Create() && rkey
.SetValue(wxEmptyString
, cmd
+ _T(" \"%1\"") );
688 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
690 wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") );
692 if ( !EnsureExtKeyExists() )
695 // VZ: is this really useful? (FIXME)
699 // make up a default value for it
701 wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL);
702 mimeType << _T("application/x-") << cmd;
706 mimeType = mimeTypeOrig;
709 wxRegKey rkey(wxRegKey::HKCR, m_ext);
710 return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType);
714 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& cmd
, int index
)
716 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
, _T("SetDefaultIcon() needs extension") );
717 wxCHECK_MSG( !m_strFileType
.IsEmpty(), FALSE
, _T("File key not found") );
718 // the next line fails on a SMBshare, I think because it is case mangled
719 // wxCHECK_MSG( !wxFileExists(cmd), FALSE, _T("Icon file not found.") );
721 if ( !EnsureExtKeyExists() )
724 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
726 return rkey
.Create() &&
727 rkey
.SetValue(wxEmptyString
,
728 wxString::Format(_T("%s,%d"), cmd
.c_str(), index
));
731 bool wxFileTypeImpl::SetDescription (const wxString
& desc
)
733 wxCHECK_MSG( !m_strFileType
.IsEmpty(), FALSE
, _T("File key not found") );
734 wxCHECK_MSG( !desc
.IsEmpty(), FALSE
, _T("No file description supplied") );
736 if ( !EnsureExtKeyExists() )
739 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
);
741 return rkey
.Create() &&
742 rkey
.SetValue(wxEmptyString
, desc
);
745 // ----------------------------------------------------------------------------
746 // remove file association
747 // ----------------------------------------------------------------------------
749 bool wxFileTypeImpl::Unassociate()
752 if ( !RemoveOpenCommand() )
754 if ( !RemoveDefaultIcon() )
756 if ( !RemoveMimeType() )
758 if ( !RemoveDescription() )
762 //this might hold other keys, eg some have CSLID keys
765 // delete the root key
766 wxRegKey key(wxRegKey::HKCR, m_ext);
768 result = key.DeleteSelf();
774 bool wxFileTypeImpl::RemoveOpenCommand()
776 return RemoveCommand(_T("open"));
779 bool wxFileTypeImpl::RemoveCommand(const wxString
& verb
)
781 wxCHECK_MSG( !m_ext
.IsEmpty() && !verb
.IsEmpty(), FALSE
,
782 _T("RemoveCommand() needs an extension and a verb") );
784 wxString sKey
= m_strFileType
;
785 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
787 // if the key already doesn't exist, it's a success
788 return !rkey
.Exists() || rkey
.DeleteSelf();
791 bool wxFileTypeImpl::RemoveMimeType()
793 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
, _T("RemoveMimeType() needs extension") );
795 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
796 return !rkey
.Exists() || rkey
.DeleteSelf();
799 bool wxFileTypeImpl::RemoveDefaultIcon()
801 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
,
802 _T("RemoveDefaultIcon() needs extension") );
804 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
805 return !rkey
.Exists() || rkey
.DeleteSelf();
808 bool wxFileTypeImpl::RemoveDescription()
810 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
,
811 _T("RemoveDescription() needs extension") );
813 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
);
814 return !rkey
.Exists() || rkey
.DeleteSelf();
820 #endif // wxUSE_MIMETYPE