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