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