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