]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/unix/mimetype.cpp
Do not generate wxEVT_PG_SELECTED with direct ClearSelection() and SelectProperty...
[wxWidgets.git] / src / unix / mimetype.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/unix/mimetype.cpp
3// Purpose: classes and functions to manage MIME types
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 23.09.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence (part of wxExtra library)
10/////////////////////////////////////////////////////////////////////////////
11
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_MIMETYPE && wxUSE_FILE
20
21#include "wx/unix/mimetype.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/dynarray.h"
25 #include "wx/string.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/utils.h"
29#endif
30
31#include "wx/file.h"
32#include "wx/confbase.h"
33
34#include "wx/ffile.h"
35#include "wx/dir.h"
36#include "wx/tokenzr.h"
37#include "wx/iconloc.h"
38#include "wx/filename.h"
39#include "wx/app.h"
40#include "wx/apptrait.h"
41
42// other standard headers
43#include <ctype.h>
44
45class wxMimeTextFile
46{
47public:
48 wxMimeTextFile()
49 {
50 }
51
52 wxMimeTextFile(const wxString& fname)
53 {
54 m_fname = fname;
55 }
56
57 bool Open()
58 {
59 wxFFile file( m_fname );
60 if (!file.IsOpened())
61 return false;
62
63 size_t size = file.Length();
64 wxCharBuffer buffer( size );
65 file.Read( (void*) (const char*) buffer, size );
66
67 // Check for valid UTF-8 here?
68 wxString all = wxString::FromUTF8( buffer, size );
69
70 wxStringTokenizer tok( all, "\n" );
71 while (tok.HasMoreTokens())
72 {
73 wxString t = tok.GetNextToken();
74 t.MakeLower();
75 if ((!!t) && (t.Find( "comment" ) != 0) && (t.Find( "#" ) != 0) && (t.Find( "generic" ) != 0))
76 m_text.Add( t );
77 }
78 return true;
79 }
80
81 unsigned int GetLineCount() const { return m_text.GetCount(); }
82 wxString &GetLine( unsigned int line ) { return m_text[line]; }
83
84 int pIndexOf(const wxString& sSearch,
85 bool bIncludeComments = false,
86 int iStart = 0)
87 {
88 wxString sTest = sSearch;
89 sTest.MakeLower();
90 for(size_t i = iStart; i < GetLineCount(); i++)
91 {
92 wxString sLine = GetLine(i);
93 if(bIncludeComments || ! sLine.StartsWith(wxT("#")))
94 {
95 if(sLine.StartsWith(sTest))
96 return (int)i;
97 }
98 }
99 return wxNOT_FOUND;
100 }
101
102 wxString GetVerb(size_t i)
103 {
104 if (i > GetLineCount() )
105 return wxEmptyString;
106
107 wxString sTmp = GetLine(i).BeforeFirst(wxT('='));
108 return sTmp;
109 }
110
111 wxString GetCmd(size_t i)
112 {
113 if (i > GetLineCount() )
114 return wxEmptyString;
115
116 wxString sTmp = GetLine(i).AfterFirst(wxT('='));
117 return sTmp;
118 }
119
120private:
121 wxArrayString m_text;
122 wxString m_fname;
123};
124
125// ----------------------------------------------------------------------------
126// constants
127// ----------------------------------------------------------------------------
128
129// MIME code tracing mask
130#define TRACE_MIME wxT("mime")
131
132
133// Read a XDG *.desktop file of type 'Application'
134void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename)
135{
136 wxLogTrace(TRACE_MIME, wxT("loading XDG file %s"), filename.c_str());
137
138 wxMimeTextFile file(filename);
139 if ( !file.Open() )
140 return;
141
142 // Here, only type 'Application' should be considered.
143 int nIndex = file.pIndexOf( "Type=" );
144 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex) != "application")
145 return;
146
147 // The hidden entry specifies a file to be ignored.
148 nIndex = file.pIndexOf( "Hidden=" );
149 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex) == "true")
150 return;
151
152 // Semicolon separated list of mime types handled by the application.
153 nIndex = file.pIndexOf( wxT("MimeType=") );
154 if (nIndex == wxNOT_FOUND)
155 return;
156 wxString mimetypes = file.GetCmd (nIndex);
157
158 // Name of the application
159 wxString nameapp;
160 nIndex = wxNOT_FOUND;
161#if wxUSE_INTL // try "Name[locale name]" first
162 wxLocale *locale = wxGetLocale();
163 if ( locale )
164 nIndex = file.pIndexOf(wxT("Name[")+locale->GetName()+wxT("]="));
165#endif // wxUSE_INTL
166 if(nIndex == wxNOT_FOUND)
167 nIndex = file.pIndexOf( wxT("Name=") );
168 if(nIndex != wxNOT_FOUND)
169 nameapp = file.GetCmd(nIndex);
170
171 // Icon of the application.
172 wxString nameicon, namemini;
173 nIndex = wxNOT_FOUND;
174#if wxUSE_INTL // try "Icon[locale name]" first
175 if ( locale )
176 nIndex = file.pIndexOf(wxT("Icon[")+locale->GetName()+wxT("]="));
177#endif // wxUSE_INTL
178 if(nIndex == wxNOT_FOUND)
179 nIndex = file.pIndexOf( wxT("Icon=") );
180 if(nIndex != wxNOT_FOUND) {
181 nameicon = wxString(wxT("--icon ")) + file.GetCmd(nIndex);
182 namemini = wxString(wxT("--miniicon ")) + file.GetCmd(nIndex);
183 }
184
185 // Replace some of the field code in the 'Exec' entry.
186 // TODO: deal with %d, %D, %n, %N, %k and %v (but last one is deprecated)
187 nIndex = file.pIndexOf( wxT("Exec=") );
188 if (nIndex == wxNOT_FOUND)
189 return;
190 wxString sCmd = file.GetCmd(nIndex);
191 // we expect %f; others including %F and %U and %u are possible
192 sCmd.Replace(wxT("%F"), wxT("%f"));
193 sCmd.Replace(wxT("%U"), wxT("%f"));
194 sCmd.Replace(wxT("%u"), wxT("%f"));
195 if (0 == sCmd.Replace ( wxT("%f"), wxT("%s") ))
196 sCmd = sCmd + wxT(" %s");
197 sCmd.Replace(wxT("%c"), nameapp);
198 sCmd.Replace(wxT("%i"), nameicon);
199 sCmd.Replace(wxT("%m"), namemini);
200
201 wxStringTokenizer tokenizer(mimetypes, wxT(";"));
202 while(tokenizer.HasMoreTokens()) {
203 wxString mimetype = tokenizer.GetNextToken().Lower();
204 nIndex = m_aTypes.Index(mimetype);
205 if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
206 wxMimeTypeCommands* entry = m_aEntries[nIndex];
207 entry->AddOrReplaceVerb(wxT("open"), sCmd);
208 }
209 }
210}
211
212void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString& dirname)
213{
214 // Don't complain if we don't have permissions to read - it confuses users
215 wxLogNull logNull;
216
217 if(! wxDir::Exists(dirname))
218 return;
219 wxDir dir(dirname);
220 if ( !dir.IsOpened() )
221 return;
222
223 wxString filename;
224 // Look into .desktop files
225 bool cont = dir.GetFirst(&filename, wxT("*.desktop"), wxDIR_FILES);
226 while (cont)
227 {
228 wxFileName p(dirname, filename);
229 LoadXDGApp( p.GetFullPath() );
230 cont = dir.GetNext(&filename);
231 }
232
233#if 0
234 // RR: I'm not sure this makes any sense. On my system we'll just
235 // scan the YAST2 and other useless directories
236
237 // Look recursively into subdirs
238 cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
239 while (cont)
240 {
241 wxFileName p(dirname, wxEmptyString);
242 p.AppendDir(filename);
243 LoadXDGAppsFilesFromDir( p.GetPath() );
244 cont = dir.GetNext(&filename);
245 }
246#endif
247}
248
249
250void wxMimeTypesManagerImpl::LoadXDGGlobs(const wxString& filename)
251{
252 if ( !wxFileName::FileExists(filename) )
253 return;
254
255 wxLogTrace(TRACE_MIME, wxT("loading XDG globs file from %s"), filename.c_str());
256
257 wxMimeTextFile file(filename);
258 if ( !file.Open() )
259 return;
260
261 size_t i;
262 for (i = 0; i < file.GetLineCount(); i++)
263 {
264 wxStringTokenizer tok( file.GetLine(i), ":" );
265 wxString mime = tok.GetNextToken();
266 wxString ext = tok.GetNextToken();
267 ext.Remove( 0, 2 );
268 wxArrayString exts;
269 exts.Add( ext );
270
271 AddToMimeData(mime, wxEmptyString, NULL, exts, wxEmptyString, true );
272 }
273}
274
275// ----------------------------------------------------------------------------
276// wxFileTypeImpl (Unix)
277// ----------------------------------------------------------------------------
278
279wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const
280{
281 wxString sTmp;
282 size_t i = 0;
283 while ( (i < m_index.GetCount() ) && sTmp.empty() )
284 {
285 sTmp = m_manager->GetCommand( verb, m_index[i] );
286 i++;
287 }
288
289 return wxFileType::ExpandCommand(sTmp, params);
290}
291
292bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
293{
294 wxString sTmp;
295 size_t i = 0;
296 while ( (i < m_index.GetCount() ) && sTmp.empty() )
297 {
298 sTmp = m_manager->m_aIcons[m_index[i]];
299 i++;
300 }
301
302 if ( sTmp.empty() )
303 return false;
304
305 if ( iconLoc )
306 {
307 iconLoc->SetFileName(sTmp);
308 }
309
310 return true;
311}
312
313bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
314{
315 mimeTypes.Clear();
316 size_t nCount = m_index.GetCount();
317 for (size_t i = 0; i < nCount; i++)
318 mimeTypes.Add(m_manager->m_aTypes[m_index[i]]);
319
320 return true;
321}
322
323size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
324 wxArrayString *commands,
325 const wxFileType::MessageParameters& params) const
326{
327 wxString vrb, cmd, sTmp;
328 size_t count = 0;
329 wxMimeTypeCommands * sPairs;
330
331 // verbs and commands have been cleared already in mimecmn.cpp...
332 // if we find no entries in the exact match, try the inexact match
333 for (size_t n = 0; ((count == 0) && (n < m_index.GetCount())); n++)
334 {
335 // list of verb = command pairs for this mimetype
336 sPairs = m_manager->m_aEntries [m_index[n]];
337 size_t i;
338 for ( i = 0; i < sPairs->GetCount(); i++ )
339 {
340 vrb = sPairs->GetVerb(i);
341 // some gnome entries have "." inside
342 vrb = vrb.AfterLast(wxT('.'));
343 cmd = sPairs->GetCmd(i);
344 if (! cmd.empty() )
345 {
346 cmd = wxFileType::ExpandCommand(cmd, params);
347 count++;
348 if ( vrb.IsSameAs(wxT("open")))
349 {
350 if ( verbs )
351 verbs->Insert(vrb, 0u);
352 if ( commands )
353 commands ->Insert(cmd, 0u);
354 }
355 else
356 {
357 if ( verbs )
358 verbs->Add(vrb);
359 if ( commands )
360 commands->Add(cmd);
361 }
362 }
363 }
364 }
365
366 return count;
367}
368
369bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
370{
371 const wxString strExtensions = m_manager->GetExtension(m_index[0]);
372 extensions.Empty();
373
374 // one extension in the space or comma-delimited list
375 wxString strExt;
376 wxString::const_iterator end = strExtensions.end();
377 for ( wxString::const_iterator p = strExtensions.begin(); /* nothing */; ++p )
378 {
379 if ( p == end || *p == wxT(' ') || *p == wxT(',') )
380 {
381 if ( !strExt.empty() )
382 {
383 extensions.Add(strExt);
384 strExt.Empty();
385 }
386 //else: repeated spaces
387 // (shouldn't happen, but it's not that important if it does happen)
388
389 if ( p == end )
390 break;
391 }
392 else if ( *p == wxT('.') )
393 {
394 // remove the dot from extension (but only if it's the first char)
395 if ( !strExt.empty() )
396 {
397 strExt += wxT('.');
398 }
399 //else: no, don't append it
400 }
401 else
402 {
403 strExt += *p;
404 }
405 }
406
407 return true;
408}
409
410// set an arbitrary command:
411// could adjust the code to ask confirmation if it already exists and
412// overwriteprompt is true, but this is currently ignored as *Associate* has
413// no overwrite prompt
414bool
415wxFileTypeImpl::SetCommand(const wxString& cmd,
416 const wxString& verb,
417 bool WXUNUSED(overwriteprompt))
418{
419 wxArrayString strExtensions;
420 wxString strDesc, strIcon;
421
422 wxArrayString strTypes;
423 GetMimeTypes(strTypes);
424 if ( strTypes.IsEmpty() )
425 return false;
426
427 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
428 entry->Add(verb + wxT("=") + cmd + wxT(" %s "));
429
430 bool ok = false;
431 size_t nCount = strTypes.GetCount();
432 for ( size_t i = 0; i < nCount; i++ )
433 {
434 if ( m_manager->DoAssociation
435 (
436 strTypes[i],
437 strIcon,
438 entry,
439 strExtensions,
440 strDesc
441 ) )
442 {
443 // DoAssociation() took ownership of entry, don't delete it below
444 ok = true;
445 }
446 }
447
448 if ( !ok )
449 delete entry;
450
451 return ok;
452}
453
454// ignore index on the grounds that we only have one icon in a Unix file
455bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int WXUNUSED(index))
456{
457 if (strIcon.empty())
458 return false;
459
460 wxArrayString strExtensions;
461 wxString strDesc;
462
463 wxArrayString strTypes;
464 GetMimeTypes(strTypes);
465 if ( strTypes.IsEmpty() )
466 return false;
467
468 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
469 bool ok = false;
470 size_t nCount = strTypes.GetCount();
471 for ( size_t i = 0; i < nCount; i++ )
472 {
473 if ( m_manager->DoAssociation
474 (
475 strTypes[i],
476 strIcon,
477 entry,
478 strExtensions,
479 strDesc
480 ) )
481 {
482 // we don't need to free entry now, DoAssociation() took ownership
483 // of it
484 ok = true;
485 }
486 }
487
488 if ( !ok )
489 delete entry;
490
491 return ok;
492}
493
494// ----------------------------------------------------------------------------
495// wxMimeTypesManagerImpl (Unix)
496// ----------------------------------------------------------------------------
497
498wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
499{
500 m_initialized = false;
501}
502
503void wxMimeTypesManagerImpl::InitIfNeeded()
504{
505 if ( !m_initialized )
506 {
507 // set the flag first to prevent recursion
508 m_initialized = true;
509
510 wxString wm = wxTheApp->GetTraits()->GetDesktopEnvironment();
511
512 if (wm == wxT("KDE"))
513 Initialize( wxMAILCAP_KDE );
514 else if (wm == wxT("GNOME"))
515 Initialize( wxMAILCAP_GNOME );
516 else
517 Initialize();
518 }
519}
520
521
522
523// read system and user mailcaps and other files
524void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
525 const wxString& sExtraDir)
526{
527#ifdef __VMS
528 // XDG tables are never installed on OpenVMS
529 return;
530#else
531
532 // Read MIME type - extension associations
533 LoadXDGGlobs( "/usr/share/mime/globs" );
534 LoadXDGGlobs( "/usr/local/share/mime/globs" );
535
536 // Load desktop files for XDG, and then override them with the defaults.
537 // We will override them one desktop file at a time, rather
538 // than one mime type at a time, but it should be a reasonable
539 // heuristic.
540 {
541 wxString xdgDataHome = wxGetenv("XDG_DATA_HOME");
542 if ( xdgDataHome.empty() )
543 xdgDataHome = wxGetHomeDir() + "/.local/share";
544 wxString xdgDataDirs = wxGetenv("XDG_DATA_DIRS");
545 if ( xdgDataDirs.empty() )
546 {
547 xdgDataDirs = "/usr/local/share:/usr/share";
548 if (mailcapStyles & wxMAILCAP_GNOME)
549 xdgDataDirs += ":/usr/share/gnome:/opt/gnome/share";
550 if (mailcapStyles & wxMAILCAP_KDE)
551 xdgDataDirs += ":/usr/share/kde3:/opt/kde3/share";
552 }
553 if ( !sExtraDir.empty() )
554 {
555 xdgDataDirs += ':';
556 xdgDataDirs += sExtraDir;
557 }
558
559 wxArrayString dirs;
560 wxStringTokenizer tokenizer(xdgDataDirs, ":");
561 while ( tokenizer.HasMoreTokens() )
562 {
563 wxString p = tokenizer.GetNextToken();
564 dirs.Add(p);
565 }
566 dirs.insert(dirs.begin(), xdgDataHome);
567
568 wxString defaultsList;
569 size_t i;
570 for (i = 0; i < dirs.GetCount(); i++)
571 {
572 wxString f = dirs[i];
573 if (f.Last() != '/') f += '/';
574 f += "applications/defaults.list";
575 if (wxFileExists(f))
576 {
577 defaultsList = f;
578 break;
579 }
580 }
581
582 // Load application files and associate them to corresponding mime types.
583 size_t nDirs = dirs.GetCount();
584 for (size_t nDir = 0; nDir < nDirs; nDir++)
585 {
586 wxString dirStr = dirs[nDir];
587 if (dirStr.Last() != '/') dirStr += '/';
588 dirStr += "applications";
589 LoadXDGAppsFilesFromDir(dirStr);
590 }
591
592 if (!defaultsList.IsEmpty())
593 {
594 wxArrayString deskTopFilesSeen;
595
596 wxMimeTextFile textfile(defaultsList);
597 if ( textfile.Open() )
598 {
599 int nIndex = textfile.pIndexOf( wxT("[Default Applications]") );
600 if (nIndex != wxNOT_FOUND)
601 {
602 for (i = nIndex+1; i < textfile.GetLineCount(); i++)
603 {
604 if (textfile.GetLine(i).Find(wxT("=")) != wxNOT_FOUND)
605 {
606 wxString desktopFile = textfile.GetCmd(i);
607
608 if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND)
609 {
610 deskTopFilesSeen.Add(desktopFile);
611 size_t j;
612 for (j = 0; j < dirs.GetCount(); j++)
613 {
614 wxString desktopPath = dirs[j];
615 if (desktopPath.Last() != '/') desktopPath += '/';
616 desktopPath += "applications/";
617 desktopPath += desktopFile;
618
619 if (wxFileExists(desktopPath))
620 LoadXDGApp(desktopPath);
621 }
622 }
623 }
624 }
625 }
626 }
627 }
628 }
629#endif
630}
631
632// clear data so you can read another group of WM files
633void wxMimeTypesManagerImpl::ClearData()
634{
635 m_aTypes.Clear();
636 m_aIcons.Clear();
637 m_aExtensions.Clear();
638 m_aDescriptions.Clear();
639
640 WX_CLEAR_ARRAY(m_aEntries);
641 m_aEntries.Empty();
642}
643
644wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
645{
646 ClearData();
647}
648
649wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
650{
651 InitIfNeeded();
652
653 wxString strType = ftInfo.GetMimeType();
654 wxString strDesc = ftInfo.GetDescription();
655 wxString strIcon = ftInfo.GetIconFile();
656
657 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
658
659 if ( ! ftInfo.GetOpenCommand().empty())
660 entry->Add(wxT("open=") + ftInfo.GetOpenCommand() + wxT(" %s "));
661 if ( ! ftInfo.GetPrintCommand().empty())
662 entry->Add(wxT("print=") + ftInfo.GetPrintCommand() + wxT(" %s "));
663
664 // now find where these extensions are in the data store and remove them
665 wxArrayString sA_Exts = ftInfo.GetExtensions();
666 wxString sExt, sExtStore;
667 size_t i, nIndex;
668 size_t nExtCount = sA_Exts.GetCount();
669 for (i=0; i < nExtCount; i++)
670 {
671 sExt = sA_Exts.Item(i);
672
673 // clean up to just a space before and after
674 sExt.Trim().Trim(false);
675 sExt = wxT(' ') + sExt + wxT(' ');
676 size_t nCount = m_aExtensions.GetCount();
677 for (nIndex = 0; nIndex < nCount; nIndex++)
678 {
679 sExtStore = m_aExtensions.Item(nIndex);
680 if (sExtStore.Replace(sExt, wxT(" ") ) > 0)
681 m_aExtensions.Item(nIndex) = sExtStore;
682 }
683 }
684
685 if ( !DoAssociation(strType, strIcon, entry, sA_Exts, strDesc) )
686 return NULL;
687
688 return GetFileTypeFromMimeType(strType);
689}
690
691bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
692 const wxString& strIcon,
693 wxMimeTypeCommands *entry,
694 const wxArrayString& strExtensions,
695 const wxString& strDesc)
696{
697 int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
698
699 if ( nIndex == wxNOT_FOUND )
700 return false;
701
702 return true;
703}
704
705int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
706 const wxString& strIcon,
707 wxMimeTypeCommands *entry,
708 const wxArrayString& strExtensions,
709 const wxString& strDesc,
710 bool replaceExisting)
711{
712 InitIfNeeded();
713
714 // ensure mimetype is always lower case
715 wxString mimeType = strType.Lower();
716
717 // is this a known MIME type?
718 int nIndex = m_aTypes.Index(mimeType);
719 if ( nIndex == wxNOT_FOUND )
720 {
721 // We put MIME types containing "application" at the end, so that
722 // if the MIME type for the extention "htm" is searched for, it will
723 // rather find "text/html" than "application/x-mozilla-bookmarks".
724 if (mimeType.Find( "application" ) == 0)
725 {
726 // new file type
727 m_aTypes.Add(mimeType);
728 m_aIcons.Add(strIcon);
729 m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
730
731 // change nIndex so we can use it below to add the extensions
732 m_aExtensions.Add(wxEmptyString);
733 nIndex = m_aExtensions.size() - 1;
734
735 m_aDescriptions.Add(strDesc);
736 }
737 else
738 {
739 // new file type
740 m_aTypes.Insert(mimeType,0);
741 m_aIcons.Insert(strIcon,0);
742 m_aEntries.Insert(entry ? entry : new wxMimeTypeCommands,0);
743
744 // change nIndex so we can use it below to add the extensions
745 m_aExtensions.Insert(wxEmptyString,0);
746 nIndex = 0;
747
748 m_aDescriptions.Insert(strDesc,0);
749 }
750 }
751 else // yes, we already have it
752 {
753 if ( replaceExisting )
754 {
755 // if new description change it
756 if ( !strDesc.empty())
757 m_aDescriptions[nIndex] = strDesc;
758
759 // if new icon change it
760 if ( !strIcon.empty())
761 m_aIcons[nIndex] = strIcon;
762
763 if ( entry )
764 {
765 delete m_aEntries[nIndex];
766 m_aEntries[nIndex] = entry;
767 }
768 }
769 else // add data we don't already have ...
770 {
771 // if new description add only if none
772 if ( m_aDescriptions[nIndex].empty() )
773 m_aDescriptions[nIndex] = strDesc;
774
775 // if new icon and no existing icon
776 if ( m_aIcons[nIndex].empty() )
777 m_aIcons[nIndex] = strIcon;
778
779 // add any new entries...
780 if ( entry )
781 {
782 wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
783
784 size_t count = entry->GetCount();
785 for ( size_t i = 0; i < count; i++ )
786 {
787 const wxString& verb = entry->GetVerb(i);
788 if ( !entryOld->HasVerb(verb) )
789 {
790 entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
791 }
792 }
793
794 // as we don't store it anywhere, it won't be deleted later as
795 // usual -- do it immediately instead
796 delete entry;
797 }
798 }
799 }
800
801 // always add the extensions to this mimetype
802 wxString& exts = m_aExtensions[nIndex];
803
804 // add all extensions we don't have yet
805 wxString ext;
806 size_t count = strExtensions.GetCount();
807 for ( size_t i = 0; i < count; i++ )
808 {
809 ext = strExtensions[i];
810 ext += wxT(' ');
811
812 if ( exts.Find(ext) == wxNOT_FOUND )
813 {
814 exts += ext;
815 }
816 }
817
818 // check data integrity
819 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
820 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
821 m_aTypes.GetCount() == m_aIcons.GetCount() &&
822 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
823
824 return nIndex;
825}
826
827wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
828{
829 if (ext.empty() )
830 return NULL;
831
832 InitIfNeeded();
833
834 size_t count = m_aExtensions.GetCount();
835 for ( size_t n = 0; n < count; n++ )
836 {
837 wxStringTokenizer tk(m_aExtensions[n], wxT(' '));
838
839 while ( tk.HasMoreTokens() )
840 {
841 // consider extensions as not being case-sensitive
842 if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
843 {
844 // found
845 wxFileType *fileType = new wxFileType;
846 fileType->m_impl->Init(this, n);
847
848 return fileType;
849 }
850 }
851 }
852
853 return NULL;
854}
855
856wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
857{
858 InitIfNeeded();
859
860 wxFileType * fileType = NULL;
861 // mime types are not case-sensitive
862 wxString mimetype(mimeType);
863 mimetype.MakeLower();
864
865 // first look for an exact match
866 int index = m_aTypes.Index(mimetype);
867
868 if ( index != wxNOT_FOUND )
869 {
870 fileType = new wxFileType;
871 fileType->m_impl->Init(this, index);
872 }
873
874 // then try to find "text/*" as match for "text/plain" (for example)
875 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
876 // the whole string - ok.
877
878 index = wxNOT_FOUND;
879 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
880
881 size_t nCount = m_aTypes.GetCount();
882 for ( size_t n = 0; n < nCount; n++ )
883 {
884 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
885 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") )
886 {
887 index = n;
888 break;
889 }
890 }
891
892 if ( index != wxNOT_FOUND )
893 {
894 // don't throw away fileType that was already found
895 if (!fileType)
896 fileType = new wxFileType;
897 fileType->m_impl->Init(this, index);
898 }
899
900 return fileType;
901}
902
903wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
904{
905 wxString command, testcmd, sV, sTmp;
906 sV = verb + wxT("=");
907
908 // list of verb = command pairs for this mimetype
909 wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
910
911 size_t i;
912 size_t nCount = sPairs->GetCount();
913 for ( i = 0; i < nCount; i++ )
914 {
915 sTmp = sPairs->GetVerbCmd (i);
916 if ( sTmp.Contains(sV) )
917 command = sTmp.AfterFirst(wxT('='));
918 }
919
920 return command;
921}
922
923void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
924{
925 InitIfNeeded();
926
927 wxString extensions;
928 const wxArrayString& exts = filetype.GetExtensions();
929 size_t nExts = exts.GetCount();
930 for ( size_t nExt = 0; nExt < nExts; nExt++ )
931 {
932 if ( nExt > 0 )
933 extensions += wxT(' ');
934
935 extensions += exts[nExt];
936 }
937
938 AddMimeTypeInfo(filetype.GetMimeType(),
939 extensions,
940 filetype.GetDescription());
941}
942
943void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
944 const wxString& strExtensions,
945 const wxString& strDesc)
946{
947 // reading mailcap may find image/* , while
948 // reading mime.types finds image/gif and no match is made
949 // this means all the get functions don't work fix this
950 wxString strIcon;
951 wxString sTmp = strExtensions;
952
953 wxArrayString sExts;
954 sTmp.Trim().Trim(false);
955
956 while (!sTmp.empty())
957 {
958 sExts.Add(sTmp.AfterLast(wxT(' ')));
959 sTmp = sTmp.BeforeLast(wxT(' '));
960 }
961
962 AddToMimeData(strMimeType, strIcon, NULL, sExts, strDesc, true);
963}
964
965size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
966{
967 InitIfNeeded();
968
969 mimetypes.Empty();
970
971 size_t count = m_aTypes.GetCount();
972 for ( size_t n = 0; n < count; n++ )
973 {
974 // don't return template types from here (i.e. anything containg '*')
975 const wxString &type = m_aTypes[n];
976 if ( type.Find(wxT('*')) == wxNOT_FOUND )
977 {
978 mimetypes.Add(type);
979 }
980 }
981
982 return mimetypes.GetCount();
983}
984
985// ----------------------------------------------------------------------------
986// writing to MIME type files
987// ----------------------------------------------------------------------------
988
989bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft)
990{
991 InitIfNeeded();
992
993 wxArrayString sMimeTypes;
994 ft->GetMimeTypes(sMimeTypes);
995
996 size_t i;
997 size_t nCount = sMimeTypes.GetCount();
998 for (i = 0; i < nCount; i ++)
999 {
1000 const wxString &sMime = sMimeTypes.Item(i);
1001 int nIndex = m_aTypes.Index(sMime);
1002 if ( nIndex == wxNOT_FOUND)
1003 {
1004 // error if we get here ??
1005 return false;
1006 }
1007 else
1008 {
1009 m_aTypes.RemoveAt(nIndex);
1010 m_aEntries.RemoveAt(nIndex);
1011 m_aExtensions.RemoveAt(nIndex);
1012 m_aDescriptions.RemoveAt(nIndex);
1013 m_aIcons.RemoveAt(nIndex);
1014 }
1015 }
1016 // check data integrity
1017 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
1018 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
1019 m_aTypes.GetCount() == m_aIcons.GetCount() &&
1020 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
1021
1022 return true;
1023}
1024
1025#endif
1026 // wxUSE_MIMETYPE && wxUSE_FILE