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