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