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