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