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