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