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