]>
git.saurik.com Git - wxWidgets.git/blob - src/palmos/mimetype.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: palmos/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"
23 // This really doesn't apply to Palm OS
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/palmos/registry.h"
45 #include "wx/palmos/private.h"
48 #include "wx/palmos/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 // this function replaces Microsoft %1 with Unix-like %s
81 static bool CanonicalizeParams(wxString
& command
)
83 // transform it from '%1' to '%s' style format string (now also test for %L
84 // as apparently MS started using it as well for the same purpose)
86 // NB: we don't make any attempt to verify that the string is valid, i.e.
87 // doesn't contain %2, or second %1 or .... But we do make sure that we
88 // return a string with _exactly_ one '%s'!
89 bool foundFilename
= false;
90 size_t len
= command
.length();
91 for ( size_t n
= 0; (n
< len
) && !foundFilename
; n
++ )
93 if ( command
[n
] == wxT('%') &&
95 (command
[n
+ 1] == wxT('1') || command
[n
+ 1] == wxT('L')) )
97 // replace it with '%s'
98 command
[n
+ 1] = wxT('s');
100 foundFilename
= true;
104 return foundFilename
;
107 void wxFileTypeImpl::Init(const wxString
& strFileType
, const wxString
& ext
)
109 // VZ: does it? (FIXME)
110 wxCHECK_RET( !ext
.IsEmpty(), _T("needs an extension") );
112 if ( ext
[0u] != wxT('.') ) {
117 m_strFileType
= strFileType
;
118 if ( !strFileType
) {
119 m_strFileType
= m_ext
.AfterFirst('.') + _T("_auto_file");
123 wxString
wxFileTypeImpl::GetVerbPath(const wxString
& verb
) const
126 path
<< m_strFileType
<< _T("\\shell\\") << verb
<< _T("\\command");
130 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
131 wxArrayString
*commands
,
132 const wxFileType::MessageParameters
& params
) const
134 wxCHECK_MSG( !m_ext
.IsEmpty(), 0, _T("GetAllCommands() needs an extension") );
136 if ( m_strFileType
.IsEmpty() )
138 // get it from the registry
139 wxFileTypeImpl
*self
= wxConstCast(this, wxFileTypeImpl
);
140 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
141 if ( !rkey
.Exists() || !rkey
.QueryValue(wxEmptyString
, self
->m_strFileType
) )
143 wxLogDebug(_T("Can't get the filetype for extension '%s'."),
150 // enum all subkeys of HKCR\filetype\shell
152 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell"));
155 bool ok
= rkey
.GetFirstKey(verb
, dummy
);
158 wxString command
= wxFileType::ExpandCommand(GetCommand(verb
), params
);
160 // we want the open bverb to eb always the first
162 if ( verb
.CmpNoCase(_T("open")) == 0 )
165 verbs
->Insert(verb
, 0);
167 commands
->Insert(command
, 0);
169 else // anything else than "open"
174 commands
->Add(command
);
179 ok
= rkey
.GetNextKey(verb
, dummy
);
185 // ----------------------------------------------------------------------------
186 // modify the registry database
187 // ----------------------------------------------------------------------------
189 bool wxFileTypeImpl::EnsureExtKeyExists()
191 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
192 if ( !rkey
.Exists() )
194 if ( !rkey
.Create() || !rkey
.SetValue(wxEmptyString
, m_strFileType
) )
196 wxLogError(_("Failed to create registry entry for '%s' files."),
205 // ----------------------------------------------------------------------------
206 // get the command to use
207 // ----------------------------------------------------------------------------
209 wxString
wxFileTypeImpl::GetCommand(const wxChar
*verb
) const
211 // suppress possible error messages
215 if ( wxRegKey(wxRegKey::HKCR
, m_ext
+ _T("\\shell")).Exists() )
217 if ( wxRegKey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell")).Exists() )
218 strKey
= m_strFileType
;
223 return wxEmptyString
;
226 strKey
<< wxT("\\shell\\") << verb
;
227 wxRegKey
key(wxRegKey::HKCR
, strKey
+ _T("\\command"));
229 if ( key
.Open(wxRegKey::Read
) ) {
230 // it's the default value of the key
231 if ( key
.QueryValue(wxEmptyString
, command
) ) {
232 bool foundFilename
= CanonicalizeParams(command
);
235 // look whether we must issue some DDE requests to the application
236 // (and not just launch it)
237 strKey
+= _T("\\DDEExec");
238 wxRegKey
keyDDE(wxRegKey::HKCR
, strKey
);
239 if ( keyDDE
.Open(wxRegKey::Read
) ) {
240 wxString ddeCommand
, ddeServer
, ddeTopic
;
241 keyDDE
.QueryValue(wxEmptyString
, ddeCommand
);
242 ddeCommand
.Replace(_T("%1"), _T("%s"));
244 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Application")).
245 QueryValue(wxEmptyString
, ddeServer
);
246 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Topic")).
247 QueryValue(wxEmptyString
, ddeTopic
);
249 if (ddeTopic
.IsEmpty())
250 ddeTopic
= wxT("System");
252 // HACK: we use a special feature of wxExecute which exists
253 // just because we need it here: it will establish DDE
254 // conversation with the program it just launched
255 command
.Prepend(_T("WX_DDE#"));
256 command
<< _T('#') << ddeServer
257 << _T('#') << ddeTopic
258 << _T('#') << ddeCommand
;
262 if ( !foundFilename
)
264 // we didn't find any '%1' - the application doesn't know which
265 // file to open (note that we only do it if there is no DDEExec
268 // HACK: append the filename at the end, hope that it will do
269 command
<< wxT(" %s");
273 //else: no such file type or no value, will return empty string
279 wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
280 const wxFileType::MessageParameters
& params
)
283 wxString cmd
= GetCommand(wxT("open"));
285 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
287 return !openCmd
->IsEmpty();
291 wxFileTypeImpl::GetPrintCommand(wxString
*printCmd
,
292 const wxFileType::MessageParameters
& params
)
295 wxString cmd
= GetCommand(wxT("print"));
297 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
299 return !printCmd
->IsEmpty();
302 // ----------------------------------------------------------------------------
303 // getting other stuff
304 // ----------------------------------------------------------------------------
306 // TODO this function is half implemented
307 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
309 if ( m_ext
.IsEmpty() ) {
310 // the only way to get the list of extensions from the file type is to
311 // scan through all extensions in the registry - too slow...
316 extensions
.Add(m_ext
);
318 // it's a lie too, we don't return _all_ extensions...
323 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
325 // suppress possible error messages
327 wxRegKey
key(wxRegKey::HKCR
, m_ext
);
329 return key
.Open(wxRegKey::Read
) &&
330 key
.QueryValue(wxT("Content Type"), *mimeType
);
333 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
337 if ( !GetMimeType(&s
) )
348 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
351 strIconKey
<< m_strFileType
<< wxT("\\DefaultIcon");
353 // suppress possible error messages
355 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
357 if ( key
.Open(wxRegKey::Read
) ) {
359 // it's the default value of the key
360 if ( key
.QueryValue(wxEmptyString
, strIcon
) ) {
361 // the format is the following: <full path to file>, <icon index>
362 // NB: icon index may be negative as well as positive and the full
363 // path may contain the environment variables inside '%'
364 wxString strFullPath
= strIcon
.BeforeLast(wxT(',')),
365 strIndex
= strIcon
.AfterLast(wxT(','));
367 // index may be omitted, in which case BeforeLast(',') is empty and
368 // AfterLast(',') is the whole string
369 if ( strFullPath
.IsEmpty() ) {
370 strFullPath
= strIndex
;
376 iconLoc
->SetFileName(wxExpandEnvVars(strFullPath
));
378 iconLoc
->SetIndex(wxAtoi(strIndex
));
385 // no such file type or no value or incorrect icon entry
389 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
391 // suppress possible error messages
393 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
395 if ( key
.Open(wxRegKey::Read
) ) {
396 // it's the default value of the key
397 if ( key
.QueryValue(wxEmptyString
, *desc
) ) {
407 wxMimeTypesManagerImpl::CreateFileType(const wxString
& filetype
, const wxString
& ext
)
409 wxFileType
*fileType
= new wxFileType
;
410 fileType
->m_impl
->Init(filetype
, ext
);
414 // extension -> file type
416 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
418 // add the leading point if necessary
420 if ( ext
[0u] != wxT('.') ) {
425 // suppress possible error messages
428 bool knownExtension
= FALSE
;
430 wxString strFileType
;
431 wxRegKey
key(wxRegKey::HKCR
, str
);
432 if ( key
.Open(wxRegKey::Read
) ) {
433 // it's the default value of the key
434 if ( key
.QueryValue(wxEmptyString
, strFileType
) ) {
435 // create the new wxFileType object
436 return CreateFileType(strFileType
, ext
);
439 // this extension doesn't have a filetype, but it's known to the
440 // system and may be has some other useful keys (open command or
441 // content-type), so still return a file type object for it
442 knownExtension
= TRUE
;
446 if ( !knownExtension
)
452 return CreateFileType(wxEmptyString
, ext
);
457 wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext)
459 wxFileType *fileType = GetFileTypeFromExtension(ext);
462 fileType = CreateFileType(wxEmptyString, ext);
469 // MIME type -> extension -> file type
471 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
473 wxString strKey
= MIME_DATABASE_KEY
;
476 // suppress possible error messages
480 wxRegKey
key(wxRegKey::HKCR
, strKey
);
481 if ( key
.Open(wxRegKey::Read
) ) {
482 if ( key
.QueryValue(wxT("Extension"), ext
) ) {
483 return GetFileTypeFromExtension(ext
);
491 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
493 // enumerate all keys under MIME_DATABASE_KEY
494 wxRegKey
key(wxRegKey::HKCR
, MIME_DATABASE_KEY
);
498 bool cont
= key
.GetFirstKey(type
, cookie
);
503 cont
= key
.GetNextKey(type
, cookie
);
506 return mimetypes
.GetCount();
509 // ----------------------------------------------------------------------------
510 // create a new association
511 // ----------------------------------------------------------------------------
513 wxFileType
*wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
515 wxCHECK_MSG( !ftInfo
.GetExtensions().IsEmpty(), NULL
,
516 _T("Associate() needs extension") );
523 wxString ext
= ftInfo
.GetExtensions()[iExtCount
];
525 wxCHECK_MSG( !ext
.empty(), NULL
,
526 _T("Associate() needs non empty extension") );
528 if ( ext
[0u] != _T('.') )
529 extWithDot
= _T('.');
532 // start by setting the HKCR\\.ext entries
533 // default is filetype; content type is mimetype
534 const wxString
& filetypeOrig
= ftInfo
.GetShortDesc();
536 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
539 // create the mapping from the extension to the filetype
544 if ( filetypeOrig
.empty() )
546 // make it up from the extension
547 filetype
<< extWithDot
.c_str() + 1 << _T("_file");
551 // just use the provided one
552 filetype
= filetypeOrig
;
555 key
.SetValue(wxEmptyString
, filetype
);
560 // key already exists, maybe we want to change it ??
561 if (!filetypeOrig
.empty())
563 filetype
= filetypeOrig
;
564 key
.SetValue(wxEmptyString
, filetype
);
568 key
.QueryValue(wxEmptyString
, filetype
);
571 // now set a mimetypeif we have it, but ignore it if none
572 const wxString
& mimetype
= ftInfo
.GetMimeType();
573 if ( !mimetype
.empty() )
576 ok
= key
.SetValue(_T("Content Type"), mimetype
);
580 // create the MIME key
581 wxString strKey
= MIME_DATABASE_KEY
;
583 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
584 ok
= keyMIME
.Create();
588 // and provide a back link to the extension
589 keyMIME
.SetValue(_T("Extension"), extWithDot
);
595 // now make other extensions have the same filetype
597 for (iExtCount
=1; iExtCount
< ftInfo
.GetExtensionsCount(); iExtCount
++ )
599 ext
= ftInfo
.GetExtensions()[iExtCount
];
600 if ( ext
[0u] != _T('.') )
601 extWithDot
= _T('.');
604 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
605 if ( !key
.Exists() ) key
.Create();
606 key
.SetValue(wxEmptyString
, filetype
);
608 // now set any mimetypes we may have, but ignore it if none
609 const wxString
& mimetype
= ftInfo
.GetMimeType();
610 if ( !mimetype
.empty() )
613 ok
= key
.SetValue(_T("Content Type"), mimetype
);
617 // create the MIME key
618 wxString strKey
= MIME_DATABASE_KEY
;
620 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
621 ok
= keyMIME
.Create();
625 // and provide a back link to the extension
626 keyMIME
.SetValue(_T("Extension"), extWithDot
);
632 } // end of for loop; all extensions now point to HKCR\.ext\Default
634 // create the filetype key itself (it will be empty for now, but
635 // SetCommand(), SetDefaultIcon() &c will use it later)
636 wxRegKey
keyFT(wxRegKey::HKCR
, filetype
);
639 wxFileType
*ft
= CreateFileType(filetype
, extWithDot
);
643 if (! ftInfo
.GetOpenCommand ().IsEmpty() ) ft
->SetCommand (ftInfo
.GetOpenCommand (), wxT("open" ) );
644 if (! ftInfo
.GetPrintCommand().IsEmpty() ) ft
->SetCommand (ftInfo
.GetPrintCommand(), wxT("print" ) );
645 // chris: I don't like the ->m_impl-> here FIX this ??
646 if (! ftInfo
.GetDescription ().IsEmpty() ) ft
->m_impl
->SetDescription (ftInfo
.GetDescription ()) ;
647 if (! ftInfo
.GetIconFile().IsEmpty() ) ft
->SetDefaultIcon (ftInfo
.GetIconFile(), ftInfo
.GetIconIndex() );
653 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
,
654 const wxString
& verb
,
655 bool WXUNUSED(overwriteprompt
))
657 wxCHECK_MSG( !m_ext
.IsEmpty() && !verb
.IsEmpty(), FALSE
,
658 _T("SetCommand() needs an extension and a verb") );
660 if ( !EnsureExtKeyExists() )
663 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
665 if ( rkey
.Exists() && overwriteprompt
)
669 rkey
.QueryValue(wxEmptyString
, old
);
673 _("Do you want to overwrite the command used to %s "
674 "files with extension \"%s\" ?\nCurrent value is \n%s, "
675 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
680 _("Confirm registry update"),
681 wxYES_NO
| wxICON_QUESTION
691 // 1. translate '%s' to '%1' instead of always adding it
692 // 2. create DDEExec value if needed (undo GetCommand)
693 return rkey
.Create() && rkey
.SetValue(wxEmptyString
, cmd
+ _T(" \"%1\"") );
697 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
699 wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") );
701 if ( !EnsureExtKeyExists() )
704 // VZ: is this really useful? (FIXME)
708 // make up a default value for it
710 wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL);
711 mimeType << _T("application/x-") << cmd;
715 mimeType = mimeTypeOrig;
718 wxRegKey rkey(wxRegKey::HKCR, m_ext);
719 return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType);
723 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& cmd
, int index
)
725 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
, _T("SetDefaultIcon() needs extension") );
726 wxCHECK_MSG( !m_strFileType
.IsEmpty(), FALSE
, _T("File key not found") );
727 // the next line fails on a SMBshare, I think because it is case mangled
728 // wxCHECK_MSG( !wxFileExists(cmd), FALSE, _T("Icon file not found.") );
730 if ( !EnsureExtKeyExists() )
733 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
735 return rkey
.Create() &&
736 rkey
.SetValue(wxEmptyString
,
737 wxString::Format(_T("%s,%d"), cmd
.c_str(), index
));
740 bool wxFileTypeImpl::SetDescription (const wxString
& desc
)
742 wxCHECK_MSG( !m_strFileType
.IsEmpty(), FALSE
, _T("File key not found") );
743 wxCHECK_MSG( !desc
.IsEmpty(), FALSE
, _T("No file description supplied") );
745 if ( !EnsureExtKeyExists() )
748 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
);
750 return rkey
.Create() &&
751 rkey
.SetValue(wxEmptyString
, desc
);
754 // ----------------------------------------------------------------------------
755 // remove file association
756 // ----------------------------------------------------------------------------
758 bool wxFileTypeImpl::Unassociate()
761 if ( !RemoveOpenCommand() )
763 if ( !RemoveDefaultIcon() )
765 if ( !RemoveMimeType() )
767 if ( !RemoveDescription() )
771 //this might hold other keys, eg some have CSLID keys
774 // delete the root key
775 wxRegKey key(wxRegKey::HKCR, m_ext);
777 result = key.DeleteSelf();
783 bool wxFileTypeImpl::RemoveOpenCommand()
785 return RemoveCommand(_T("open"));
788 bool wxFileTypeImpl::RemoveCommand(const wxString
& verb
)
790 wxCHECK_MSG( !m_ext
.IsEmpty() && !verb
.IsEmpty(), FALSE
,
791 _T("RemoveCommand() needs an extension and a verb") );
793 wxString sKey
= m_strFileType
;
794 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
796 // if the key already doesn't exist, it's a success
797 return !rkey
.Exists() || rkey
.DeleteSelf();
800 bool wxFileTypeImpl::RemoveMimeType()
802 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
, _T("RemoveMimeType() needs extension") );
804 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
805 return !rkey
.Exists() || rkey
.DeleteSelf();
808 bool wxFileTypeImpl::RemoveDefaultIcon()
810 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
,
811 _T("RemoveDefaultIcon() needs extension") );
813 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
814 return !rkey
.Exists() || rkey
.DeleteSelf();
817 bool wxFileTypeImpl::RemoveDescription()
819 wxCHECK_MSG( !m_ext
.IsEmpty(), FALSE
,
820 _T("RemoveDescription() needs extension") );
822 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
);
823 return !rkey
.Exists() || rkey
.DeleteSelf();
826 #endif // wxUSE_MIMETYPE