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