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