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