Fixed mime type recognition under Gnome by reading appropriate
[wxWidgets.git] / src / unix / mimetype.cpp
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 // known bugs; there may be others!! chris elliott, biol75@york.ac.uk 27 Mar 01
13
14 // 1) .mailcap and .mimetypes can be either in a netscape or metamail format
15 // and entries may get confused during writing (I've tried to fix this; please let me know
16 // any files that fail)
17 // 2) KDE and Gnome do not yet fully support international read/write
18 // 3) Gnome key lines like open.latex."LaTeX this file"=latex %f will have odd results
19 // 4) writing to files comments out the existing data; I hope this avoids losing
20 // any data which we could not read, and data which we did not store like test=
21 // 5) results from reading files with multiple entries (especially matches with type/* )
22 // may (or may not) work for getXXX commands
23 // 6) Loading the png icons in Gnome doesn't work for me...
24 // 7) In Gnome, if keys.mime exists but keys.users does not, there is
25 // an error message in debug mode, but the file is still written OK
26 // 8) Deleting entries is only allowed from the user file; sytem wide entries
27 // will be preserved during unassociate
28 // 9) KDE does not yet handle multiple actions; Netscape mode never will
29
30 // TODO: this file is a mess, we need to split it and review everything (VZ)
31
32 // for compilers that support precompilation, includes "wx.h".
33 #include "wx/wxprec.h"
34
35 #ifdef __BORLANDC__
36 #pragma hdrstop
37 #endif
38
39 #if wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE
40
41 #include "wx/unix/mimetype.h"
42
43 #ifndef WX_PRECOMP
44 #include "wx/dynarray.h"
45 #include "wx/string.h"
46 #include "wx/intl.h"
47 #include "wx/log.h"
48 #include "wx/utils.h"
49 #endif
50
51 #include "wx/file.h"
52 #include "wx/confbase.h"
53
54 #include "wx/ffile.h"
55 #include "wx/textfile.h"
56 #include "wx/dir.h"
57 #include "wx/tokenzr.h"
58 #include "wx/iconloc.h"
59 #include "wx/filename.h"
60 #include "wx/app.h"
61 #include "wx/apptrait.h"
62
63 #if wxUSE_LIBGNOMEVFS
64 // Not GUI dependent
65 #include "wx/gtk/gnome/gvfs.h"
66 #endif
67
68 // other standard headers
69 #include <ctype.h>
70
71 // this class is a wxTextFile specialization for dealing with files storing
72 // various MIME-related information
73 //
74 // it should be used instead of wxTextFile even if none of its additional
75 // methods are used just because it handles files with mixed encodings (often
76 // the case for MIME files which contain strings for different languages)
77 // correctly, see OnRead()
78 class wxMimeTextFile : public wxTextFile
79 {
80 public:
81 // constructors
82 wxMimeTextFile () : wxTextFile () { }
83 wxMimeTextFile(const wxString& strFile) : wxTextFile(strFile) { }
84
85 int pIndexOf(const wxString& sSearch,
86 bool bIncludeComments = false,
87 int iStart = 0)
88 {
89 wxString sTest = sSearch;
90 sTest.MakeLower();
91 for(size_t i = iStart; i < GetLineCount(); i++)
92 {
93 wxString sLine = GetLine(i).Trim(false);
94 if(bIncludeComments || ! sLine.StartsWith(wxT("#")))
95 {
96 sLine.MakeLower();
97 if(sLine.StartsWith(sTest))
98 return (int)i;
99 }
100 }
101 return wxNOT_FOUND;
102 }
103
104 bool CommentLine(int nIndex)
105 {
106 if (nIndex < 0)
107 return false;
108 if (nIndex >= (int)GetLineCount() )
109 return false;
110
111 GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#"));
112 return true;
113 }
114
115 bool CommentLine(const wxString & sTest)
116 {
117 int nIndex = pIndexOf(sTest);
118 if (nIndex < 0)
119 return false;
120 if (nIndex >= (int)GetLineCount() )
121 return false;
122
123 GetLine(nIndex) = GetLine(nIndex).Prepend(wxT("#"));
124 return true;
125 }
126
127 wxString GetVerb(size_t i)
128 {
129 if (i > GetLineCount() )
130 return wxEmptyString;
131
132 wxString sTmp = GetLine(i).BeforeFirst(wxT('='));
133 return sTmp;
134 }
135
136 wxString GetCmd(size_t i)
137 {
138 if (i > GetLineCount() )
139 return wxEmptyString;
140
141 wxString sTmp = GetLine(i).AfterFirst(wxT('='));
142 return sTmp;
143 }
144 };
145
146 // ----------------------------------------------------------------------------
147 // constants
148 // ----------------------------------------------------------------------------
149
150 // MIME code tracing mask
151 #define TRACE_MIME wxT("mime")
152
153 // give trace messages about the results of mailcap tests
154 #define TRACE_MIME_TEST wxT("mimetest")
155
156 // ----------------------------------------------------------------------------
157 // private functions
158 // ----------------------------------------------------------------------------
159
160 // there are some fields which we don't understand but for which we don't give
161 // warnings as we know that they're not important - this function is used to
162 // test for them
163 static bool IsKnownUnimportantField(const wxString& field);
164
165 // ----------------------------------------------------------------------------
166 // private classes
167 // ----------------------------------------------------------------------------
168
169
170 // This class uses both mailcap and mime.types to gather information about file
171 // types.
172 //
173 // The information about mailcap file was extracted from metamail(1) sources
174 // and documentation and subsequently revised when I found the RFC 1524
175 // describing it.
176 //
177 // Format of mailcap file: spaces are ignored, each line is either a comment
178 // (starts with '#') or a line of the form <field1>;<field2>;...;<fieldN>.
179 // A backslash can be used to quote semicolons and newlines (and, in fact,
180 // anything else including itself).
181 //
182 // The first field is always the MIME type in the form of type/subtype (see RFC
183 // 822) where subtype may be '*' meaning "any". Following metamail, we accept
184 // "type" which means the same as "type/*", although I'm not sure whether this
185 // is standard.
186 //
187 // The second field is always the command to run. It is subject to
188 // parameter/filename expansion described below.
189 //
190 // All the following fields are optional and may not be present at all. If
191 // they're present they may appear in any order, although each of them should
192 // appear only once. The optional fields are the following:
193 // * notes=xxx is an uninterpreted string which is silently ignored
194 // * test=xxx is the command to be used to determine whether this mailcap line
195 // applies to our data or not. The RHS of this field goes through the
196 // parameter/filename expansion (as the 2nd field) and the resulting string
197 // is executed. The line applies only if the command succeeds, i.e. returns 0
198 // exit code.
199 // * print=xxx is the command to be used to print (and not view) the data of
200 // this type (parameter/filename expansion is done here too)
201 // * edit=xxx is the command to open/edit the data of this type
202 // * needsterminal means that a new interactive console must be created for
203 // the viewer
204 // * copiousoutput means that the viewer doesn't interact with the user but
205 // produces (possibly) a lof of lines of output on stdout (i.e. "cat" is a
206 // good example), thus it might be a good idea to use some kind of paging
207 // mechanism.
208 // * textualnewlines means not to perform CR/LF translation (not honored)
209 // * compose and composetyped fields are used to determine the program to be
210 // called to create a new message pert in the specified format (unused).
211 //
212 // Parameter/filename expansion:
213 // * %s is replaced with the (full) file name
214 // * %t is replaced with MIME type/subtype of the entry
215 // * for multipart type only %n is replaced with the nnumber of parts and %F is
216 // replaced by an array of (content-type, temporary file name) pairs for all
217 // message parts (TODO)
218 // * %{parameter} is replaced with the value of parameter taken from
219 // Content-type header line of the message.
220 //
221 //
222 // There are 2 possible formats for mime.types file, one entry per line (used
223 // for global mime.types and called Mosaic format) and "expanded" format where
224 // an entry takes multiple lines (used for users mime.types and called
225 // Netscape format).
226 //
227 // For both formats spaces are ignored and lines starting with a '#' are
228 // comments. Each record has one of two following forms:
229 // a) for "brief" format:
230 // <mime type> <space separated list of extensions>
231 // b) for "expanded" format:
232 // type=<mime type> BACKSLASH
233 // desc="<description>" BACKSLASH
234 // exts="<comma separated list of extensions>"
235 //
236 // (where BACKSLASH is a literal '\\' which we can't put here because cpp
237 // misinterprets it)
238 //
239 // We try to autodetect the format of mime.types: if a non-comment line starts
240 // with "type=" we assume the second format, otherwise the first one.
241
242 // there may be more than one entry for one and the same mime type, to
243 // choose the right one we have to run the command specified in the test
244 // field on our data.
245
246 // ----------------------------------------------------------------------------
247 // wxGNOME
248 // ----------------------------------------------------------------------------
249
250 // GNOME stores the info we're interested in in several locations:
251 // 1. xxx.keys files under /usr/share/mime-info
252 // 2. xxx.keys files under ~/.gnome/mime-info
253 //
254 // Update (Chris Elliott): apparently there may be an optional "[lang]" prefix
255 // just before the field name.
256
257
258 void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename,
259 const wxArrayString& dirs)
260 {
261 wxMimeTextFile textfile(filename);
262 if ( !textfile.Open() )
263 return;
264
265 wxLogTrace(TRACE_MIME, wxT("--- Opened Gnome file %s ---"),
266 filename.c_str());
267
268 wxArrayString search_dirs( dirs );
269
270 // values for the entry being parsed
271 wxString curMimeType, curIconFile;
272 wxMimeTypeCommands * entry = new wxMimeTypeCommands;
273
274 wxArrayString strExtensions;
275 wxString strDesc;
276
277 const wxChar *pc;
278 size_t nLineCount = textfile.GetLineCount();
279 size_t nLine = 0;
280 while ( nLine < nLineCount )
281 {
282 pc = textfile[nLine].c_str();
283 if ( *pc != wxT('#') )
284 {
285
286 wxLogTrace(TRACE_MIME, wxT("--- Reading from Gnome file %s '%s' ---"),
287 filename.c_str(), pc);
288
289 // trim trailing space and tab
290 while ((*pc == wxT(' ')) || (*pc == wxT('\t')))
291 pc++;
292
293 wxString sTmp(pc);
294 int equal_pos = sTmp.Find( wxT('=') );
295 if (equal_pos > 0)
296 {
297 wxString left_of_equal = sTmp.Left( equal_pos );
298 const wxChar *right_of_equal = pc;
299 right_of_equal += equal_pos+1;
300
301 if (left_of_equal == wxT("icon_filename"))
302 {
303 // GNOME 2:
304 curIconFile = right_of_equal;
305
306 wxFileName newFile( curIconFile );
307 if (newFile.IsRelative() || newFile.FileExists())
308 {
309 size_t nDirs = search_dirs.GetCount();
310
311 for (size_t nDir = 0; nDir < nDirs; nDir++)
312 {
313 newFile.SetPath( search_dirs[nDir] );
314 newFile.AppendDir( wxT("pixmaps") );
315 newFile.AppendDir( wxT("document-icons") );
316 newFile.SetExt( wxT("png") );
317 if (newFile.FileExists())
318 {
319 curIconFile = newFile.GetFullPath();
320 // reorder search_dirs for speedup (fewer
321 // calls to FileExist() required)
322 if (nDir != 0)
323 {
324 const wxString &tmp = search_dirs[nDir];
325 search_dirs.RemoveAt( nDir );
326 search_dirs.Insert( tmp, 0 );
327 }
328 break;
329 }
330 }
331 }
332 }
333 else if (left_of_equal == wxT("open"))
334 {
335 sTmp = right_of_equal;
336 sTmp.Replace( wxT("%f"), wxT("%s") );
337 sTmp.Prepend( wxT("open=") );
338 entry->Add(sTmp);
339 }
340 else if (left_of_equal == wxT("view"))
341 {
342 sTmp = right_of_equal;
343 sTmp.Replace( wxT("%f"), wxT("%s") );
344 sTmp.Prepend( wxT("view=") );
345 entry->Add(sTmp);
346 }
347 else if (left_of_equal == wxT("print"))
348 {
349 sTmp = right_of_equal;
350 sTmp.Replace( wxT("%f"), wxT("%s") );
351 sTmp.Prepend( wxT("print=") );
352 entry->Add(sTmp);
353 }
354 else if (left_of_equal == wxT("description"))
355 {
356 strDesc = right_of_equal;
357 }
358 else if (left_of_equal == wxT("short_list_application_ids_for_novice_user_level"))
359 {
360 sTmp = right_of_equal;
361 if (sTmp.Contains( wxT(",") ))
362 sTmp = sTmp.BeforeFirst( wxT(',') );
363 sTmp.Prepend( wxT("open=") );
364 sTmp.Append( wxT(" %s") );
365 entry->Add(sTmp);
366 }
367
368 } // emd of has an equals sign
369 else
370 {
371 // not a comment and not an equals sign
372 if (sTmp.Contains(wxT('/')))
373 {
374 // this is the start of the new mimetype
375 // overwrite any existing data
376 if (! curMimeType.empty())
377 {
378 AddToMimeData( curMimeType, curIconFile, entry, strExtensions, strDesc );
379
380 // now get ready for next bit
381 entry = new wxMimeTypeCommands;
382 }
383
384 curMimeType = sTmp.BeforeFirst(wxT(':'));
385 }
386 }
387 } // end of not a comment
388
389 // ignore blank lines
390 nLine++;
391 } // end of while, save any data
392
393 if ( curMimeType.empty() )
394 delete entry;
395 else
396 AddToMimeData( curMimeType, curIconFile, entry, strExtensions, strDesc);
397 }
398
399 void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& filename)
400 {
401 wxMimeTextFile textfile(filename);
402 if ( !textfile.Open() )
403 return;
404
405 wxLogTrace(TRACE_MIME,
406 wxT("--- Opened Gnome file %s ---"),
407 filename.c_str());
408
409 // values for the entry being parsed
410 wxString curMimeType, curExtList;
411
412 const wxChar *pc;
413 size_t nLineCount = textfile.GetLineCount();
414 for ( size_t nLine = 0; /* nothing */; nLine++ )
415 {
416 if ( nLine < nLineCount )
417 {
418 pc = textfile[nLine].c_str();
419 if ( *pc == wxT('#') )
420 {
421 // skip comments
422 continue;
423 }
424 }
425 else
426 {
427 // so that we will fall into the "if" below
428 pc = NULL;
429 }
430
431 if ( !pc || !*pc )
432 {
433 // end of the entry
434 if ( !curMimeType.empty() && !curExtList.empty() )
435 {
436 wxLogTrace(TRACE_MIME,
437 wxT("--- At end of Gnome file finding mimetype %s ---"),
438 curMimeType.c_str());
439
440 AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString);
441 }
442
443 if ( !pc )
444 {
445 // the end: this can only happen if nLine == nLineCount
446 break;
447 }
448
449 curExtList.Empty();
450
451 continue;
452 }
453
454 // what do we have here?
455 if ( *pc == wxT('\t') )
456 {
457 // this is a field=value ling
458 pc++; // skip leading TAB
459
460 static const int lenField = 5; // strlen("ext: ")
461 if ( wxStrncmp(pc, wxT("ext: "), lenField) == 0 )
462 {
463 // skip it and take everything left until the end of line
464 curExtList = pc + lenField;
465 }
466 //else: some other field, we don't care
467 }
468 else
469 {
470 // this is the start of the new section
471 wxLogTrace(TRACE_MIME,
472 wxT("--- In Gnome file finding mimetype %s ---"),
473 curMimeType.c_str());
474
475 if (! curMimeType.empty())
476 AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString);
477
478 curMimeType.Empty();
479
480 while ( *pc != wxT(':') && *pc != wxT('\0') )
481 {
482 curMimeType += *pc++;
483 }
484 }
485 }
486 }
487
488
489 void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
490 const wxString& dirbase, const wxArrayString& dirs)
491 {
492 wxASSERT_MSG( !dirbase.empty() && !wxEndsWithPathSeparator(dirbase),
493 wxT("base directory shouldn't end with a slash") );
494
495 wxString dirname = dirbase;
496 dirname << wxT("/mime-info");
497
498 // Don't complain if we don't have permissions to read - it confuses users
499 wxLogNull logNull;
500
501 if ( !wxDir::Exists(dirname) )
502 return;
503
504 wxDir dir(dirname);
505 if ( !dir.IsOpened() )
506 return;
507
508 // we will concatenate it with filename to get the full path below
509 dirname += wxT('/');
510
511 wxString filename;
512 bool cont;
513
514 cont = dir.GetFirst(&filename, wxT("*.mime"), wxDIR_FILES);
515 while ( cont )
516 {
517 LoadGnomeMimeTypesFromMimeFile(dirname + filename);
518
519 cont = dir.GetNext(&filename);
520 }
521
522 cont = dir.GetFirst(&filename, wxT("*.keys"), wxDIR_FILES);
523 while ( cont )
524 {
525 LoadGnomeDataFromKeyFile(dirname + filename, dirs);
526
527 cont = dir.GetNext(&filename);
528 }
529
530 // FIXME: Hack alert: We scan all icons and deduce the
531 // mime-type from the file name.
532 dirname = dirbase;
533 dirname << wxT("/pixmaps/document-icons");
534
535 // these are always empty in this file
536 wxArrayString strExtensions;
537 wxString strDesc;
538
539 if ( !wxDir::Exists(dirname) )
540 {
541 // Just test for default GPE dir also
542 dirname = wxT("/usr/share/gpe/pixmaps/default/filemanager/document-icons");
543
544 if ( !wxDir::Exists(dirname) )
545 return;
546 }
547
548 wxDir dir2( dirname );
549
550 cont = dir2.GetFirst(&filename, wxT("gnome-*.png"), wxDIR_FILES);
551 while ( cont )
552 {
553 wxString mimeType = filename;
554 mimeType.Remove( 0, 6 ); // remove "gnome-"
555 mimeType.Remove( mimeType.Len() - 4, 4 ); // remove ".png"
556 int pos = mimeType.Find( wxT("-") );
557 if (pos != wxNOT_FOUND)
558 {
559 mimeType.SetChar( pos, wxT('/') );
560 wxString iconFile = dirname;
561 iconFile << wxT("/");
562 iconFile << filename;
563 AddToMimeData( mimeType, iconFile, NULL, strExtensions, strDesc, true );
564 }
565
566 cont = dir2.GetNext(&filename);
567 }
568 }
569
570 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir)
571 {
572 wxArrayString dirs;
573
574 wxString gnomedir = wxGetenv( wxT("GNOMEDIR") );
575 if (!gnomedir.empty())
576 {
577 gnomedir << wxT("/share");
578 dirs.Add( gnomedir );
579 }
580
581 dirs.Add(wxT("/usr/share"));
582 dirs.Add(wxT("/usr/local/share"));
583
584 gnomedir = wxGetHomeDir();
585 gnomedir << wxT("/.gnome");
586 dirs.Add( gnomedir );
587
588 if (!sExtraDir.empty())
589 dirs.Add( sExtraDir );
590
591 size_t nDirs = dirs.GetCount();
592 for ( size_t nDir = 0; nDir < nDirs; nDir++ )
593 {
594 LoadGnomeMimeFilesFromDir(dirs[nDir], dirs);
595 }
596 }
597
598 // ----------------------------------------------------------------------------
599 // KDE
600 // ----------------------------------------------------------------------------
601
602
603 // KDE stores the icon info in its .kdelnk files. The file for mimetype/subtype
604 // may be found in either of the following locations
605 //
606 // 1. $KDEDIR/share/mimelnk/mimetype/subtype.kdelnk
607 // 2. ~/.kde/share/mimelnk/mimetype/subtype.kdelnk
608 //
609 // The format of a .kdelnk file is almost the same as the one used by
610 // wxFileConfig, i.e. there are groups, comments and entries. The icon is the
611 // value for the entry "Type"
612
613 // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
614 // for now write to .kdelnk but should eventually do .desktop instead (in preference??)
615
616 bool wxMimeTypesManagerImpl::CheckKDEDirsExist( const wxString &sOK, const wxString &sTest )
617 {
618 if (sTest.empty())
619 {
620 return wxDir::Exists(sOK);
621 }
622 else
623 {
624 wxString sStart = sOK + wxT("/") + sTest.BeforeFirst(wxT('/'));
625 if (!wxDir::Exists(sStart))
626 wxMkdir(sStart);
627 wxString sEnd = sTest.AfterFirst(wxT('/'));
628 return CheckKDEDirsExist(sStart, sEnd);
629 }
630 }
631
632 bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index, bool delete_index)
633 {
634 wxMimeTextFile appoutfile, mimeoutfile;
635 wxString sHome = wxGetHomeDir();
636 wxString sTmp = wxT(".kde/share/mimelnk/");
637 wxString sMime = m_aTypes[index];
638 CheckKDEDirsExist(sHome, sTmp + sMime.BeforeFirst(wxT('/')) );
639 sTmp = sHome + wxT('/') + sTmp + sMime + wxT(".kdelnk");
640
641 bool bTemp;
642 bool bMimeExists = mimeoutfile.Open(sTmp);
643 if (!bMimeExists)
644 {
645 bTemp = mimeoutfile.Create(sTmp);
646 // some unknown error eg out of disk space
647 if (!bTemp)
648 return false;
649 }
650
651 sTmp = wxT(".kde/share/applnk/");
652 CheckKDEDirsExist(sHome, sTmp + sMime.AfterFirst(wxT('/')) );
653 sTmp = sHome + wxT('/') + sTmp + sMime.AfterFirst(wxT('/')) + wxT(".kdelnk");
654
655 bool bAppExists;
656 bAppExists = appoutfile.Open(sTmp);
657 if (!bAppExists)
658 {
659 bTemp = appoutfile.Create(sTmp);
660 // some unknown error eg out of disk space
661 if (!bTemp)
662 return false;
663 }
664
665 // fixed data; write if new file
666 if (!bMimeExists)
667 {
668 mimeoutfile.AddLine(wxT("#KDE Config File"));
669 mimeoutfile.AddLine(wxT("[KDE Desktop Entry]"));
670 mimeoutfile.AddLine(wxT("Version=1.0"));
671 mimeoutfile.AddLine(wxT("Type=MimeType"));
672 mimeoutfile.AddLine(wxT("MimeType=") + sMime);
673 }
674
675 if (!bAppExists)
676 {
677 mimeoutfile.AddLine(wxT("#KDE Config File"));
678 mimeoutfile.AddLine(wxT("[KDE Desktop Entry]"));
679 appoutfile.AddLine(wxT("Version=1.0"));
680 appoutfile.AddLine(wxT("Type=Application"));
681 appoutfile.AddLine(wxT("MimeType=") + sMime + wxT(';'));
682 }
683
684 // variable data
685 // ignore locale
686 mimeoutfile.CommentLine(wxT("Comment="));
687 if (!delete_index)
688 mimeoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]);
689 appoutfile.CommentLine(wxT("Name="));
690 if (!delete_index)
691 appoutfile.AddLine(wxT("Comment=") + m_aDescriptions[index]);
692
693 sTmp = m_aIcons[index];
694 // we can either give the full path, or the shortfilename if its in
695 // one of the directories we search
696 mimeoutfile.CommentLine(wxT("Icon=") );
697 if (!delete_index)
698 mimeoutfile.AddLine(wxT("Icon=") + sTmp );
699 appoutfile.CommentLine(wxT("Icon=") );
700 if (!delete_index)
701 appoutfile.AddLine(wxT("Icon=") + sTmp );
702
703 sTmp = wxT(" ") + m_aExtensions[index];
704
705 wxStringTokenizer tokenizer(sTmp, wxT(" "));
706 sTmp = wxT("Patterns=");
707 mimeoutfile.CommentLine(sTmp);
708 while ( tokenizer.HasMoreTokens() )
709 {
710 // holds an extension; need to change it to *.ext;
711 wxString e = wxT("*.") + tokenizer.GetNextToken() + wxT(";");
712 sTmp += e;
713 }
714
715 if (!delete_index)
716 mimeoutfile.AddLine(sTmp);
717
718 wxMimeTypeCommands * entries = m_aEntries[index];
719 // if we don't find open just have an empty string ... FIX this
720 sTmp = entries->GetCommandForVerb(wxT("open"));
721 sTmp.Replace( wxT("%s"), wxT("%f") );
722
723 mimeoutfile.CommentLine(wxT("DefaultApp=") );
724 if (!delete_index)
725 mimeoutfile.AddLine(wxT("DefaultApp=") + sTmp);
726
727 sTmp.Replace( wxT("%f"), wxT("") );
728 appoutfile.CommentLine(wxT("Exec="));
729 if (!delete_index)
730 appoutfile.AddLine(wxT("Exec=") + sTmp);
731
732 if (entries->GetCount() > 1)
733 {
734 //other actions as well as open
735 }
736
737 bTemp = false;
738 if (mimeoutfile.Write())
739 bTemp = true;
740 mimeoutfile.Close();
741 if (appoutfile.Write())
742 bTemp = true;
743 appoutfile.Close();
744
745 return bTemp;
746 }
747
748 void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
749 const wxString& subdir,
750 const wxString& filename,
751 const wxArrayString& icondirs)
752 {
753 wxFileName fullname(dirbase, filename);
754 wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"),
755 fullname.GetFullPath().c_str());
756
757 wxMimeTextFile file;
758 if ( !file.Open(fullname.GetFullPath()) )
759 return;
760
761 wxMimeTypeCommands * entry = new wxMimeTypeCommands;
762 wxArrayString sExts;
763 wxString mimetype, mime_desc, strIcon;
764
765 int nIndex = file.pIndexOf( wxT("MimeType=") );
766 if (nIndex == wxNOT_FOUND)
767 {
768 // construct mimetype from the directory name and the basename of the
769 // file (it always has .kdelnk extension)
770 mimetype << subdir << wxT('/') << filename.BeforeLast( wxT('.') );
771 }
772 else
773 mimetype = file.GetCmd(nIndex);
774
775 // first find the description string: it is the value in either "Comment="
776 // line or "Comment[<locale_name>]=" one
777 nIndex = wxNOT_FOUND;
778
779 wxString comment;
780
781 #if wxUSE_INTL
782 wxLocale *locale = wxGetLocale();
783 if ( locale )
784 {
785 // try "Comment[locale name]" first
786 comment << wxT("Comment[") + locale->GetName() + wxT("]=");
787 nIndex = file.pIndexOf(comment);
788 }
789 #endif
790
791 if ( nIndex == wxNOT_FOUND )
792 {
793 comment = wxT("Comment=");
794 nIndex = file.pIndexOf(comment);
795 }
796
797 if ( nIndex != wxNOT_FOUND )
798 mime_desc = file.GetCmd(nIndex);
799 //else: no description
800
801 // next find the extensions
802 wxString mime_extension;
803
804 nIndex = file.pIndexOf(wxT("Patterns="));
805 if ( nIndex != wxNOT_FOUND )
806 {
807 wxString exts = file.GetCmd(nIndex);
808
809 wxStringTokenizer tokenizer(exts, wxT(";"));
810 while ( tokenizer.HasMoreTokens() )
811 {
812 wxString e = tokenizer.GetNextToken();
813
814 // don't support too difficult patterns
815 if ( e.Left(2) != wxT("*.") )
816 continue;
817
818 if ( !mime_extension.empty() )
819 {
820 // separate from the previous ext
821 mime_extension << wxT(' ');
822 }
823
824 mime_extension << e.Mid(2);
825 }
826 }
827
828 sExts.Add(mime_extension);
829
830 // ok, now we can take care of icon:
831
832 nIndex = file.pIndexOf(wxT("Icon="));
833 if ( nIndex != wxNOT_FOUND )
834 {
835 strIcon = file.GetCmd(nIndex);
836
837 wxLogTrace(TRACE_MIME, wxT(" icon %s"), strIcon.c_str());
838
839 // it could be the real path, but more often a short name
840 if (!wxFileExists(strIcon))
841 {
842 // icon is just the short name
843 if ( !strIcon.empty() )
844 {
845 // we must check if the file exists because it may be stored
846 // in many locations, at least ~/.kde and $KDEDIR
847 size_t nDir, nDirs = icondirs.GetCount();
848 for ( nDir = 0; nDir < nDirs; nDir++ )
849 {
850 wxFileName fnameIcon( strIcon );
851 wxFileName fname( icondirs[nDir], fnameIcon.GetName() );
852 fname.SetExt( wxT("png") );
853 if (fname.FileExists())
854 {
855 strIcon = fname.GetFullPath();
856 wxLogTrace(TRACE_MIME, wxT(" iconfile %s"), strIcon.c_str());
857 break;
858 }
859 }
860 }
861 }
862 }
863
864 // now look for lines which know about the application
865 // exec= or DefaultApp=
866
867 nIndex = file.pIndexOf(wxT("DefaultApp"));
868
869 if ( nIndex == wxNOT_FOUND )
870 {
871 // no entry try exec
872 nIndex = file.pIndexOf(wxT("Exec"));
873 }
874
875 if ( nIndex != wxNOT_FOUND )
876 {
877 // we expect %f; others including %F and %U and %u are possible
878 wxString sTmp = file.GetCmd(nIndex);
879 if (0 == sTmp.Replace( wxT("%f"), wxT("%s") ))
880 sTmp += wxT(" %s");
881 entry->AddOrReplaceVerb(wxString(wxT("open")), sTmp );
882 }
883
884 AddToMimeData(mimetype, strIcon, entry, sExts, mime_desc);
885 }
886
887 void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase,
888 const wxString& subdir,
889 const wxArrayString& icondirs)
890 {
891 wxFileName dirname(dirbase, wxEmptyString);
892 dirname.AppendDir(subdir);
893
894 // Don't complain if we don't have permissions to read - it confuses users
895 wxLogNull logNull;
896
897 wxDir dir(dirname.GetPath());
898 if(! dir.IsOpened())
899 return;
900
901 wxLogTrace(TRACE_MIME, wxT("--- Loading from KDE directory %s ---"),
902 dirname.GetPath().c_str());
903
904 wxString filename;
905 bool cont = dir.GetFirst(&filename, wxT("*.kdelnk"), wxDIR_FILES);
906 while(cont) {
907 LoadKDELinksForMimeSubtype(dirname.GetPath(), subdir,
908 filename, icondirs);
909 cont = dir.GetNext(&filename);
910 }
911
912 // new standard for Gnome and KDE
913 cont = dir.GetFirst(&filename, wxT("*.desktop"), wxDIR_FILES);
914 while(cont) {
915 LoadKDELinksForMimeSubtype(dirname.GetPath(), subdir,
916 filename, icondirs);
917 cont = dir.GetNext(&filename);
918 }
919 }
920
921 void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString& dirname,
922 const wxArrayString& icondirs)
923 {
924 // Don't complain if we don't have permissions to read - it confuses users
925 wxLogNull logNull;
926
927 if(! wxDir::Exists(dirname))
928 return;
929
930 wxDir dir(dirname);
931 if ( !dir.IsOpened() )
932 return;
933
934 wxString subdir;
935 bool cont = dir.GetFirst(&subdir, wxEmptyString, wxDIR_DIRS);
936 while ( cont )
937 {
938 LoadKDELinksForMimeType(dirname, subdir, icondirs);
939
940 cont = dir.GetNext(&subdir);
941 }
942 }
943
944 // Read a KDE .desktop file of type 'Application'
945 void wxMimeTypesManagerImpl::LoadKDEApp(const wxString& filename)
946 {
947 wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"), filename.c_str());
948
949 wxMimeTextFile file;
950 if ( !file.Open(filename) )
951 return;
952
953 // Here, only type 'Application' should be considered.
954 int nIndex = file.pIndexOf( wxT("Type=") );
955 if (nIndex != wxNOT_FOUND &&
956 file.GetCmd(nIndex).Lower() != wxT("application"))
957 return;
958
959 // The hidden entry specifies a file to be ignored.
960 nIndex = file.pIndexOf( wxT("Hidden=") );
961 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex).Lower() == wxT("true"))
962 return;
963
964 // Semicolon separated list of mime types handled by the application.
965 nIndex = file.pIndexOf( wxT("MimeType=") );
966 if (nIndex == wxNOT_FOUND)
967 return;
968 wxString mimetypes = file.GetCmd (nIndex);
969
970 // Name of the application
971 wxString nameapp;
972 nIndex = wxNOT_FOUND;
973 #if wxUSE_INTL // try "Name[locale name]" first
974 wxLocale *locale = wxGetLocale();
975 if ( locale )
976 nIndex = file.pIndexOf(_T("Name[")+locale->GetName()+_T("]="));
977 #endif // wxUSE_INTL
978 if(nIndex == wxNOT_FOUND)
979 nIndex = file.pIndexOf( wxT("Name=") );
980 if(nIndex != wxNOT_FOUND)
981 nameapp = file.GetCmd(nIndex);
982
983 // Icon of the application.
984 wxString nameicon, namemini;
985 nIndex = wxNOT_FOUND;
986 #if wxUSE_INTL // try "Icon[locale name]" first
987 if ( locale )
988 nIndex = file.pIndexOf(_T("Icon[")+locale->GetName()+_T("]="));
989 #endif // wxUSE_INTL
990 if(nIndex == wxNOT_FOUND)
991 nIndex = file.pIndexOf( wxT("Icon=") );
992 if(nIndex != wxNOT_FOUND) {
993 nameicon = wxString(wxT("--icon ")) + file.GetCmd(nIndex);
994 namemini = wxString(wxT("--miniicon ")) + file.GetCmd(nIndex);
995 }
996
997 // Replace some of the field code in the 'Exec' entry.
998 // TODO: deal with %d, %D, %n, %N, %k and %v (but last one is deprecated)
999 nIndex = file.pIndexOf( wxT("Exec=") );
1000 if (nIndex == wxNOT_FOUND)
1001 return;
1002 wxString sCmd = file.GetCmd(nIndex);
1003 // we expect %f; others including %F and %U and %u are possible
1004 sCmd.Replace(wxT("%F"), wxT("%f"));
1005 sCmd.Replace(wxT("%U"), wxT("%f"));
1006 sCmd.Replace(wxT("%u"), wxT("%f"));
1007 if (0 == sCmd.Replace ( wxT("%f"), wxT("%s") ))
1008 sCmd = sCmd + wxT(" %s");
1009 sCmd.Replace(wxT("%c"), nameapp);
1010 sCmd.Replace(wxT("%i"), nameicon);
1011 sCmd.Replace(wxT("%m"), namemini);
1012
1013 wxStringTokenizer tokenizer(mimetypes, _T(";"));
1014 while(tokenizer.HasMoreTokens()) {
1015 wxString mimetype = tokenizer.GetNextToken().Lower();
1016 nIndex = m_aTypes.Index(mimetype);
1017 if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
1018 wxMimeTypeCommands* entry = m_aEntries[nIndex];
1019 entry->AddOrReplaceVerb(wxT("open"), sCmd);
1020 }
1021 }
1022 }
1023
1024 void wxMimeTypesManagerImpl::LoadKDEAppsFilesFromDir(const wxString& dirname)
1025 {
1026 // Don't complain if we don't have permissions to read - it confuses users
1027 wxLogNull logNull;
1028
1029 if(! wxDir::Exists(dirname))
1030 return;
1031 wxDir dir(dirname);
1032 if ( !dir.IsOpened() )
1033 return;
1034
1035 wxString filename;
1036 // Look into .desktop files
1037 bool cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES);
1038 while(cont) {
1039 wxFileName p(dirname, filename);
1040 LoadKDEApp( p.GetFullPath() );
1041 cont = dir.GetNext(&filename);
1042 }
1043 // Look recursively into subdirs
1044 cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
1045 while(cont) {
1046 wxFileName p(dirname, wxEmptyString);
1047 p.AppendDir(filename);
1048 LoadKDEAppsFilesFromDir( p.GetPath() );
1049 cont = dir.GetNext(&filename);
1050 }
1051 }
1052
1053 // Return base KDE directories.
1054 // 1) Environment variable $KDEHOME, or "~/.kde" if not set.
1055 // 2) List of directories in colon separated environment variable $KDEDIRS.
1056 // 3) Environment variable $KDEDIR in case $KDEDIRS is not set.
1057 // Notice at least the local kde directory is added to the list. If it is the
1058 // only one, use later the application 'kde-config' to get additional paths.
1059 static void GetKDEBaseDirs(wxArrayString& basedirs)
1060 {
1061 wxString env = wxGetenv( wxT("KDEHOME") );
1062 if(env.IsEmpty())
1063 env = wxGetHomeDir() + wxT("/.kde");
1064 basedirs.Add(env);
1065
1066 env = wxGetenv( wxT("KDEDIRS") );
1067 if(env.IsEmpty()) {
1068 env = wxGetenv( wxT("KDEDIR") );
1069 if(! env.IsEmpty())
1070 basedirs.Add(env);
1071 } else {
1072 wxStringTokenizer tokenizer(env, wxT(":"));
1073 while(tokenizer.HasMoreTokens())
1074 basedirs.Add( tokenizer.GetNextToken() );
1075 }
1076 }
1077
1078 static wxString ReadPathFromKDEConfig(const wxString& request)
1079 {
1080 wxString str;
1081 wxArrayString output;
1082 if(wxExecute(wxT("kde-config --path ")+request, output) == 0 &&
1083 output.GetCount() > 0)
1084 str = output.Item(0);
1085 return str;
1086 }
1087
1088 // Try to find the "Theme" entry in the configuration file, provided it exists.
1089 static wxString GetKDEThemeInFile(const wxFileName& filename)
1090 {
1091 wxString theme;
1092 wxMimeTextFile config;
1093 if ( filename.FileExists() && config.Open(filename.GetFullPath()) )
1094 {
1095 size_t cnt = config.GetLineCount();
1096 for ( size_t i = 0; i < cnt; i++ )
1097 {
1098 if ( config[i].StartsWith(wxT("Theme="), &theme) )
1099 break;
1100 }
1101 }
1102
1103 return theme;
1104 }
1105
1106 // Try to find a file "kdeglobals" in one of the directories and read the
1107 // "Theme" entry there.
1108 static wxString GetKDETheme(const wxArrayString& basedirs)
1109 {
1110 wxString theme;
1111 for(size_t i = 0; i < basedirs.GetCount(); i++) {
1112 wxFileName filename(basedirs.Item(i), wxEmptyString);
1113 filename.AppendDir( wxT("share") );
1114 filename.AppendDir( wxT("config") );
1115 filename.SetName( wxT("kdeglobals") );
1116 theme = GetKDEThemeInFile(filename);
1117 if(! theme.IsEmpty())
1118 return theme;
1119 }
1120 // If $KDEDIRS and $KDEDIR were set, we try nothing more. Otherwise, we
1121 // try to get the configuration file with 'kde-config'.
1122 if(basedirs.GetCount() > 1)
1123 return theme;
1124 wxString paths = ReadPathFromKDEConfig(wxT("config"));
1125 if(! paths.IsEmpty()) {
1126 wxStringTokenizer tokenizer(paths, wxT(":"));
1127 while( tokenizer.HasMoreTokens() ) {
1128 wxFileName filename(tokenizer.GetNextToken(), wxT("kdeglobals"));
1129 theme = GetKDEThemeInFile(filename);
1130 if(! theme.IsEmpty())
1131 return theme;
1132 }
1133 }
1134 return theme;
1135 }
1136
1137 // Get list of directories of icons.
1138 static void GetKDEIconDirs(const wxArrayString& basedirs,
1139 wxArrayString& icondirs)
1140 {
1141 wxString theme = GetKDETheme(basedirs);
1142 if(theme.IsEmpty())
1143 theme = wxT("default.kde");
1144
1145 for(size_t i = 0; i < basedirs.GetCount(); i++) {
1146 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1147 dirname.AppendDir( wxT("share") );
1148 dirname.AppendDir( wxT("icons") );
1149 dirname.AppendDir(theme);
1150 dirname.AppendDir( wxT("32x32") );
1151 dirname.AppendDir( wxT("mimetypes") );
1152 if( wxDir::Exists( dirname.GetPath() ) )
1153 icondirs.Add( dirname.GetPath() );
1154 }
1155
1156 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1157 if(basedirs.GetCount() > 1)
1158 return;
1159 wxString paths = ReadPathFromKDEConfig(wxT("icon"));
1160 if(! paths.IsEmpty()) {
1161 wxStringTokenizer tokenizer(paths, wxT(":"));
1162 while( tokenizer.HasMoreTokens() ) {
1163 wxFileName dirname(tokenizer.GetNextToken(), wxEmptyString);
1164 dirname.AppendDir(theme);
1165 dirname.AppendDir( wxT("32x32") );
1166 dirname.AppendDir( wxT("mimetypes") );
1167 if(icondirs.Index(dirname.GetPath()) == wxNOT_FOUND &&
1168 wxDir::Exists( dirname.GetPath() ) )
1169 icondirs.Add( dirname.GetPath() );
1170 }
1171 }
1172 }
1173
1174 // Get list of directories of mime types.
1175 static void GetKDEMimeDirs(const wxArrayString& basedirs,
1176 wxArrayString& mimedirs)
1177 {
1178 for(size_t i = 0; i < basedirs.GetCount(); i++) {
1179 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1180 dirname.AppendDir( wxT("share") );
1181 dirname.AppendDir( wxT("mimelnk") );
1182 if( wxDir::Exists( dirname.GetPath() ) )
1183 mimedirs.Add( dirname.GetPath() );
1184 }
1185
1186 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1187 if(basedirs.GetCount() > 1)
1188 return;
1189 wxString paths = ReadPathFromKDEConfig(wxT("mime"));
1190 if(! paths.IsEmpty()) {
1191 wxStringTokenizer tokenizer(paths, wxT(":"));
1192 while( tokenizer.HasMoreTokens() ) {
1193 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1194 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1195 if(mimedirs.Index(dirname) == wxNOT_FOUND &&
1196 wxDir::Exists(dirname) )
1197 mimedirs.Add(dirname);
1198 }
1199 }
1200 }
1201
1202 // Get list of directories of application desktop files.
1203 static void GetKDEAppsDirs(const wxArrayString& basedirs,
1204 wxArrayString& appsdirs)
1205 {
1206 for(size_t i = 0; i < basedirs.GetCount(); i++) {
1207 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1208 dirname.AppendDir( wxT("share") );
1209 dirname.AppendDir( wxT("applnk") );
1210 if( wxDir::Exists( dirname.GetPath() ) )
1211 appsdirs.Add( dirname.GetPath() );
1212 }
1213
1214 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1215 if(basedirs.GetCount() > 1)
1216 return;
1217 wxString paths = ReadPathFromKDEConfig(wxT("apps"));
1218 if(! paths.IsEmpty()) {
1219 wxStringTokenizer tokenizer(paths, wxT(":"));
1220 while( tokenizer.HasMoreTokens() ) {
1221 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1222 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1223 if(appsdirs.Index(dirname) == wxNOT_FOUND &&
1224 wxDir::Exists(dirname) )
1225 appsdirs.Add(dirname);
1226 }
1227 }
1228 paths = ReadPathFromKDEConfig(wxT("xdgdata-apps"));
1229 if(! paths.IsEmpty()) {
1230 wxStringTokenizer tokenizer(paths, wxT(":"));
1231 while( tokenizer.HasMoreTokens() ) {
1232 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1233 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1234 if(appsdirs.Index(dirname) == wxNOT_FOUND &&
1235 wxDir::Exists(dirname) )
1236 appsdirs.Add(dirname);
1237 }
1238 }
1239 }
1240
1241 // Fill database with all mime types.
1242 void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir)
1243 {
1244 wxArrayString basedirs;
1245 GetKDEBaseDirs(basedirs);
1246
1247 wxArrayString icondirs;
1248 GetKDEIconDirs(basedirs, icondirs);
1249 wxArrayString mimedirs;
1250 GetKDEMimeDirs(basedirs, mimedirs);
1251 wxArrayString appsdirs;
1252 GetKDEAppsDirs(basedirs, appsdirs);
1253
1254 if(! sExtraDir.IsEmpty()) {
1255 icondirs.Add(sExtraDir + wxT("/icons"));
1256 mimedirs.Add(sExtraDir + wxT("/mimelnk"));
1257 appsdirs.Add(sExtraDir + wxT("/applnk"));
1258 }
1259
1260 // Load mime types
1261 size_t nDirs = mimedirs.GetCount(), nDir;
1262 for(nDir = 0; nDir < nDirs; nDir++)
1263 LoadKDELinkFilesFromDir(mimedirs[nDir], icondirs);
1264
1265 // Load application files and associate them to corresponding mime types.
1266 nDirs = appsdirs.GetCount();
1267 for(nDir = 0; nDir < nDirs; nDir++)
1268 LoadKDEAppsFilesFromDir(appsdirs[nDir]);
1269 }
1270
1271 // ----------------------------------------------------------------------------
1272 // wxFileTypeImpl (Unix)
1273 // ----------------------------------------------------------------------------
1274
1275 wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const
1276 {
1277 wxString sTmp;
1278 size_t i = 0;
1279 while ( (i < m_index.GetCount() ) && sTmp.empty() )
1280 {
1281 sTmp = m_manager->GetCommand( verb, m_index[i] );
1282 i++;
1283 }
1284
1285 return wxFileType::ExpandCommand(sTmp, params);
1286 }
1287
1288 bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
1289 {
1290 wxString sTmp;
1291 size_t i = 0;
1292 while ( (i < m_index.GetCount() ) && sTmp.empty() )
1293 {
1294 sTmp = m_manager->m_aIcons[m_index[i]];
1295 i++;
1296 }
1297
1298 if ( sTmp.empty() )
1299 return false;
1300
1301 if ( iconLoc )
1302 {
1303 iconLoc->SetFileName(sTmp);
1304 }
1305
1306 return true;
1307 }
1308
1309 bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
1310 {
1311 mimeTypes.Clear();
1312 size_t nCount = m_index.GetCount();
1313 for (size_t i = 0; i < nCount; i++)
1314 mimeTypes.Add(m_manager->m_aTypes[m_index[i]]);
1315
1316 return true;
1317 }
1318
1319 size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
1320 wxArrayString *commands,
1321 const wxFileType::MessageParameters& params) const
1322 {
1323 wxString vrb, cmd, sTmp;
1324 size_t count = 0;
1325 wxMimeTypeCommands * sPairs;
1326
1327 // verbs and commands have been cleared already in mimecmn.cpp...
1328 // if we find no entries in the exact match, try the inexact match
1329 for (size_t n = 0; ((count == 0) && (n < m_index.GetCount())); n++)
1330 {
1331 // list of verb = command pairs for this mimetype
1332 sPairs = m_manager->m_aEntries [m_index[n]];
1333 size_t i;
1334 for ( i = 0; i < sPairs->GetCount(); i++ )
1335 {
1336 vrb = sPairs->GetVerb(i);
1337 // some gnome entries have "." inside
1338 vrb = vrb.AfterLast(wxT('.'));
1339 cmd = sPairs->GetCmd(i);
1340 if (! cmd.empty() )
1341 {
1342 cmd = wxFileType::ExpandCommand(cmd, params);
1343 count++;
1344 if ( vrb.IsSameAs(wxT("open")))
1345 {
1346 if ( verbs )
1347 verbs->Insert(vrb, 0u);
1348 if ( commands )
1349 commands ->Insert(cmd, 0u);
1350 }
1351 else
1352 {
1353 if ( verbs )
1354 verbs->Add(vrb);
1355 if ( commands )
1356 commands->Add(cmd);
1357 }
1358 }
1359 }
1360 }
1361
1362 return count;
1363 }
1364
1365 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
1366 {
1367 const wxString strExtensions = m_manager->GetExtension(m_index[0]);
1368 extensions.Empty();
1369
1370 // one extension in the space or comma-delimited list
1371 wxString strExt;
1372 wxString::const_iterator end = strExtensions.end();
1373 for ( wxString::const_iterator p = strExtensions.begin(); /* nothing */; ++p )
1374 {
1375 if ( p == end || *p == wxT(' ') || *p == wxT(',') )
1376 {
1377 if ( !strExt.empty() )
1378 {
1379 extensions.Add(strExt);
1380 strExt.Empty();
1381 }
1382 //else: repeated spaces
1383 // (shouldn't happen, but it's not that important if it does happen)
1384
1385 if ( p == end )
1386 break;
1387 }
1388 else if ( *p == wxT('.') )
1389 {
1390 // remove the dot from extension (but only if it's the first char)
1391 if ( !strExt.empty() )
1392 {
1393 strExt += wxT('.');
1394 }
1395 //else: no, don't append it
1396 }
1397 else
1398 {
1399 strExt += *p;
1400 }
1401 }
1402
1403 return true;
1404 }
1405
1406 // set an arbitrary command:
1407 // could adjust the code to ask confirmation if it already exists and
1408 // overwriteprompt is true, but this is currently ignored as *Associate* has
1409 // no overwrite prompt
1410 bool
1411 wxFileTypeImpl::SetCommand(const wxString& cmd,
1412 const wxString& verb,
1413 bool WXUNUSED(overwriteprompt))
1414 {
1415 wxArrayString strExtensions;
1416 wxString strDesc, strIcon;
1417
1418 wxArrayString strTypes;
1419 GetMimeTypes(strTypes);
1420 if ( strTypes.IsEmpty() )
1421 return false;
1422
1423 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
1424 entry->Add(verb + wxT("=") + cmd + wxT(" %s "));
1425
1426 bool ok = false;
1427 size_t nCount = strTypes.GetCount();
1428 for ( size_t i = 0; i < nCount; i++ )
1429 {
1430 if ( m_manager->DoAssociation
1431 (
1432 strTypes[i],
1433 strIcon,
1434 entry,
1435 strExtensions,
1436 strDesc
1437 ) )
1438 {
1439 // DoAssociation() took ownership of entry, don't delete it below
1440 ok = true;
1441 }
1442 }
1443
1444 if ( !ok )
1445 delete entry;
1446
1447 return ok;
1448 }
1449
1450 // ignore index on the grounds that we only have one icon in a Unix file
1451 bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int WXUNUSED(index))
1452 {
1453 if (strIcon.empty())
1454 return false;
1455
1456 wxArrayString strExtensions;
1457 wxString strDesc;
1458
1459 wxArrayString strTypes;
1460 GetMimeTypes(strTypes);
1461 if ( strTypes.IsEmpty() )
1462 return false;
1463
1464 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
1465 bool ok = false;
1466 size_t nCount = strTypes.GetCount();
1467 for ( size_t i = 0; i < nCount; i++ )
1468 {
1469 if ( m_manager->DoAssociation
1470 (
1471 strTypes[i],
1472 strIcon,
1473 entry,
1474 strExtensions,
1475 strDesc
1476 ) )
1477 {
1478 // we don't need to free entry now, DoAssociation() took ownership
1479 // of it
1480 ok = true;
1481 }
1482 }
1483
1484 if ( !ok )
1485 delete entry;
1486
1487 return ok;
1488 }
1489
1490 // ----------------------------------------------------------------------------
1491 // wxMimeTypesManagerImpl (Unix)
1492 // ----------------------------------------------------------------------------
1493
1494 wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1495 {
1496 m_initialized = false;
1497 m_mailcapStylesInited = 0;
1498 }
1499
1500 void wxMimeTypesManagerImpl::InitIfNeeded()
1501 {
1502 if ( !m_initialized )
1503 {
1504 // set the flag first to prevent recursion
1505 m_initialized = true;
1506
1507 wxString wm = wxTheApp->GetTraits()->GetDesktopEnvironment();
1508
1509 if (wm == wxT("KDE"))
1510 Initialize( wxMAILCAP_KDE );
1511 else if (wm == wxT("GNOME"))
1512 Initialize( wxMAILCAP_GNOME );
1513 else
1514 Initialize();
1515 }
1516 }
1517
1518 // read system and user mailcaps and other files
1519 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
1520 const wxString& sExtraDir)
1521 {
1522 // read mimecap amd mime.types
1523 if ( (mailcapStyles & wxMAILCAP_NETSCAPE) ||
1524 (mailcapStyles & wxMAILCAP_STANDARD) )
1525 GetMimeInfo(sExtraDir);
1526
1527 // read GNOME tables
1528 if (mailcapStyles & wxMAILCAP_GNOME)
1529 GetGnomeMimeInfo(sExtraDir);
1530
1531 // read KDE tables which are never installed on OpenVMS
1532 #ifndef __VMS
1533 if (mailcapStyles & wxMAILCAP_KDE)
1534 GetKDEMimeInfo(sExtraDir);
1535 #endif
1536
1537 // Load desktop files for Gnome, and then override them with the Gnome defaults.
1538 // We will override them one desktop file at a time, rather
1539 // than one mime type at a time, but it should be a reasonable
1540 // heuristic.
1541 if (mailcapStyles & wxMAILCAP_GNOME)
1542 {
1543 wxString xdgDataHome = wxGetenv(wxT("XDG_DATA_HOME"));
1544 if ( xdgDataHome.empty() )
1545 xdgDataHome = wxGetHomeDir() + wxT("/.local/share");
1546 wxString xdgDataDirs = wxGetenv(wxT("XDG_DATA_DIRS"));
1547 if ( xdgDataDirs.empty() )
1548 xdgDataDirs = wxT("/usr/local/share:/usr/share:/usr/share/gnome");
1549 wxArrayString dirs;
1550
1551 wxStringTokenizer tokenizer(xdgDataDirs, wxT(":"));
1552 while ( tokenizer.HasMoreTokens() )
1553 {
1554 wxString p = tokenizer.GetNextToken();
1555 dirs.Add(p);
1556 }
1557 dirs.insert(dirs.begin(), xdgDataHome);
1558
1559 wxString defaultsList;
1560 size_t i;
1561 for (i = 0; i < dirs.GetCount(); i++)
1562 {
1563 wxString f(dirs[i] + wxT("/applications/defaults.list"));
1564 if (wxFileExists(f))
1565 {
1566 defaultsList = f;
1567 break;
1568 }
1569 }
1570
1571 // Load application files and associate them to corresponding mime types.
1572 size_t nDirs = dirs.GetCount();
1573 for (size_t nDir = 0; nDir < nDirs; nDir++)
1574 {
1575 wxString dirStr(dirs[nDir] + wxT("/applications"));
1576 LoadKDEAppsFilesFromDir(dirStr);
1577 }
1578
1579 if (!defaultsList.IsEmpty())
1580 {
1581 wxArrayString deskTopFilesSeen;
1582
1583 wxMimeTextFile textfile(defaultsList);
1584 if ( textfile.Open() )
1585 {
1586 int nIndex = textfile.pIndexOf( wxT("[Default Applications]") );
1587 if (nIndex != wxNOT_FOUND)
1588 {
1589 for (i = nIndex+1; i < textfile.GetLineCount(); i++)
1590 {
1591 if (textfile[i].Find(wxT("=")) != wxNOT_FOUND)
1592 {
1593 wxString mimeType = textfile.GetVerb(i);
1594 wxString desktopFile = textfile.GetCmd(i);
1595
1596 if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND)
1597 {
1598 deskTopFilesSeen.Add(desktopFile);
1599 size_t j;
1600 for (j = 0; j < dirs.GetCount(); j++)
1601 {
1602 wxString desktopPath(dirs[j] + wxT("/applications/") + desktopFile);
1603 if (wxFileExists(desktopPath))
1604 {
1605 LoadKDEApp(desktopPath);
1606 }
1607 }
1608 }
1609 }
1610 }
1611 }
1612 }
1613 }
1614 }
1615
1616 m_mailcapStylesInited |= mailcapStyles;
1617 }
1618
1619 // clear data so you can read another group of WM files
1620 void wxMimeTypesManagerImpl::ClearData()
1621 {
1622 m_aTypes.Clear();
1623 m_aIcons.Clear();
1624 m_aExtensions.Clear();
1625 m_aDescriptions.Clear();
1626
1627 WX_CLEAR_ARRAY(m_aEntries);
1628 m_aEntries.Empty();
1629
1630 m_mailcapStylesInited = 0;
1631 }
1632
1633 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
1634 {
1635 ClearData();
1636 }
1637
1638 void wxMimeTypesManagerImpl::GetMimeInfo(const wxString& sExtraDir)
1639 {
1640 // read this for netscape or Metamail formats
1641
1642 // directories where we look for mailcap and mime.types by default
1643 // used by netscape and pine and other mailers, using 2 different formats!
1644
1645 // (taken from metamail(1) sources)
1646 //
1647 // although RFC 1524 specifies the search path of
1648 // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more
1649 // places - OTOH, the RFC also says that this path can be changed with
1650 // MAILCAPS environment variable (containing the colon separated full
1651 // filenames to try) which is not done yet (TODO?)
1652
1653 wxString strHome = wxGetenv(wxT("HOME"));
1654
1655 wxArrayString dirs;
1656 dirs.Add( strHome + wxT("/.") );
1657 dirs.Add( wxT("/etc/") );
1658 dirs.Add( wxT("/usr/etc/") );
1659 dirs.Add( wxT("/usr/local/etc/") );
1660 dirs.Add( wxT("/etc/mail/") );
1661 dirs.Add( wxT("/usr/public/lib/") );
1662 if (!sExtraDir.empty())
1663 dirs.Add( sExtraDir + wxT("/") );
1664
1665 wxString file;
1666 size_t nDirs = dirs.GetCount();
1667 for ( size_t nDir = 0; nDir < nDirs; nDir++ )
1668 {
1669 file = dirs[nDir];
1670 file += wxT("mailcap");
1671 if ( wxFile::Exists(file) )
1672 {
1673 ReadMailcap(file);
1674 }
1675
1676 file = dirs[nDir];
1677 file += wxT("mime.types");
1678 if ( wxFile::Exists(file) )
1679 ReadMimeTypes(file);
1680 }
1681 }
1682
1683 bool wxMimeTypesManagerImpl::WriteToMimeTypes(int index, bool delete_index)
1684 {
1685 // check we have the right manager
1686 if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD) )
1687 return false;
1688
1689 bool bTemp;
1690 wxString strHome = wxGetenv(wxT("HOME"));
1691
1692 // and now the users mailcap
1693 wxString strUserMailcap = strHome + wxT("/.mime.types");
1694
1695 wxMimeTextFile file;
1696 if ( wxFile::Exists(strUserMailcap) )
1697 {
1698 bTemp = file.Open(strUserMailcap);
1699 }
1700 else
1701 {
1702 if (delete_index)
1703 return false;
1704
1705 bTemp = file.Create(strUserMailcap);
1706 }
1707
1708 if (bTemp)
1709 {
1710 int nIndex;
1711 // test for netscape's header and return false if its found
1712 nIndex = file.pIndexOf(wxT("#--Netscape"));
1713 if (nIndex != wxNOT_FOUND)
1714 {
1715 wxFAIL_MSG(wxT("Error in .mime.types\nTrying to mix Netscape and Metamail formats\nFile not modified"));
1716 return false;
1717 }
1718
1719 // write it in alternative format
1720 // get rid of unwanted entries
1721 wxString strType = m_aTypes[index];
1722 nIndex = file.pIndexOf(strType);
1723
1724 // get rid of all the unwanted entries...
1725 if (nIndex != wxNOT_FOUND)
1726 file.CommentLine(nIndex);
1727
1728 if (!delete_index)
1729 {
1730 // add the new entries in
1731 wxString sTmp = strType.Append( wxT(' '), 40 - strType.Len() );
1732 sTmp += m_aExtensions[index];
1733 file.AddLine(sTmp);
1734 }
1735
1736 bTemp = file.Write();
1737 file.Close();
1738 }
1739
1740 return bTemp;
1741 }
1742
1743 bool wxMimeTypesManagerImpl::WriteToNSMimeTypes(int index, bool delete_index)
1744 {
1745 //check we have the right managers
1746 if (! ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) )
1747 return false;
1748
1749 bool bTemp;
1750 wxString strHome = wxGetenv(wxT("HOME"));
1751
1752 // and now the users mailcap
1753 wxString strUserMailcap = strHome + wxT("/.mime.types");
1754
1755 wxMimeTextFile file;
1756 if ( wxFile::Exists(strUserMailcap) )
1757 {
1758 bTemp = file.Open(strUserMailcap);
1759 }
1760 else
1761 {
1762 if (delete_index)
1763 return false;
1764
1765 bTemp = file.Create(strUserMailcap);
1766 }
1767
1768 if (bTemp)
1769 {
1770 // write it in the format that Netscape uses
1771 int nIndex;
1772 // test for netscape's header and insert if required...
1773 // this is a comment so use true
1774 nIndex = file.pIndexOf(wxT("#--Netscape"), true);
1775 if (nIndex == wxNOT_FOUND)
1776 {
1777 // either empty file or metamail format
1778 // at present we can't cope with mixed formats, so exit to preseve
1779 // metamail entreies
1780 if (file.GetLineCount() > 0)
1781 {
1782 wxFAIL_MSG(wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap"));
1783 return false;
1784 }
1785
1786 file.InsertLine(wxT( "#--Netscape Communications Corporation MIME Information" ), 0);
1787 nIndex = 0;
1788 }
1789
1790 wxString strType = wxT("type=") + m_aTypes[index];
1791 nIndex = file.pIndexOf(strType);
1792
1793 // get rid of all the unwanted entries...
1794 if (nIndex != wxNOT_FOUND)
1795 {
1796 wxString sOld = file[nIndex];
1797 while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
1798 {
1799 file.CommentLine(nIndex);
1800 sOld = file[nIndex];
1801
1802 wxLogTrace(TRACE_MIME, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex, sOld.c_str());
1803
1804 nIndex++;
1805 }
1806
1807 if (nIndex < (int) file.GetLineCount())
1808 file.CommentLine(nIndex);
1809 }
1810 else
1811 nIndex = (int) file.GetLineCount();
1812
1813 wxString sTmp = strType + wxT(" \\");
1814 if (!delete_index)
1815 file.InsertLine(sTmp, nIndex);
1816
1817 if ( ! m_aDescriptions.Item(index).empty() )
1818 {
1819 sTmp = wxT("desc=\"") + m_aDescriptions[index]+ wxT("\" \\"); //.trim ??
1820 if (!delete_index)
1821 {
1822 nIndex++;
1823 file.InsertLine(sTmp, nIndex);
1824 }
1825 }
1826
1827 wxString sExts = m_aExtensions.Item(index);
1828 sTmp = wxT("exts=\"") + sExts.Trim(false).Trim() + wxT("\"");
1829 if (!delete_index)
1830 {
1831 nIndex++;
1832 file.InsertLine(sTmp, nIndex);
1833 }
1834
1835 bTemp = file.Write();
1836 file.Close();
1837 }
1838
1839 return bTemp;
1840 }
1841
1842 bool wxMimeTypesManagerImpl::WriteToMailCap(int index, bool delete_index)
1843 {
1844 //check we have the right managers
1845 if ( !( ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) ||
1846 ( m_mailcapStylesInited & wxMAILCAP_STANDARD) ) )
1847 return false;
1848
1849 bool bTemp = false;
1850 wxString strHome = wxGetenv(wxT("HOME"));
1851
1852 // and now the users mailcap
1853 wxString strUserMailcap = strHome + wxT("/.mailcap");
1854
1855 wxMimeTextFile file;
1856 if ( wxFile::Exists(strUserMailcap) )
1857 {
1858 bTemp = file.Open(strUserMailcap);
1859 }
1860 else
1861 {
1862 if (delete_index)
1863 return false;
1864
1865 bTemp = file.Create(strUserMailcap);
1866 }
1867
1868 if (bTemp)
1869 {
1870 // now got a file we can write to ....
1871 wxMimeTypeCommands * entries = m_aEntries[index];
1872 size_t iOpen;
1873 wxString sCmd = entries->GetCommandForVerb(wxT("open"), &iOpen);
1874 wxString sTmp;
1875
1876 sTmp = m_aTypes[index];
1877 wxString sOld;
1878 int nIndex = file.pIndexOf(sTmp);
1879
1880 // get rid of all the unwanted entries...
1881 if (nIndex == wxNOT_FOUND)
1882 {
1883 nIndex = (int) file.GetLineCount();
1884 }
1885 else
1886 {
1887 sOld = file[nIndex];
1888 wxLogTrace(TRACE_MIME, wxT("--- Deleting from mailcap line '%d' ---"), nIndex);
1889
1890 while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
1891 {
1892 file.CommentLine(nIndex);
1893 if (nIndex < (int) file.GetLineCount())
1894 sOld = sOld + file[nIndex];
1895 }
1896
1897 if (nIndex < (int)
1898 file.GetLineCount()) file.CommentLine(nIndex);
1899 }
1900
1901 sTmp += wxT(";") + sCmd; //includes wxT(" %s ");
1902
1903 // write it in the format that Netscape uses (default)
1904 if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) )
1905 {
1906 if (! delete_index)
1907 file.InsertLine(sTmp, nIndex);
1908 nIndex++;
1909 }
1910 else
1911 {
1912 // write extended format
1913
1914 // TODO - FIX this code:
1915 // ii) lost entries
1916 // sOld holds all the entries, but our data store only has some
1917 // eg test= is not stored
1918
1919 // so far we have written the mimetype and command out
1920 wxStringTokenizer sT(sOld, wxT(";\\"));
1921 if (sT.CountTokens() > 2)
1922 {
1923 // first one mimetype; second one command, rest unknown...
1924 wxString s;
1925 s = sT.GetNextToken();
1926 s = sT.GetNextToken();
1927
1928 // first unknown
1929 s = sT.GetNextToken();
1930 while ( ! s.empty() )
1931 {
1932 bool bKnownToken = false;
1933 if (s.Contains(wxT("description=")))
1934 bKnownToken = true;
1935 if (s.Contains(wxT("x11-bitmap=")))
1936 bKnownToken = true;
1937
1938 size_t i;
1939 size_t nCount = entries->GetCount();
1940 for (i=0; i < nCount; i++)
1941 {
1942 if (s.Contains(entries->GetVerb(i)))
1943 bKnownToken = true;
1944 }
1945
1946 if (!bKnownToken)
1947 {
1948 sTmp += wxT("; \\");
1949 file.InsertLine(sTmp, nIndex);
1950 sTmp = s;
1951 }
1952
1953 s = sT.GetNextToken();
1954 }
1955 }
1956
1957 if (! m_aDescriptions[index].empty() )
1958 {
1959 sTmp += wxT("; \\");
1960 file.InsertLine(sTmp, nIndex);
1961 nIndex++;
1962 sTmp = wxT(" description=\"") + m_aDescriptions[index] + wxT("\"");
1963 }
1964
1965 if (! m_aIcons[index].empty() )
1966 {
1967 sTmp += wxT("; \\");
1968 file.InsertLine(sTmp, nIndex);
1969 nIndex++;
1970 sTmp = wxT(" x11-bitmap=\"") + m_aIcons[index] + wxT("\"");
1971 }
1972
1973 if ( entries->GetCount() > 1 )
1974 {
1975 size_t i;
1976 for (i=0; i < entries->GetCount(); i++)
1977 if ( i != iOpen )
1978 {
1979 sTmp += wxT("; \\");
1980 file.InsertLine(sTmp, nIndex);
1981 nIndex++;
1982 sTmp = wxT(" ") + entries->GetVerbCmd(i);
1983 }
1984 }
1985
1986 file.InsertLine(sTmp, nIndex);
1987 nIndex++;
1988 }
1989
1990 bTemp = file.Write();
1991 file.Close();
1992 }
1993
1994 return bTemp;
1995 }
1996
1997 wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
1998 {
1999 InitIfNeeded();
2000
2001 wxString strType = ftInfo.GetMimeType();
2002 wxString strDesc = ftInfo.GetDescription();
2003 wxString strIcon = ftInfo.GetIconFile();
2004
2005 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2006
2007 if ( ! ftInfo.GetOpenCommand().empty())
2008 entry->Add(wxT("open=") + ftInfo.GetOpenCommand() + wxT(" %s "));
2009 if ( ! ftInfo.GetPrintCommand().empty())
2010 entry->Add(wxT("print=") + ftInfo.GetPrintCommand() + wxT(" %s "));
2011
2012 // now find where these extensions are in the data store and remove them
2013 wxArrayString sA_Exts = ftInfo.GetExtensions();
2014 wxString sExt, sExtStore;
2015 size_t i, nIndex;
2016 size_t nExtCount = sA_Exts.GetCount();
2017 for (i=0; i < nExtCount; i++)
2018 {
2019 sExt = sA_Exts.Item(i);
2020
2021 // clean up to just a space before and after
2022 sExt.Trim().Trim(false);
2023 sExt = wxT(' ') + sExt + wxT(' ');
2024 size_t nCount = m_aExtensions.GetCount();
2025 for (nIndex = 0; nIndex < nCount; nIndex++)
2026 {
2027 sExtStore = m_aExtensions.Item(nIndex);
2028 if (sExtStore.Replace(sExt, wxT(" ") ) > 0)
2029 m_aExtensions.Item(nIndex) = sExtStore;
2030 }
2031 }
2032
2033 if ( !DoAssociation(strType, strIcon, entry, sA_Exts, strDesc) )
2034 return NULL;
2035
2036 return GetFileTypeFromMimeType(strType);
2037 }
2038
2039 bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
2040 const wxString& strIcon,
2041 wxMimeTypeCommands *entry,
2042 const wxArrayString& strExtensions,
2043 const wxString& strDesc)
2044 {
2045 int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
2046
2047 if ( nIndex == wxNOT_FOUND )
2048 return false;
2049
2050 return WriteMimeInfo(nIndex, false);
2051 }
2052
2053 bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex, bool delete_mime )
2054 {
2055 bool ok = true;
2056
2057 if ( m_mailcapStylesInited & wxMAILCAP_STANDARD )
2058 {
2059 // write in metamail format;
2060 if (WriteToMimeTypes(nIndex, delete_mime) )
2061 if ( WriteToMailCap(nIndex, delete_mime) )
2062 ok = false;
2063 }
2064
2065 if ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE )
2066 {
2067 // write in netsacpe format;
2068 if (WriteToNSMimeTypes(nIndex, delete_mime) )
2069 if ( WriteToMailCap(nIndex, delete_mime) )
2070 ok = false;
2071 }
2072
2073 // Don't write GNOME files here as this is not
2074 // allowed and simply doesn't work
2075
2076 if (m_mailcapStylesInited & wxMAILCAP_KDE)
2077 {
2078 // write in KDE format;
2079 if (WriteKDEMimeFile(nIndex, delete_mime) )
2080 ok = false;
2081 }
2082
2083 return ok;
2084 }
2085
2086 int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
2087 const wxString& strIcon,
2088 wxMimeTypeCommands *entry,
2089 const wxArrayString& strExtensions,
2090 const wxString& strDesc,
2091 bool replaceExisting)
2092 {
2093 InitIfNeeded();
2094
2095 // ensure mimetype is always lower case
2096 wxString mimeType = strType.Lower();
2097
2098 // is this a known MIME type?
2099 int nIndex = m_aTypes.Index(mimeType);
2100 if ( nIndex == wxNOT_FOUND )
2101 {
2102 // new file type
2103 m_aTypes.Add(mimeType);
2104 m_aIcons.Add(strIcon);
2105 m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
2106
2107 // change nIndex so we can use it below to add the extensions
2108 m_aExtensions.Add(wxEmptyString);
2109 nIndex = m_aExtensions.size() - 1;
2110
2111 m_aDescriptions.Add(strDesc);
2112 }
2113 else // yes, we already have it
2114 {
2115 if ( replaceExisting )
2116 {
2117 // if new description change it
2118 if ( !strDesc.empty())
2119 m_aDescriptions[nIndex] = strDesc;
2120
2121 // if new icon change it
2122 if ( !strIcon.empty())
2123 m_aIcons[nIndex] = strIcon;
2124
2125 if ( entry )
2126 {
2127 delete m_aEntries[nIndex];
2128 m_aEntries[nIndex] = entry;
2129 }
2130 }
2131 else // add data we don't already have ...
2132 {
2133 // if new description add only if none
2134 if ( m_aDescriptions[nIndex].empty() )
2135 m_aDescriptions[nIndex] = strDesc;
2136
2137 // if new icon and no existing icon
2138 if ( m_aIcons[nIndex].empty() )
2139 m_aIcons[nIndex] = strIcon;
2140
2141 // add any new entries...
2142 if ( entry )
2143 {
2144 wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
2145
2146 size_t count = entry->GetCount();
2147 for ( size_t i = 0; i < count; i++ )
2148 {
2149 const wxString& verb = entry->GetVerb(i);
2150 if ( !entryOld->HasVerb(verb) )
2151 {
2152 entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
2153 }
2154 }
2155
2156 // as we don't store it anywhere, it won't be deleted later as
2157 // usual -- do it immediately instead
2158 delete entry;
2159 }
2160 }
2161 }
2162
2163 // always add the extensions to this mimetype
2164 wxString& exts = m_aExtensions[nIndex];
2165
2166 // add all extensions we don't have yet
2167 wxString ext;
2168 size_t count = strExtensions.GetCount();
2169 for ( size_t i = 0; i < count; i++ )
2170 {
2171 ext = strExtensions[i];
2172 ext += wxT(' ');
2173
2174 if ( exts.Find(ext) == wxNOT_FOUND )
2175 {
2176 exts += ext;
2177 }
2178 }
2179
2180 // check data integrity
2181 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
2182 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
2183 m_aTypes.GetCount() == m_aIcons.GetCount() &&
2184 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
2185
2186 return nIndex;
2187 }
2188
2189 wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
2190 {
2191 if (ext.empty() )
2192 return NULL;
2193
2194 InitIfNeeded();
2195
2196 size_t count = m_aExtensions.GetCount();
2197 for ( size_t n = 0; n < count; n++ )
2198 {
2199 wxStringTokenizer tk(m_aExtensions[n], wxT(' '));
2200
2201 while ( tk.HasMoreTokens() )
2202 {
2203 // consider extensions as not being case-sensitive
2204 if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
2205 {
2206 // found
2207 wxFileType *fileType = new wxFileType;
2208 fileType->m_impl->Init(this, n);
2209
2210 return fileType;
2211 }
2212 }
2213 }
2214
2215 return NULL;
2216 }
2217
2218 wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
2219 {
2220 InitIfNeeded();
2221
2222 wxFileType * fileType = NULL;
2223 // mime types are not case-sensitive
2224 wxString mimetype(mimeType);
2225 mimetype.MakeLower();
2226
2227 // first look for an exact match
2228 int index = m_aTypes.Index(mimetype);
2229 if ( index != wxNOT_FOUND )
2230 {
2231 fileType = new wxFileType;
2232 fileType->m_impl->Init(this, index);
2233 }
2234
2235 // then try to find "text/*" as match for "text/plain" (for example)
2236 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
2237 // the whole string - ok.
2238
2239 index = wxNOT_FOUND;
2240 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
2241
2242 size_t nCount = m_aTypes.GetCount();
2243 for ( size_t n = 0; n < nCount; n++ )
2244 {
2245 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
2246 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") )
2247 {
2248 index = n;
2249 break;
2250 }
2251 }
2252
2253 if ( index != wxNOT_FOUND )
2254 {
2255 // don't throw away fileType that was already found
2256 if (!fileType)
2257 fileType = new wxFileType;
2258 fileType->m_impl->Init(this, index);
2259 }
2260
2261 return fileType;
2262 }
2263
2264 wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
2265 {
2266 wxString command, testcmd, sV, sTmp;
2267 sV = verb + wxT("=");
2268
2269 // list of verb = command pairs for this mimetype
2270 wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
2271
2272 size_t i;
2273 size_t nCount = sPairs->GetCount();
2274 for ( i = 0; i < nCount; i++ )
2275 {
2276 sTmp = sPairs->GetVerbCmd (i);
2277 if ( sTmp.Contains(sV) )
2278 command = sTmp.AfterFirst(wxT('='));
2279 }
2280
2281 return command;
2282 }
2283
2284 void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
2285 {
2286 InitIfNeeded();
2287
2288 wxString extensions;
2289 const wxArrayString& exts = filetype.GetExtensions();
2290 size_t nExts = exts.GetCount();
2291 for ( size_t nExt = 0; nExt < nExts; nExt++ )
2292 {
2293 if ( nExt > 0 )
2294 extensions += wxT(' ');
2295
2296 extensions += exts[nExt];
2297 }
2298
2299 AddMimeTypeInfo(filetype.GetMimeType(),
2300 extensions,
2301 filetype.GetDescription());
2302
2303 AddMailcapInfo(filetype.GetMimeType(),
2304 filetype.GetOpenCommand(),
2305 filetype.GetPrintCommand(),
2306 wxT(""),
2307 filetype.GetDescription());
2308 }
2309
2310 void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
2311 const wxString& strExtensions,
2312 const wxString& strDesc)
2313 {
2314 // reading mailcap may find image/* , while
2315 // reading mime.types finds image/gif and no match is made
2316 // this means all the get functions don't work fix this
2317 wxString strIcon;
2318 wxString sTmp = strExtensions;
2319
2320 wxArrayString sExts;
2321 sTmp.Trim().Trim(false);
2322
2323 while (!sTmp.empty())
2324 {
2325 sExts.Add(sTmp.AfterLast(wxT(' ')));
2326 sTmp = sTmp.BeforeLast(wxT(' '));
2327 }
2328
2329 AddToMimeData(strMimeType, strIcon, NULL, sExts, strDesc, true);
2330 }
2331
2332 void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType,
2333 const wxString& strOpenCmd,
2334 const wxString& strPrintCmd,
2335 const wxString& strTest,
2336 const wxString& strDesc)
2337 {
2338 InitIfNeeded();
2339
2340 wxMimeTypeCommands *entry = new wxMimeTypeCommands;
2341 entry->Add(wxT("open=") + strOpenCmd);
2342 entry->Add(wxT("print=") + strPrintCmd);
2343 entry->Add(wxT("test=") + strTest);
2344
2345 wxString strIcon;
2346 wxArrayString strExtensions;
2347
2348 AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
2349 }
2350
2351 bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
2352 {
2353 wxLogTrace(TRACE_MIME, wxT("--- Parsing mime.types file '%s' ---"),
2354 strFileName.c_str());
2355
2356 wxMimeTextFile file(strFileName);
2357 if ( !file.Open() )
2358 return false;
2359
2360 // the information we extract
2361 wxString strMimeType, strDesc, strExtensions;
2362
2363 size_t nLineCount = file.GetLineCount();
2364 const wxChar *pc = NULL;
2365 for ( size_t nLine = 0; nLine < nLineCount; nLine++ )
2366 {
2367 if ( pc == NULL )
2368 {
2369 // now we're at the start of the line
2370 pc = file[nLine].c_str();
2371 }
2372 else
2373 {
2374 // we didn't finish with the previous line yet
2375 nLine--;
2376 }
2377
2378 // skip whitespace
2379 while ( wxIsspace(*pc) )
2380 pc++;
2381
2382 // comment or blank line?
2383 if ( *pc == wxT('#') || !*pc )
2384 {
2385 // skip the whole line
2386 pc = NULL;
2387 continue;
2388 }
2389
2390 // detect file format
2391 const wxChar *pEqualSign = wxStrchr(pc, wxT('='));
2392 if ( pEqualSign == NULL )
2393 {
2394 // brief format
2395 // ------------
2396
2397 // first field is mime type
2398 for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != wxT('\0'); pc++ )
2399 {
2400 strMimeType += *pc;
2401 }
2402
2403 // skip whitespace
2404 while ( wxIsspace(*pc) )
2405 pc++;
2406
2407 // take all the rest of the string
2408 strExtensions = pc;
2409
2410 // no description...
2411 strDesc.Empty();
2412 }
2413 else
2414 {
2415 // expanded format
2416 // ---------------
2417
2418 // the string on the left of '=' is the field name
2419 wxString strLHS(pc, pEqualSign - pc);
2420
2421 // eat whitespace
2422 for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ )
2423 ;
2424
2425 const wxChar *pEnd;
2426 if ( *pc == wxT('"') )
2427 {
2428 // the string is quoted and ends at the matching quote
2429 pEnd = wxStrchr(++pc, wxT('"'));
2430 if ( pEnd == NULL )
2431 {
2432 wxLogWarning(wxT("Mime.types file %s, line %lu: unterminated quoted string."),
2433 strFileName.c_str(), nLine + 1L);
2434 }
2435 }
2436 else
2437 {
2438 // unquoted string ends at the first space or at the end of
2439 // line
2440 for ( pEnd = pc; *pEnd && !wxIsspace(*pEnd); pEnd++ )
2441 ;
2442 }
2443
2444 // now we have the RHS (field value)
2445 wxString strRHS(pc, pEnd - pc);
2446
2447 // check what follows this entry
2448 if ( *pEnd == wxT('"') )
2449 {
2450 // skip this quote
2451 pEnd++;
2452 }
2453
2454 for ( pc = pEnd; wxIsspace(*pc); pc++ )
2455 ;
2456
2457 // if there is something left, it may be either a '\\' to continue
2458 // the line or the next field of the same entry
2459 bool entryEnded = *pc == wxT('\0');
2460 bool nextFieldOnSameLine = false;
2461 if ( !entryEnded )
2462 {
2463 nextFieldOnSameLine = ((*pc != wxT('\\')) || (pc[1] != wxT('\0')));
2464 }
2465
2466 // now see what we got
2467 if ( strLHS == wxT("type") )
2468 {
2469 strMimeType = strRHS;
2470 }
2471 else if ( strLHS.StartsWith(wxT("desc")) )
2472 {
2473 strDesc = strRHS;
2474 }
2475 else if ( strLHS == wxT("exts") )
2476 {
2477 strExtensions = strRHS;
2478 }
2479 else if ( strLHS == wxT("icon") )
2480 {
2481 // this one is simply ignored: it usually refers to Netscape
2482 // built in icons which are useless for us anyhow
2483 }
2484 else if ( !strLHS.StartsWith(wxT("x-")) )
2485 {
2486 // we suppose that all fields starting with "X-" are
2487 // unregistered extensions according to the standard practice,
2488 // but it may be worth telling the user about other junk in
2489 // his mime.types file
2490 wxLogWarning(wxT("Unknown field in file %s, line %lu: '%s'."),
2491 strFileName.c_str(), nLine + 1L, strLHS.c_str());
2492 }
2493
2494 if ( !entryEnded )
2495 {
2496 if ( !nextFieldOnSameLine )
2497 pc = NULL;
2498 //else: don't reset it
2499
2500 // as we don't reset strMimeType, the next field in this entry
2501 // will be interpreted correctly.
2502
2503 continue;
2504 }
2505 }
2506
2507 // depending on the format (Mosaic or Netscape) either space or comma
2508 // is used to separate the extensions
2509 strExtensions.Replace(wxT(","), wxT(" "));
2510
2511 // also deal with the leading dot
2512 if ( !strExtensions.empty() && strExtensions[0u] == wxT('.') )
2513 {
2514 strExtensions.erase(0, 1);
2515 }
2516
2517 wxLogTrace(TRACE_MIME, wxT("mime.types: '%s' => '%s' (%s)"),
2518 strExtensions.c_str(),
2519 strMimeType.c_str(),
2520 strDesc.c_str());
2521
2522 AddMimeTypeInfo(strMimeType, strExtensions, strDesc);
2523
2524 // finished with this line
2525 pc = NULL;
2526 }
2527
2528 return true;
2529 }
2530
2531 // ----------------------------------------------------------------------------
2532 // UNIX mailcap files parsing
2533 // ----------------------------------------------------------------------------
2534
2535 // the data for a single MIME type
2536 struct MailcapLineData
2537 {
2538 // field values
2539 wxString type,
2540 cmdOpen,
2541 test,
2542 icon,
2543 desc;
2544
2545 wxArrayString verbs,
2546 commands;
2547
2548 // flags
2549 bool testfailed,
2550 needsterminal,
2551 copiousoutput;
2552
2553 MailcapLineData() { testfailed = needsterminal = copiousoutput = false; }
2554 };
2555
2556 // process a non-standard (i.e. not the first or second one) mailcap field
2557 bool
2558 wxMimeTypesManagerImpl::ProcessOtherMailcapField(MailcapLineData& data,
2559 const wxString& curField)
2560 {
2561 if ( curField.empty() )
2562 {
2563 // we don't care
2564 return true;
2565 }
2566
2567 // is this something of the form foo=bar?
2568 if ( curField.find('=') != wxString::npos )
2569 {
2570 // split "LHS = RHS" in 2
2571 wxString lhs = curField.BeforeFirst(wxT('=')),
2572 rhs = curField.AfterFirst(wxT('='));
2573
2574 lhs.Trim(true); // from right
2575 rhs.Trim(false); // from left
2576
2577 // it might be quoted
2578 if ( !rhs.empty() && rhs[0u] == wxT('"') && rhs.Last() == wxT('"') )
2579 {
2580 rhs = rhs.Mid(1, rhs.length() - 2);
2581 }
2582
2583 // is it a command verb or something else?
2584 if ( lhs == wxT("test") )
2585 {
2586 if ( wxSystem(rhs) == 0 )
2587 {
2588 // ok, test passed
2589 wxLogTrace(TRACE_MIME_TEST,
2590 wxT("Test '%s' for mime type '%s' succeeded."),
2591 rhs.c_str(), data.type.c_str());
2592 }
2593 else
2594 {
2595 wxLogTrace(TRACE_MIME_TEST,
2596 wxT("Test '%s' for mime type '%s' failed, skipping."),
2597 rhs.c_str(), data.type.c_str());
2598
2599 data.testfailed = true;
2600 }
2601 }
2602 else if ( lhs == wxT("desc") )
2603 {
2604 data.desc = rhs;
2605 }
2606 else if ( lhs == wxT("x11-bitmap") )
2607 {
2608 data.icon = rhs;
2609 }
2610 else if ( lhs == wxT("notes") )
2611 {
2612 // ignore
2613 }
2614 else // not a (recognized) special case, must be a verb (e.g. "print")
2615 {
2616 data.verbs.Add(lhs);
2617 data.commands.Add(rhs);
2618 }
2619 }
2620 else // '=' not found
2621 {
2622 // so it must be a simple flag
2623 if ( curField == wxT("needsterminal") )
2624 {
2625 data.needsterminal = true;
2626 }
2627 else if ( curField == wxT("copiousoutput"))
2628 {
2629 // copiousoutput impies that the viewer is a console program
2630 data.needsterminal =
2631 data.copiousoutput = true;
2632 }
2633 else if ( !IsKnownUnimportantField(curField) )
2634 {
2635 return false;
2636 }
2637 }
2638
2639 return true;
2640 }
2641
2642 bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
2643 bool fallback)
2644 {
2645 wxLogTrace(TRACE_MIME, wxT("--- Parsing mailcap file '%s' ---"),
2646 strFileName.c_str());
2647
2648 wxMimeTextFile file(strFileName);
2649 if ( !file.Open() )
2650 return false;
2651
2652 // indices of MIME types (in m_aTypes) we already found in this file
2653 //
2654 // (see the comments near the end of function for the reason we need this)
2655 wxArrayInt aIndicesSeenHere;
2656
2657 // accumulator for the current field
2658 wxString curField;
2659 curField.reserve(1024);
2660
2661 const wxChar *pPagerEnv = wxGetenv(wxT("PAGER"));
2662
2663 const wxArrayString empty_extensions_list;
2664
2665 size_t nLineCount = file.GetLineCount();
2666 for ( size_t nLine = 0; nLine < nLineCount; nLine++ )
2667 {
2668 // now we're at the start of the line
2669 const wxChar *pc = file[nLine].c_str();
2670
2671 // skip whitespace
2672 while ( wxIsspace(*pc) )
2673 pc++;
2674
2675 // comment or empty string?
2676 if ( *pc == wxT('#') || *pc == wxT('\0') )
2677 continue;
2678
2679 // no, do parse
2680 // ------------
2681
2682 // what field are we currently in? The first 2 are fixed and there may
2683 // be an arbitrary number of other fields parsed by
2684 // ProcessOtherMailcapField()
2685 //
2686 // the first field is the MIME type
2687 enum
2688 {
2689 Field_Type,
2690 Field_OpenCmd,
2691 Field_Other
2692 } currentToken = Field_Type;
2693
2694 // the flags and field values on the current line
2695 MailcapLineData data;
2696
2697 bool cont = true;
2698 while ( cont )
2699 {
2700 switch ( *pc )
2701 {
2702 case wxT('\\'):
2703 // interpret the next character literally (notice that
2704 // backslash can be used for line continuation)
2705 if ( *++pc == wxT('\0') )
2706 {
2707 // fetch the next line if there is one
2708 if ( nLine == nLineCount - 1 )
2709 {
2710 // something is wrong, bail out
2711 cont = false;
2712
2713 wxLogDebug(wxT("Mailcap file %s, line %lu: '\\' on the end of the last line ignored."),
2714 strFileName.c_str(),
2715 nLine + 1L);
2716 }
2717 else
2718 {
2719 // pass to the beginning of the next line
2720 pc = file[++nLine].c_str();
2721
2722 // skip pc++ at the end of the loop
2723 continue;
2724 }
2725 }
2726 else
2727 {
2728 // just a normal character
2729 curField += *pc;
2730 }
2731 break;
2732
2733 case wxT('\0'):
2734 cont = false; // end of line reached, exit the loop
2735
2736 // fall through to still process this field
2737
2738 case wxT(';'):
2739 // trim whitespaces from both sides
2740 curField.Trim(true).Trim(false);
2741
2742 switch ( currentToken )
2743 {
2744 case Field_Type:
2745 data.type = curField.Lower();
2746 if ( data.type.empty() )
2747 {
2748 // I don't think that this is a valid mailcap
2749 // entry, but try to interpret it somehow
2750 data.type = wxT('*');
2751 }
2752
2753 if ( data.type.Find(wxT('/')) == wxNOT_FOUND )
2754 {
2755 // we interpret "type" as "type/*"
2756 data.type += wxT("/*");
2757 }
2758
2759 currentToken = Field_OpenCmd;
2760 break;
2761
2762 case Field_OpenCmd:
2763 data.cmdOpen = curField;
2764
2765 currentToken = Field_Other;
2766 break;
2767
2768 case Field_Other:
2769 if ( !ProcessOtherMailcapField(data, curField) )
2770 {
2771 // don't flood the user with error messages if
2772 // we don't understand something in his
2773 // mailcap, but give them in debug mode because
2774 // this might be useful for the programmer
2775 wxLogDebug
2776 (
2777 wxT("Mailcap file %s, line %lu: unknown field '%s' for the MIME type '%s' ignored."),
2778 strFileName.c_str(),
2779 nLine + 1L,
2780 curField.c_str(),
2781 data.type.c_str()
2782 );
2783 }
2784 else if ( data.testfailed )
2785 {
2786 // skip this entry entirely
2787 cont = false;
2788 }
2789
2790 // it already has this value
2791 //currentToken = Field_Other;
2792 break;
2793
2794 default:
2795 wxFAIL_MSG(wxT("unknown field type in mailcap"));
2796 }
2797
2798 // next token starts immediately after ';'
2799 curField.Empty();
2800 break;
2801
2802 default:
2803 curField += *pc;
2804 }
2805
2806 // continue in the same line
2807 pc++;
2808 }
2809
2810 // we read the entire entry, check what have we got
2811 // ------------------------------------------------
2812
2813 // check that we really read something reasonable
2814 if ( currentToken < Field_Other )
2815 {
2816 wxLogWarning(wxT("Mailcap file %s, line %lu: incomplete entry ignored."),
2817 strFileName.c_str(), nLine + 1L);
2818
2819 continue;
2820 }
2821
2822 // if the test command failed, it's as if the entry were not there at all
2823 if ( data.testfailed )
2824 {
2825 continue;
2826 }
2827
2828 // support for flags:
2829 // 1. create an xterm for 'needsterminal'
2830 // 2. append "| $PAGER" for 'copiousoutput'
2831 //
2832 // Note that the RFC says that having both needsterminal and
2833 // copiousoutput is probably a mistake, so it seems that running
2834 // programs with copiousoutput inside an xterm as it is done now
2835 // is a bad idea (FIXME)
2836 if ( data.copiousoutput )
2837 {
2838 data.cmdOpen << wxT(" | ") << (pPagerEnv ? pPagerEnv : wxT("more"));
2839 }
2840
2841 if ( data.needsterminal )
2842 {
2843 data.cmdOpen.insert(0, wxT("xterm -e sh -c '"));
2844 data.cmdOpen.append(wxT("'"));
2845 }
2846
2847 if ( !data.cmdOpen.empty() )
2848 {
2849 data.verbs.Insert(wxT("open"), 0);
2850 data.commands.Insert(data.cmdOpen, 0);
2851 }
2852
2853 // we have to decide whether the new entry should replace any entries
2854 // for the same MIME type we had previously found or not
2855 bool overwrite;
2856
2857 // the fall back entries have the lowest priority, by definition
2858 if ( fallback )
2859 {
2860 overwrite = false;
2861 }
2862 else
2863 {
2864 // have we seen this one before?
2865 int nIndex = m_aTypes.Index(data.type);
2866
2867 // and if we have, was it in this file? if not, we should
2868 // overwrite the previously seen one
2869 overwrite = nIndex == wxNOT_FOUND ||
2870 aIndicesSeenHere.Index(nIndex) == wxNOT_FOUND;
2871 }
2872
2873 wxLogTrace(TRACE_MIME, wxT("mailcap %s: %s [%s]"),
2874 data.type.c_str(), data.cmdOpen.c_str(),
2875 overwrite ? wxT("replace") : wxT("add"));
2876
2877 int n = AddToMimeData
2878 (
2879 data.type,
2880 data.icon,
2881 new wxMimeTypeCommands(data.verbs, data.commands),
2882 empty_extensions_list,
2883 data.desc,
2884 overwrite
2885 );
2886
2887 if ( overwrite )
2888 {
2889 aIndicesSeenHere.Add(n);
2890 }
2891 }
2892
2893 return true;
2894 }
2895
2896 size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
2897 {
2898 InitIfNeeded();
2899
2900 mimetypes.Empty();
2901
2902 size_t count = m_aTypes.GetCount();
2903 for ( size_t n = 0; n < count; n++ )
2904 {
2905 // don't return template types from here (i.e. anything containg '*')
2906 const wxString &type = m_aTypes[n];
2907 if ( type.Find(wxT('*')) == wxNOT_FOUND )
2908 {
2909 mimetypes.Add(type);
2910 }
2911 }
2912
2913 return mimetypes.GetCount();
2914 }
2915
2916 // ----------------------------------------------------------------------------
2917 // writing to MIME type files
2918 // ----------------------------------------------------------------------------
2919
2920 bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft)
2921 {
2922 InitIfNeeded();
2923
2924 wxArrayString sMimeTypes;
2925 ft->GetMimeTypes(sMimeTypes);
2926
2927 size_t i;
2928 size_t nCount = sMimeTypes.GetCount();
2929 for (i = 0; i < nCount; i ++)
2930 {
2931 const wxString &sMime = sMimeTypes.Item(i);
2932 int nIndex = m_aTypes.Index(sMime);
2933 if ( nIndex == wxNOT_FOUND)
2934 {
2935 // error if we get here ??
2936 return false;
2937 }
2938 else
2939 {
2940 WriteMimeInfo(nIndex, true);
2941 m_aTypes.RemoveAt(nIndex);
2942 m_aEntries.RemoveAt(nIndex);
2943 m_aExtensions.RemoveAt(nIndex);
2944 m_aDescriptions.RemoveAt(nIndex);
2945 m_aIcons.RemoveAt(nIndex);
2946 }
2947 }
2948 // check data integrity
2949 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
2950 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
2951 m_aTypes.GetCount() == m_aIcons.GetCount() &&
2952 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
2953
2954 return true;
2955 }
2956
2957 // ----------------------------------------------------------------------------
2958 // private functions
2959 // ----------------------------------------------------------------------------
2960
2961 static bool IsKnownUnimportantField(const wxString& fieldAll)
2962 {
2963 static const wxChar * const knownFields[] =
2964 {
2965 wxT("x-mozilla-flags"),
2966 wxT("nametemplate"),
2967 wxT("textualnewlines"),
2968 };
2969
2970 wxString field = fieldAll.BeforeFirst(wxT('='));
2971 for ( size_t n = 0; n < WXSIZEOF(knownFields); n++ )
2972 {
2973 if ( field.CmpNoCase(knownFields[n]) == 0 )
2974 return true;
2975 }
2976
2977 return false;
2978 }
2979
2980 #endif
2981 // wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE