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