Ignore "%*" in the shell command strings in MSW registry.
[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 __WXMSW__
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 key(wxRegKey::HKCR, extWithDot);
640 if ( !key.Exists() ) key.Create();
641 key.SetValue(wxEmptyString, filetype);
642
643 // now set any mimetypes we may have, but ignore it if none
644 const wxString& mimetype = ftInfo.GetMimeType();
645 if ( !mimetype.empty() )
646 {
647 // set the MIME type
648 ok = key.SetValue(wxT("Content Type"), mimetype);
649
650 if ( ok )
651 {
652 // create the MIME key
653 wxString strKey = MIME_DATABASE_KEY;
654 strKey << mimetype;
655 wxRegKey keyMIME(wxRegKey::HKCR, strKey);
656 ok = keyMIME.Create();
657
658 if ( ok )
659 {
660 // and provide a back link to the extension
661 keyMIME.SetValue(wxT("Extension"), extWithDot);
662 }
663 }
664 }
665
666 } // end of for loop; all extensions now point to HKCR\.ext\Default
667
668 // create the filetype key itself (it will be empty for now, but
669 // SetCommand(), SetDefaultIcon() &c will use it later)
670 wxRegKey keyFT(wxRegKey::HKCR, filetype);
671 keyFT.Create();
672
673 wxFileType *ft = CreateFileType(filetype, extWithDot);
674
675 if (ft)
676 {
677 if (! ftInfo.GetOpenCommand ().empty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) );
678 if (! ftInfo.GetPrintCommand().empty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) );
679 // chris: I don't like the ->m_impl-> here FIX this ??
680 if (! ftInfo.GetDescription ().empty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ;
681 if (! ftInfo.GetIconFile().empty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() );
682
683 }
684
685 return ft;
686 }
687
688 bool wxFileTypeImpl::SetCommand(const wxString& cmd,
689 const wxString& verb,
690 bool WXUNUSED(overwriteprompt))
691 {
692 wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false,
693 wxT("SetCommand() needs an extension and a verb") );
694
695 if ( !EnsureExtKeyExists() )
696 return false;
697
698 wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
699 #if 0
700 if ( rkey.Exists() && overwriteprompt )
701 {
702 #if wxUSE_GUI
703 wxString old;
704 rkey.QueryValue(wxEmptyString, old);
705 if ( wxMessageBox
706 (
707 wxString::Format(
708 _("Do you want to overwrite the command used to %s "
709 "files with extension \"%s\" ?\nCurrent value is \n%s, "
710 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
711 verb.c_str(),
712 m_ext.c_str(),
713 old.c_str(),
714 cmd.c_str()),
715 _("Confirm registry update"),
716 wxYES_NO | wxICON_QUESTION
717 ) != wxYES )
718 #endif // wxUSE_GUI
719 {
720 // cancelled by user
721 return false;
722 }
723 }
724 #endif
725 // TODO:
726 // 1. translate '%s' to '%1' instead of always adding it
727 // 2. create DDEExec value if needed (undo GetCommand)
728 return rkey.Create() && rkey.SetValue(wxEmptyString, cmd + wxT(" \"%1\"") );
729 }
730
731 /* // no longer used
732 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
733 {
734 wxCHECK_MSG( !m_ext.empty(), false, wxT("SetMimeType() needs extension") );
735
736 if ( !EnsureExtKeyExists() )
737 return false;
738
739 // VZ: is this really useful? (FIXME)
740 wxString mimeType;
741 if ( !mimeTypeOrig )
742 {
743 // make up a default value for it
744 wxString cmd;
745 wxFileName::SplitPath(GetCommand(wxT("open")), NULL, &cmd, NULL);
746 mimeType << wxT("application/x-") << cmd;
747 }
748 else
749 {
750 mimeType = mimeTypeOrig;
751 }
752
753 wxRegKey rkey(wxRegKey::HKCR, m_ext);
754 return rkey.Create() && rkey.SetValue(wxT("Content Type"), mimeType);
755 }
756 */
757
758 bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index)
759 {
760 wxCHECK_MSG( !m_ext.empty(), false, wxT("SetDefaultIcon() needs extension") );
761 wxCHECK_MSG( !m_strFileType.empty(), false, wxT("File key not found") );
762 // the next line fails on a SMBshare, I think because it is case mangled
763 // wxCHECK_MSG( !wxFileExists(cmd), false, wxT("Icon file not found.") );
764
765 if ( !EnsureExtKeyExists() )
766 return false;
767
768 wxRegKey rkey(wxRegKey::HKCR, m_strFileType + wxT("\\DefaultIcon"));
769
770 return rkey.Create() &&
771 rkey.SetValue(wxEmptyString,
772 wxString::Format(wxT("%s,%d"), cmd.c_str(), index));
773 }
774
775 bool wxFileTypeImpl::SetDescription (const wxString& desc)
776 {
777 wxCHECK_MSG( !m_strFileType.empty(), false, wxT("File key not found") );
778 wxCHECK_MSG( !desc.empty(), false, wxT("No file description supplied") );
779
780 if ( !EnsureExtKeyExists() )
781 return false;
782
783 wxRegKey rkey(wxRegKey::HKCR, m_strFileType );
784
785 return rkey.Create() &&
786 rkey.SetValue(wxEmptyString, desc);
787 }
788
789 // ----------------------------------------------------------------------------
790 // remove file association
791 // ----------------------------------------------------------------------------
792
793 bool wxFileTypeImpl::Unassociate()
794 {
795 bool result = true;
796 if ( !RemoveOpenCommand() )
797 result = false;
798 if ( !RemoveDefaultIcon() )
799 result = false;
800 if ( !RemoveMimeType() )
801 result = false;
802 if ( !RemoveDescription() )
803 result = false;
804
805 /*
806 //this might hold other keys, eg some have CSLID keys
807 if ( result )
808 {
809 // delete the root key
810 wxRegKey key(wxRegKey::HKCR, m_ext);
811 if ( key.Exists() )
812 result = key.DeleteSelf();
813 }
814 */
815 return result;
816 }
817
818 bool wxFileTypeImpl::RemoveOpenCommand()
819 {
820 return RemoveCommand(wxT("open"));
821 }
822
823 bool wxFileTypeImpl::RemoveCommand(const wxString& verb)
824 {
825 wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false,
826 wxT("RemoveCommand() needs an extension and a verb") );
827
828 wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
829
830 // if the key already doesn't exist, it's a success
831 return !rkey.Exists() || rkey.DeleteSelf();
832 }
833
834 bool wxFileTypeImpl::RemoveMimeType()
835 {
836 wxCHECK_MSG( !m_ext.empty(), false, wxT("RemoveMimeType() needs extension") );
837
838 wxRegKey rkey(wxRegKey::HKCR, m_ext);
839 return !rkey.Exists() || rkey.DeleteSelf();
840 }
841
842 bool wxFileTypeImpl::RemoveDefaultIcon()
843 {
844 wxCHECK_MSG( !m_ext.empty(), false,
845 wxT("RemoveDefaultIcon() needs extension") );
846
847 wxRegKey rkey (wxRegKey::HKCR, m_strFileType + wxT("\\DefaultIcon"));
848 return !rkey.Exists() || rkey.DeleteSelf();
849 }
850
851 bool wxFileTypeImpl::RemoveDescription()
852 {
853 wxCHECK_MSG( !m_ext.empty(), false,
854 wxT("RemoveDescription() needs extension") );
855
856 wxRegKey rkey (wxRegKey::HKCR, m_strFileType );
857 return !rkey.Exists() || rkey.DeleteSelf();
858 }
859
860 #endif // wxUSE_MIMETYPE