| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: msw/mimetype.cpp |
| 3 | // Purpose: classes and functions to manage MIME types |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 23.09.98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // Licence: wxWindows licence (part of wxExtra library) |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "mimetype.h" |
| 14 | #endif |
| 15 | |
| 16 | // for compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #if wxUSE_MIMETYPE |
| 24 | |
| 25 | #ifndef WX_PRECOMP |
| 26 | #include "wx/string.h" |
| 27 | #if wxUSE_GUI |
| 28 | #include "wx/icon.h" |
| 29 | #include "wx/msgdlg.h" |
| 30 | #endif |
| 31 | #endif //WX_PRECOMP |
| 32 | |
| 33 | #include "wx/log.h" |
| 34 | #include "wx/file.h" |
| 35 | #include "wx/iconloc.h" |
| 36 | #include "wx/intl.h" |
| 37 | #include "wx/dynarray.h" |
| 38 | #include "wx/confbase.h" |
| 39 | |
| 40 | #ifdef __WXMSW__ |
| 41 | #include "wx/msw/registry.h" |
| 42 | #include "wx/msw/private.h" |
| 43 | #endif // OS |
| 44 | |
| 45 | #include "wx/msw/mimetype.h" |
| 46 | |
| 47 | // other standard headers |
| 48 | #include <ctype.h> |
| 49 | |
| 50 | // in case we're compiling in non-GUI mode |
| 51 | class WXDLLEXPORT wxIcon; |
| 52 | |
| 53 | // These classes use Windows registry to retrieve the required information. |
| 54 | // |
| 55 | // Keys used (not all of them are documented, so it might actually stop working |
| 56 | // in future versions of Windows...): |
| 57 | // 1. "HKCR\MIME\Database\Content Type" contains subkeys for all known MIME |
| 58 | // types, each key has a string value "Extension" which gives (dot preceded) |
| 59 | // extension for the files of this MIME type. |
| 60 | // |
| 61 | // 2. "HKCR\.ext" contains |
| 62 | // a) unnamed value containing the "filetype" |
| 63 | // b) value "Content Type" containing the MIME type |
| 64 | // |
| 65 | // 3. "HKCR\filetype" contains |
| 66 | // a) unnamed value containing the description |
| 67 | // b) subkey "DefaultIcon" with single unnamed value giving the icon index in |
| 68 | // an icon file |
| 69 | // c) shell\open\command and shell\open\print subkeys containing the commands |
| 70 | // to open/print the file (the positional parameters are introduced by %1, |
| 71 | // %2, ... in these strings, we change them to %s ourselves) |
| 72 | |
| 73 | // although I don't know of any official documentation which mentions this |
| 74 | // location, uses it, so it isn't likely to change |
| 75 | static const wxChar *MIME_DATABASE_KEY = wxT("MIME\\Database\\Content Type\\"); |
| 76 | |
| 77 | // this function replaces Microsoft %1 with Unix-like %s |
| 78 | static bool CanonicalizeParams(wxString& command) |
| 79 | { |
| 80 | // transform it from '%1' to '%s' style format string (now also test for %L |
| 81 | // as apparently MS started using it as well for the same purpose) |
| 82 | |
| 83 | // NB: we don't make any attempt to verify that the string is valid, i.e. |
| 84 | // doesn't contain %2, or second %1 or .... But we do make sure that we |
| 85 | // return a string with _exactly_ one '%s'! |
| 86 | bool foundFilename = false; |
| 87 | size_t len = command.length(); |
| 88 | for ( size_t n = 0; (n < len) && !foundFilename; n++ ) |
| 89 | { |
| 90 | if ( command[n] == wxT('%') && |
| 91 | (n + 1 < len) && |
| 92 | (command[n + 1] == wxT('1') || command[n + 1] == wxT('L')) ) |
| 93 | { |
| 94 | // replace it with '%s' |
| 95 | command[n + 1] = wxT('s'); |
| 96 | |
| 97 | foundFilename = true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return foundFilename; |
| 102 | } |
| 103 | |
| 104 | void wxFileTypeImpl::Init(const wxString& strFileType, const wxString& ext) |
| 105 | { |
| 106 | // VZ: does it? (FIXME) |
| 107 | wxCHECK_RET( !ext.IsEmpty(), _T("needs an extension") ); |
| 108 | |
| 109 | if ( ext[0u] != wxT('.') ) { |
| 110 | m_ext = wxT('.'); |
| 111 | } |
| 112 | m_ext << ext; |
| 113 | |
| 114 | m_strFileType = strFileType; |
| 115 | if ( !strFileType ) { |
| 116 | m_strFileType = m_ext.AfterFirst('.') + _T("_auto_file"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | wxString wxFileTypeImpl::GetVerbPath(const wxString& verb) const |
| 121 | { |
| 122 | wxString path; |
| 123 | path << m_strFileType << _T("\\shell\\") << verb << _T("\\command"); |
| 124 | return path; |
| 125 | } |
| 126 | |
| 127 | size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs, |
| 128 | wxArrayString *commands, |
| 129 | const wxFileType::MessageParameters& params) const |
| 130 | { |
| 131 | wxCHECK_MSG( !m_ext.IsEmpty(), 0, _T("GetAllCommands() needs an extension") ); |
| 132 | |
| 133 | if ( m_strFileType.IsEmpty() ) |
| 134 | { |
| 135 | // get it from the registry |
| 136 | wxFileTypeImpl *self = wxConstCast(this, wxFileTypeImpl); |
| 137 | wxRegKey rkey(wxRegKey::HKCR, m_ext); |
| 138 | if ( !rkey.Exists() || !rkey.QueryValue(wxEmptyString, self->m_strFileType) ) |
| 139 | { |
| 140 | wxLogDebug(_T("Can't get the filetype for extension '%s'."), |
| 141 | m_ext.c_str()); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // enum all subkeys of HKCR\filetype\shell |
| 148 | size_t count = 0; |
| 149 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\shell")); |
| 150 | long dummy; |
| 151 | wxString verb; |
| 152 | bool ok = rkey.GetFirstKey(verb, dummy); |
| 153 | while ( ok ) |
| 154 | { |
| 155 | wxString command = wxFileType::ExpandCommand(GetCommand(verb), params); |
| 156 | |
| 157 | // we want the open bverb to eb always the first |
| 158 | |
| 159 | if ( verb.CmpNoCase(_T("open")) == 0 ) |
| 160 | { |
| 161 | if ( verbs ) |
| 162 | verbs->Insert(verb, 0); |
| 163 | if ( commands ) |
| 164 | commands->Insert(command, 0); |
| 165 | } |
| 166 | else // anything else than "open" |
| 167 | { |
| 168 | if ( verbs ) |
| 169 | verbs->Add(verb); |
| 170 | if ( commands ) |
| 171 | commands->Add(command); |
| 172 | } |
| 173 | |
| 174 | count++; |
| 175 | |
| 176 | ok = rkey.GetNextKey(verb, dummy); |
| 177 | } |
| 178 | |
| 179 | return count; |
| 180 | } |
| 181 | |
| 182 | // ---------------------------------------------------------------------------- |
| 183 | // modify the registry database |
| 184 | // ---------------------------------------------------------------------------- |
| 185 | |
| 186 | bool wxFileTypeImpl::EnsureExtKeyExists() |
| 187 | { |
| 188 | wxRegKey rkey(wxRegKey::HKCR, m_ext); |
| 189 | if ( !rkey.Exists() ) |
| 190 | { |
| 191 | if ( !rkey.Create() || !rkey.SetValue(wxEmptyString, m_strFileType) ) |
| 192 | { |
| 193 | wxLogError(_("Failed to create registry entry for '%s' files."), |
| 194 | m_ext.c_str()); |
| 195 | return FALSE; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return TRUE; |
| 200 | } |
| 201 | |
| 202 | // ---------------------------------------------------------------------------- |
| 203 | // get the command to use |
| 204 | // ---------------------------------------------------------------------------- |
| 205 | |
| 206 | wxString wxFileTypeImpl::GetCommand(const wxChar *verb) const |
| 207 | { |
| 208 | // suppress possible error messages |
| 209 | wxLogNull nolog; |
| 210 | wxString strKey; |
| 211 | |
| 212 | if ( wxRegKey(wxRegKey::HKCR, m_ext + _T("\\shell")).Exists() ) |
| 213 | strKey = m_ext; |
| 214 | if ( wxRegKey(wxRegKey::HKCR, m_strFileType + _T("\\shell")).Exists() ) |
| 215 | strKey = m_strFileType; |
| 216 | |
| 217 | if ( !strKey ) |
| 218 | { |
| 219 | // no info |
| 220 | return wxEmptyString; |
| 221 | } |
| 222 | |
| 223 | strKey << wxT("\\shell\\") << verb; |
| 224 | wxRegKey key(wxRegKey::HKCR, strKey + _T("\\command")); |
| 225 | wxString command; |
| 226 | if ( key.Open(wxRegKey::Read) ) { |
| 227 | // it's the default value of the key |
| 228 | if ( key.QueryValue(wxEmptyString, command) ) { |
| 229 | bool foundFilename = CanonicalizeParams(command); |
| 230 | |
| 231 | #if wxUSE_IPC |
| 232 | // look whether we must issue some DDE requests to the application |
| 233 | // (and not just launch it) |
| 234 | strKey += _T("\\DDEExec"); |
| 235 | wxRegKey keyDDE(wxRegKey::HKCR, strKey); |
| 236 | if ( keyDDE.Open(wxRegKey::Read) ) { |
| 237 | wxString ddeCommand, ddeServer, ddeTopic; |
| 238 | keyDDE.QueryValue(wxEmptyString, ddeCommand); |
| 239 | ddeCommand.Replace(_T("%1"), _T("%s")); |
| 240 | |
| 241 | wxRegKey(wxRegKey::HKCR, strKey + _T("\\Application")). |
| 242 | QueryValue(wxEmptyString, ddeServer); |
| 243 | wxRegKey(wxRegKey::HKCR, strKey + _T("\\Topic")). |
| 244 | QueryValue(wxEmptyString, ddeTopic); |
| 245 | |
| 246 | if (ddeTopic.IsEmpty()) |
| 247 | ddeTopic = wxT("System"); |
| 248 | |
| 249 | // HACK: we use a special feature of wxExecute which exists |
| 250 | // just because we need it here: it will establish DDE |
| 251 | // conversation with the program it just launched |
| 252 | command.Prepend(_T("WX_DDE#")); |
| 253 | command << _T('#') << ddeServer |
| 254 | << _T('#') << ddeTopic |
| 255 | << _T('#') << ddeCommand; |
| 256 | } |
| 257 | else |
| 258 | #endif // wxUSE_IPC |
| 259 | if ( !foundFilename ) |
| 260 | { |
| 261 | // we didn't find any '%1' - the application doesn't know which |
| 262 | // file to open (note that we only do it if there is no DDEExec |
| 263 | // subkey) |
| 264 | // |
| 265 | // HACK: append the filename at the end, hope that it will do |
| 266 | command << wxT(" %s"); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | //else: no such file type or no value, will return empty string |
| 271 | |
| 272 | return command; |
| 273 | } |
| 274 | |
| 275 | bool |
| 276 | wxFileTypeImpl::GetOpenCommand(wxString *openCmd, |
| 277 | const wxFileType::MessageParameters& params) |
| 278 | const |
| 279 | { |
| 280 | wxString cmd = GetCommand(wxT("open")); |
| 281 | |
| 282 | *openCmd = wxFileType::ExpandCommand(cmd, params); |
| 283 | |
| 284 | return !openCmd->IsEmpty(); |
| 285 | } |
| 286 | |
| 287 | bool |
| 288 | wxFileTypeImpl::GetPrintCommand(wxString *printCmd, |
| 289 | const wxFileType::MessageParameters& params) |
| 290 | const |
| 291 | { |
| 292 | wxString cmd = GetCommand(wxT("print")); |
| 293 | |
| 294 | *printCmd = wxFileType::ExpandCommand(cmd, params); |
| 295 | |
| 296 | return !printCmd->IsEmpty(); |
| 297 | } |
| 298 | |
| 299 | // ---------------------------------------------------------------------------- |
| 300 | // getting other stuff |
| 301 | // ---------------------------------------------------------------------------- |
| 302 | |
| 303 | // TODO this function is half implemented |
| 304 | bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions) |
| 305 | { |
| 306 | if ( m_ext.IsEmpty() ) { |
| 307 | // the only way to get the list of extensions from the file type is to |
| 308 | // scan through all extensions in the registry - too slow... |
| 309 | return FALSE; |
| 310 | } |
| 311 | else { |
| 312 | extensions.Empty(); |
| 313 | extensions.Add(m_ext); |
| 314 | |
| 315 | // it's a lie too, we don't return _all_ extensions... |
| 316 | return TRUE; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const |
| 321 | { |
| 322 | // suppress possible error messages |
| 323 | wxLogNull nolog; |
| 324 | wxRegKey key(wxRegKey::HKCR, m_ext); |
| 325 | |
| 326 | return key.Open(wxRegKey::Read) && |
| 327 | key.QueryValue(wxT("Content Type"), *mimeType); |
| 328 | } |
| 329 | |
| 330 | bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const |
| 331 | { |
| 332 | wxString s; |
| 333 | |
| 334 | if ( !GetMimeType(&s) ) |
| 335 | { |
| 336 | return FALSE; |
| 337 | } |
| 338 | |
| 339 | mimeTypes.Clear(); |
| 340 | mimeTypes.Add(s); |
| 341 | return TRUE; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const |
| 346 | { |
| 347 | wxString strIconKey; |
| 348 | strIconKey << m_strFileType << wxT("\\DefaultIcon"); |
| 349 | |
| 350 | // suppress possible error messages |
| 351 | wxLogNull nolog; |
| 352 | wxRegKey key(wxRegKey::HKCR, strIconKey); |
| 353 | |
| 354 | if ( key.Open(wxRegKey::Read) ) { |
| 355 | wxString strIcon; |
| 356 | // it's the default value of the key |
| 357 | if ( key.QueryValue(wxEmptyString, strIcon) ) { |
| 358 | // the format is the following: <full path to file>, <icon index> |
| 359 | // NB: icon index may be negative as well as positive and the full |
| 360 | // path may contain the environment variables inside '%' |
| 361 | wxString strFullPath = strIcon.BeforeLast(wxT(',')), |
| 362 | strIndex = strIcon.AfterLast(wxT(',')); |
| 363 | |
| 364 | // index may be omitted, in which case BeforeLast(',') is empty and |
| 365 | // AfterLast(',') is the whole string |
| 366 | if ( strFullPath.IsEmpty() ) { |
| 367 | strFullPath = strIndex; |
| 368 | strIndex = wxT("0"); |
| 369 | } |
| 370 | |
| 371 | if ( iconLoc ) |
| 372 | { |
| 373 | iconLoc->SetFileName(wxExpandEnvVars(strFullPath)); |
| 374 | |
| 375 | iconLoc->SetIndex(wxAtoi(strIndex)); |
| 376 | } |
| 377 | |
| 378 | return TRUE; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // no such file type or no value or incorrect icon entry |
| 383 | return FALSE; |
| 384 | } |
| 385 | |
| 386 | bool wxFileTypeImpl::GetDescription(wxString *desc) const |
| 387 | { |
| 388 | // suppress possible error messages |
| 389 | wxLogNull nolog; |
| 390 | wxRegKey key(wxRegKey::HKCR, m_strFileType); |
| 391 | |
| 392 | if ( key.Open(wxRegKey::Read) ) { |
| 393 | // it's the default value of the key |
| 394 | if ( key.QueryValue(wxEmptyString, *desc) ) { |
| 395 | return TRUE; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return FALSE; |
| 400 | } |
| 401 | |
| 402 | // helper function |
| 403 | wxFileType * |
| 404 | wxMimeTypesManagerImpl::CreateFileType(const wxString& filetype, const wxString& ext) |
| 405 | { |
| 406 | wxFileType *fileType = new wxFileType; |
| 407 | fileType->m_impl->Init(filetype, ext); |
| 408 | return fileType; |
| 409 | } |
| 410 | |
| 411 | // extension -> file type |
| 412 | wxFileType * |
| 413 | wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) |
| 414 | { |
| 415 | // add the leading point if necessary |
| 416 | wxString str; |
| 417 | if ( ext[0u] != wxT('.') ) { |
| 418 | str = wxT('.'); |
| 419 | } |
| 420 | str << ext; |
| 421 | |
| 422 | // suppress possible error messages |
| 423 | wxLogNull nolog; |
| 424 | |
| 425 | bool knownExtension = FALSE; |
| 426 | |
| 427 | wxString strFileType; |
| 428 | wxRegKey key(wxRegKey::HKCR, str); |
| 429 | if ( key.Open(wxRegKey::Read) ) { |
| 430 | // it's the default value of the key |
| 431 | if ( key.QueryValue(wxEmptyString, strFileType) ) { |
| 432 | // create the new wxFileType object |
| 433 | return CreateFileType(strFileType, ext); |
| 434 | } |
| 435 | else { |
| 436 | // this extension doesn't have a filetype, but it's known to the |
| 437 | // system and may be has some other useful keys (open command or |
| 438 | // content-type), so still return a file type object for it |
| 439 | knownExtension = TRUE; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if ( !knownExtension ) |
| 444 | { |
| 445 | // unknown extension |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | return CreateFileType(wxEmptyString, ext); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | wxFileType * |
| 454 | wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext) |
| 455 | { |
| 456 | wxFileType *fileType = GetFileTypeFromExtension(ext); |
| 457 | if ( !fileType ) |
| 458 | { |
| 459 | fileType = CreateFileType(wxEmptyString, ext); |
| 460 | } |
| 461 | |
| 462 | return fileType; |
| 463 | } |
| 464 | */ |
| 465 | |
| 466 | // MIME type -> extension -> file type |
| 467 | wxFileType * |
| 468 | wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) |
| 469 | { |
| 470 | wxString strKey = MIME_DATABASE_KEY; |
| 471 | strKey << mimeType; |
| 472 | |
| 473 | // suppress possible error messages |
| 474 | wxLogNull nolog; |
| 475 | |
| 476 | wxString ext; |
| 477 | wxRegKey key(wxRegKey::HKCR, strKey); |
| 478 | if ( key.Open(wxRegKey::Read) ) { |
| 479 | if ( key.QueryValue(wxT("Extension"), ext) ) { |
| 480 | return GetFileTypeFromExtension(ext); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // unknown MIME type |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) |
| 489 | { |
| 490 | // enumerate all keys under MIME_DATABASE_KEY |
| 491 | wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); |
| 492 | |
| 493 | wxString type; |
| 494 | long cookie; |
| 495 | bool cont = key.GetFirstKey(type, cookie); |
| 496 | while ( cont ) |
| 497 | { |
| 498 | mimetypes.Add(type); |
| 499 | |
| 500 | cont = key.GetNextKey(type, cookie); |
| 501 | } |
| 502 | |
| 503 | return mimetypes.GetCount(); |
| 504 | } |
| 505 | |
| 506 | // ---------------------------------------------------------------------------- |
| 507 | // create a new association |
| 508 | // ---------------------------------------------------------------------------- |
| 509 | |
| 510 | wxFileType *wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo) |
| 511 | { |
| 512 | wxCHECK_MSG( !ftInfo.GetExtensions().IsEmpty(), NULL, |
| 513 | _T("Associate() needs extension") ); |
| 514 | |
| 515 | bool ok; |
| 516 | int iExtCount = 0 ; |
| 517 | wxString filetype; |
| 518 | wxString extWithDot; |
| 519 | |
| 520 | wxString ext = ftInfo.GetExtensions()[iExtCount]; |
| 521 | |
| 522 | wxCHECK_MSG( !ext.empty(), NULL, |
| 523 | _T("Associate() needs non empty extension") ); |
| 524 | |
| 525 | if ( ext[0u] != _T('.') ) |
| 526 | extWithDot = _T('.'); |
| 527 | extWithDot += ext; |
| 528 | |
| 529 | // start by setting the HKCR\\.ext entries |
| 530 | // default is filetype; content type is mimetype |
| 531 | const wxString& filetypeOrig = ftInfo.GetShortDesc(); |
| 532 | |
| 533 | wxRegKey key(wxRegKey::HKCR, extWithDot); |
| 534 | if ( !key.Exists() ) |
| 535 | { |
| 536 | // create the mapping from the extension to the filetype |
| 537 | ok = key.Create(); |
| 538 | if ( ok ) |
| 539 | { |
| 540 | |
| 541 | if ( filetypeOrig.empty() ) |
| 542 | { |
| 543 | // make it up from the extension |
| 544 | filetype << extWithDot.c_str() + 1 << _T("_file"); |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | // just use the provided one |
| 549 | filetype = filetypeOrig; |
| 550 | } |
| 551 | |
| 552 | key.SetValue(wxEmptyString, filetype); |
| 553 | } |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | // key already exists, maybe we want to change it ?? |
| 558 | if (!filetypeOrig.empty()) |
| 559 | { |
| 560 | filetype = filetypeOrig; |
| 561 | key.SetValue(wxEmptyString, filetype); |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | key.QueryValue(wxEmptyString, filetype); |
| 566 | } |
| 567 | } |
| 568 | // now set a mimetypeif we have it, but ignore it if none |
| 569 | const wxString& mimetype = ftInfo.GetMimeType(); |
| 570 | if ( !mimetype.empty() ) |
| 571 | { |
| 572 | // set the MIME type |
| 573 | ok = key.SetValue(_T("Content Type"), mimetype); |
| 574 | |
| 575 | if ( ok ) |
| 576 | { |
| 577 | // create the MIME key |
| 578 | wxString strKey = MIME_DATABASE_KEY; |
| 579 | strKey << mimetype; |
| 580 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); |
| 581 | ok = keyMIME.Create(); |
| 582 | |
| 583 | if ( ok ) |
| 584 | { |
| 585 | // and provide a back link to the extension |
| 586 | keyMIME.SetValue(_T("Extension"), extWithDot); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
| 592 | // now make other extensions have the same filetype |
| 593 | |
| 594 | for (iExtCount=1; iExtCount < ftInfo.GetExtensionsCount(); iExtCount++ ) |
| 595 | { |
| 596 | ext = ftInfo.GetExtensions()[iExtCount]; |
| 597 | if ( ext[0u] != _T('.') ) |
| 598 | extWithDot = _T('.'); |
| 599 | extWithDot += ext; |
| 600 | |
| 601 | wxRegKey key(wxRegKey::HKCR, extWithDot); |
| 602 | if ( !key.Exists() ) key.Create(); |
| 603 | key.SetValue(wxEmptyString, filetype); |
| 604 | |
| 605 | // now set any mimetypes we may have, but ignore it if none |
| 606 | const wxString& mimetype = ftInfo.GetMimeType(); |
| 607 | if ( !mimetype.empty() ) |
| 608 | { |
| 609 | // set the MIME type |
| 610 | ok = key.SetValue(_T("Content Type"), mimetype); |
| 611 | |
| 612 | if ( ok ) |
| 613 | { |
| 614 | // create the MIME key |
| 615 | wxString strKey = MIME_DATABASE_KEY; |
| 616 | strKey << mimetype; |
| 617 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); |
| 618 | ok = keyMIME.Create(); |
| 619 | |
| 620 | if ( ok ) |
| 621 | { |
| 622 | // and provide a back link to the extension |
| 623 | keyMIME.SetValue(_T("Extension"), extWithDot); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | |
| 629 | } // end of for loop; all extensions now point to HKCR\.ext\Default |
| 630 | |
| 631 | // create the filetype key itself (it will be empty for now, but |
| 632 | // SetCommand(), SetDefaultIcon() &c will use it later) |
| 633 | wxRegKey keyFT(wxRegKey::HKCR, filetype); |
| 634 | keyFT.Create(); |
| 635 | |
| 636 | wxFileType *ft = CreateFileType(filetype, extWithDot); |
| 637 | |
| 638 | if (ft) |
| 639 | { |
| 640 | if (! ftInfo.GetOpenCommand ().IsEmpty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) ); |
| 641 | if (! ftInfo.GetPrintCommand().IsEmpty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) ); |
| 642 | // chris: I don't like the ->m_impl-> here FIX this ?? |
| 643 | if (! ftInfo.GetDescription ().IsEmpty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ; |
| 644 | if (! ftInfo.GetIconFile().IsEmpty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() ); |
| 645 | |
| 646 | } |
| 647 | return ft; |
| 648 | } |
| 649 | |
| 650 | bool wxFileTypeImpl::SetCommand(const wxString& cmd, |
| 651 | const wxString& verb, |
| 652 | bool WXUNUSED(overwriteprompt)) |
| 653 | { |
| 654 | wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE, |
| 655 | _T("SetCommand() needs an extension and a verb") ); |
| 656 | |
| 657 | if ( !EnsureExtKeyExists() ) |
| 658 | return FALSE; |
| 659 | |
| 660 | wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb)); |
| 661 | #if 0 |
| 662 | if ( rkey.Exists() && overwriteprompt ) |
| 663 | { |
| 664 | #if wxUSE_GUI |
| 665 | wxString old; |
| 666 | rkey.QueryValue(wxEmptyString, old); |
| 667 | if ( wxMessageBox |
| 668 | ( |
| 669 | wxString::Format( |
| 670 | _("Do you want to overwrite the command used to %s " |
| 671 | "files with extension \"%s\" ?\nCurrent value is \n%s, " |
| 672 | "\nNew value is \n%s %1"), // bug here FIX need %1 ?? |
| 673 | verb.c_str(), |
| 674 | m_ext.c_str(), |
| 675 | old.c_str(), |
| 676 | cmd.c_str()), |
| 677 | _("Confirm registry update"), |
| 678 | wxYES_NO | wxICON_QUESTION |
| 679 | ) != wxYES ) |
| 680 | #endif // wxUSE_GUI |
| 681 | { |
| 682 | // cancelled by user |
| 683 | return FALSE; |
| 684 | } |
| 685 | } |
| 686 | #endif |
| 687 | // TODO: |
| 688 | // 1. translate '%s' to '%1' instead of always adding it |
| 689 | // 2. create DDEExec value if needed (undo GetCommand) |
| 690 | return rkey.Create() && rkey.SetValue(wxEmptyString, cmd + _T(" \"%1\"") ); |
| 691 | } |
| 692 | |
| 693 | /* // no longer used |
| 694 | bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig) |
| 695 | { |
| 696 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") ); |
| 697 | |
| 698 | if ( !EnsureExtKeyExists() ) |
| 699 | return FALSE; |
| 700 | |
| 701 | // VZ: is this really useful? (FIXME) |
| 702 | wxString mimeType; |
| 703 | if ( !mimeTypeOrig ) |
| 704 | { |
| 705 | // make up a default value for it |
| 706 | wxString cmd; |
| 707 | wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL); |
| 708 | mimeType << _T("application/x-") << cmd; |
| 709 | } |
| 710 | else |
| 711 | { |
| 712 | mimeType = mimeTypeOrig; |
| 713 | } |
| 714 | |
| 715 | wxRegKey rkey(wxRegKey::HKCR, m_ext); |
| 716 | return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType); |
| 717 | } |
| 718 | */ |
| 719 | |
| 720 | bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index) |
| 721 | { |
| 722 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetDefaultIcon() needs extension") ); |
| 723 | wxCHECK_MSG( !m_strFileType.IsEmpty(), FALSE, _T("File key not found") ); |
| 724 | // the next line fails on a SMBshare, I think because it is case mangled |
| 725 | // wxCHECK_MSG( !wxFileExists(cmd), FALSE, _T("Icon file not found.") ); |
| 726 | |
| 727 | if ( !EnsureExtKeyExists() ) |
| 728 | return FALSE; |
| 729 | |
| 730 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon")); |
| 731 | |
| 732 | return rkey.Create() && |
| 733 | rkey.SetValue(wxEmptyString, |
| 734 | wxString::Format(_T("%s,%d"), cmd.c_str(), index)); |
| 735 | } |
| 736 | |
| 737 | bool wxFileTypeImpl::SetDescription (const wxString& desc) |
| 738 | { |
| 739 | wxCHECK_MSG( !m_strFileType.IsEmpty(), FALSE, _T("File key not found") ); |
| 740 | wxCHECK_MSG( !desc.IsEmpty(), FALSE, _T("No file description supplied") ); |
| 741 | |
| 742 | if ( !EnsureExtKeyExists() ) |
| 743 | return FALSE; |
| 744 | |
| 745 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType ); |
| 746 | |
| 747 | return rkey.Create() && |
| 748 | rkey.SetValue(wxEmptyString, desc); |
| 749 | } |
| 750 | |
| 751 | // ---------------------------------------------------------------------------- |
| 752 | // remove file association |
| 753 | // ---------------------------------------------------------------------------- |
| 754 | |
| 755 | bool wxFileTypeImpl::Unassociate() |
| 756 | { |
| 757 | bool result = TRUE; |
| 758 | if ( !RemoveOpenCommand() ) |
| 759 | result = FALSE; |
| 760 | if ( !RemoveDefaultIcon() ) |
| 761 | result = FALSE; |
| 762 | if ( !RemoveMimeType() ) |
| 763 | result = FALSE; |
| 764 | if ( !RemoveDescription() ) |
| 765 | result = FALSE; |
| 766 | |
| 767 | /* |
| 768 | //this might hold other keys, eg some have CSLID keys |
| 769 | if ( result ) |
| 770 | { |
| 771 | // delete the root key |
| 772 | wxRegKey key(wxRegKey::HKCR, m_ext); |
| 773 | if ( key.Exists() ) |
| 774 | result = key.DeleteSelf(); |
| 775 | } |
| 776 | */ |
| 777 | return result; |
| 778 | } |
| 779 | |
| 780 | bool wxFileTypeImpl::RemoveOpenCommand() |
| 781 | { |
| 782 | return RemoveCommand(_T("open")); |
| 783 | } |
| 784 | |
| 785 | bool wxFileTypeImpl::RemoveCommand(const wxString& verb) |
| 786 | { |
| 787 | wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE, |
| 788 | _T("RemoveCommand() needs an extension and a verb") ); |
| 789 | |
| 790 | wxString sKey = m_strFileType; |
| 791 | wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb)); |
| 792 | |
| 793 | // if the key already doesn't exist, it's a success |
| 794 | return !rkey.Exists() || rkey.DeleteSelf(); |
| 795 | } |
| 796 | |
| 797 | bool wxFileTypeImpl::RemoveMimeType() |
| 798 | { |
| 799 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("RemoveMimeType() needs extension") ); |
| 800 | |
| 801 | wxRegKey rkey(wxRegKey::HKCR, m_ext); |
| 802 | return !rkey.Exists() || rkey.DeleteSelf(); |
| 803 | } |
| 804 | |
| 805 | bool wxFileTypeImpl::RemoveDefaultIcon() |
| 806 | { |
| 807 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, |
| 808 | _T("RemoveDefaultIcon() needs extension") ); |
| 809 | |
| 810 | wxRegKey rkey (wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon")); |
| 811 | return !rkey.Exists() || rkey.DeleteSelf(); |
| 812 | } |
| 813 | |
| 814 | bool wxFileTypeImpl::RemoveDescription() |
| 815 | { |
| 816 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, |
| 817 | _T("RemoveDescription() needs extension") ); |
| 818 | |
| 819 | wxRegKey rkey (wxRegKey::HKCR, m_strFileType ); |
| 820 | return !rkey.Exists() || rkey.DeleteSelf(); |
| 821 | } |
| 822 | |
| 823 | #endif // wxUSE_MIMETYPE |