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