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