]> git.saurik.com Git - wxWidgets.git/blame - src/common/mimecmn.cpp
changed wxUSE_DRAGIMAG to wxUSE_DRAGIMAGE and use it everywhere now
[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"
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
VS
36#endif
37
7dc3cc31
VS
38#ifndef WX_PRECOMP
39 #include "wx/string.h"
40 #if wxUSE_GUI
41 #include "wx/icon.h"
42 #endif
43#endif //WX_PRECOMP
44
7dc3cc31
VS
45#include "wx/log.h"
46#include "wx/file.h"
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
75wxFileTypeInfo::wxFileTypeInfo(const char *mimeType,
76 const char *openCmd,
77 const char *printCmd,
78 const char *desc,
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 {
90 const char *ext = va_arg(argptr, const char *);
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
c7ce8392
VZ
270bool wxFileType::GetIcon(wxIcon *icon,
271 wxString *iconFile,
272 int *iconIndex) const
7dc3cc31 273{
a6c65e88
VZ
274 if ( m_info )
275 {
276 if ( iconFile )
277 *iconFile = m_info->GetIconFile();
278 if ( iconIndex )
279 *iconIndex = m_info->GetIconIndex();
280
281#if wxUSE_GUI
282 if ( icon && !m_info->GetIconFile().empty() )
283 {
284 // FIXME: what about the index?
285 icon->LoadFile(m_info->GetIconFile());
286 }
287#endif // wxUSE_GUI
288
289 return TRUE;
290 }
291
2b813b73 292#if defined(__WXMSW__) || defined(__UNIX__)
c7ce8392
VZ
293 return m_impl->GetIcon(icon, iconFile, iconIndex);
294#else
7dc3cc31 295 return m_impl->GetIcon(icon);
c7ce8392 296#endif
7dc3cc31
VS
297}
298
299bool wxFileType::GetDescription(wxString *desc) const
300{
a6c65e88
VZ
301 wxCHECK_MSG( desc, FALSE, _T("invalid parameter in GetDescription") );
302
303 if ( m_info )
304 {
305 *desc = m_info->GetDescription();
306
307 return TRUE;
308 }
309
7dc3cc31
VS
310 return m_impl->GetDescription(desc);
311}
312
313bool
314wxFileType::GetOpenCommand(wxString *openCmd,
315 const wxFileType::MessageParameters& params) const
316{
a6c65e88
VZ
317 wxCHECK_MSG( openCmd, FALSE, _T("invalid parameter in GetOpenCommand") );
318
319 if ( m_info )
320 {
321 *openCmd = ExpandCommand(m_info->GetOpenCommand(), params);
322
323 return TRUE;
324 }
325
7dc3cc31
VS
326 return m_impl->GetOpenCommand(openCmd, params);
327}
328
329bool
330wxFileType::GetPrintCommand(wxString *printCmd,
331 const wxFileType::MessageParameters& params) const
332{
a6c65e88
VZ
333 wxCHECK_MSG( printCmd, FALSE, _T("invalid parameter in GetPrintCommand") );
334
335 if ( m_info )
336 {
337 *printCmd = ExpandCommand(m_info->GetPrintCommand(), params);
338
339 return TRUE;
340 }
341
7dc3cc31
VS
342 return m_impl->GetPrintCommand(printCmd, params);
343}
344
c7ce8392
VZ
345
346size_t wxFileType::GetAllCommands(wxArrayString *verbs,
347 wxArrayString *commands,
348 const wxFileType::MessageParameters& params) const
349{
350 if ( verbs )
351 verbs->Clear();
352 if ( commands )
353 commands->Clear();
354
2900bd1c 355#if defined (__WXMSW__) || defined(__UNIX__)
c7ce8392 356 return m_impl->GetAllCommands(verbs, commands, params);
2b813b73 357#else // !__WXMSW__ || Unix
c7ce8392
VZ
358 // we don't know how to retrieve all commands, so just try the 2 we know
359 // about
360 size_t count = 0;
361 wxString cmd;
a6c65e88 362 if ( GetOpenCommand(&cmd, params) )
c7ce8392
VZ
363 {
364 if ( verbs )
365 verbs->Add(_T("Open"));
366 if ( commands )
367 commands->Add(cmd);
368 count++;
369 }
370
371 if ( GetPrintCommand(&cmd, params) )
372 {
373 if ( verbs )
374 verbs->Add(_T("Print"));
375 if ( commands )
376 commands->Add(cmd);
377
378 count++;
379 }
380
381 return count;
2b813b73 382#endif // __WXMSW__/| __UNIX__
c7ce8392
VZ
383}
384
a6c65e88 385bool wxFileType::Unassociate()
c7ce8392 386{
2b813b73 387#if defined(__WXMSW__)
a6c65e88 388 return m_impl->Unassociate();
e4ad24c9 389#elif defined(__UNIX__) && !defined(__WXPM__)
2b813b73 390 return m_impl->Unassociate(this);
2900bd1c 391#else
a6c65e88 392 wxFAIL_MSG( _T("not implemented") ); // TODO
c7ce8392 393 return FALSE;
2900bd1c 394#endif
2b813b73
VZ
395}
396
397bool wxFileType::SetCommand(const wxString& cmd, const wxString& verb,
398bool overwriteprompt)
399{
2900bd1c 400#if defined (__WXMSW__) || defined(__UNIX__)
2b813b73
VZ
401 return m_impl->SetCommand(cmd, verb, overwriteprompt);
402#else
403 wxFAIL_MSG(_T("not implemented"));
2b813b73 404 return FALSE;
c7ce8392
VZ
405#endif
406}
407
2b813b73
VZ
408bool wxFileType::SetDefaultIcon(const wxString& cmd, int index)
409{
410 wxString sTmp = cmd;
411#ifdef __WXMSW__
412 // VZ: should we do this?
413 // chris elliott : only makes sense in MS windows
414 if ( sTmp.empty() )
415 GetOpenCommand(&sTmp, wxFileType::MessageParameters("", ""));
416#endif
417 wxCHECK_MSG( !sTmp.empty(), FALSE, _T("need the icon file") );
418
2900bd1c 419#if defined (__WXMSW__) || defined(__UNIX__)
2b813b73
VZ
420 return m_impl->SetDefaultIcon (cmd, index);
421#else
422 wxFAIL_MSG(_T("not implemented"));
423
424 return FALSE;
425#endif
426}
427
428
7dc3cc31
VS
429// ----------------------------------------------------------------------------
430// wxMimeTypesManager
431// ----------------------------------------------------------------------------
432
433void wxMimeTypesManager::EnsureImpl()
434{
c7ce8392 435 if ( !m_impl )
7dc3cc31
VS
436 m_impl = new wxMimeTypesManagerImpl;
437}
438
439bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
440 const wxString& wildcard)
441{
442 wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND,
443 wxT("first MIME type can't contain wildcards") );
444
445 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
a6c65e88
VZ
446 if ( wildcard.BeforeFirst(wxT('/')).
447 IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) )
7dc3cc31
VS
448 {
449 wxString strSubtype = wildcard.AfterFirst(wxT('/'));
450
451 if ( strSubtype == wxT("*") ||
452 strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) )
453 {
454 // matches (either exactly or it's a wildcard)
455 return TRUE;
456 }
457 }
458
459 return FALSE;
460}
461
462wxMimeTypesManager::wxMimeTypesManager()
463{
464 m_impl = NULL;
465}
466
467wxMimeTypesManager::~wxMimeTypesManager()
468{
8a16a98e
GT
469 if ( m_impl )
470 delete m_impl;
7dc3cc31
VS
471}
472
2b813b73
VZ
473bool wxMimeTypesManager::Unassociate(wxFileType *ft)
474{
e4ad24c9 475#if defined(__UNIX__) && !defined(__WXPM__)
2b813b73
VZ
476 return m_impl->Unassociate(ft);
477#else
478 return ft->Unassociate();
479#endif
480}
481
482
7dc3cc31 483wxFileType *
a6c65e88 484wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo)
7dc3cc31
VS
485{
486 EnsureImpl();
a6c65e88 487
e4ad24c9 488#if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
a6c65e88
VZ
489 return m_impl->Associate(ftInfo);
490#else // other platforms
491 wxFAIL_MSG( _T("not implemented") ); // TODO
492 return NULL;
493#endif // platforms
7dc3cc31
VS
494}
495
c7ce8392 496wxFileType *
a6c65e88 497wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
c7ce8392
VZ
498{
499 EnsureImpl();
a6c65e88
VZ
500 wxFileType *ft = m_impl->GetFileTypeFromExtension(ext);
501
502 if ( !ft ) {
503 // check the fallbacks
504 //
505 // TODO linear search is potentially slow, perhaps we should use a
506 // sorted array?
507 size_t count = m_fallbacks.GetCount();
508 for ( size_t n = 0; n < count; n++ ) {
509 if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) {
510 ft = new wxFileType(m_fallbacks[n]);
511
512 break;
513 }
514 }
515 }
c7ce8392 516
a6c65e88 517 return ft;
c7ce8392
VZ
518}
519
7dc3cc31
VS
520wxFileType *
521wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
522{
523 EnsureImpl();
a6c65e88
VZ
524 wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType);
525
526 if ( ft ) {
527 // check the fallbacks
528 //
529 // TODO linear search is potentially slow, perhaps we should use a sorted
530 // array?
531 size_t count = m_fallbacks.GetCount();
532 for ( size_t n = 0; n < count; n++ ) {
533 if ( wxMimeTypesManager::IsOfType(mimeType,
534 m_fallbacks[n].GetMimeType()) ) {
535 ft = new wxFileType(m_fallbacks[n]);
536
537 break;
538 }
539 }
540 }
541
542 return ft;
7dc3cc31
VS
543}
544
545bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
546{
547 EnsureImpl();
548 return m_impl->ReadMailcap(filename, fallback);
549}
550
551bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
552{
553 EnsureImpl();
554 return m_impl->ReadMimeTypes(filename);
555}
556
557void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
558{
559 EnsureImpl();
a6c65e88
VZ
560 for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) {
561 AddFallback(*ft);
7dc3cc31
VS
562 }
563}
564
565size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
566{
567 EnsureImpl();
a6c65e88
VZ
568 size_t countAll = m_impl->EnumAllFileTypes(mimetypes);
569
570 // add the fallback filetypes
571 size_t count = m_fallbacks.GetCount();
572 for ( size_t n = 0; n < count; n++ ) {
573 if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) {
574 mimetypes.Add(m_fallbacks[n].GetMimeType());
575 countAll++;
576 }
577 }
7dc3cc31 578
a6c65e88
VZ
579 return countAll;
580}
7dc3cc31 581
2b813b73
VZ
582void wxMimeTypesManager::Initialize(int mcapStyle,
583 const wxString& sExtraDir)
584{
e4ad24c9 585#if defined(__UNIX__) && !defined(__WXPM__)
2b813b73
VZ
586 EnsureImpl();
587
588 m_impl->Initialize(mcapStyle, sExtraDir);
33ac7e6f
KB
589#else
590 (void)mcapStyle;
591 (void)sExtraDir;
2b813b73
VZ
592#endif // Unix
593}
594
595// and this function clears all the data from the manager
596void wxMimeTypesManager::ClearData()
597{
e4ad24c9 598#if defined(__UNIX__) && !defined(__WXPM__)
2b813b73
VZ
599 EnsureImpl();
600
601 m_impl->ClearData();
602#endif // Unix
603}
604
7dc3cc31 605// ----------------------------------------------------------------------------
a6c65e88 606// global data and wxMimeTypeCmnModule
7dc3cc31
VS
607// ----------------------------------------------------------------------------
608
609// private object
610static wxMimeTypesManager gs_mimeTypesManager;
611
612// and public pointer
a6c65e88 613wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager;
7dc3cc31 614
66806a0b
VS
615class wxMimeTypeCmnModule: public wxModule
616{
66806a0b 617public:
c7ce8392
VZ
618 wxMimeTypeCmnModule() : wxModule() { }
619 virtual bool OnInit() { return TRUE; }
620 virtual void OnExit()
621 {
622 // this avoids false memory leak allerts:
623 if ( gs_mimeTypesManager.m_impl != NULL )
624 {
625 delete gs_mimeTypesManager.m_impl;
626 gs_mimeTypesManager.m_impl = NULL;
627 }
66806a0b 628 }
c7ce8392
VZ
629
630 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule)
66806a0b
VS
631};
632
633IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule)
1e6feb95
VZ
634
635#endif // wxUSE_MIMETYPE