]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/mimetype.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        common/mimetype.cpp 
   3 // Purpose:     classes and functions to manage MIME types 
   4 // Author:      David Webster 
   8 // Copyright:   Adopted from msw port --(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/string.h" 
  29 #include "wx/iconloc.h" 
  31 #include "wx/dynarray.h" 
  32 #include "wx/confbase.h" 
  36 #include "wx/os2/mimetype.h" 
  38 // other standard headers 
  41 // in case we're compiling in non-GUI mode 
  42 class WXDLLEXPORT wxIcon
; 
  44 // These classes use Windows registry to retrieve the required information. 
  46 // Keys used (not all of them are documented, so it might actually stop working 
  47 // in futur versions of Windows...): 
  48 //  1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME 
  49 //     types, each key has a string value "Extension" which gives (dot preceded) 
  50 //     extension for the files of this MIME type. 
  52 //  2. "HKCR\.ext" contains 
  53 //   a) unnamed value containing the "filetype" 
  54 //   b) value "Content Type" containing the MIME type 
  56 // 3. "HKCR\filetype" contains 
  57 //   a) unnamed value containing the description 
  58 //   b) subkey "DefaultIcon" with single unnamed value giving the icon index in 
  60 //   c) shell\open\command and shell\open\print subkeys containing the commands 
  61 //      to open/print the file (the positional parameters are introduced by %1, 
  62 //      %2, ... in these strings, we change them to %s ourselves) 
  64 // although I don't know of any official documentation which mentions this 
  65 // location, uses it, so it isn't likely to change 
  66 static const wxChar 
*MIME_DATABASE_KEY 
= wxT("MIME\\Database\\Content Type\\"); 
  68 wxString 
