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