]>
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"
43 #include "wx/palmos/mimetype.h"
45 // other standard headers
48 // in case we're compiling in non-GUI mode
49 class WXDLLEXPORT wxIcon
;
51 // These classes use Windows registry to retrieve the required information.
53 // Keys used (not all of them are documented, so it might actually stop working
54 // in future versions of Windows...):
55 // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME
56 // types, each key has a string value "Extension" which gives (dot preceded)
57 // extension for the files of this MIME type.
59 // 2. "HKCR\.ext" contains
60 // a) unnamed value containing the "filetype"
61 // b) value "Content Type" containing the MIME type
63 // 3. "HKCR\filetype" contains
64 // a) unnamed value containing the description
65 // b) subkey "DefaultIcon" with single unnamed value giving the icon index in
67 // c) shell\open\command and shell\open\print subkeys containing the commands
68 // to open/print the file (the positional parameters are introduced by %1,
69 // %2, ... in these strings, we change them to %s ourselves)
71 // although I don't know of any official documentation which mentions this
72 // location, uses it, so it isn't likely to change
73 static const wxChar
*MIME_DATABASE_KEY
= wxT("MIME\\Database\\Content Type\\");
75 // this function replaces Microsoft %1 with Unix-like %s
76 static bool CanonicalizeParams(wxString
& command
)
78 // transform it from '%1' to '%s' style format string (now also test for %L
79 // as apparently MS started using it as well for the same purpose)
81 // NB: we don't make any attempt to verify that the string is valid, i.e.
82 // doesn't contain %2, or second %1 or .... But we do make sure that we
83 // return a string with _exactly_ one '%s'!
84 bool foundFilename
= false;
85 size_t len
= command
.length();
86 for ( size_t n
= 0; (n
< len
) && !foundFilename
; n
++ )
88 if ( command
[n
] == wxT('%') &&
90 (command
[n
+ 1] == wxT('1') || command
[n
+ 1] == wxT('L')) )
92 // replace it with '%s'
93 command
[n
+ 1] = wxT('s');
102 void wxFileTypeImpl::Init(const wxString
& strFileType
, const wxString
& ext
)
104 // VZ: does it? (FIXME)
105 wxCHECK_RET( !ext
.empty(), _T("needs an extension") );
107 if ( ext
[0u] != wxT('.') ) {
112 m_strFileType
= strFileType
;
113 if ( !strFileType
) {
114 m_strFileType
= m_ext
.AfterFirst('.') + _T("_auto_file");
118 wxString
wxFileTypeImpl::GetVerbPath(const wxString
& verb
) const
121 path
<< m_strFileType
<< _T("\\shell\\") << verb
<< _T("\\command");
125 size_t wxFileTypeImpl::GetAllCommands(wxArrayString
*verbs
,
126 wxArrayString
*commands
,
127 const wxFileType::MessageParameters
& params
) const
129 wxCHECK_MSG( !m_ext
.empty(), 0, _T("GetAllCommands() needs an extension") );
131 if ( m_strFileType
.empty() )
133 // get it from the registry
134 wxFileTypeImpl
*self
= wxConstCast(this, wxFileTypeImpl
);
135 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
136 if ( !rkey
.Exists() || !rkey
.QueryValue(wxEmptyString
, self
->m_strFileType
) )
138 wxLogDebug(_T("Can't get the filetype for extension '%s'."),
145 // enum all subkeys of HKCR\filetype\shell
147 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell"));
150 bool ok
= rkey
.GetFirstKey(verb
, dummy
);
153 wxString command
= wxFileType::ExpandCommand(GetCommand(verb
), params
);
155 // we want the open bverb to eb always the first
157 if ( verb
.CmpNoCase(_T("open")) == 0 )
160 verbs
->Insert(verb
, 0);
162 commands
->Insert(command
, 0);
164 else // anything else than "open"
169 commands
->Add(command
);
174 ok
= rkey
.GetNextKey(verb
, dummy
);
180 // ----------------------------------------------------------------------------
181 // modify the registry database
182 // ----------------------------------------------------------------------------
184 bool wxFileTypeImpl::EnsureExtKeyExists()
186 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
187 if ( !rkey
.Exists() )
189 if ( !rkey
.Create() || !rkey
.SetValue(wxEmptyString
, m_strFileType
) )
191 wxLogError(_("Failed to create registry entry for '%s' files."),
200 // ----------------------------------------------------------------------------
201 // get the command to use
202 // ----------------------------------------------------------------------------
204 wxString
wxFileTypeImpl::GetCommand(const wxChar
*verb
) const
206 // suppress possible error messages
210 if ( wxRegKey(wxRegKey::HKCR
, m_ext
+ _T("\\shell")).Exists() )
212 if ( wxRegKey(wxRegKey::HKCR
, m_strFileType
+ _T("\\shell")).Exists() )
213 strKey
= m_strFileType
;
218 return wxEmptyString
;
221 strKey
<< wxT("\\shell\\") << verb
;
222 wxRegKey
key(wxRegKey::HKCR
, strKey
+ _T("\\command"));
224 if ( key
.Open(wxRegKey::Read
) ) {
225 // it's the default value of the key
226 if ( key
.QueryValue(wxEmptyString
, command
) ) {
227 bool foundFilename
= CanonicalizeParams(command
);
230 // look whether we must issue some DDE requests to the application
231 // (and not just launch it)
232 strKey
+= _T("\\DDEExec");
233 wxRegKey
keyDDE(wxRegKey::HKCR
, strKey
);
234 if ( keyDDE
.Open(wxRegKey::Read
) ) {
235 wxString ddeCommand
, ddeServer
, ddeTopic
;
236 keyDDE
.QueryValue(wxEmptyString
, ddeCommand
);
237 ddeCommand
.Replace(_T("%1"), _T("%s"));
239 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Application")).
240 QueryValue(wxEmptyString
, ddeServer
);
241 wxRegKey(wxRegKey::HKCR
, strKey
+ _T("\\Topic")).
242 QueryValue(wxEmptyString
, ddeTopic
);
244 if (ddeTopic
.IsEmpty())
245 ddeTopic
= wxT("System");
247 // HACK: we use a special feature of wxExecute which exists
248 // just because we need it here: it will establish DDE
249 // conversation with the program it just launched
250 command
.Prepend(_T("WX_DDE#"));
251 command
<< _T('#') << ddeServer
252 << _T('#') << ddeTopic
253 << _T('#') << ddeCommand
;
257 if ( !foundFilename
)
259 // we didn't find any '%1' - the application doesn't know which
260 // file to open (note that we only do it if there is no DDEExec
263 // HACK: append the filename at the end, hope that it will do
264 command
<< wxT(" %s");
268 //else: no such file type or no value, will return empty string
274 wxFileTypeImpl::GetOpenCommand(wxString
*openCmd
,
275 const wxFileType::MessageParameters
& params
)
278 wxString cmd
= GetCommand(wxT("open"));
280 *openCmd
= wxFileType::ExpandCommand(cmd
, params
);
282 return !openCmd
->empty();
286 wxFileTypeImpl::GetPrintCommand(wxString
*printCmd
,
287 const wxFileType::MessageParameters
& params
)
290 wxString cmd
= GetCommand(wxT("print"));
292 *printCmd
= wxFileType::ExpandCommand(cmd
, params
);
294 return !printCmd
->empty();
297 // ----------------------------------------------------------------------------
298 // getting other stuff
299 // ----------------------------------------------------------------------------
301 // TODO this function is half implemented
302 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
)
304 if ( m_ext
.empty() ) {
305 // the only way to get the list of extensions from the file type is to
306 // scan through all extensions in the registry - too slow...
311 extensions
.Add(m_ext
);
313 // it's a lie too, we don't return _all_ extensions...
318 bool wxFileTypeImpl::GetMimeType(wxString
*mimeType
) const
320 // suppress possible error messages
322 wxRegKey
key(wxRegKey::HKCR
, m_ext
);
324 return key
.Open(wxRegKey::Read
) &&
325 key
.QueryValue(wxT("Content Type"), *mimeType
);
328 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const
332 if ( !GetMimeType(&s
) )
343 bool wxFileTypeImpl::GetIcon(wxIconLocation
*iconLoc
) const
346 strIconKey
<< m_strFileType
<< wxT("\\DefaultIcon");
348 // suppress possible error messages
350 wxRegKey
key(wxRegKey::HKCR
, strIconKey
);
352 if ( key
.Open(wxRegKey::Read
) ) {
354 // it's the default value of the key
355 if ( key
.QueryValue(wxEmptyString
, strIcon
) ) {
356 // the format is the following: <full path to file>, <icon index>
357 // NB: icon index may be negative as well as positive and the full
358 // path may contain the environment variables inside '%'
359 wxString strFullPath
= strIcon
.BeforeLast(wxT(',')),
360 strIndex
= strIcon
.AfterLast(wxT(','));
362 // index may be omitted, in which case BeforeLast(',') is empty and
363 // AfterLast(',') is the whole string
364 if ( strFullPath
.empty() ) {
365 strFullPath
= strIndex
;
371 iconLoc
->SetFileName(wxExpandEnvVars(strFullPath
));
373 iconLoc
->SetIndex(wxAtoi(strIndex
));
380 // no such file type or no value or incorrect icon entry
384 bool wxFileTypeImpl::GetDescription(wxString
*desc
) const
386 // suppress possible error messages
388 wxRegKey
key(wxRegKey::HKCR
, m_strFileType
);
390 if ( key
.Open(wxRegKey::Read
) ) {
391 // it's the default value of the key
392 if ( key
.QueryValue(wxEmptyString
, *desc
) ) {
402 wxMimeTypesManagerImpl::CreateFileType(const wxString
& filetype
, const wxString
& ext
)
404 wxFileType
*fileType
= new wxFileType
;
405 fileType
->m_impl
->Init(filetype
, ext
);
409 // extension -> file type
411 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
)
413 // add the leading point if necessary
415 if ( ext
[0u] != wxT('.') ) {
420 // suppress possible error messages
423 bool knownExtension
= false;
425 wxString strFileType
;
426 wxRegKey
key(wxRegKey::HKCR
, str
);
427 if ( key
.Open(wxRegKey::Read
) ) {
428 // it's the default value of the key
429 if ( key
.QueryValue(wxEmptyString
, strFileType
) ) {
430 // create the new wxFileType object
431 return CreateFileType(strFileType
, ext
);
434 // this extension doesn't have a filetype, but it's known to the
435 // system and may be has some other useful keys (open command or
436 // content-type), so still return a file type object for it
437 knownExtension
= true;
441 if ( !knownExtension
)
447 return CreateFileType(wxEmptyString
, ext
);
452 wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext)
454 wxFileType *fileType = GetFileTypeFromExtension(ext);
457 fileType = CreateFileType(wxEmptyString, ext);
464 // MIME type -> extension -> file type
466 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
)
468 wxString strKey
= MIME_DATABASE_KEY
;
471 // suppress possible error messages
475 wxRegKey
key(wxRegKey::HKCR
, strKey
);
476 if ( key
.Open(wxRegKey::Read
) ) {
477 if ( key
.QueryValue(wxT("Extension"), ext
) ) {
478 return GetFileTypeFromExtension(ext
);
486 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& mimetypes
)
488 // enumerate all keys under MIME_DATABASE_KEY
489 wxRegKey
key(wxRegKey::HKCR
, MIME_DATABASE_KEY
);
493 bool cont
= key
.GetFirstKey(type
, cookie
);
498 cont
= key
.GetNextKey(type
, cookie
);
501 return mimetypes
.GetCount();
504 // ----------------------------------------------------------------------------
505 // create a new association
506 // ----------------------------------------------------------------------------
508 wxFileType
*wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo
& ftInfo
)
510 wxCHECK_MSG( !ftInfo
.GetExtensions().IsEmpty(), NULL
,
511 _T("Associate() needs extension") );
518 wxString ext
= ftInfo
.GetExtensions()[iExtCount
];
520 wxCHECK_MSG( !ext
.empty(), NULL
,
521 _T("Associate() needs non empty extension") );
523 if ( ext
[0u] != _T('.') )
524 extWithDot
= _T('.');
527 // start by setting the HKCR\\.ext entries
528 // default is filetype; content type is mimetype
529 const wxString
& filetypeOrig
= ftInfo
.GetShortDesc();
531 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
534 // create the mapping from the extension to the filetype
539 if ( filetypeOrig
.empty() )
541 // make it up from the extension
542 filetype
<< extWithDot
.c_str() + 1 << _T("_file");
546 // just use the provided one
547 filetype
= filetypeOrig
;
550 key
.SetValue(wxEmptyString
, filetype
);
555 // key already exists, maybe we want to change it ??
556 if (!filetypeOrig
.empty())
558 filetype
= filetypeOrig
;
559 key
.SetValue(wxEmptyString
, filetype
);
563 key
.QueryValue(wxEmptyString
, filetype
);
566 // now set a mimetypeif we have it, but ignore it if none
567 const wxString
& mimetype
= ftInfo
.GetMimeType();
568 if ( !mimetype
.empty() )
571 ok
= key
.SetValue(_T("Content Type"), mimetype
);
575 // create the MIME key
576 wxString strKey
= MIME_DATABASE_KEY
;
578 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
579 ok
= keyMIME
.Create();
583 // and provide a back link to the extension
584 keyMIME
.SetValue(_T("Extension"), extWithDot
);
590 // now make other extensions have the same filetype
592 for (iExtCount
=1; iExtCount
< ftInfo
.GetExtensionsCount(); iExtCount
++ )
594 ext
= ftInfo
.GetExtensions()[iExtCount
];
595 if ( ext
[0u] != _T('.') )
596 extWithDot
= _T('.');
599 wxRegKey
key(wxRegKey::HKCR
, extWithDot
);
600 if ( !key
.Exists() ) key
.Create();
601 key
.SetValue(wxEmptyString
, filetype
);
603 // now set any mimetypes we may have, but ignore it if none
604 const wxString
& mimetype
= ftInfo
.GetMimeType();
605 if ( !mimetype
.empty() )
608 ok
= key
.SetValue(_T("Content Type"), mimetype
);
612 // create the MIME key
613 wxString strKey
= MIME_DATABASE_KEY
;
615 wxRegKey
keyMIME(wxRegKey::HKCR
, strKey
);
616 ok
= keyMIME
.Create();
620 // and provide a back link to the extension
621 keyMIME
.SetValue(_T("Extension"), extWithDot
);
627 } // end of for loop; all extensions now point to HKCR\.ext\Default
629 // create the filetype key itself (it will be empty for now, but
630 // SetCommand(), SetDefaultIcon() &c will use it later)
631 wxRegKey
keyFT(wxRegKey::HKCR
, filetype
);
634 wxFileType
*ft
= CreateFileType(filetype
, extWithDot
);
638 if (! ftInfo
.GetOpenCommand ().IsEmpty() ) ft
->SetCommand (ftInfo
.GetOpenCommand (), wxT("open" ) );
639 if (! ftInfo
.GetPrintCommand().IsEmpty() ) ft
->SetCommand (ftInfo
.GetPrintCommand(), wxT("print" ) );
640 // chris: I don't like the ->m_impl-> here FIX this ??
641 if (! ftInfo
.GetDescription ().IsEmpty() ) ft
->m_impl
->SetDescription (ftInfo
.GetDescription ()) ;
642 if (! ftInfo
.GetIconFile().IsEmpty() ) ft
->SetDefaultIcon (ftInfo
.GetIconFile(), ftInfo
.GetIconIndex() );
648 bool wxFileTypeImpl::SetCommand(const wxString
& cmd
,
649 const wxString
& verb
,
650 bool WXUNUSED(overwriteprompt
))
652 wxCHECK_MSG( !m_ext
.empty() && !verb
.empty(), false,
653 _T("SetCommand() needs an extension and a verb") );
655 if ( !EnsureExtKeyExists() )
658 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
660 if ( rkey
.Exists() && overwriteprompt
)
664 rkey
.QueryValue(wxEmptyString
, old
);
668 _("Do you want to overwrite the command used to %s "
669 "files with extension \"%s\" ?\nCurrent value is \n%s, "
670 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
675 _("Confirm registry update"),
676 wxYES_NO
| wxICON_QUESTION
686 // 1. translate '%s' to '%1' instead of always adding it
687 // 2. create DDEExec value if needed (undo GetCommand)
688 return rkey
.Create() && rkey
.SetValue(wxEmptyString
, cmd
+ _T(" \"%1\"") );
692 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
694 wxCHECK_MSG( !m_ext.empty(), false, _T("SetMimeType() needs extension") );
696 if ( !EnsureExtKeyExists() )
699 // VZ: is this really useful? (FIXME)
703 // make up a default value for it
705 wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL);
706 mimeType << _T("application/x-") << cmd;
710 mimeType = mimeTypeOrig;
713 wxRegKey rkey(wxRegKey::HKCR, m_ext);
714 return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType);
718 bool wxFileTypeImpl::SetDefaultIcon(const wxString
& cmd
, int index
)
720 wxCHECK_MSG( !m_ext
.empty(), false, _T("SetDefaultIcon() needs extension") );
721 wxCHECK_MSG( !m_strFileType
.empty(), false, _T("File key not found") );
722 // the next line fails on a SMBshare, I think because it is case mangled
723 // wxCHECK_MSG( !wxFileExists(cmd), false, _T("Icon file not found.") );
725 if ( !EnsureExtKeyExists() )
728 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
730 return rkey
.Create() &&
731 rkey
.SetValue(wxEmptyString
,
732 wxString::Format(_T("%s,%d"), cmd
.c_str(), index
));
735 bool wxFileTypeImpl::SetDescription (const wxString
& desc
)
737 wxCHECK_MSG( !m_strFileType
.empty(), false, _T("File key not found") );
738 wxCHECK_MSG( !desc
.empty(), false, _T("No file description supplied") );
740 if ( !EnsureExtKeyExists() )
743 wxRegKey
rkey(wxRegKey::HKCR
, m_strFileType
);
745 return rkey
.Create() &&
746 rkey
.SetValue(wxEmptyString
, desc
);
749 // ----------------------------------------------------------------------------
750 // remove file association
751 // ----------------------------------------------------------------------------
753 bool wxFileTypeImpl::Unassociate()
756 if ( !RemoveOpenCommand() )
758 if ( !RemoveDefaultIcon() )
760 if ( !RemoveMimeType() )
762 if ( !RemoveDescription() )
766 //this might hold other keys, eg some have CSLID keys
769 // delete the root key
770 wxRegKey key(wxRegKey::HKCR, m_ext);
772 result = key.DeleteSelf();
778 bool wxFileTypeImpl::RemoveOpenCommand()
780 return RemoveCommand(_T("open"));
783 bool wxFileTypeImpl::RemoveCommand(const wxString
& verb
)
785 wxCHECK_MSG( !m_ext
.empty() && !verb
.empty(), false,
786 _T("RemoveCommand() needs an extension and a verb") );
788 wxString sKey
= m_strFileType
;
789 wxRegKey
rkey(wxRegKey::HKCR
, GetVerbPath(verb
));
791 // if the key already doesn't exist, it's a success
792 return !rkey
.Exists() || rkey
.DeleteSelf();
795 bool wxFileTypeImpl::RemoveMimeType()
797 wxCHECK_MSG( !m_ext
.empty(), false, _T("RemoveMimeType() needs extension") );
799 wxRegKey
rkey(wxRegKey::HKCR
, m_ext
);
800 return !rkey
.Exists() || rkey
.DeleteSelf();
803 bool wxFileTypeImpl::RemoveDefaultIcon()
805 wxCHECK_MSG( !m_ext
.empty(), false,
806 _T("RemoveDefaultIcon() needs extension") );
808 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
+ _T("\\DefaultIcon"));
809 return !rkey
.Exists() || rkey
.DeleteSelf();
812 bool wxFileTypeImpl::RemoveDescription()
814 wxCHECK_MSG( !m_ext
.empty(), false,
815 _T("RemoveDescription() needs extension") );
817 wxRegKey
rkey (wxRegKey::HKCR
, m_strFileType
);
818 return !rkey
.Exists() || rkey
.DeleteSelf();
821 #endif // wxUSE_MIMETYPE
822 #endif // __WXPALMOS__