]> git.saurik.com Git - wxWidgets.git/blob - src/msw/mimetype.cpp
Make wxToolBar styles consistent and add wxTB_DEFAULT_STYLE.
[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 // Since Windows Vista the association used by Explorer is different from
231 // the association information stored in the traditional part of the
232 // registry. Unfortunately the new schema doesn't seem to be documented
233 // anywhere so using it involves a bit of guesswork:
234 //
235 // The information is stored under Explorer-specific key whose path is
236 // below. The interesting part is UserChoice subkey which is the only one
237 // we use so far but there is also OpenWithProgids subkey which can exist
238 // even if UserChoice doesn't. However in practice there doesn't seem to be
239 // any cases when OpenWithProgids values for the given extension are
240 // different from those found directly under HKCR\.ext, so for now we don't
241 // bother to use this, apparently the programs registering their file type
242 // associations do it in both places. We do use UserChoice because when the
243 // association is manually changed by the user it's only recorded there and
244 // so must override whatever value was created under HKCR by the setup
245 // program.
246
247 {
248 wxRegKey explorerKey
249 (
250 wxRegKey::HKCU,
251 wxT("Software\\Microsoft\\Windows\\CurrentVersion\\")
252 wxT("Explorer\\FileExts\\") +
253 m_ext +
254 wxT("\\UserChoice")
255 );
256 if ( explorerKey.Open(wxRegKey::Read) &&
257 explorerKey.QueryValue(wxT("Progid"), strKey) )
258 {
259 strKey = wxFileTypeImplGetCurVer(strKey);
260 }
261 }
262
263 if (!strKey && wxRegKey(wxRegKey::HKCR, m_ext + wxT("\\shell")).Exists())
264 strKey = m_ext;
265
266 if ( !strKey && !m_strFileType.empty())
267 {
268 wxString fileType = wxFileTypeImplGetCurVer(m_strFileType);
269 if (wxRegKey(wxRegKey::HKCR, fileType + wxT("\\shell")).Exists())
270 strKey = fileType;
271 }
272
273 if ( !strKey )
274 {
275 // no info
276 return wxEmptyString;
277 }
278
279 strKey << wxT("\\shell\\") << verb;
280 wxRegKey key(wxRegKey::HKCR, strKey + wxT("\\command"));
281 wxString command;
282 if ( key.Open(wxRegKey::Read) ) {
283 // it's the default value of the key
284 if ( key.QueryValue(wxEmptyString, command) ) {
285 bool foundFilename = CanonicalizeParams(command);
286
287 #if wxUSE_IPC
288 // look whether we must issue some DDE requests to the application
289 // (and not just launch it)
290 strKey += wxT("\\DDEExec");
291 wxRegKey keyDDE(wxRegKey::HKCR, strKey);
292 if ( keyDDE.Open(wxRegKey::Read) ) {
293 wxString ddeCommand, ddeServer, ddeTopic;
294 keyDDE.QueryValue(wxEmptyString, ddeCommand);
295 ddeCommand.Replace(wxT("%1"), wxT("%s"));
296
297 wxRegKey keyServer(wxRegKey::HKCR, strKey + wxT("\\Application"));
298 keyServer.QueryValue(wxEmptyString, ddeServer);
299 wxRegKey keyTopic(wxRegKey::HKCR, strKey + wxT("\\Topic"));
300 keyTopic.QueryValue(wxEmptyString, ddeTopic);
301
302 if (ddeTopic.empty())
303 ddeTopic = wxT("System");
304
305 // HACK: we use a special feature of wxExecute which exists
306 // just because we need it here: it will establish DDE
307 // conversation with the program it just launched
308 command.Prepend(wxT("WX_DDE#"));
309 command << wxT('#') << ddeServer
310 << wxT('#') << ddeTopic
311 << wxT('#') << ddeCommand;
312 }
313 else
314 #endif // wxUSE_IPC
315 if ( !foundFilename )
316 {
317 // we didn't find any '%1' - the application doesn't know which
318 // file to open (note that we only do it if there is no DDEExec
319 // subkey)
320 //
321 // HACK: append the filename at the end, hope that it will do
322 command << wxT(" %s");
323 }
324 }
325 }
326 //else: no such file type or no value, will return empty string
327
328 return command;
329 }
330
331 bool
332 wxFileTypeImpl::GetOpenCommand(wxString *openCmd,
333 const wxFileType::MessageParameters& params)
334 const
335 {
336 wxString cmd = GetCommand(wxT("open"));
337
338 *openCmd = wxFileType::ExpandCommand(cmd, params);
339
340 return !openCmd->empty();
341 }
342
343 bool
344 wxFileTypeImpl::GetPrintCommand(wxString *printCmd,
345 const wxFileType::MessageParameters& params)
346 const
347 {
348 wxString cmd = GetCommand(wxT("print"));
349
350 *printCmd = wxFileType::ExpandCommand(cmd, params);
351
352 return !printCmd->empty();
353 }
354
355 // ----------------------------------------------------------------------------
356 // getting other stuff
357 // ----------------------------------------------------------------------------
358
359 // TODO this function is half implemented
360 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
361 {
362 if ( m_ext.empty() ) {
363 // the only way to get the list of extensions from the file type is to
364 // scan through all extensions in the registry - too slow...
365 return false;
366 }
367 else {
368 extensions.Empty();
369 extensions.Add(m_ext);
370
371 // it's a lie too, we don't return _all_ extensions...
372 return true;
373 }
374 }
375
376 bool wxFileTypeImpl::GetMimeType(wxString *mimeType) const
377 {
378 // suppress possible error messages
379 wxLogNull nolog;
380 wxRegKey key(wxRegKey::HKCR, m_ext);
381
382 return key.Open(wxRegKey::Read) &&
383 key.QueryValue(wxT("Content Type"), *mimeType);
384 }
385
386 bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
387 {
388 wxString s;
389
390 if ( !GetMimeType(&s) )
391 {
392 return false;
393 }
394
395 mimeTypes.Clear();
396 mimeTypes.Add(s);
397 return true;
398 }
399
400
401 bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
402 {
403 wxString strIconKey;
404 strIconKey << m_strFileType << wxT("\\DefaultIcon");
405
406 // suppress possible error messages
407 wxLogNull nolog;
408 wxRegKey key(wxRegKey::HKCR, strIconKey);
409
410 if ( key.Open(wxRegKey::Read) ) {
411 wxString strIcon;
412 // it's the default value of the key
413 if ( key.QueryValue(wxEmptyString, strIcon) ) {
414 // the format is the following: <full path to file>, <icon index>
415 // NB: icon index may be negative as well as positive and the full
416 // path may contain the environment variables inside '%'
417 wxString strFullPath = strIcon.BeforeLast(wxT(',')),
418 strIndex = strIcon.AfterLast(wxT(','));
419
420 // index may be omitted, in which case BeforeLast(',') is empty and
421 // AfterLast(',') is the whole string
422 if ( strFullPath.empty() ) {
423 strFullPath = strIndex;
424 strIndex = wxT("0");
425 }
426
427 if ( iconLoc )
428 {
429 iconLoc->SetFileName(wxExpandEnvVars(strFullPath));
430
431 iconLoc->SetIndex(wxAtoi(strIndex));
432 }
433
434 return true;
435 }
436 }
437
438 // no such file type or no value or incorrect icon entry
439 return false;
440 }
441
442 bool wxFileTypeImpl::GetDescription(wxString *desc) const
443 {
444 // suppress possible error messages
445 wxLogNull nolog;
446 wxRegKey key(wxRegKey::HKCR, m_strFileType);
447
448 if ( key.Open(wxRegKey::Read) ) {
449 // it's the default value of the key
450 if ( key.QueryValue(wxEmptyString, *desc) ) {
451 return true;
452 }
453 }
454
455 return false;
456 }
457
458 // helper function
459 wxFileType *
460 wxMimeTypesManagerImpl::CreateFileType(const wxString& filetype, const wxString& ext)
461 {
462 wxFileType *fileType = new wxFileType;
463 fileType->m_impl->Init(filetype, ext);
464 return fileType;
465 }
466
467 // extension -> file type
468 wxFileType *
469 wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
470 {
471 // add the leading point if necessary
472 wxString str;
473 if ( ext[0u] != wxT('.') ) {
474 str = wxT('.');
475 }
476 str << ext;
477
478 // suppress possible error messages
479 wxLogNull nolog;
480
481 bool knownExtension = false;
482
483 wxString strFileType;
484 wxRegKey key(wxRegKey::HKCR, str);
485 if ( key.Open(wxRegKey::Read) ) {
486 // it's the default value of the key
487 if ( key.QueryValue(wxEmptyString, strFileType) ) {
488 // create the new wxFileType object
489 return CreateFileType(strFileType, ext);
490 }
491 else {
492 // this extension doesn't have a filetype, but it's known to the
493 // system and may be has some other useful keys (open command or
494 // content-type), so still return a file type object for it
495 knownExtension = true;
496 }
497 }
498
499 if ( !knownExtension )
500 {
501 // unknown extension
502 return NULL;
503 }
504
505 return CreateFileType(wxEmptyString, ext);
506 }
507
508 /*
509 wxFileType *
510 wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext)
511 {
512 wxFileType *fileType = GetFileTypeFromExtension(ext);
513 if ( !fileType )
514 {
515 fileType = CreateFileType(wxEmptyString, ext);
516 }
517
518 return fileType;
519 }
520 */
521
522 // MIME type -> extension -> file type
523 wxFileType *
524 wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
525 {
526 wxString strKey = MIME_DATABASE_KEY;
527 strKey << mimeType;
528
529 // suppress possible error messages
530 wxLogNull nolog;
531
532 wxString ext;
533 wxRegKey key(wxRegKey::HKCR, strKey);
534 if ( key.Open(wxRegKey::Read) ) {
535 if ( key.QueryValue(wxT("Extension"), ext) ) {
536 return GetFileTypeFromExtension(ext);
537 }
538 }
539
540 // unknown MIME type
541 return NULL;
542 }
543
544 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
545 {
546 // enumerate all keys under MIME_DATABASE_KEY
547 wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY);
548
549 wxString type;
550 long cookie;
551 bool cont = key.GetFirstKey(type, cookie);
552 while ( cont )
553 {
554 mimetypes.Add(type);
555
556 cont = key.GetNextKey(type, cookie);
557 }
558
559 return mimetypes.GetCount();
560 }
561
562 // ----------------------------------------------------------------------------
563 // create a new association
564 // ----------------------------------------------------------------------------
565
566 wxFileType *wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
567 {
568 wxCHECK_MSG( !ftInfo.GetExtensions().empty(), NULL,
569 wxT("Associate() needs extension") );
570
571 bool ok;
572 size_t iExtCount = 0;
573 wxString filetype;
574 wxString extWithDot;
575
576 wxString ext = ftInfo.GetExtensions()[iExtCount];
577
578 wxCHECK_MSG( !ext.empty(), NULL,
579 wxT("Associate() needs non empty extension") );
580
581 if ( ext[0u] != wxT('.') )
582 extWithDot = wxT('.');
583 extWithDot += ext;
584
585 // start by setting the HKCR\\.ext entries
586 // default is filetype; content type is mimetype
587 const wxString& filetypeOrig = ftInfo.GetShortDesc();
588
589 wxRegKey key(wxRegKey::HKCR, extWithDot);
590 if ( !key.Exists() )
591 {
592 // create the mapping from the extension to the filetype
593 ok = key.Create();
594 if ( ok )
595 {
596
597 if ( filetypeOrig.empty() )
598 {
599 // make it up from the extension
600 filetype << extWithDot.c_str() + 1 << wxT("_file");
601 }
602 else
603 {
604 // just use the provided one
605 filetype = filetypeOrig;
606 }
607
608 key.SetValue(wxEmptyString, filetype);
609 }
610 }
611 else
612 {
613 // key already exists, maybe we want to change it ??
614 if (!filetypeOrig.empty())
615 {
616 filetype = filetypeOrig;
617 key.SetValue(wxEmptyString, filetype);
618 }
619 else
620 {
621 key.QueryValue(wxEmptyString, filetype);
622 }
623 }
624
625 // now set a mimetypeif we have it, but ignore it if none
626 const wxString& mimetype = ftInfo.GetMimeType();
627 if ( !mimetype.empty() )
628 {
629 // set the MIME type
630 ok = key.SetValue(wxT("Content Type"), mimetype);
631
632 if ( ok )
633 {
634 // create the MIME key
635 wxString strKey = MIME_DATABASE_KEY;
636 strKey << mimetype;
637 wxRegKey keyMIME(wxRegKey::HKCR, strKey);
638 ok = keyMIME.Create();
639
640 if ( ok )
641 {
642 // and provide a back link to the extension
643 keyMIME.SetValue(wxT("Extension"), extWithDot);
644 }
645 }
646 }
647
648
649 // now make other extensions have the same filetype
650
651 for (iExtCount=1; iExtCount < ftInfo.GetExtensionsCount(); iExtCount++ )
652 {
653 ext = ftInfo.GetExtensions()[iExtCount];
654 if ( ext[0u] != wxT('.') )
655 extWithDot = wxT('.');
656 extWithDot += ext;
657
658 wxRegKey key2(wxRegKey::HKCR, extWithDot);
659 if ( !key2.Exists() )
660 key2.Create();
661 key2.SetValue(wxEmptyString, filetype);
662
663 // now set any mimetypes we may have, but ignore it if none
664 const wxString& mimetype2 = ftInfo.GetMimeType();
665 if ( !mimetype2.empty() )
666 {
667 // set the MIME type
668 ok = key2.SetValue(wxT("Content Type"), mimetype2);
669
670 if ( ok )
671 {
672 // create the MIME key
673 wxString strKey = MIME_DATABASE_KEY;
674 strKey << mimetype2;
675 wxRegKey keyMIME(wxRegKey::HKCR, strKey);
676 ok = keyMIME.Create();
677
678 if ( ok )
679 {
680 // and provide a back link to the extension
681 keyMIME.SetValue(wxT("Extension"), extWithDot);
682 }
683 }
684 }
685
686 } // end of for loop; all extensions now point to HKCR\.ext\Default
687
688 // create the filetype key itself (it will be empty for now, but
689 // SetCommand(), SetDefaultIcon() &c will use it later)
690 wxRegKey keyFT(wxRegKey::HKCR, filetype);
691 keyFT.Create();
692
693 wxFileType *ft = CreateFileType(filetype, extWithDot);
694
695 if (ft)
696 {
697 if (! ftInfo.GetOpenCommand ().empty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) );
698 if (! ftInfo.GetPrintCommand().empty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) );
699 // chris: I don't like the ->m_impl-> here FIX this ??
700 if (! ftInfo.GetDescription ().empty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ;
701 if (! ftInfo.GetIconFile().empty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() );
702
703 }
704
705 return ft;
706 }
707
708 bool wxFileTypeImpl::SetCommand(const wxString& cmd,
709 const wxString& verb,
710 bool WXUNUSED(overwriteprompt))
711 {
712 wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false,
713 wxT("SetCommand() needs an extension and a verb") );
714
715 if ( !EnsureExtKeyExists() )
716 return false;
717
718 wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
719 #if 0
720 if ( rkey.Exists() && overwriteprompt )
721 {
722 #if wxUSE_GUI
723 wxString old;
724 rkey.QueryValue(wxEmptyString, old);
725 if ( wxMessageBox
726 (
727 wxString::Format(
728 _("Do you want to overwrite the command used to %s "
729 "files with extension \"%s\" ?\nCurrent value is \n%s, "
730 "\nNew value is \n%s %1"), // bug here FIX need %1 ??
731 verb.c_str(),
732 m_ext.c_str(),
733 old.c_str(),
734 cmd.c_str()),
735 _("Confirm registry update"),
736 wxYES_NO | wxICON_QUESTION
737 ) != wxYES )
738 #endif // wxUSE_GUI
739 {
740 // cancelled by user
741 return false;
742 }
743 }
744 #endif
745 // TODO:
746 // 1. translate '%s' to '%1' instead of always adding it
747 // 2. create DDEExec value if needed (undo GetCommand)
748 return rkey.Create() && rkey.SetValue(wxEmptyString, cmd + wxT(" \"%1\"") );
749 }
750
751 /* // no longer used
752 bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig)
753 {
754 wxCHECK_MSG( !m_ext.empty(), false, wxT("SetMimeType() needs extension") );
755
756 if ( !EnsureExtKeyExists() )
757 return false;
758
759 // VZ: is this really useful? (FIXME)
760 wxString mimeType;
761 if ( !mimeTypeOrig )
762 {
763 // make up a default value for it
764 wxString cmd;
765 wxFileName::SplitPath(GetCommand(wxT("open")), NULL, &cmd, NULL);
766 mimeType << wxT("application/x-") << cmd;
767 }
768 else
769 {
770 mimeType = mimeTypeOrig;
771 }
772
773 wxRegKey rkey(wxRegKey::HKCR, m_ext);
774 return rkey.Create() && rkey.SetValue(wxT("Content Type"), mimeType);
775 }
776 */
777
778 bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index)
779 {
780 wxCHECK_MSG( !m_ext.empty(), false, wxT("SetDefaultIcon() needs extension") );
781 wxCHECK_MSG( !m_strFileType.empty(), false, wxT("File key not found") );
782 // the next line fails on a SMBshare, I think because it is case mangled
783 // wxCHECK_MSG( !wxFileExists(cmd), false, wxT("Icon file not found.") );
784
785 if ( !EnsureExtKeyExists() )
786 return false;
787
788 wxRegKey rkey(wxRegKey::HKCR, m_strFileType + wxT("\\DefaultIcon"));
789
790 return rkey.Create() &&
791 rkey.SetValue(wxEmptyString,
792 wxString::Format(wxT("%s,%d"), cmd.c_str(), index));
793 }
794
795 bool wxFileTypeImpl::SetDescription (const wxString& desc)
796 {
797 wxCHECK_MSG( !m_strFileType.empty(), false, wxT("File key not found") );
798 wxCHECK_MSG( !desc.empty(), false, wxT("No file description supplied") );
799
800 if ( !EnsureExtKeyExists() )
801 return false;
802
803 wxRegKey rkey(wxRegKey::HKCR, m_strFileType );
804
805 return rkey.Create() &&
806 rkey.SetValue(wxEmptyString, desc);
807 }
808
809 // ----------------------------------------------------------------------------
810 // remove file association
811 // ----------------------------------------------------------------------------
812
813 bool wxFileTypeImpl::Unassociate()
814 {
815 bool result = true;
816 if ( !RemoveOpenCommand() )
817 result = false;
818 if ( !RemoveDefaultIcon() )
819 result = false;
820 if ( !RemoveMimeType() )
821 result = false;
822 if ( !RemoveDescription() )
823 result = false;
824
825 /*
826 //this might hold other keys, eg some have CSLID keys
827 if ( result )
828 {
829 // delete the root key
830 wxRegKey key(wxRegKey::HKCR, m_ext);
831 if ( key.Exists() )
832 result = key.DeleteSelf();
833 }
834 */
835 return result;
836 }
837
838 bool wxFileTypeImpl::RemoveOpenCommand()
839 {
840 return RemoveCommand(wxT("open"));
841 }
842
843 bool wxFileTypeImpl::RemoveCommand(const wxString& verb)
844 {
845 wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false,
846 wxT("RemoveCommand() needs an extension and a verb") );
847
848 wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb));
849
850 // if the key already doesn't exist, it's a success
851 return !rkey.Exists() || rkey.DeleteSelf();
852 }
853
854 bool wxFileTypeImpl::RemoveMimeType()
855 {
856 wxCHECK_MSG( !m_ext.empty(), false, wxT("RemoveMimeType() needs extension") );
857
858 wxRegKey rkey(wxRegKey::HKCR, m_ext);
859 return !rkey.Exists() || rkey.DeleteSelf();
860 }
861
862 bool wxFileTypeImpl::RemoveDefaultIcon()
863 {
864 wxCHECK_MSG( !m_ext.empty(), false,
865 wxT("RemoveDefaultIcon() needs extension") );
866
867 wxRegKey rkey (wxRegKey::HKCR, m_strFileType + wxT("\\DefaultIcon"));
868 return !rkey.Exists() || rkey.DeleteSelf();
869 }
870
871 bool wxFileTypeImpl::RemoveDescription()
872 {
873 wxCHECK_MSG( !m_ext.empty(), false,
874 wxT("RemoveDescription() needs extension") );
875
876 wxRegKey rkey (wxRegKey::HKCR, m_strFileType );
877 return !rkey.Exists() || rkey.DeleteSelf();
878 }
879
880 #endif // wxUSE_MIMETYPE