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