]> git.saurik.com Git - wxWidgets.git/blob - src/common/mimecmn.cpp
regenerated with tmake
[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 license (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
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
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
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 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
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(wxIcon *icon,
271 wxString *iconFile,
272 int *iconIndex) const
273 {
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
292 #if defined(__WXMSW__) || defined(__UNIX__)
293 return m_impl->GetIcon(icon, iconFile, iconIndex);
294 #else
295 return m_impl->GetIcon(icon);
296 #endif
297 }
298
299 bool wxFileType::GetDescription(wxString *desc) const
300 {
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
310 return m_impl->GetDescription(desc);
311 }
312
313 bool
314 wxFileType::GetOpenCommand(wxString *openCmd,
315 const wxFileType::MessageParameters& params) const
316 {
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
326 return m_impl->GetOpenCommand(openCmd, params);
327 }
328
329 bool
330 wxFileType::GetPrintCommand(wxString *printCmd,
331 const wxFileType::MessageParameters& params) const
332 {
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
342 return m_impl->GetPrintCommand(printCmd, params);
343 }
344
345
346 size_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
355 #if defined (__WXMSW__) || defined(__UNIX__)
356 return m_impl->GetAllCommands(verbs, commands, params);
357 #else // !__WXMSW__ || Unix
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;
362 if ( GetOpenCommand(&cmd, params) )
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;
382 #endif // __WXMSW__/| __UNIX__
383 }
384
385 bool wxFileType::Unassociate()
386 {
387 #if defined(__WXMSW__)
388 return m_impl->Unassociate();
389 #elif defined(__UNIX__) && !defined(__WXPM__)
390 return m_impl->Unassociate(this);
391 #else
392 wxFAIL_MSG( _T("not implemented") ); // TODO
393 return FALSE;
394 #endif
395 }
396
397 bool wxFileType::SetCommand(const wxString& cmd, const wxString& verb,
398 bool overwriteprompt)
399 {
400 #if defined (__WXMSW__) || defined(__UNIX__)
401 return m_impl->SetCommand(cmd, verb, overwriteprompt);
402 #else
403 wxFAIL_MSG(_T("not implemented"));
404 return FALSE;
405 #endif
406 }
407
408 bool 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
419 #if defined (__WXMSW__) || defined(__UNIX__)
420 return m_impl->SetDefaultIcon (cmd, index);
421 #else
422 wxFAIL_MSG(_T("not implemented"));
423
424 return FALSE;
425 #endif
426 }
427
428
429 // ----------------------------------------------------------------------------
430 // wxMimeTypesManager
431 // ----------------------------------------------------------------------------
432
433 void wxMimeTypesManager::EnsureImpl()
434 {
435 if ( !m_impl )
436 m_impl = new wxMimeTypesManagerImpl;
437 }
438
439 bool 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)
446 if ( wildcard.BeforeFirst(wxT('/')).
447 IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) )
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
462 wxMimeTypesManager::wxMimeTypesManager()
463 {
464 m_impl = NULL;
465 }
466
467 wxMimeTypesManager::~wxMimeTypesManager()
468 {
469 if ( m_impl )
470 delete m_impl;
471 }
472
473 bool wxMimeTypesManager::Unassociate(wxFileType *ft)
474 {
475 #if defined(__UNIX__) && !defined(__WXPM__)
476 return m_impl->Unassociate(ft);
477 #else
478 return ft->Unassociate();
479 #endif
480 }
481
482
483 wxFileType *
484 wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo)
485 {
486 EnsureImpl();
487
488 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
489 return m_impl->Associate(ftInfo);
490 #else // other platforms
491 wxFAIL_MSG( _T("not implemented") ); // TODO
492 return NULL;
493 #endif // platforms
494 }
495
496 wxFileType *
497 wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
498 {
499 EnsureImpl();
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 }
516
517 return ft;
518 }
519
520 wxFileType *
521 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
522 {
523 EnsureImpl();
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;
543 }
544
545 bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
546 {
547 EnsureImpl();
548 return m_impl->ReadMailcap(filename, fallback);
549 }
550
551 bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
552 {
553 EnsureImpl();
554 return m_impl->ReadMimeTypes(filename);
555 }
556
557 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
558 {
559 EnsureImpl();
560 for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) {
561 AddFallback(*ft);
562 }
563 }
564
565 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
566 {
567 EnsureImpl();
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 }
578
579 return countAll;
580 }
581
582 void wxMimeTypesManager::Initialize(int mcapStyle,
583 const wxString& sExtraDir)
584 {
585 #if defined(__UNIX__) && !defined(__WXPM__)
586 EnsureImpl();
587
588 m_impl->Initialize(mcapStyle, sExtraDir);
589 #else
590 (void)mcapStyle;
591 (void)sExtraDir;
592 #endif // Unix
593 }
594
595 // and this function clears all the data from the manager
596 void wxMimeTypesManager::ClearData()
597 {
598 #if defined(__UNIX__) && !defined(__WXPM__)
599 EnsureImpl();
600
601 m_impl->ClearData();
602 #endif // Unix
603 }
604
605 // ----------------------------------------------------------------------------
606 // global data and wxMimeTypeCmnModule
607 // ----------------------------------------------------------------------------
608
609 // private object
610 static wxMimeTypesManager gs_mimeTypesManager;
611
612 // and public pointer
613 wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager;
614
615 class wxMimeTypeCmnModule: public wxModule
616 {
617 public:
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 }
628 }
629
630 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule)
631 };
632
633 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule)
634
635 #endif // wxUSE_MIMETYPE