/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/mimetype.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/msw/mimetype.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22