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