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