]> git.saurik.com Git - wxWidgets.git/blame - src/common/mimecmn.cpp
construct brush used for painting disabled bitmap buttons dynamically (heavily modifi...
[wxWidgets.git] / src / common / mimecmn.cpp
CommitLineData
7dc3cc31
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/mimecmn.cpp
3// Purpose: classes and functions to manage MIME types
4// Author: Vadim Zeitlin
5// Modified by:
c7ce8392 6// Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32
7dc3cc31
VS
7// Created: 23.09.98
8// RCS-ID: $Id$
9// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 10// Licence: wxWindows licence (part of wxExtra library)
7dc3cc31
VS
11/////////////////////////////////////////////////////////////////////////////
12
a6c65e88
VZ
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
7dc3cc31 21#ifdef __GNUG__
a6c65e88 22 #pragma implementation "mimetypebase.h"
7dc3cc31
VS
23#endif
24
25// for compilers that support precompilation, includes "wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
1e6feb95 29 #pragma hdrstop
7dc3cc31
VS
30#endif
31
1e6feb95
VZ
32#if wxUSE_MIMETYPE
33
7dc3cc31
VS
34#ifndef WX_PRECOMP
35 #include "wx/string.h"
7dc3cc31
VS
36#endif //WX_PRECOMP
37
2db1bd22 38#include "wx/module.h"
7dc3cc31
VS
39#include "wx/log.h"
40#include "wx/file.h"
da0766ab 41#include "wx/iconloc.h"
7dc3cc31
VS
42#include "wx/intl.h"
43#include "wx/dynarray.h"
44#include "wx/confbase.h"
45
7dc3cc31
VS
46#include "wx/mimetype.h"
47
48// other standard headers
49#include <ctype.h>
50
7dc3cc31 51// implementation classes:
7dc3cc31 52#if defined(__WXMSW__)
c7ce8392 53 #include "wx/msw/mimetype.h"
5fde6fcc 54#elif defined(__WXMAC__)
c7ce8392 55 #include "wx/mac/mimetype.h"
64621b07 56#elif defined(__WXPM__) || defined (__EMX__)
c7ce8392 57 #include "wx/os2/mimetype.h"
6691d737 58 #undef __UNIX__
c7ce8392
VZ
59#else // Unix
60 #include "wx/unix/mimetype.h"
7dc3cc31
VS
61#endif
62
63// ============================================================================
64// common classes
65// ============================================================================
66
67// ----------------------------------------------------------------------------
68// wxFileTypeInfo
69// ----------------------------------------------------------------------------
70
57e67135
VZ
71wxFileTypeInfo::wxFileTypeInfo(const wxChar *mimeType,
72 const wxChar *openCmd,
73 const wxChar *printCmd,
74 const wxChar *desc,
7dc3cc31
VS
75 ...)
76 : m_mimeType(mimeType),
77 m_openCmd(openCmd),
78 m_printCmd(printCmd),
79 m_desc(desc)
80{
81 va_list argptr;
82 va_start(argptr, desc);
83
84 for ( ;; )
85 {
57e67135 86 const wxChar *ext = va_arg(argptr, const wxChar *);
7dc3cc31
VS
87 if ( !ext )
88 {
89 // NULL terminates the list
90 break;
91 }
92
93 m_exts.Add(ext);
94 }
95
96 va_end(argptr);
97}
98
2b813b73
VZ
99
100wxFileTypeInfo::wxFileTypeInfo(const wxArrayString& sArray)
101{
102 m_mimeType = sArray [0u];
103 m_openCmd = sArray [1u];
104 m_printCmd = sArray [2u];
105 m_desc = sArray [3u];
106
107 size_t count = sArray.GetCount();
108 for ( size_t i = 4; i < count; i++ )
109 {
110 m_exts.Add(sArray[i]);
111 }
112}
113
7dc3cc31
VS
114#include "wx/arrimpl.cpp"
115WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
116
7dc3cc31
VS
117// ============================================================================
118// implementation of the wrapper classes
119// ============================================================================
120
121// ----------------------------------------------------------------------------
122// wxFileType
123// ----------------------------------------------------------------------------
124
a6c65e88 125/* static */
7dc3cc31
VS
126wxString wxFileType::ExpandCommand(const wxString& command,
127 const wxFileType::MessageParameters& params)
128{
4e32eea1 129 bool hasFilename = false;
7dc3cc31
VS
130
131 wxString str;
132 for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) {
133 if ( *pc == wxT('%') ) {
134 switch ( *++pc ) {
135 case wxT('s'):
136 // '%s' expands into file name (quoted because it might
137 // contain spaces) - except if there are already quotes
138 // there because otherwise some programs may get confused
139 // by double double quotes
140#if 0
141 if ( *(pc - 2) == wxT('"') )
142 str << params.GetFileName();
143 else
144 str << wxT('"') << params.GetFileName() << wxT('"');
145#endif
146 str << params.GetFileName();
4e32eea1 147 hasFilename = true;
7dc3cc31
VS
148 break;
149
150 case wxT('t'):
151 // '%t' expands into MIME type (quote it too just to be
152 // consistent)
153 str << wxT('\'') << params.GetMimeType() << wxT('\'');
154 break;
155
156 case wxT('{'):
157 {
158 const wxChar *pEnd = wxStrchr(pc, wxT('}'));
159 if ( pEnd == NULL ) {
160 wxString mimetype;
f6bcfd97 161 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
7dc3cc31
VS
162 params.GetMimeType().c_str());
163 str << wxT("%{");
164 }
165 else {
166 wxString param(pc + 1, pEnd - pc - 1);
167 str << wxT('\'') << params.GetParamValue(param) << wxT('\'');
168 pc = pEnd;
169 }
170 }
171 break;
172
173 case wxT('n'):
174 case wxT('F'):
175 // TODO %n is the number of parts, %F is an array containing
176 // the names of temp files these parts were written to
177 // and their mime types.
178 break;
179
180 default:
181 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
182 *pc, command.c_str());
183 str << *pc;
184 }
185 }
186 else {
187 str << *pc;
188 }
189 }
190
191 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
f6bcfd97
BP
192 // the program will accept the data on stdin so normally we should append
193 // "< %s" to the end of the command in such case, but not all commands
194 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
195 // and appending "< %s" to this command makes the test fail... I don't
196 // know of the correct solution, try to guess what we have to do.
2b813b73
VZ
197
198 // test now carried out on reading file so test should never get here
f6bcfd97
BP
199 if ( !hasFilename && !str.IsEmpty()
200#ifdef __UNIX__
201 && !str.StartsWith(_T("test "))
202#endif // Unix
203 ) {
7dc3cc31
VS
204 str << wxT(" < '") << params.GetFileName() << wxT('\'');
205 }
206
207 return str;
208}
209
a6c65e88
VZ
210wxFileType::wxFileType(const wxFileTypeInfo& info)
211{
212 m_info = &info;
213 m_impl = NULL;
214}
215
7dc3cc31
VS
216wxFileType::wxFileType()
217{
a6c65e88 218 m_info = NULL;
7dc3cc31
VS
219 m_impl = new wxFileTypeImpl;
220}
221
222wxFileType::~wxFileType()
223{
dca2d56f
GT
224 if ( m_impl )
225 delete m_impl;
7dc3cc31
VS
226}
227
228bool wxFileType::GetExtensions(wxArrayString& extensions)
229{
a6c65e88
VZ
230 if ( m_info )
231 {
232 extensions = m_info->GetExtensions();
4e32eea1 233 return true;
a6c65e88
VZ
234 }
235
7dc3cc31
VS
236 return m_impl->GetExtensions(extensions);
237}
238
239bool wxFileType::GetMimeType(wxString *mimeType) const
240{
4e32eea1 241 wxCHECK_MSG( mimeType, false, _T("invalid parameter in GetMimeType") );
a6c65e88
VZ
242
243 if ( m_info )
244 {
245 *mimeType = m_info->GetMimeType();
246
4e32eea1 247 return true;
a6c65e88
VZ
248 }
249
7dc3cc31
VS
250 return m_impl->GetMimeType(mimeType);
251}
252
4d2976ad
VS
253bool wxFileType::GetMimeTypes(wxArrayString& mimeTypes) const
254{
a6c65e88
VZ
255 if ( m_info )
256 {
257 mimeTypes.Clear();
258 mimeTypes.Add(m_info->GetMimeType());
259
4e32eea1 260 return true;
a6c65e88
VZ
261 }
262
4d2976ad
VS
263 return m_impl->GetMimeTypes(mimeTypes);
264}
265
da0766ab 266bool wxFileType::GetIcon(wxIconLocation *iconLoc) const
7dc3cc31 267{
a6c65e88
VZ
268 if ( m_info )
269 {
da0766ab 270 if ( iconLoc )
a6c65e88 271 {
da0766ab
VZ
272 iconLoc->SetFileName(m_info->GetIconFile());
273#ifdef __WXMSW__
274 iconLoc->SetIndex(m_info->GetIconIndex());
275#endif // __WXMSW__
a6c65e88 276 }
a6c65e88 277
4e32eea1 278 return true;
a6c65e88
VZ
279 }
280
da0766ab 281 return m_impl->GetIcon(iconLoc);
7dc3cc31
VS
282}
283
11d395f9
VZ
284bool
285wxFileType::GetIcon(wxIconLocation *iconloc,
286 const MessageParameters& params) const
287{
288 if ( !GetIcon(iconloc) )
289 {
290 return false;
291 }
292
293 // we may have "%s" in the icon location string, at least under Windows, so
294 // expand this
295 if ( iconloc )
296 {
297 iconloc->SetFileName(ExpandCommand(iconloc->GetFileName(), params));
298 }
299
300 return true;
301}
302
7dc3cc31
VS
303bool wxFileType::GetDescription(wxString *desc) const
304{
4e32eea1 305 wxCHECK_MSG( desc, false, _T("invalid parameter in GetDescription") );
a6c65e88
VZ
306
307 if ( m_info )
308 {
309 *desc = m_info->GetDescription();
310
4e32eea1 311 return true;
a6c65e88
VZ
312 }
313
7dc3cc31
VS
314 return m_impl->GetDescription(desc);
315}
316
317bool
318wxFileType::GetOpenCommand(wxString *openCmd,
319 const wxFileType::MessageParameters& params) const
320{
4e32eea1 321 wxCHECK_MSG( openCmd, false, _T("invalid parameter in GetOpenCommand") );
a6c65e88
VZ
322
323 if ( m_info )
324 {
325 *openCmd = ExpandCommand(m_info->GetOpenCommand(), params);
326
4e32eea1 327 return true;
a6c65e88
VZ
328 }
329
7dc3cc31
VS
330 return m_impl->GetOpenCommand(openCmd, params);
331}
332
0532a258
VZ
333wxString wxFileType::GetOpenCommand(const wxString& filename) const
334{
335 wxString cmd;
336 if ( !GetOpenCommand(&cmd, filename) )
337 {
338 // return empty string to indicate an error
339 cmd.clear();
340 }
341
342 return cmd;
343}
344
7dc3cc31
VS
345bool
346wxFileType::GetPrintCommand(wxString *printCmd,
347 const wxFileType::MessageParameters& params) const
348{
4e32eea1 349 wxCHECK_MSG( printCmd, false, _T("invalid parameter in GetPrintCommand") );
a6c65e88
VZ
350
351 if ( m_info )
352 {
353 *printCmd = ExpandCommand(m_info->GetPrintCommand(), params);
354
4e32eea1 355 return true;
a6c65e88
VZ
356 }
357
7dc3cc31
VS
358 return m_impl->GetPrintCommand(printCmd, params);
359}
360
c7ce8392
VZ
361
362size_t wxFileType::GetAllCommands(wxArrayString *verbs,
363 wxArrayString *commands,
364 const wxFileType::MessageParameters& params) const
365{
366 if ( verbs )
367 verbs->Clear();
368 if ( commands )
369 commands->Clear();
370
2900bd1c 371#if defined (__WXMSW__) || defined(__UNIX__)
c7ce8392 372 return m_impl->GetAllCommands(verbs, commands, params);
2b813b73 373#else // !__WXMSW__ || Unix
c7ce8392
VZ
374 // we don't know how to retrieve all commands, so just try the 2 we know
375 // about
376 size_t count = 0;
377 wxString cmd;
a6c65e88 378 if ( GetOpenCommand(&cmd, params) )
c7ce8392
VZ
379 {
380 if ( verbs )
381 verbs->Add(_T("Open"));
382 if ( commands )
383 commands->Add(cmd);
384 count++;
385 }
386
387 if ( GetPrintCommand(&cmd, params) )
388 {
389 if ( verbs )
390 verbs->Add(_T("Print"));
391 if ( commands )
392 commands->Add(cmd);
393
394 count++;
395 }
396
397 return count;
2b813b73 398#endif // __WXMSW__/| __UNIX__
c7ce8392
VZ
399}
400
a6c65e88 401bool wxFileType::Unassociate()
c7ce8392 402{
2b813b73 403#if defined(__WXMSW__)
a6c65e88 404 return m_impl->Unassociate();
6691d737 405#elif defined(__UNIX__)
2b813b73 406 return m_impl->Unassociate(this);
2900bd1c 407#else
a6c65e88 408 wxFAIL_MSG( _T("not implemented") ); // TODO
4e32eea1 409 return false;
2900bd1c 410#endif
2b813b73
VZ
411}
412
413bool wxFileType::SetCommand(const wxString& cmd, const wxString& verb,
414bool overwriteprompt)
415{
2900bd1c 416#if defined (__WXMSW__) || defined(__UNIX__)
2b813b73
VZ
417 return m_impl->SetCommand(cmd, verb, overwriteprompt);
418#else
419 wxFAIL_MSG(_T("not implemented"));
4e32eea1 420 return false;
c7ce8392
VZ
421#endif
422}
423
2b813b73
VZ
424bool wxFileType::SetDefaultIcon(const wxString& cmd, int index)
425{
426 wxString sTmp = cmd;
427#ifdef __WXMSW__
428 // VZ: should we do this?
429 // chris elliott : only makes sense in MS windows
430 if ( sTmp.empty() )
2b5f62a0 431 GetOpenCommand(&sTmp, wxFileType::MessageParameters(wxT(""), wxT("")));
2b813b73 432#endif
4e32eea1 433 wxCHECK_MSG( !sTmp.empty(), false, _T("need the icon file") );
2b813b73 434
2900bd1c 435#if defined (__WXMSW__) || defined(__UNIX__)
2b813b73
VZ
436 return m_impl->SetDefaultIcon (cmd, index);
437#else
438 wxFAIL_MSG(_T("not implemented"));
439
4e32eea1 440 return false;
2b813b73
VZ
441#endif
442}
443
444
7dc3cc31
VS
445// ----------------------------------------------------------------------------
446// wxMimeTypesManager
447// ----------------------------------------------------------------------------
448
449void wxMimeTypesManager::EnsureImpl()
450{
c7ce8392 451 if ( !m_impl )
7dc3cc31
VS
452 m_impl = new wxMimeTypesManagerImpl;
453}
454
455bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
456 const wxString& wildcard)
457{
458 wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND,
459 wxT("first MIME type can't contain wildcards") );
460
4e32eea1 461 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
a6c65e88 462 if ( wildcard.BeforeFirst(wxT('/')).
4e32eea1 463 IsSameAs(mimeType.BeforeFirst(wxT('/')), false) )
7dc3cc31
VS
464 {
465 wxString strSubtype = wildcard.AfterFirst(wxT('/'));
466
467 if ( strSubtype == wxT("*") ||
4e32eea1 468 strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), false) )
7dc3cc31
VS
469 {
470 // matches (either exactly or it's a wildcard)
4e32eea1 471 return true;
7dc3cc31
VS
472 }
473 }
474
4e32eea1 475 return false;
7dc3cc31
VS
476}
477
478wxMimeTypesManager::wxMimeTypesManager()
479{
480 m_impl = NULL;
481}
482
483wxMimeTypesManager::~wxMimeTypesManager()
484{
8a16a98e
GT
485 if ( m_impl )
486 delete m_impl;
7dc3cc31
VS
487}
488
2b813b73
VZ
489bool wxMimeTypesManager::Unassociate(wxFileType *ft)
490{
6691d737 491#if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
2b813b73
VZ
492 return m_impl->Unassociate(ft);
493#else
494 return ft->Unassociate();
495#endif
496}
497
498
7dc3cc31 499wxFileType *
a6c65e88 500wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo)
7dc3cc31
VS
501{
502 EnsureImpl();
a6c65e88 503
6691d737 504#if defined(__WXMSW__) || defined(__UNIX__)
a6c65e88
VZ
505 return m_impl->Associate(ftInfo);
506#else // other platforms
507 wxFAIL_MSG( _T("not implemented") ); // TODO
508 return NULL;
509#endif // platforms
7dc3cc31
VS
510}
511
c7ce8392 512wxFileType *
a6c65e88 513wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
c7ce8392
VZ
514{
515 EnsureImpl();
a6c65e88
VZ
516 wxFileType *ft = m_impl->GetFileTypeFromExtension(ext);
517
518 if ( !ft ) {
519 // check the fallbacks
520 //
521 // TODO linear search is potentially slow, perhaps we should use a
522 // sorted array?
523 size_t count = m_fallbacks.GetCount();
524 for ( size_t n = 0; n < count; n++ ) {
525 if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) {
526 ft = new wxFileType(m_fallbacks[n]);
527
528 break;
529 }
530 }
531 }
c7ce8392 532
a6c65e88 533 return ft;
c7ce8392
VZ
534}
535
7dc3cc31
VS
536wxFileType *
537wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
538{
539 EnsureImpl();
a6c65e88
VZ
540 wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType);
541
f627fbee 542 if ( !ft ) {
a6c65e88
VZ
543 // check the fallbacks
544 //
f627fbee
VZ
545 // TODO linear search is potentially slow, perhaps we should use a
546 // sorted array?
a6c65e88
VZ
547 size_t count = m_fallbacks.GetCount();
548 for ( size_t n = 0; n < count; n++ ) {
549 if ( wxMimeTypesManager::IsOfType(mimeType,
550 m_fallbacks[n].GetMimeType()) ) {
551 ft = new wxFileType(m_fallbacks[n]);
552
553 break;
554 }
555 }
556 }
557
558 return ft;
7dc3cc31
VS
559}
560
561bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
562{
563 EnsureImpl();
564 return m_impl->ReadMailcap(filename, fallback);
565}
566
567bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
568{
569 EnsureImpl();
570 return m_impl->ReadMimeTypes(filename);
571}
572
573void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
574{
575 EnsureImpl();
a6c65e88
VZ
576 for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) {
577 AddFallback(*ft);
7dc3cc31
VS
578 }
579}
580
581size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
582{
583 EnsureImpl();
a6c65e88
VZ
584 size_t countAll = m_impl->EnumAllFileTypes(mimetypes);
585
586 // add the fallback filetypes
587 size_t count = m_fallbacks.GetCount();
588 for ( size_t n = 0; n < count; n++ ) {
589 if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) {
590 mimetypes.Add(m_fallbacks[n].GetMimeType());
591 countAll++;
592 }
593 }
7dc3cc31 594
a6c65e88
VZ
595 return countAll;
596}
7dc3cc31 597
2b813b73
VZ
598void wxMimeTypesManager::Initialize(int mcapStyle,
599 const wxString& sExtraDir)
600{
6691d737 601#if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
2b813b73
VZ
602 EnsureImpl();
603
604 m_impl->Initialize(mcapStyle, sExtraDir);
33ac7e6f 605#else
4e32eea1
WS
606 (void)mcapStyle;
607 (void)sExtraDir;
2b813b73
VZ
608#endif // Unix
609}
610
611// and this function clears all the data from the manager
612void wxMimeTypesManager::ClearData()
613{
6691d737 614#if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
2b813b73
VZ
615 EnsureImpl();
616
617 m_impl->ClearData();
618#endif // Unix
619}
620
7dc3cc31 621// ----------------------------------------------------------------------------
a6c65e88 622// global data and wxMimeTypeCmnModule
7dc3cc31
VS
623// ----------------------------------------------------------------------------
624
625// private object
626static wxMimeTypesManager gs_mimeTypesManager;
627
628// and public pointer
a6c65e88 629wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager;
7dc3cc31 630
66806a0b
VS
631class wxMimeTypeCmnModule: public wxModule
632{
66806a0b 633public:
c7ce8392 634 wxMimeTypeCmnModule() : wxModule() { }
4e32eea1 635 virtual bool OnInit() { return true; }
c7ce8392
VZ
636 virtual void OnExit()
637 {
638 // this avoids false memory leak allerts:
639 if ( gs_mimeTypesManager.m_impl != NULL )
640 {
641 delete gs_mimeTypesManager.m_impl;
642 gs_mimeTypesManager.m_impl = NULL;
65e50848 643 gs_mimeTypesManager.m_fallbacks.Clear();
c7ce8392 644 }
66806a0b 645 }
c7ce8392
VZ
646
647 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule)
66806a0b
VS
648};
649
650IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule)
1e6feb95
VZ
651
652#endif // wxUSE_MIMETYPE