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