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