wxFileTypeImpl::GetCommand(const wxChar 
*WXUNUSED(verb
)) const 
  70 // TODO: OS/2 doesn't have a registry but uses Prf 
  72     // suppress possible error messages 
  76     if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() ) 
  78     if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() ) 
  79         strKey = m_strFileType; 
  87     strKey << wxT("\\shell\\") << verb; 
  88     wxRegKey key(wxRegKey::HKCR, strKey + _T("\\command")); 
  91         // it's the default value of the key 
  92         if ( key.QueryValue(wxT(""), command) ) { 
  93             // transform it from '%1' to '%s' style format string (now also 
  94             // test for %L - apparently MS started using it as well for the 
  97             // NB: we don't make any attempt to verify that the string is valid, 
  98             //     i.e. doesn't contain %2, or second %1 or .... But we do make 
  99             //     sure that we return a string with _exactly_ one '%s'! 
 100             bool foundFilename = false; 
 101             size_t len = command.Len(); 
 102             for ( size_t n = 0; (n < len) && !foundFilename; n++ ) { 
 103                 if ( command[n] == wxT('%') && 
 105                      (command[n + 1] == wxT('1') || 
 106                       command[n + 1] == wxT('L')) ) { 
 107                     // replace it with '%s' 
 108                     command[n + 1] = wxT('s'); 
 110                     foundFilename = true; 
 115             // look whether we must issue some DDE requests to the application 
 116             // (and not just launch it) 
 117             strKey += _T("\\DDEExec"); 
 118             wxRegKey keyDDE(wxRegKey::HKCR, strKey); 
 119             if ( keyDDE.Open() ) { 
 120                 wxString ddeCommand, ddeServer, ddeTopic; 
 121                 keyDDE.QueryValue(_T(""), ddeCommand); 
 122                 ddeCommand.Replace(_T("%1"), _T("%s")); 
 124                 wxRegKey(wxRegKey::HKCR, strKey + _T("\\Application")). 
 125                     QueryValue(_T(""), ddeServer); 
 126                 wxRegKey(wxRegKey::HKCR, strKey + _T("\\Topic")). 
 127                     QueryValue(_T(""), ddeTopic); 
 129                 // HACK: we use a special feature of wxExecute which exists 
 130                 //       just because we need it here: it will establish DDE 
 131                 //       conversation with the program it just launched 
 132                 command.Prepend(_T("WX_DDE#")); 
 133                 command << _T('#') << ddeServer 
 134                         << _T('#') << ddeTopic 
 135                         << _T('#') << ddeCommand; 
 139                 if ( !foundFilename ) { 
 140                 // we didn't find any '%1' - the application doesn't know which 
 141                 // file to open (note that we only do it if there is no DDEExec 
 144                 // HACK: append the filename at the end, hope that it will do 
 145                 command << wxT(" %s"); 
 149     //else: no such file type or no value, will return empty string 
 153    return wxEmptyString
; 
 157 wxFileTypeImpl::GetOpenCommand(wxString 
*openCmd
, 
 158                                const wxFileType::MessageParameters
& params
) 
 163         cmd 
= m_info
->GetOpenCommand(); 
 166         cmd 
= GetCommand(wxT("open")); 
 169     *openCmd 
= wxFileType::ExpandCommand(cmd
, params
); 
 171     return !openCmd
->empty(); 
 175 wxFileTypeImpl::GetPrintCommand(wxString 
*printCmd
, 
 176                                 const wxFileType::MessageParameters
& params
) 
 181         cmd 
= m_info
->GetPrintCommand(); 
 184         cmd 
= GetCommand(wxT("print")); 
 187     *printCmd 
= wxFileType::ExpandCommand(cmd
, params
); 
 189     return !printCmd
->empty(); 
 192 // TODO this function is half implemented 
 193 bool wxFileTypeImpl::GetExtensions(wxArrayString
& extensions
) 
 196         extensions 
= m_info
->GetExtensions(); 
 200     else if ( m_ext
.empty() ) { 
 201         // the only way to get the list of extensions from the file type is to 
 202         // scan through all extensions in the registry - too slow... 
 207         extensions
.Add(m_ext
); 
 209         // it's a lie too, we don't return _all_ extensions... 
 214 bool wxFileTypeImpl::GetMimeType(wxString 
*mimeType
) const 
 217         // we already have it 
 218         *mimeType 
= m_info
->GetMimeType(); 
 223     // suppress possible error messages 
 225 // TODO:  substitue reg key stuff (maybe make a Prf class for OS/2??) 
 227     wxRegKey key(wxRegKey::HKCR, wxT(".") + m_ext); 
 228     if ( key.Open() && key.QueryValue(wxT("Content Type"), *mimeType) ) { 
 238 bool wxFileTypeImpl::GetMimeTypes(wxArrayString
& mimeTypes
) const 
 252 bool wxFileTypeImpl::GetIcon(wxIconLocation 
*WXUNUSED(iconLoc
)) const 
 255         // we don't have icons in the fallback resources 
 260     strIconKey 
<< m_strFileType 
<< wxT("\\DefaultIcon"); 
 262     // suppress possible error messages 
 266     wxRegKey key(wxRegKey::HKCR, strIconKey); 
 270         // it's the default value of the key 
 271         if ( key.QueryValue(wxEmptyString, strIcon) ) { 
 272             // the format is the following: <full path to file>, <icon index> 
 273             // NB: icon index may be negative as well as positive and the full 
 274             //     path may contain the environment variables inside '%' 
 275             wxString strFullPath = strIcon.BeforeLast(wxT(',')), 
 276             strIndex = strIcon.AfterLast(wxT(',')); 
 278             // index may be omitted, in which case BeforeLast(',') is empty and 
 279             // AfterLast(',') is the whole string 
 280             if ( strFullPath.empty() ) { 
 281                 strFullPath = strIndex; 
 287                 iconLoc->SetFileName(wxExpandEnvVars(strFullPath)); 
 289                 iconLoc->SetIndex(wxAtoi(strIndex)); 
 296     // no such file type or no value or incorrect icon entry 
 301 bool wxFileTypeImpl::GetDescription(wxString 
*desc
) const 
 304         // we already have it 
 305         *desc 
= m_info
->GetDescription(); 
 310     // suppress possible error messages 
 314     wxRegKey key(wxRegKey::HKCR, m_strFileType); 
 317         // it's the default value of the key 
 318         if ( key.QueryValue(wxT(""), *desc) ) { 
 326 // extension -> file type 
 328 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString
& ext
) 
 330     // add the leading point if necessary 
 332     if ( ext
[(size_t) 0] != wxT('.') ) { 
 337     // suppress possible error messages 
 340     bool knownExtension 
= false; 
 342     wxString strFileType
; 
 345     wxRegKey key(wxRegKey::HKCR, str); 
 347         // it's the default value of the key 
 348         if ( key.QueryValue(wxT(""), strFileType) ) { 
 349             // create the new wxFileType object 
 350             wxFileType *fileType = new wxFileType; 
 351             fileType->m_impl->Init(strFileType, ext); 
 356             // this extension doesn't have a filetype, but it's known to the 
 357             // system and may be has some other useful keys (open command or 
 358             // content-type), so still return a file type object for it 
 359             knownExtension = true; 
 363     // check the fallbacks 
 364     // TODO linear search is potentially slow, perhaps we should use a sorted 
 366     size_t count 
= m_fallbacks
.GetCount(); 
 367     for ( size_t n 
= 0; n 
< count
; n
++ ) { 
 368         if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND 
) { 
 369             wxFileType 
*fileType 
= new wxFileType
; 
 370             fileType
->m_impl
->Init(m_fallbacks
[n
]); 
 376     if ( knownExtension 
) 
 378         wxFileType 
*fileType 
= new wxFileType
; 
 379         fileType
->m_impl
->Init(wxEmptyString
, ext
); 
 390 // MIME type -> extension -> file type 
 392 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString
& mimeType
) 
 394     wxString strKey 
= MIME_DATABASE_KEY
; 
 397     // suppress possible error messages 
 403     wxRegKey key(wxRegKey::HKCR, strKey); 
 405         if ( key.QueryValue(wxT("Extension"), ext) ) { 
 406             return GetFileTypeFromExtension(ext); 
 410     // check the fallbacks 
 411     // TODO linear search is potentially slow, perhaps we should use a sorted 
 413     size_t count = m_fallbacks.GetCount(); 
 414     for ( size_t n = 0; n < count; n++ ) { 
 415         if ( wxMimeTypesManager::IsOfType(mimeType, 
 416                                           m_fallbacks[n].GetMimeType()) ) { 
 417             wxFileType *fileType = new wxFileType; 
 418             fileType->m_impl->Init(m_fallbacks[n]); 
 428 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString
& WXUNUSED(mimetypes
)) 
 430     // enumerate all keys under MIME_DATABASE_KEY 
 433     wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); 
 437     bool cont = key.GetFirstKey(type, cookie); 
 442         cont = key.GetNextKey(type, cookie); 
 445     return mimetypes.GetCount(); 
 450 #endif //wxUSE_MIMETYPE