]>
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 license (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) const | |
340 | { | |
341 | #if wxUSE_GUI | |
342 | wxString strIconKey; | |
343 | strIconKey << m_strFileType << wxT("\\DefaultIcon"); | |
344 | ||
345 | // suppress possible error messages | |
346 | wxLogNull nolog; | |
347 | wxRegKey key(wxRegKey::HKCR, strIconKey); | |
348 | ||
349 | if ( key.Open() ) { | |
350 | wxString strIcon; | |
351 | // it's the default value of the key | |
352 | if ( key.QueryValue(wxT(""), strIcon) ) { | |
353 | // the format is the following: <full path to file>, <icon index> | |
354 | // NB: icon index may be negative as well as positive and the full | |
355 | // path may contain the environment variables inside '%' | |
356 | wxString strFullPath = strIcon.BeforeLast(wxT(',')), | |
357 | strIndex = strIcon.AfterLast(wxT(',')); | |
358 | ||
359 | // index may be omitted, in which case BeforeLast(',') is empty and | |
360 | // AfterLast(',') is the whole string | |
361 | if ( strFullPath.IsEmpty() ) { | |
362 | strFullPath = strIndex; | |
363 | strIndex = wxT("0"); | |
364 | } | |
365 | ||
366 | wxString strExpPath = wxExpandEnvVars(strFullPath); | |
367 | // here we need C based counting! | |
368 | int nIndex = wxAtoi(strIndex); | |
369 | ||
370 | HICON hIcon = ExtractIcon(GetModuleHandle(NULL), strExpPath, nIndex); | |
371 | switch ( (int)hIcon ) { | |
372 | case 0: // means no icons were found | |
373 | case 1: // means no such file or it wasn't a DLL/EXE/OCX/ICO/... | |
374 | wxLogDebug(wxT("incorrect registry entry '%s': no such icon."), | |
375 | key.GetName().c_str()); | |
376 | break; | |
377 | ||
378 | default: | |
379 | icon->SetHICON((WXHICON)hIcon); | |
380 | wxSize size = wxGetHiconSize(hIcon); | |
381 | icon->SetSize(size); | |
382 | if ( iconIndex ) | |
383 | *iconIndex = nIndex; | |
384 | if ( iconFile ) | |
385 | *iconFile = strFullPath; | |
386 | return TRUE; | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | // no such file type or no value or incorrect icon entry | |
392 | #endif // wxUSE_GUI | |
393 | ||
394 | return FALSE; | |
395 | } | |
396 | ||
397 | bool wxFileTypeImpl::GetDescription(wxString *desc) const | |
398 | { | |
399 | // suppress possible error messages | |
400 | wxLogNull nolog; | |
401 | wxRegKey key(wxRegKey::HKCR, m_strFileType); | |
402 | ||
403 | if ( key.Open() ) { | |
404 | // it's the default value of the key | |
405 | if ( key.QueryValue(wxT(""), *desc) ) { | |
406 | return TRUE; | |
407 | } | |
408 | } | |
409 | ||
410 | return FALSE; | |
411 | } | |
412 | ||
413 | // helper function | |
414 | wxFileType * | |
415 | wxMimeTypesManagerImpl::CreateFileType(const wxString& filetype, const wxString& ext) | |
416 | { | |
417 | wxFileType *fileType = new wxFileType; | |
418 | fileType->m_impl->Init(filetype, ext); | |
419 | return fileType; | |
420 | } | |
421 | ||
422 | // extension -> file type | |
423 | wxFileType * | |
424 | wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext) | |
425 | { | |
426 | // add the leading point if necessary | |
427 | wxString str; | |
428 | if ( ext[0u] != wxT('.') ) { | |
429 | str = wxT('.'); | |
430 | } | |
431 | str << ext; | |
432 | ||
433 | // suppress possible error messages | |
434 | wxLogNull nolog; | |
435 | ||
436 | bool knownExtension = FALSE; | |
437 | ||
438 | wxString strFileType; | |
439 | wxRegKey key(wxRegKey::HKCR, str); | |
440 | if ( key.Open() ) { | |
441 | // it's the default value of the key | |
442 | if ( key.QueryValue(wxT(""), strFileType) ) { | |
443 | // create the new wxFileType object | |
444 | return CreateFileType(strFileType, ext); | |
445 | } | |
446 | else { | |
447 | // this extension doesn't have a filetype, but it's known to the | |
448 | // system and may be has some other useful keys (open command or | |
449 | // content-type), so still return a file type object for it | |
450 | knownExtension = TRUE; | |
451 | } | |
452 | } | |
453 | ||
454 | if ( !knownExtension ) | |
455 | { | |
456 | // unknown extension | |
457 | return NULL; | |
458 | } | |
459 | ||
460 | return CreateFileType(wxEmptyString, ext); | |
461 | } | |
462 | ||
463 | /* | |
464 | wxFileType * | |
465 | wxMimeTypesManagerImpl::GetOrAllocateFileTypeFromExtension(const wxString& ext) | |
466 | { | |
467 | wxFileType *fileType = GetFileTypeFromExtension(ext); | |
468 | if ( !fileType ) | |
469 | { | |
470 | fileType = CreateFileType(wxEmptyString, ext); | |
471 | } | |
472 | ||
473 | return fileType; | |
474 | } | |
475 | */ | |
476 | ||
477 | // MIME type -> extension -> file type | |
478 | wxFileType * | |
479 | wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType) | |
480 | { | |
481 | wxString strKey = MIME_DATABASE_KEY; | |
482 | strKey << mimeType; | |
483 | ||
484 | // suppress possible error messages | |
485 | wxLogNull nolog; | |
486 | ||
487 | wxString ext; | |
488 | wxRegKey key(wxRegKey::HKCR, strKey); | |
489 | if ( key.Open() ) { | |
490 | if ( key.QueryValue(wxT("Extension"), ext) ) { | |
491 | return GetFileTypeFromExtension(ext); | |
492 | } | |
493 | } | |
494 | ||
495 | // unknown MIME type | |
496 | return NULL; | |
497 | } | |
498 | ||
499 | size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes) | |
500 | { | |
501 | // enumerate all keys under MIME_DATABASE_KEY | |
502 | wxRegKey key(wxRegKey::HKCR, MIME_DATABASE_KEY); | |
503 | ||
504 | wxString type; | |
505 | long cookie; | |
506 | bool cont = key.GetFirstKey(type, cookie); | |
507 | while ( cont ) | |
508 | { | |
509 | mimetypes.Add(type); | |
510 | ||
511 | cont = key.GetNextKey(type, cookie); | |
512 | } | |
513 | ||
514 | return mimetypes.GetCount(); | |
515 | } | |
516 | ||
517 | // ---------------------------------------------------------------------------- | |
518 | // create a new association | |
519 | // ---------------------------------------------------------------------------- | |
520 | ||
521 | wxFileType *wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo) | |
522 | { | |
523 | wxCHECK_MSG( !ftInfo.GetExtensions().IsEmpty(), NULL, | |
524 | _T("Associate() needs extension") ); | |
525 | ||
526 | bool ok = FALSE ; | |
527 | int iExtCount = 0 ; | |
528 | wxString filetype; | |
529 | wxString extWithDot; | |
530 | ||
531 | wxString ext = ftInfo.GetExtensions()[iExtCount]; | |
532 | ||
533 | wxCHECK_MSG( !ext.empty(), NULL, | |
534 | _T("Associate() needs non empty extension") ); | |
535 | ||
536 | if ( ext[0u] != _T('.') ) | |
537 | extWithDot = _T('.'); | |
538 | extWithDot += ext; | |
539 | ||
540 | // start by setting the HKCR\\.ext entries | |
541 | // default is filetype; content type is mimetype | |
542 | const wxString& filetypeOrig = ftInfo.GetShortDesc(); | |
543 | ||
544 | wxRegKey key(wxRegKey::HKCR, extWithDot); | |
545 | if ( !key.Exists() ) | |
546 | { | |
547 | // create the mapping from the extension to the filetype | |
548 | ok = key.Create(); | |
549 | if ( ok ) | |
550 | { | |
551 | ||
552 | if ( filetypeOrig.empty() ) | |
553 | { | |
554 | // make it up from the extension | |
555 | filetype << extWithDot.c_str() + 1 << _T("_file"); | |
556 | } | |
557 | else | |
558 | { | |
559 | // just use the provided one | |
560 | filetype = filetypeOrig; | |
561 | } | |
562 | ||
563 | ok = key.SetValue(_T(""), filetype); | |
564 | } | |
565 | } | |
566 | else | |
567 | { | |
568 | // key already exists, maybe we want to change it ?? | |
569 | if (!filetypeOrig.empty()) | |
570 | { | |
571 | filetype = filetypeOrig; | |
572 | ok = key.SetValue(_T(""), filetype); | |
573 | } | |
574 | else | |
575 | { | |
576 | ok = key.QueryValue(_T(""), filetype); | |
577 | } | |
578 | } | |
579 | // now set a mimetypeif we have it, but ignore it if none | |
580 | const wxString& mimetype = ftInfo.GetMimeType(); | |
581 | if ( !mimetype.empty() ) | |
582 | { | |
583 | // set the MIME type | |
584 | ok = key.SetValue(_T("Content Type"), mimetype); | |
585 | ||
586 | if ( ok ) | |
587 | { | |
588 | // create the MIME key | |
589 | wxString strKey = MIME_DATABASE_KEY; | |
590 | strKey << mimetype; | |
591 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); | |
592 | ok = keyMIME.Create(); | |
593 | ||
594 | if ( ok ) | |
595 | { | |
596 | // and provide a back link to the extension | |
597 | ok = keyMIME.SetValue(_T("Extension"), extWithDot); | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | ||
603 | // now make other extensions have the same filetype | |
604 | ||
605 | for (iExtCount=1; iExtCount < ftInfo.GetExtensionsCount(); iExtCount++ ) | |
606 | { | |
607 | ext = ftInfo.GetExtensions()[iExtCount]; | |
608 | if ( ext[0u] != _T('.') ) | |
609 | extWithDot = _T('.'); | |
610 | extWithDot += ext; | |
611 | ||
612 | wxRegKey key(wxRegKey::HKCR, extWithDot); | |
613 | if ( !key.Exists() ) ok = key.Create(); | |
614 | ok = key.SetValue(_T(""), filetype); | |
615 | ||
616 | // now set any mimetypes we may have, but ignore it if none | |
617 | const wxString& mimetype = ftInfo.GetMimeType(); | |
618 | if ( !mimetype.empty() ) | |
619 | { | |
620 | // set the MIME type | |
621 | ok = key.SetValue(_T("Content Type"), mimetype); | |
622 | ||
623 | if ( ok ) | |
624 | { | |
625 | // create the MIME key | |
626 | wxString strKey = MIME_DATABASE_KEY; | |
627 | strKey << mimetype; | |
628 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); | |
629 | ok = keyMIME.Create(); | |
630 | ||
631 | if ( ok ) | |
632 | { | |
633 | // and provide a back link to the extension | |
634 | ok = keyMIME.SetValue(_T("Extension"), extWithDot); | |
635 | } | |
636 | } | |
637 | } | |
638 | ||
639 | ||
640 | } // end of for loop; all extensions now point to HKCR\.ext\Default | |
641 | ||
642 | // create the filetype key itself (it will be empty for now, but | |
643 | // SetCommand(), SetDefaultIcon() &c will use it later) | |
644 | wxRegKey keyFT(wxRegKey::HKCR, filetype); | |
645 | ok = keyFT.Create(); | |
646 | ||
647 | wxFileType *ft = NULL; | |
648 | ft = CreateFileType(filetype, extWithDot); | |
649 | ||
650 | if (ft) | |
651 | { | |
652 | if (! ftInfo.GetOpenCommand ().IsEmpty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) ); | |
653 | if (! ftInfo.GetPrintCommand().IsEmpty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) ); | |
654 | // chris: I don't like the ->m_impl-> here FIX this ?? | |
655 | if (! ftInfo.GetDescription ().IsEmpty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ; | |
656 | if (! ftInfo.GetIconFile().IsEmpty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() ); | |
657 | ||
658 | } | |
659 | return ft; | |
660 | } | |
661 | ||
662 | bool wxFileTypeImpl::SetCommand(const wxString& cmd, | |
663 | const wxString& verb, | |
664 | bool WXUNUSED(overwriteprompt)) | |
665 | { | |
666 | wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE, | |
667 | _T("SetCommand() needs an extension and a verb") ); | |
668 | ||
669 | if ( !EnsureExtKeyExists() ) | |
670 | return FALSE; | |
671 | ||
672 | wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb)); | |
673 | #if 0 | |
674 | if ( rkey.Exists() && overwriteprompt ) | |
675 | { | |
676 | #if wxUSE_GUI | |
677 | wxString old; | |
678 | rkey.QueryValue(wxT(""), old); | |
679 | if ( wxMessageBox | |
680 | ( | |
681 | wxString::Format( | |
682 | _("Do you want to overwrite the command used to %s " | |
683 | "files with extension \"%s\" ?\nCurrent value is \n%s, " | |
684 | "\nNew value is \n%s %1"), // bug here FIX need %1 ?? | |
685 | verb.c_str(), | |
686 | m_ext.c_str(), | |
687 | old.c_str(), | |
688 | cmd.c_str()), | |
689 | _("Confirm registry update"), | |
690 | wxYES_NO | wxICON_QUESTION | |
691 | ) != wxYES ) | |
692 | #endif // wxUSE_GUI | |
693 | { | |
694 | // cancelled by user | |
695 | return FALSE; | |
696 | } | |
697 | } | |
698 | #endif | |
699 | // TODO: | |
700 | // 1. translate '%s' to '%1' instead of always adding it | |
701 | // 2. create DDEExec value if needed (undo GetCommand) | |
702 | return rkey.Create() && rkey.SetValue(_T(""), cmd + _T(" \"%1\"") ); | |
703 | } | |
704 | ||
705 | /* // no longer used | |
706 | bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig) | |
707 | { | |
708 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetMimeType() needs extension") ); | |
709 | ||
710 | if ( !EnsureExtKeyExists() ) | |
711 | return FALSE; | |
712 | ||
713 | // VZ: is this really useful? (FIXME) | |
714 | wxString mimeType; | |
715 | if ( !mimeTypeOrig ) | |
716 | { | |
717 | // make up a default value for it | |
718 | wxString cmd; | |
719 | wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL); | |
720 | mimeType << _T("application/x-") << cmd; | |
721 | } | |
722 | else | |
723 | { | |
724 | mimeType = mimeTypeOrig; | |
725 | } | |
726 | ||
727 | wxRegKey rkey(wxRegKey::HKCR, m_ext); | |
728 | return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType); | |
729 | } | |
730 | */ | |
731 | ||
732 | bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index) | |
733 | { | |
734 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("SetDefaultIcon() needs extension") ); | |
735 | wxCHECK_MSG( !m_strFileType.IsEmpty(), FALSE, _T("File key not found") ); | |
736 | // the next line fails on a SMBshare, I think because it is case mangled | |
737 | // wxCHECK_MSG( !wxFileExists(cmd), FALSE, _T("Icon file not found.") ); | |
738 | ||
739 | if ( !EnsureExtKeyExists() ) | |
740 | return FALSE; | |
741 | ||
742 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon")); | |
743 | ||
744 | return rkey.Create() && | |
745 | rkey.SetValue(_T(""), | |
746 | wxString::Format(_T("%s,%d"), cmd.c_str(), index)); | |
747 | } | |
748 | ||
749 | bool wxFileTypeImpl::SetDescription (const wxString& desc) | |
750 | { | |
751 | wxCHECK_MSG( !m_strFileType.IsEmpty(), FALSE, _T("File key not found") ); | |
752 | wxCHECK_MSG( !desc.IsEmpty(), FALSE, _T("No file description supplied") ); | |
753 | ||
754 | if ( !EnsureExtKeyExists() ) | |
755 | return FALSE; | |
756 | ||
757 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType ); | |
758 | ||
759 | return rkey.Create() && | |
760 | rkey.SetValue(_T(""), desc); | |
761 | } | |
762 | ||
763 | // ---------------------------------------------------------------------------- | |
764 | // remove file association | |
765 | // ---------------------------------------------------------------------------- | |
766 | ||
767 | bool wxFileTypeImpl::Unassociate() | |
768 | { | |
769 | bool result = TRUE; | |
770 | if ( !RemoveOpenCommand() ) | |
771 | result = FALSE; | |
772 | if ( !RemoveDefaultIcon() ) | |
773 | result = FALSE; | |
774 | if ( !RemoveMimeType() ) | |
775 | result = FALSE; | |
776 | if ( !RemoveDescription() ) | |
777 | result = FALSE; | |
778 | ||
779 | /* | |
780 | //this might hold other keys, eg some have CSLID keys | |
781 | if ( result ) | |
782 | { | |
783 | // delete the root key | |
784 | wxRegKey key(wxRegKey::HKCR, m_ext); | |
785 | if ( key.Exists() ) | |
786 | result = key.DeleteSelf(); | |
787 | } | |
788 | */ | |
789 | return result; | |
790 | } | |
791 | ||
792 | bool wxFileTypeImpl::RemoveOpenCommand() | |
793 | { | |
794 | return RemoveCommand(_T("open")); | |
795 | } | |
796 | ||
797 | bool wxFileTypeImpl::RemoveCommand(const wxString& verb) | |
798 | { | |
799 | wxCHECK_MSG( !m_ext.IsEmpty() && !verb.IsEmpty(), FALSE, | |
800 | _T("RemoveCommand() needs an extension and a verb") ); | |
801 | ||
802 | wxString sKey = m_strFileType; | |
803 | wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb)); | |
804 | ||
805 | // if the key already doesn't exist, it's a success | |
806 | return !rkey.Exists() || rkey.DeleteSelf(); | |
807 | } | |
808 | ||
809 | bool wxFileTypeImpl::RemoveMimeType() | |
810 | { | |
811 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, _T("RemoveMimeType() needs extension") ); | |
812 | ||
813 | wxRegKey rkey(wxRegKey::HKCR, m_ext); | |
814 | return !rkey.Exists() || rkey.DeleteSelf(); | |
815 | } | |
816 | ||
817 | bool wxFileTypeImpl::RemoveDefaultIcon() | |
818 | { | |
819 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, | |
820 | _T("RemoveDefaultIcon() needs extension") ); | |
821 | ||
822 | wxRegKey rkey (wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon")); | |
823 | return !rkey.Exists() || rkey.DeleteSelf(); | |
824 | } | |
825 | ||
826 | bool wxFileTypeImpl::RemoveDescription() | |
827 | { | |
828 | wxCHECK_MSG( !m_ext.IsEmpty(), FALSE, | |
829 | _T("RemoveDescription() needs extension") ); | |
830 | ||
831 | wxRegKey rkey (wxRegKey::HKCR, m_strFileType ); | |
832 | return !rkey.Exists() || rkey.DeleteSelf(); | |
833 | } | |
834 | ||
835 | #endif | |
836 | // __WIN16__ | |
837 | ||
838 | #endif // wxUSE_MIMETYPE |