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