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