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