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