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