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