]>
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 | ||
6c3afc42 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
7dc3cc31 VS |
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) | |
6e36105b | 107 | wxCHECK_RET( !ext.empty(), _T("needs an extension") ); |
c7ce8392 VZ |
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 | { | |
6e36105b | 131 | wxCHECK_MSG( !m_ext.empty(), 0, _T("GetAllCommands() needs an extension") ); |
c7ce8392 | 132 | |
6e36105b | 133 | if ( m_strFileType.empty() ) |
c7ce8392 VZ |
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()); | |
598ddd96 | 195 | return false; |
c7ce8392 VZ |
196 | } |
197 | } | |
198 | ||
598ddd96 | 199 | return true; |
c7ce8392 VZ |
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 | ||
6e36105b WS |
241 | wxRegKey keyServer(wxRegKey::HKCR, strKey + _T("\\Application")); |
242 | keyServer.QueryValue(wxEmptyString, ddeServer); | |
243 | wxRegKey keyTopic(wxRegKey::HKCR, strKey + _T("\\Topic")); | |
244 | keyTopic.QueryValue(wxEmptyString, ddeTopic); | |
7dc3cc31 | 245 | |
6e36105b | 246 | if (ddeTopic.empty()) |
2b5f62a0 VZ |
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 | ||
6e36105b | 284 | return !openCmd->empty(); |
7dc3cc31 VS |
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 | ||
6e36105b | 296 | return !printCmd->empty(); |
7dc3cc31 VS |
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 | { | |
6e36105b | 306 | if ( m_ext.empty() ) { |
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... | |
598ddd96 | 309 | return false; |
7dc3cc31 VS |
310 | } |
311 | else { | |
312 | extensions.Empty(); | |
313 | extensions.Add(m_ext); | |
314 | ||
315 | // it's a lie too, we don't return _all_ extensions... | |
598ddd96 | 316 | return true; |
7dc3cc31 VS |
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 | { |
598ddd96 | 336 | return false; |
f6bcfd97 BP |
337 | } |
338 | ||
339 | mimeTypes.Clear(); | |
340 | mimeTypes.Add(s); | |
598ddd96 | 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 | |
6e36105b | 366 | if ( strFullPath.empty() ) { |
7dc3cc31 VS |
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 | 377 | |
598ddd96 | 378 | return true; |
7dc3cc31 VS |
379 | } |
380 | } | |
381 | ||
382 | // no such file type or no value or incorrect icon entry | |
598ddd96 | 383 | return false; |
7dc3cc31 VS |
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) ) { |
598ddd96 | 395 | return true; |
7dc3cc31 VS |
396 | } |
397 | } | |
398 | ||
598ddd96 | 399 | return false; |
7dc3cc31 VS |
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 | ||
598ddd96 | 425 | bool knownExtension = false; |
7dc3cc31 VS |
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 | |
598ddd96 | 439 | knownExtension = true; |
7dc3cc31 VS |
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 | { |
6e36105b | 512 | wxCHECK_MSG( !ftInfo.GetExtensions().empty(), NULL, |
a6c65e88 VZ |
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 | } |
92218ce6 WS |
554 | } |
555 | else | |
556 | { | |
557 | // key already exists, maybe we want to change it ?? | |
558 | if (!filetypeOrig.empty()) | |
559 | { | |
560 | filetype = filetypeOrig; | |
561 | key.SetValue(wxEmptyString, filetype); | |
2b813b73 VZ |
562 | } |
563 | else | |
564 | { | |
92218ce6 | 565 | key.QueryValue(wxEmptyString, filetype); |
2b813b73 | 566 | } |
92218ce6 WS |
567 | } |
568 | ||
569 | // now set a mimetypeif we have it, but ignore it if none | |
570 | const wxString& mimetype = ftInfo.GetMimeType(); | |
571 | if ( !mimetype.empty() ) | |
572 | { | |
573 | // set the MIME type | |
574 | ok = key.SetValue(_T("Content Type"), mimetype); | |
575 | ||
576 | if ( ok ) | |
c7ce8392 | 577 | { |
92218ce6 WS |
578 | // create the MIME key |
579 | wxString strKey = MIME_DATABASE_KEY; | |
580 | strKey << mimetype; | |
581 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); | |
582 | ok = keyMIME.Create(); | |
c7ce8392 VZ |
583 | |
584 | if ( ok ) | |
585 | { | |
92218ce6 WS |
586 | // and provide a back link to the extension |
587 | keyMIME.SetValue(_T("Extension"), extWithDot); | |
c7ce8392 VZ |
588 | } |
589 | } | |
92218ce6 | 590 | } |
c7ce8392 | 591 | |
2b813b73 VZ |
592 | |
593 | // now make other extensions have the same filetype | |
33ac7e6f | 594 | |
2b813b73 | 595 | for (iExtCount=1; iExtCount < ftInfo.GetExtensionsCount(); iExtCount++ ) |
92218ce6 WS |
596 | { |
597 | ext = ftInfo.GetExtensions()[iExtCount]; | |
598 | if ( ext[0u] != _T('.') ) | |
599 | extWithDot = _T('.'); | |
600 | extWithDot += ext; | |
2b813b73 | 601 | |
92218ce6 WS |
602 | wxRegKey key(wxRegKey::HKCR, extWithDot); |
603 | if ( !key.Exists() ) key.Create(); | |
604 | key.SetValue(wxEmptyString, filetype); | |
2b813b73 VZ |
605 | |
606 | // now set any mimetypes we may have, but ignore it if none | |
607 | const wxString& mimetype = ftInfo.GetMimeType(); | |
608 | if ( !mimetype.empty() ) | |
609 | { | |
610 | // set the MIME type | |
611 | ok = key.SetValue(_T("Content Type"), mimetype); | |
612 | ||
92218ce6 WS |
613 | if ( ok ) |
614 | { | |
2b813b73 VZ |
615 | // create the MIME key |
616 | wxString strKey = MIME_DATABASE_KEY; | |
617 | strKey << mimetype; | |
618 | wxRegKey keyMIME(wxRegKey::HKCR, strKey); | |
619 | ok = keyMIME.Create(); | |
c7ce8392 | 620 | |
92218ce6 WS |
621 | if ( ok ) |
622 | { | |
2b813b73 | 623 | // and provide a back link to the extension |
999836aa | 624 | keyMIME.SetValue(_T("Extension"), extWithDot); |
92218ce6 WS |
625 | } |
626 | } | |
c7ce8392 | 627 | } |
2b813b73 VZ |
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 | { |
92218ce6 WS |
640 | if (! ftInfo.GetOpenCommand ().empty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) ); |
641 | if (! ftInfo.GetPrintCommand().empty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) ); | |
642 | // chris: I don't like the ->m_impl-> here FIX this ?? | |
643 | if (! ftInfo.GetDescription ().empty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ; | |
644 | if (! ftInfo.GetIconFile().empty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() ); | |
645 | ||
646 | } | |
c7ce8392 VZ |
647 | |
648 | return ft; | |
649 | } | |
7dc3cc31 | 650 | |
a6c65e88 VZ |
651 | bool wxFileTypeImpl::SetCommand(const wxString& cmd, |
652 | const wxString& verb, | |
33ac7e6f | 653 | bool WXUNUSED(overwriteprompt)) |
a6c65e88 | 654 | { |
6e36105b | 655 | wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false, |
a6c65e88 VZ |
656 | _T("SetCommand() needs an extension and a verb") ); |
657 | ||
658 | if ( !EnsureExtKeyExists() ) | |
598ddd96 | 659 | return false; |
a6c65e88 VZ |
660 | |
661 | wxRegKey rkey(wxRegKey::HKCR, GetVerbPath(verb)); | |
2b813b73 | 662 | #if 0 |
a6c65e88 VZ |
663 | if ( rkey.Exists() && overwriteprompt ) |
664 | { | |
665 | #if wxUSE_GUI | |
666 | wxString old; | |
fda7962d | 667 | rkey.QueryValue(wxEmptyString, old); |
a6c65e88 VZ |
668 | if ( wxMessageBox |
669 | ( | |
670 | wxString::Format( | |
671 | _("Do you want to overwrite the command used to %s " | |
2b813b73 VZ |
672 | "files with extension \"%s\" ?\nCurrent value is \n%s, " |
673 | "\nNew value is \n%s %1"), // bug here FIX need %1 ?? | |
a6c65e88 VZ |
674 | verb.c_str(), |
675 | m_ext.c_str(), | |
676 | old.c_str(), | |
677 | cmd.c_str()), | |
678 | _("Confirm registry update"), | |
679 | wxYES_NO | wxICON_QUESTION | |
680 | ) != wxYES ) | |
681 | #endif // wxUSE_GUI | |
682 | { | |
683 | // cancelled by user | |
598ddd96 | 684 | return false; |
a6c65e88 VZ |
685 | } |
686 | } | |
2b813b73 | 687 | #endif |
a6c65e88 VZ |
688 | // TODO: |
689 | // 1. translate '%s' to '%1' instead of always adding it | |
690 | // 2. create DDEExec value if needed (undo GetCommand) | |
fda7962d | 691 | return rkey.Create() && rkey.SetValue(wxEmptyString, cmd + _T(" \"%1\"") ); |
a6c65e88 VZ |
692 | } |
693 | ||
2b813b73 | 694 | /* // no longer used |
a6c65e88 VZ |
695 | bool wxFileTypeImpl::SetMimeType(const wxString& mimeTypeOrig) |
696 | { | |
6e36105b | 697 | wxCHECK_MSG( !m_ext.empty(), false, _T("SetMimeType() needs extension") ); |
a6c65e88 VZ |
698 | |
699 | if ( !EnsureExtKeyExists() ) | |
598ddd96 | 700 | return false; |
a6c65e88 VZ |
701 | |
702 | // VZ: is this really useful? (FIXME) | |
703 | wxString mimeType; | |
704 | if ( !mimeTypeOrig ) | |
705 | { | |
706 | // make up a default value for it | |
707 | wxString cmd; | |
708 | wxSplitPath(GetCommand(_T("open")), NULL, &cmd, NULL); | |
709 | mimeType << _T("application/x-") << cmd; | |
710 | } | |
711 | else | |
712 | { | |
713 | mimeType = mimeTypeOrig; | |
714 | } | |
715 | ||
716 | wxRegKey rkey(wxRegKey::HKCR, m_ext); | |
717 | return rkey.Create() && rkey.SetValue(_T("Content Type"), mimeType); | |
718 | } | |
2b813b73 | 719 | */ |
a6c65e88 VZ |
720 | |
721 | bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index) | |
722 | { | |
6e36105b WS |
723 | wxCHECK_MSG( !m_ext.empty(), false, _T("SetDefaultIcon() needs extension") ); |
724 | wxCHECK_MSG( !m_strFileType.empty(), false, _T("File key not found") ); | |
2b813b73 | 725 | // the next line fails on a SMBshare, I think because it is case mangled |
598ddd96 | 726 | // wxCHECK_MSG( !wxFileExists(cmd), false, _T("Icon file not found.") ); |
a6c65e88 VZ |
727 | |
728 | if ( !EnsureExtKeyExists() ) | |
598ddd96 | 729 | return false; |
a6c65e88 VZ |
730 | |
731 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType + _T("\\DefaultIcon")); | |
732 | ||
733 | return rkey.Create() && | |
fda7962d | 734 | rkey.SetValue(wxEmptyString, |
a6c65e88 VZ |
735 | wxString::Format(_T("%s,%d"), cmd.c_str(), index)); |
736 | } | |
737 | ||
2b813b73 VZ |
738 | bool wxFileTypeImpl::SetDescription (const wxString& desc) |
739 | { | |
6e36105b WS |
740 | wxCHECK_MSG( !m_strFileType.empty(), false, _T("File key not found") ); |
741 | wxCHECK_MSG( !desc.empty(), false, _T("No file description supplied") ); | |
2b813b73 VZ |
742 | |
743 | if ( !EnsureExtKeyExists() ) | |
598ddd96 | 744 | return false; |
2b813b73 VZ |
745 | |
746 | wxRegKey rkey(wxRegKey::HKCR, m_strFileType ); | |
747 | ||
748 | return rkey.Create() && | |
fda7962d | 749 | rkey.SetValue(wxEmptyString, desc); |
2b813b73 VZ |
750 | } |
751 | ||
a6c65e88 VZ |
752 | // ---------------------------------------------------------------------------- |
753 | // remove file association | |
754 | // ---------------------------------------------------------------------------- | |
755 | ||
756 | bool wxFileTypeImpl::Unassociate() | |
757 | { | |
598ddd96 | 758 | bool result = true; |
a6c65e88 | 759 | if ( !RemoveOpenCommand() ) |
598ddd96 | 760 | result = false; |
a6c65e88 | 761 | if ( !RemoveDefaultIcon() ) |
598ddd96 | 762 | result = false; |
a6c65e88 | 763 | if ( !RemoveMimeType() ) |
598ddd96 | 764 | result = false; |
92218ce6 | 765 | if ( !RemoveDescription() ) |
598ddd96 | 766 | result = false; |
a6c65e88 | 767 | |
2b813b73 VZ |
768 | /* |
769 | //this might hold other keys, eg some have CSLID keys | |
a6c65e88 VZ |
770 | if ( result ) |
771 | { | |
772 | // delete the root key | |
773 | wxRegKey key(wxRegKey::HKCR, m_ext); | |
774 | if ( key.Exists() ) | |
775 | result = key.DeleteSelf(); | |
776 | } | |
2b813b73 | 777 | */ |
a6c65e88 VZ |
778 | return result; |
779 | } | |
780 | ||
781 | bool wxFileTypeImpl::RemoveOpenCommand() | |
782 | { | |
783 | return RemoveCommand(_T("open")); | |
784 | } | |
785 | ||
786 | bool wxFileTypeImpl::RemoveCommand(const wxString& verb) | |
787 | { | |
6e36105b | 788 | wxCHECK_MSG( !m_ext.empty() && !verb.empty(), false, |
a6c65e88 VZ |
789 | _T("RemoveCommand() needs an extension and a verb") ); |
790 | ||
a6c65e88 VZ |
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 | { | |
6e36105b | 799 | wxCHECK_MSG( !m_ext.empty(), false, _T("RemoveMimeType() needs extension") ); |
a6c65e88 VZ |
800 | |
801 | wxRegKey rkey(wxRegKey::HKCR, m_ext); | |
802 | return !rkey.Exists() || rkey.DeleteSelf(); | |
803 | } | |
804 | ||
805 | bool wxFileTypeImpl::RemoveDefaultIcon() | |
806 | { | |
6e36105b | 807 | wxCHECK_MSG( !m_ext.empty(), false, |
a6c65e88 VZ |
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 | { | |
6e36105b | 816 | wxCHECK_MSG( !m_ext.empty(), false, |
2b813b73 VZ |
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 |