]> git.saurik.com Git - wxWidgets.git/blob - src/common/mimecmn.cpp
Watcom compilation fix
[wxWidgets.git] / src / common / mimecmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
22 #pragma implementation "mimetypebase.h"
23 #endif
24
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if wxUSE_MIMETYPE
33
34 #ifndef WX_PRECOMP
35 #include "wx/module.h"
36 #endif
37 // this one is needed for MSVC5
38 #include "wx/module.h"
39
40 #ifndef WX_PRECOMP
41 #include "wx/string.h"
42 #endif //WX_PRECOMP
43
44 #include "wx/log.h"
45 #include "wx/file.h"
46 #include "wx/iconloc.h"
47 #include "wx/intl.h"
48 #include "wx/dynarray.h"
49 #include "wx/confbase.h"
50
51 #include "wx/mimetype.h"
52
53 // other standard headers
54 #include <ctype.h>
55
56 // implementation classes:
57 #if defined(__WXMSW__)
58 #include "wx/msw/mimetype.h"
59 #elif defined(__WXMAC__)
60 #include "wx/mac/mimetype.h"
61 #elif defined(__WXPM__)
62 #include "wx/os2/mimetype.h"
63 #else // Unix
64 #include "wx/unix/mimetype.h"
65 #endif
66
67 // ============================================================================
68 // common classes
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxFileTypeInfo
73 // ----------------------------------------------------------------------------
74
75 wxFileTypeInfo::wxFileTypeInfo(const wxChar *mimeType,
76 const wxChar *openCmd,
77 const wxChar *printCmd,
78 const wxChar *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 wxChar *ext = va_arg(argptr, const wxChar *);
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
103
104 wxFileTypeInfo::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
118 #include "wx/arrimpl.cpp"
119 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
120
121 // ============================================================================
122 // implementation of the wrapper classes
123 // ============================================================================
124
125 // ----------------------------------------------------------------------------
126 // wxFileType
127 // ----------------------------------------------------------------------------
128
129 /* static */
130 wxString 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;
165 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
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'
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.
201
202 // test now carried out on reading file so test should never get here
203 if ( !hasFilename && !str.IsEmpty()
204 #ifdef __UNIX__
205 && !str.StartsWith(_T("test "))
206 #endif // Unix
207 ) {
208 str << wxT(" < '") << params.GetFileName() << wxT('\'');
209 }
210
211 return str;
212 }
213
214 wxFileType::wxFileType(const wxFileTypeInfo& info)
215 {
216 m_info = &info;
217 m_impl = NULL;
218 }
219
220 wxFileType::wxFileType()
221 {
222 m_info = NULL;
223 m_impl = new wxFileTypeImpl;
224 }
225
226 wxFileType::~wxFileType()
227 {
228 if ( m_impl )
229 delete m_impl;
230 }
231
232 bool wxFileType::GetExtensions(wxArrayString& extensions)
233 {
234 if ( m_info )
235 {
236 extensions = m_info->GetExtensions();
237 return TRUE;
238 }
239
240 return m_impl->GetExtensions(extensions);
241 }
242
243 bool wxFileType::GetMimeType(wxString *mimeType) const
244 {
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
254 return m_impl->GetMimeType(mimeType);
255 }
256
257 bool wxFileType::GetMimeTypes(wxArrayString& mimeTypes) const
258 {
259 if ( m_info )
260 {
261 mimeTypes.Clear();
262 mimeTypes.Add(m_info->GetMimeType());
263
264 return TRUE;
265 }
266
267 return m_impl->GetMimeTypes(mimeTypes);
268 }
269
270 bool wxFileType::GetIcon(wxIconLocation *iconLoc) const
271 {
272 if ( m_info )
273 {
274 if ( iconLoc )
275 {
276 iconLoc->SetFileName(m_info->GetIconFile());
277 #ifdef __WXMSW__
278 iconLoc->SetIndex(m_info->GetIconIndex());
279 #endif // __WXMSW__
280 }
281
282 return TRUE;
283 }
284
285 return m_impl->GetIcon(iconLoc);
286 }
287
288 bool
289 wxFileType::GetIcon(wxIconLocation *iconloc,
290 const MessageParameters& params) const
291 {
292 if ( !GetIcon(iconloc) )
293 {
294 return false;
295 }
296
297 // we may have "%s" in the icon location string, at least under Windows, so
298 // expand this
299 if ( iconloc )
300 {
301 iconloc->SetFileName(ExpandCommand(iconloc->GetFileName(), params));
302 }
303
304 return true;
305 }
306
307 bool wxFileType::GetDescription(wxString *desc) const
308 {
309 wxCHECK_MSG( desc, FALSE, _T("invalid parameter in GetDescription") );
310
311 if ( m_info )
312 {
313 *desc = m_info->GetDescription();
314
315 return TRUE;
316 }
317
318 return m_impl->GetDescription(desc);
319 }
320
321 bool
322 wxFileType::GetOpenCommand(wxString *openCmd,
323 const wxFileType::MessageParameters& params) const
324 {
325 wxCHECK_MSG( openCmd, FALSE, _T("invalid parameter in GetOpenCommand") );
326
327 if ( m_info )
328 {
329 *openCmd = ExpandCommand(m_info->GetOpenCommand(), params);
330
331 return TRUE;
332 }
333
334 return m_impl->GetOpenCommand(openCmd, params);
335 }
336
337 wxString wxFileType::GetOpenCommand(const wxString& filename) const
338 {
339 wxString cmd;
340 if ( !GetOpenCommand(&cmd, filename) )
341 {
342 // return empty string to indicate an error
343 cmd.clear();
344 }
345
346 return cmd;
347 }
348
349 bool
350 wxFileType::GetPrintCommand(wxString *printCmd,
351 const wxFileType::MessageParameters& params) const
352 {
353 wxCHECK_MSG( printCmd, FALSE, _T("invalid parameter in GetPrintCommand") );
354
355 if ( m_info )
356 {
357 *printCmd = ExpandCommand(m_info->GetPrintCommand(), params);
358
359 return TRUE;
360 }
361
362 return m_impl->GetPrintCommand(printCmd, params);
363 }
364
365
366 size_t wxFileType::GetAllCommands(wxArrayString *verbs,
367 wxArrayString *commands,
368 const wxFileType::MessageParameters& params) const
369 {
370 if ( verbs )
371 verbs->Clear();
372 if ( commands )
373 commands->Clear();
374
375 #if defined (__WXMSW__) || defined(__UNIX__)
376 return m_impl->GetAllCommands(verbs, commands, params);
377 #else // !__WXMSW__ || Unix
378 // we don't know how to retrieve all commands, so just try the 2 we know
379 // about
380 size_t count = 0;
381 wxString cmd;
382 if ( GetOpenCommand(&cmd, params) )
383 {
384 if ( verbs )
385 verbs->Add(_T("Open"));
386 if ( commands )
387 commands->Add(cmd);
388 count++;
389 }
390
391 if ( GetPrintCommand(&cmd, params) )
392 {
393 if ( verbs )
394 verbs->Add(_T("Print"));
395 if ( commands )
396 commands->Add(cmd);
397
398 count++;
399 }
400
401 return count;
402 #endif // __WXMSW__/| __UNIX__
403 }
404
405 bool wxFileType::Unassociate()
406 {
407 #if defined(__WXMSW__)
408 return m_impl->Unassociate();
409 #elif defined(__UNIX__) && !defined(__WXPM__)
410 return m_impl->Unassociate(this);
411 #else
412 wxFAIL_MSG( _T("not implemented") ); // TODO
413 return FALSE;
414 #endif
415 }
416
417 bool wxFileType::SetCommand(const wxString& cmd, const wxString& verb,
418 bool overwriteprompt)
419 {
420 #if defined (__WXMSW__) || defined(__UNIX__)
421 return m_impl->SetCommand(cmd, verb, overwriteprompt);
422 #else
423 wxFAIL_MSG(_T("not implemented"));
424 return FALSE;
425 #endif
426 }
427
428 bool wxFileType::SetDefaultIcon(const wxString& cmd, int index)
429 {
430 wxString sTmp = cmd;
431 #ifdef __WXMSW__
432 // VZ: should we do this?
433 // chris elliott : only makes sense in MS windows
434 if ( sTmp.empty() )
435 GetOpenCommand(&sTmp, wxFileType::MessageParameters(wxT(""), wxT("")));
436 #endif
437 wxCHECK_MSG( !sTmp.empty(), FALSE, _T("need the icon file") );
438
439 #if defined (__WXMSW__) || defined(__UNIX__)
440 return m_impl->SetDefaultIcon (cmd, index);
441 #else
442 wxFAIL_MSG(_T("not implemented"));
443
444 return FALSE;
445 #endif
446 }
447
448
449 // ----------------------------------------------------------------------------
450 // wxMimeTypesManager
451 // ----------------------------------------------------------------------------
452
453 void wxMimeTypesManager::EnsureImpl()
454 {
455 if ( !m_impl )
456 m_impl = new wxMimeTypesManagerImpl;
457 }
458
459 bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
460 const wxString& wildcard)
461 {
462 wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND,
463 wxT("first MIME type can't contain wildcards") );
464
465 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
466 if ( wildcard.BeforeFirst(wxT('/')).
467 IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) )
468 {
469 wxString strSubtype = wildcard.AfterFirst(wxT('/'));
470
471 if ( strSubtype == wxT("*") ||
472 strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) )
473 {
474 // matches (either exactly or it's a wildcard)
475 return TRUE;
476 }
477 }
478
479 return FALSE;
480 }
481
482 wxMimeTypesManager::wxMimeTypesManager()
483 {
484 m_impl = NULL;
485 }
486
487 wxMimeTypesManager::~wxMimeTypesManager()
488 {
489 if ( m_impl )
490 delete m_impl;
491 }
492
493 bool wxMimeTypesManager::Unassociate(wxFileType *ft)
494 {
495 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
496 return m_impl->Unassociate(ft);
497 #else
498 return ft->Unassociate();
499 #endif
500 }
501
502
503 wxFileType *
504 wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo)
505 {
506 EnsureImpl();
507
508 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
509 return m_impl->Associate(ftInfo);
510 #else // other platforms
511 wxFAIL_MSG( _T("not implemented") ); // TODO
512 return NULL;
513 #endif // platforms
514 }
515
516 wxFileType *
517 wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
518 {
519 EnsureImpl();
520 wxFileType *ft = m_impl->GetFileTypeFromExtension(ext);
521
522 if ( !ft ) {
523 // check the fallbacks
524 //
525 // TODO linear search is potentially slow, perhaps we should use a
526 // sorted array?
527 size_t count = m_fallbacks.GetCount();
528 for ( size_t n = 0; n < count; n++ ) {
529 if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) {
530 ft = new wxFileType(m_fallbacks[n]);
531
532 break;
533 }
534 }
535 }
536
537 return ft;
538 }
539
540 wxFileType *
541 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
542 {
543 EnsureImpl();
544 wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType);
545
546 if ( !ft ) {
547 // check the fallbacks
548 //
549 // TODO linear search is potentially slow, perhaps we should use a
550 // sorted array?
551 size_t count = m_fallbacks.GetCount();
552 for ( size_t n = 0; n < count; n++ ) {
553 if ( wxMimeTypesManager::IsOfType(mimeType,
554 m_fallbacks[n].GetMimeType()) ) {
555 ft = new wxFileType(m_fallbacks[n]);
556
557 break;
558 }
559 }
560 }
561
562 return ft;
563 }
564
565 bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
566 {
567 EnsureImpl();
568 return m_impl->ReadMailcap(filename, fallback);
569 }
570
571 bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
572 {
573 EnsureImpl();
574 return m_impl->ReadMimeTypes(filename);
575 }
576
577 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
578 {
579 EnsureImpl();
580 for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) {
581 AddFallback(*ft);
582 }
583 }
584
585 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
586 {
587 EnsureImpl();
588 size_t countAll = m_impl->EnumAllFileTypes(mimetypes);
589
590 // add the fallback filetypes
591 size_t count = m_fallbacks.GetCount();
592 for ( size_t n = 0; n < count; n++ ) {
593 if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) {
594 mimetypes.Add(m_fallbacks[n].GetMimeType());
595 countAll++;
596 }
597 }
598
599 return countAll;
600 }
601
602 void wxMimeTypesManager::Initialize(int mcapStyle,
603 const wxString& sExtraDir)
604 {
605 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
606 EnsureImpl();
607
608 m_impl->Initialize(mcapStyle, sExtraDir);
609 #else
610 (void)mcapStyle;
611 (void)sExtraDir;
612 #endif // Unix
613 }
614
615 // and this function clears all the data from the manager
616 void wxMimeTypesManager::ClearData()
617 {
618 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
619 EnsureImpl();
620
621 m_impl->ClearData();
622 #endif // Unix
623 }
624
625 // ----------------------------------------------------------------------------
626 // global data and wxMimeTypeCmnModule
627 // ----------------------------------------------------------------------------
628
629 // private object
630 static wxMimeTypesManager gs_mimeTypesManager;
631
632 // and public pointer
633 wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager;
634
635 class wxMimeTypeCmnModule: public wxModule
636 {
637 public:
638 wxMimeTypeCmnModule() : wxModule() { }
639 virtual bool OnInit() { return TRUE; }
640 virtual void OnExit()
641 {
642 // this avoids false memory leak allerts:
643 if ( gs_mimeTypesManager.m_impl != NULL )
644 {
645 delete gs_mimeTypesManager.m_impl;
646 gs_mimeTypesManager.m_impl = NULL;
647 gs_mimeTypesManager.m_fallbacks.Clear();
648 }
649 }
650
651 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule)
652 };
653
654 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule)
655
656 #endif // wxUSE_MIMETYPE