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