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