]> git.saurik.com Git - wxWidgets.git/blame - src/unix/mimetype.cpp
Use font encoding
[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;
1dfeae4c
JS
753 {
754 wxLogNull logNull;
755 if ( !file.Open(fullname.GetFullPath()) )
756 if ( !file.Open(fullname.GetFullPath(), wxConvISO8859_1) )
757 return;
758 }
b9517a0a 759
25d599ae 760 wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"),
31383ebc 761 fullname.GetFullPath().c_str());
d0ee33f5 762
678ebfcd 763 wxMimeTypeCommands * entry = new wxMimeTypeCommands;
2b813b73
VZ
764 wxArrayString sExts;
765 wxString mimetype, mime_desc, strIcon;
766
2b5f62a0 767 int nIndex = file.pIndexOf( wxT("MimeType=") );
2b813b73 768 if (nIndex == wxNOT_FOUND)
678ebfcd
VZ
769 {
770 // construct mimetype from the directory name and the basename of the
771 // file (it always has .kdelnk extension)
2b5f62a0 772 mimetype << subdir << wxT('/') << filename.BeforeLast( wxT('.') );
678ebfcd 773 }
dfea7acc
DS
774 else
775 mimetype = file.GetCmd(nIndex);
b9517a0a 776
276c7463
VZ
777 // first find the description string: it is the value in either "Comment="
778 // line or "Comment[<locale_name>]=" one
2b813b73 779 nIndex = wxNOT_FOUND;
276c7463
VZ
780
781 wxString comment;
7c2e5dec 782
276c7463
VZ
783#if wxUSE_INTL
784 wxLocale *locale = wxGetLocale();
785 if ( locale )
786 {
787 // try "Comment[locale name]" first
dfea7acc 788 comment << wxT("Comment[") + locale->GetName() + wxT("]=");
2b813b73 789 nIndex = file.pIndexOf(comment);
276c7463 790 }
7c2e5dec 791#endif
cdf339c9 792
2b813b73 793 if ( nIndex == wxNOT_FOUND )
276c7463 794 {
dfea7acc 795 comment = wxT("Comment=");
2b813b73 796 nIndex = file.pIndexOf(comment);
276c7463
VZ
797 }
798
dfea7acc
DS
799 if ( nIndex != wxNOT_FOUND )
800 mime_desc = file.GetCmd(nIndex);
276c7463 801 //else: no description
cdf339c9 802
276c7463
VZ
803 // next find the extensions
804 wxString mime_extension;
805
dfea7acc 806 nIndex = file.pIndexOf(wxT("Patterns="));
2b813b73 807 if ( nIndex != wxNOT_FOUND )
678ebfcd 808 {
dfea7acc 809 wxString exts = file.GetCmd(nIndex);
5bd3a2da 810
dfea7acc 811 wxStringTokenizer tokenizer(exts, wxT(";"));
276c7463 812 while ( tokenizer.HasMoreTokens() )
cdf339c9 813 {
276c7463 814 wxString e = tokenizer.GetNextToken();
7c2e5dec
DS
815
816 // don't support too difficult patterns
dfea7acc 817 if ( e.Left(2) != wxT("*.") )
7c2e5dec 818 continue;
276c7463
VZ
819
820 if ( !mime_extension.empty() )
821 {
822 // separate from the previous ext
dfea7acc 823 mime_extension << wxT(' ');
276c7463
VZ
824 }
825
cdf339c9 826 mime_extension << e.Mid(2);
cdf339c9 827 }
cdf339c9 828 }
dfea7acc 829
2b813b73 830 sExts.Add(mime_extension);
5bd3a2da 831
cdf339c9
VS
832 // ok, now we can take care of icon:
833
dfea7acc 834 nIndex = file.pIndexOf(wxT("Icon="));
2b813b73 835 if ( nIndex != wxNOT_FOUND )
b9517a0a 836 {
2b813b73 837 strIcon = file.GetCmd(nIndex);
7c2e5dec 838
25d599ae 839 wxLogTrace(TRACE_MIME, wxT(" icon %s"), strIcon.c_str());
d0ee33f5 840
7c2e5dec 841 // it could be the real path, but more often a short name
2b813b73 842 if (!wxFileExists(strIcon))
678ebfcd 843 {
2b813b73 844 // icon is just the short name
678ebfcd 845 if ( !strIcon.empty() )
cdf339c9 846 {
678ebfcd
VZ
847 // we must check if the file exists because it may be stored
848 // in many locations, at least ~/.kde and $KDEDIR
849 size_t nDir, nDirs = icondirs.GetCount();
850 for ( nDir = 0; nDir < nDirs; nDir++ )
10274336 851 {
3d780434
RR
852 wxFileName fnameIcon( strIcon );
853 wxFileName fname( icondirs[nDir], fnameIcon.GetName() );
10274336 854 fname.SetExt( wxT("png") );
1d529ef7 855 if (fname.FileExists())
678ebfcd 856 {
1d529ef7 857 strIcon = fname.GetFullPath();
25d599ae 858 wxLogTrace(TRACE_MIME, wxT(" iconfile %s"), strIcon.c_str());
678ebfcd
VZ
859 break;
860 }
10274336 861 }
2b813b73 862 }
678ebfcd
VZ
863 }
864 }
dfea7acc 865
2b813b73
VZ
866 // now look for lines which know about the application
867 // exec= or DefaultApp=
868
869 nIndex = file.pIndexOf(wxT("DefaultApp"));
b9517a0a 870
2b813b73 871 if ( nIndex == wxNOT_FOUND )
678ebfcd 872 {
2b813b73
VZ
873 // no entry try exec
874 nIndex = file.pIndexOf(wxT("Exec"));
678ebfcd 875 }
2b813b73
VZ
876
877 if ( nIndex != wxNOT_FOUND )
678ebfcd 878 {
2b813b73 879 // we expect %f; others including %F and %U and %u are possible
7c2e5dec 880 wxString sTmp = file.GetCmd(nIndex);
dfea7acc 881 if (0 == sTmp.Replace( wxT("%f"), wxT("%s") ))
15d4b8cd 882 sTmp += wxT(" %s");
dfea7acc 883 entry->AddOrReplaceVerb(wxString(wxT("open")), sTmp );
b9517a0a 884 }
2b813b73 885
dfea7acc 886 AddToMimeData(mimetype, strIcon, entry, sExts, mime_desc);
b9517a0a
VZ
887}
888
2b813b73 889void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase,
cdf339c9
VS
890 const wxString& subdir,
891 const wxArrayString& icondirs)
b9517a0a 892{
31383ebc
RR
893 wxFileName dirname(dirbase, wxEmptyString);
894 dirname.AppendDir(subdir);
895 wxDir dir(dirname.GetPath());
896 if(! dir.IsOpened())
b9517a0a
VZ
897 return;
898
25d599ae 899 wxLogTrace(TRACE_MIME, wxT("--- Loading from KDE directory %s ---"),
31383ebc 900 dirname.GetPath().c_str());
b9517a0a
VZ
901
902 wxString filename;
dfea7acc 903 bool cont = dir.GetFirst(&filename, wxT("*.kdelnk"), wxDIR_FILES);
31383ebc
RR
904 while(cont) {
905 LoadKDELinksForMimeSubtype(dirname.GetPath(), subdir,
906 filename, icondirs);
2b813b73
VZ
907 cont = dir.GetNext(&filename);
908 }
dfea7acc 909
2b813b73 910 // new standard for Gnome and KDE
dfea7acc 911 cont = dir.GetFirst(&filename, wxT("*.desktop"), wxDIR_FILES);
31383ebc
RR
912 while(cont) {
913 LoadKDELinksForMimeSubtype(dirname.GetPath(), subdir,
914 filename, icondirs);
b9517a0a
VZ
915 cont = dir.GetNext(&filename);
916 }
917}
918
31383ebc 919void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString& dirname,
cdf339c9 920 const wxArrayString& icondirs)
b9517a0a 921{
31383ebc 922 if(! wxDir::Exists(dirname))
b9517a0a
VZ
923 return;
924
925 wxDir dir(dirname);
926 if ( !dir.IsOpened() )
927 return;
928
b9517a0a
VZ
929 wxString subdir;
930 bool cont = dir.GetFirst(&subdir, wxEmptyString, wxDIR_DIRS);
931 while ( cont )
932 {
2b813b73 933 LoadKDELinksForMimeType(dirname, subdir, icondirs);
b9517a0a
VZ
934
935 cont = dir.GetNext(&subdir);
936 }
937}
938
31383ebc
RR
939// Read a KDE .desktop file of type 'Application'
940void wxMimeTypesManagerImpl::LoadKDEApp(const wxString& filename)
b9517a0a 941{
31383ebc 942 wxMimeTextFile file;
1dfeae4c
JS
943
944 {
945 wxLogNull logNull;
946 if ( !file.Open(filename) )
947 if ( !file.Open(filename, wxConvISO8859_1) )
948 return;
949 }
950
31383ebc 951 wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"), filename.c_str());
320c341a 952
31383ebc
RR
953 // Here, only type 'Application' should be considered.
954 int nIndex = file.pIndexOf( wxT("Type=") );
955 if (nIndex != wxNOT_FOUND &&
956 file.GetCmd(nIndex).Lower() != wxT("application"))
957 return;
dfea7acc 958
31383ebc
RR
959 // The hidden entry specifies a file to be ignored.
960 nIndex = file.pIndexOf( wxT("Hidden=") );
961 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex).Lower() == wxT("true"))
962 return;
dfea7acc 963
31383ebc
RR
964 // Semicolon separated list of mime types handled by the application.
965 nIndex = file.pIndexOf( wxT("MimeType=") );
966 if (nIndex == wxNOT_FOUND)
967 return;
968 wxString mimetypes = file.GetCmd (nIndex);
dfea7acc 969
31383ebc
RR
970 // Name of the application
971 wxString nameapp;
972 nIndex = wxNOT_FOUND;
973#if wxUSE_INTL // try "Name[locale name]" first
974 wxLocale *locale = wxGetLocale();
975 if ( locale )
976 nIndex = file.pIndexOf(_T("Name[")+locale->GetName()+_T("]="));
977#endif // wxUSE_INTL
978 if(nIndex == wxNOT_FOUND)
979 nIndex = file.pIndexOf( wxT("Name=") );
980 if(nIndex != wxNOT_FOUND)
981 nameapp = file.GetCmd(nIndex);
982
983 // Icon of the application.
984 wxString nameicon, namemini;
985 nIndex = wxNOT_FOUND;
986#if wxUSE_INTL // try "Icon[locale name]" first
987 if ( locale )
988 nIndex = file.pIndexOf(_T("Icon[")+locale->GetName()+_T("]="));
989#endif // wxUSE_INTL
990 if(nIndex == wxNOT_FOUND)
991 nIndex = file.pIndexOf( wxT("Icon=") );
992 if(nIndex != wxNOT_FOUND) {
993 nameicon = wxString(wxT("--icon ")) + file.GetCmd(nIndex);
994 namemini = wxString(wxT("--miniicon ")) + file.GetCmd(nIndex);
995 }
996
997 // Replace some of the field code in the 'Exec' entry.
998 // TODO: deal with %d, %D, %n, %N, %k and %v (but last one is deprecated)
999 nIndex = file.pIndexOf( wxT("Exec=") );
1000 if (nIndex == wxNOT_FOUND)
1001 return;
1002 wxString sCmd = file.GetCmd(nIndex);
1003 // we expect %f; others including %F and %U and %u are possible
1004 sCmd.Replace(wxT("%F"), wxT("%f"));
1005 sCmd.Replace(wxT("%U"), wxT("%f"));
1006 sCmd.Replace(wxT("%u"), wxT("%f"));
1007 if (0 == sCmd.Replace ( wxT("%f"), wxT("%s") ))
1008 sCmd = sCmd + wxT(" %s");
1009 sCmd.Replace(wxT("%c"), nameapp);
1010 sCmd.Replace(wxT("%i"), nameicon);
1011 sCmd.Replace(wxT("%m"), namemini);
1012
1013 wxStringTokenizer tokenizer(mimetypes, _T(";"));
1014 while(tokenizer.HasMoreTokens()) {
1015 wxString mimetype = tokenizer.GetNextToken().Lower();
1016 int nIndex = m_aTypes.Index(mimetype);
1017 if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
1018 wxMimeTypeCommands* entry = m_aEntries[nIndex];
1019 entry->AddOrReplaceVerb(wxT("open"), sCmd);
10274336 1020 }
31383ebc
RR
1021 }
1022}
d0ee33f5 1023
31383ebc
RR
1024void wxMimeTypesManagerImpl::LoadKDEAppsFilesFromDir(const wxString& dirname)
1025{
1026 if(! wxDir::Exists(dirname))
1027 return;
1028 wxDir dir(dirname);
1029 if ( !dir.IsOpened() )
1030 return;
d0ee33f5 1031
31383ebc
RR
1032 wxString filename;
1033 // Look into .desktop files
1034 bool cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES);
1035 while(cont) {
1036 wxFileName p(dirname, filename);
1037 LoadKDEApp( p.GetFullPath() );
1038 cont = dir.GetNext(&filename);
1039 }
1040 // Look recursively into subdirs
1041 cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
1042 while(cont) {
1043 wxFileName p(dirname, wxEmptyString);
1044 p.AppendDir(filename);
1045 LoadKDEAppsFilesFromDir( p.GetPath() );
1046 cont = dir.GetNext(&filename);
1047 }
1048}
d0ee33f5 1049
31383ebc
RR
1050// Return base KDE directories.
1051// 1) Environment variable $KDEHOME, or "~/.kde" if not set.
1052// 2) List of directories in colon separated environment variable $KDEDIRS.
1053// 3) Environment variable $KDEDIR in case $KDEDIRS is not set.
1054// Notice at least the local kde directory is added to the list. If it is the
1055// only one, use later the application 'kde-config' to get additional paths.
1056static void GetKDEBaseDirs(wxArrayString& basedirs)
1057{
1058 wxString env = wxGetenv( wxT("KDEHOME") );
1059 if(env.IsEmpty())
1060 env = wxGetHomeDir() + wxT("/.kde");
1061 basedirs.Add(env);
1062
1063 env = wxGetenv( wxT("KDEDIRS") );
1064 if(env.IsEmpty()) {
1065 env = wxGetenv( wxT("KDEDIR") );
1066 if(! env.IsEmpty())
1067 basedirs.Add(env);
1068 } else {
1069 wxStringTokenizer tokenizer(env, wxT(":"));
1070 while(tokenizer.HasMoreTokens())
1071 basedirs.Add( tokenizer.GetNextToken() );
1072 }
1073}
d0ee33f5 1074
31383ebc
RR
1075static wxString ReadPathFromKDEConfig(const wxString& request)
1076{
1077 wxString str;
1078 wxArrayString output;
1079 if(wxExecute(wxT("kde-config --path ")+request, output) == 0 &&
1080 output.Count() > 0)
1081 str = output.Item(0);
1082 return str;
1083}
d0ee33f5 1084
31383ebc
RR
1085// Try to find the "Theme" entry in the configuration file, provided it exists.
1086static wxString GetKDEThemeInFile(const wxFileName& filename)
1087{
1088 wxString theme;
1089 wxTextFile config;
1090 if(filename.FileExists() && config.Open( filename.GetFullPath() )) {
1091 size_t cnt = config.GetLineCount();
1092 for(size_t i = 0; i < cnt; i++)
1093 if(config[i].StartsWith(wxT("Theme="), &theme))
1094 break;
1095 }
1096 return theme;
1097}
d0ee33f5 1098
31383ebc
RR
1099// Try to find a file "kdeglobals" in one of the directories and read the
1100// "Theme" entry there.
1101static wxString GetKDETheme(const wxArrayString& basedirs)
1102{
1103 wxString theme;
1104 for(size_t i = 0; i < basedirs.Count(); i++) {
1105 wxFileName filename(basedirs.Item(i), wxEmptyString);
1106 filename.AppendDir( wxT("share") );
1107 filename.AppendDir( wxT("config") );
1108 filename.SetName( wxT("kdeglobals") );
1109 theme = GetKDEThemeInFile(filename);
1110 if(! theme.IsEmpty())
1111 return theme;
1112 }
1113 // If $KDEDIRS and $KDEDIR were set, we try nothing more. Otherwise, we
1114 // try to get the configuration file with 'kde-config'.
1115 if(basedirs.Count() > 1)
1116 return theme;
1117 wxString paths = ReadPathFromKDEConfig(wxT("config"));
1118 if(! paths.IsEmpty()) {
1119 wxStringTokenizer tokenizer(paths, wxT(":"));
1120 while( tokenizer.HasMoreTokens() ) {
1121 wxFileName filename(tokenizer.GetNextToken(), wxT("kdeglobals"));
1122 theme = GetKDEThemeInFile(filename);
1123 if(! theme.IsEmpty())
1124 return theme;
10274336 1125 }
31383ebc
RR
1126 }
1127 return theme;
1128}
d0ee33f5 1129
31383ebc
RR
1130// Get list of directories of icons.
1131static void GetKDEIconDirs(const wxArrayString& basedirs,
1132 wxArrayString& icondirs)
1133{
1134 wxString theme = GetKDETheme(basedirs);
1135 if(theme.IsEmpty())
1136 theme = wxT("default.kde");
1137
1138 for(size_t i = 0; i < basedirs.Count(); i++) {
1139 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1140 dirname.AppendDir( wxT("share") );
1141 dirname.AppendDir( wxT("icons") );
1142 dirname.AppendDir(theme);
1143 dirname.AppendDir( wxT("32x32") );
1144 dirname.AppendDir( wxT("mimetypes") );
1145 if( wxDir::Exists( dirname.GetPath() ) )
1146 icondirs.Add( dirname.GetPath() );
1147 }
1148
1149 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1150 if(basedirs.Count() > 1)
1151 return;
1152 wxString paths = ReadPathFromKDEConfig(wxT("icon"));
1153 if(! paths.IsEmpty()) {
1154 wxStringTokenizer tokenizer(paths, wxT(":"));
1155 while( tokenizer.HasMoreTokens() ) {
1156 wxFileName dirname(tokenizer.GetNextToken(), wxEmptyString);
1157 dirname.AppendDir(theme);
1158 dirname.AppendDir( wxT("32x32") );
1159 dirname.AppendDir( wxT("mimetypes") );
1160 if(icondirs.Index(dirname.GetPath()) == wxNOT_FOUND &&
1161 wxDir::Exists( dirname.GetPath() ) )
1162 icondirs.Add( dirname.GetPath() );
10274336 1163 }
31383ebc
RR
1164 }
1165}
d0ee33f5 1166
31383ebc
RR
1167// Get list of directories of mime types.
1168static void GetKDEMimeDirs(const wxArrayString& basedirs,
1169 wxArrayString& mimedirs)
1170{
1171 for(size_t i = 0; i < basedirs.Count(); i++) {
1172 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1173 dirname.AppendDir( wxT("share") );
1174 dirname.AppendDir( wxT("mimelnk") );
1175 if( wxDir::Exists( dirname.GetPath() ) )
1176 mimedirs.Add( dirname.GetPath() );
1d529ef7 1177 }
cdf339c9 1178
31383ebc
RR
1179 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1180 if(basedirs.Count() > 1)
1181 return;
1182 wxString paths = ReadPathFromKDEConfig(wxT("mime"));
1183 if(! paths.IsEmpty()) {
1184 wxStringTokenizer tokenizer(paths, wxT(":"));
1185 while( tokenizer.HasMoreTokens() ) {
1186 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1187 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1188 if(mimedirs.Index(dirname) == wxNOT_FOUND &&
1189 wxDir::Exists(dirname) )
1190 mimedirs.Add(dirname);
1191 }
1192 }
1193}
509a6196 1194
31383ebc
RR
1195// Get list of directories of application desktop files.
1196static void GetKDEAppsDirs(const wxArrayString& basedirs,
1197 wxArrayString& appsdirs)
1198{
1199 for(size_t i = 0; i < basedirs.Count(); i++) {
1200 wxFileName dirname(basedirs.Item(i), wxEmptyString);
1201 dirname.AppendDir( wxT("share") );
1202 dirname.AppendDir( wxT("applnk") );
1203 if( wxDir::Exists( dirname.GetPath() ) )
1204 appsdirs.Add( dirname.GetPath() );
509a6196 1205 }
31383ebc
RR
1206
1207 // If $KDEDIRS and $KDEDIR were not set, use 'kde-config'
1208 if(basedirs.Count() > 1)
1209 return;
1210 wxString paths = ReadPathFromKDEConfig(wxT("apps"));
1211 if(! paths.IsEmpty()) {
1212 wxStringTokenizer tokenizer(paths, wxT(":"));
1213 while( tokenizer.HasMoreTokens() ) {
1214 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1215 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1216 if(appsdirs.Index(dirname) == wxNOT_FOUND &&
1217 wxDir::Exists(dirname) )
1218 appsdirs.Add(dirname);
1219 }
1220 }
1221 paths = ReadPathFromKDEConfig(wxT("xdgdata-apps"));
1222 if(! paths.IsEmpty()) {
1223 wxStringTokenizer tokenizer(paths, wxT(":"));
1224 while( tokenizer.HasMoreTokens() ) {
1225 wxFileName p(tokenizer.GetNextToken(), wxEmptyString);
1226 wxString dirname = p.GetPath(); // To remove possible trailing '/'
1227 if(appsdirs.Index(dirname) == wxNOT_FOUND &&
1228 wxDir::Exists(dirname) )
1229 appsdirs.Add(dirname);
1230 }
509a6196 1231 }
31383ebc 1232}
509a6196 1233
31383ebc
RR
1234// Fill database with all mime types.
1235void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir)
1236{
1237 wxArrayString basedirs;
1238 GetKDEBaseDirs(basedirs);
2b813b73 1239
31383ebc
RR
1240 wxArrayString icondirs;
1241 GetKDEIconDirs(basedirs, icondirs);
1242 wxArrayString mimedirs;
1243 GetKDEMimeDirs(basedirs, mimedirs);
1244 wxArrayString appsdirs;
1245 GetKDEAppsDirs(basedirs, appsdirs);
1246
1247 if(! sExtraDir.IsEmpty()) {
1248 icondirs.Add(sExtraDir + wxT("/icons"));
1249 mimedirs.Add(sExtraDir + wxT("/mimelnk"));
1250 appsdirs.Add(sExtraDir + wxT("/applnk"));
b9517a0a 1251 }
31383ebc
RR
1252
1253 // Load mime types
1254 size_t nDirs = mimedirs.GetCount(), nDir;
1255 for(nDir = 0; nDir < nDirs; nDir++)
1256 LoadKDELinkFilesFromDir(mimedirs[nDir], icondirs);
1257
1258 // Load application files and associate them to corresponding mime types.
1259 nDirs = appsdirs.GetCount();
1260 for(nDir = 0; nDir < nDirs; nDir++)
1261 LoadKDEAppsFilesFromDir(appsdirs[nDir]);
b9517a0a
VZ
1262}
1263
2b813b73
VZ
1264// ----------------------------------------------------------------------------
1265// wxFileTypeImpl (Unix)
1266// ----------------------------------------------------------------------------
1267
2b813b73 1268wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const
b9517a0a 1269{
2b813b73
VZ
1270 wxString sTmp;
1271 size_t i = 0;
678ebfcd 1272 while ( (i < m_index.GetCount() ) && sTmp.empty() )
b9517a0a 1273 {
dfea7acc
DS
1274 sTmp = m_manager->GetCommand( verb, m_index[i] );
1275 i++;
b9517a0a
VZ
1276 }
1277
2b813b73
VZ
1278 return wxFileType::ExpandCommand(sTmp, params);
1279}
b9517a0a 1280
da0766ab 1281bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
2b813b73
VZ
1282{
1283 wxString sTmp;
1284 size_t i = 0;
678ebfcd 1285 while ( (i < m_index.GetCount() ) && sTmp.empty() )
c786f763
VZ
1286 {
1287 sTmp = m_manager->m_aIcons[m_index[i]];
dfea7acc 1288 i++;
c786f763 1289 }
dfea7acc
DS
1290
1291 if ( sTmp.empty() )
d0ee33f5 1292 return false;
b9517a0a 1293
da0766ab 1294 if ( iconLoc )
c786f763 1295 {
a49686b4 1296 iconLoc->SetFileName(sTmp);
2b813b73 1297 }
c786f763 1298
d0ee33f5 1299 return true;
c786f763 1300}
2b813b73 1301
dfea7acc 1302bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
2b813b73
VZ
1303{
1304 mimeTypes.Clear();
15d4b8cd
RR
1305 size_t nCount = m_index.GetCount();
1306 for (size_t i = 0; i < nCount; i++)
2b813b73 1307 mimeTypes.Add(m_manager->m_aTypes[m_index[i]]);
dfea7acc 1308
d0ee33f5 1309 return true;
2b813b73
VZ
1310}
1311
2b813b73
VZ
1312size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
1313 wxArrayString *commands,
1314 const wxFileType::MessageParameters& params) const
1315{
2b813b73
VZ
1316 wxString vrb, cmd, sTmp;
1317 size_t count = 0;
678ebfcd 1318 wxMimeTypeCommands * sPairs;
2b813b73
VZ
1319
1320 // verbs and commands have been cleared already in mimecmn.cpp...
1321 // if we find no entries in the exact match, try the inexact match
7c2e5dec 1322 for (size_t n = 0; ((count == 0) && (n < m_index.GetCount())); n++)
2b813b73
VZ
1323 {
1324 // list of verb = command pairs for this mimetype
1325 sPairs = m_manager->m_aEntries [m_index[n]];
1326 size_t i;
dfea7acc
DS
1327 for ( i = 0; i < sPairs->GetCount(); i++ )
1328 {
1329 vrb = sPairs->GetVerb(i);
7c2e5dec 1330 // some gnome entries have "." inside
dfea7acc
DS
1331 vrb = vrb.AfterLast(wxT('.'));
1332 cmd = sPairs->GetCmd(i);
1333 if (! cmd.empty() )
2b813b73 1334 {
dfea7acc 1335 cmd = wxFileType::ExpandCommand(cmd, params);
7c2e5dec 1336 count++;
dfea7acc
DS
1337 if ( vrb.IsSameAs(wxT("open")))
1338 {
d6a7ca31
VZ
1339 if ( verbs )
1340 verbs->Insert(vrb, 0u);
1341 if ( commands )
1342 commands ->Insert(cmd, 0u);
dfea7acc
DS
1343 }
1344 else
1345 {
d6a7ca31
VZ
1346 if ( verbs )
1347 verbs->Add(vrb);
1348 if ( commands )
1349 commands->Add(cmd);
dfea7acc
DS
1350 }
1351 }
2b813b73 1352 }
2b813b73 1353 }
2b813b73 1354
dfea7acc 1355 return count;
2b813b73
VZ
1356}
1357
1358bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
1359{
1360 wxString strExtensions = m_manager->GetExtension(m_index[0]);
1361 extensions.Empty();
1362
1c4cd9e0 1363 // one extension in the space or comma-delimited list
2b813b73 1364 wxString strExt;
dfea7acc
DS
1365 for ( const wxChar *p = strExtensions; /* nothing */; p++ )
1366 {
1367 if ( *p == wxT(' ') || *p == wxT(',') || *p == wxT('\0') )
1368 {
1369 if ( !strExt.empty() )
1370 {
2b813b73
VZ
1371 extensions.Add(strExt);
1372 strExt.Empty();
1373 }
7c2e5dec
DS
1374 //else: repeated spaces
1375 // (shouldn't happen, but it's not that important if it does happen)
2b813b73
VZ
1376
1377 if ( *p == wxT('\0') )
1378 break;
1379 }
dfea7acc
DS
1380 else if ( *p == wxT('.') )
1381 {
2b813b73 1382 // remove the dot from extension (but only if it's the first char)
dfea7acc
DS
1383 if ( !strExt.empty() )
1384 {
2b813b73
VZ
1385 strExt += wxT('.');
1386 }
1387 //else: no, don't append it
1388 }
dfea7acc
DS
1389 else
1390 {
2b813b73
VZ
1391 strExt += *p;
1392 }
1393 }
1394
d0ee33f5 1395 return true;
2b813b73
VZ
1396}
1397
7c2e5dec 1398// set an arbitrary command:
2b813b73 1399// could adjust the code to ask confirmation if it already exists and
d0ee33f5 1400// overwriteprompt is true, but this is currently ignored as *Associate* has
2b813b73 1401// no overwrite prompt
17a1ebd1
VZ
1402bool
1403wxFileTypeImpl::SetCommand(const wxString& cmd,
1404 const wxString& verb,
1405 bool WXUNUSED(overwriteprompt))
d84afea9 1406{
2b813b73 1407 wxArrayString strExtensions;
678ebfcd 1408 wxString strDesc, strIcon;
2b813b73 1409
2b813b73 1410 wxArrayString strTypes;
dfea7acc 1411 GetMimeTypes(strTypes);
926ce9e3 1412 if ( strTypes.IsEmpty() )
dfea7acc 1413 return false;
2b813b73 1414
926ce9e3
VZ
1415 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
1416 entry->Add(verb + wxT("=") + cmd + wxT(" %s "));
1417
dfea7acc 1418 bool ok = true;
15d4b8cd
RR
1419 size_t nCount = strTypes.GetCount();
1420 for ( size_t i = 0; i < nCount; i++ )
d84afea9 1421 {
dfea7acc
DS
1422 if (!m_manager->DoAssociation(strTypes[i], strIcon, entry, strExtensions, strDesc))
1423 ok = false;
d84afea9 1424 }
2b813b73 1425
dfea7acc 1426 return ok;
d84afea9 1427}
2b813b73 1428
15d4b8cd 1429// ignore index on the grounds that we only have one icon in a Unix file
17a1ebd1 1430bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int WXUNUSED(index))
d84afea9 1431{
dfea7acc
DS
1432 if (strIcon.empty())
1433 return false;
1434
2b813b73
VZ
1435 wxArrayString strExtensions;
1436 wxString strDesc;
1437
2b813b73 1438 wxArrayString strTypes;
dfea7acc 1439 GetMimeTypes(strTypes);
cb0b7b7d 1440 if ( strTypes.IsEmpty() )
dfea7acc 1441 return false;
2b813b73 1442
cb0b7b7d 1443 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
dfea7acc 1444 bool ok = true;
15d4b8cd
RR
1445 size_t nCount = strTypes.GetCount();
1446 for ( size_t i = 0; i < nCount; i++ )
d84afea9 1447 {
cb0b7b7d
VZ
1448 if ( !m_manager->DoAssociation
1449 (
1450 strTypes[i],
1451 strIcon,
1452 entry,
1453 strExtensions,
1454 strDesc
1455 ) )
1456 {
dfea7acc 1457 ok = false;
cb0b7b7d 1458 }
d84afea9 1459 }
2b813b73 1460
dfea7acc 1461 return ok;
d84afea9
GD
1462}
1463
2b813b73
VZ
1464// ----------------------------------------------------------------------------
1465// wxMimeTypesManagerImpl (Unix)
1466// ----------------------------------------------------------------------------
1467
2b813b73
VZ
1468wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
1469{
d0ee33f5 1470 m_initialized = false;
2b813b73
VZ
1471 m_mailcapStylesInited = 0;
1472}
1473
1d529ef7
RR
1474void wxMimeTypesManagerImpl::InitIfNeeded()
1475{
1476 if ( !m_initialized )
1477 {
1478 // set the flag first to prevent recursion
d0ee33f5 1479 m_initialized = true;
88bbc332
RR
1480
1481 wxString wm = wxTheApp->GetTraits()->GetDesktopEnvironment();
1482
1483 if (wm == wxT("KDE"))
1484 Initialize( wxMAILCAP_KDE );
1485 else if (wm == wxT("GNOME"))
1486 Initialize( wxMAILCAP_GNOME );
1487 else
1488 Initialize();
1d529ef7
RR
1489 }
1490}
1491
2b813b73
VZ
1492// read system and user mailcaps and other files
1493void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
1494 const wxString& sExtraDir)
1495{
1496 // read mimecap amd mime.types
a06c1b9b
VZ
1497 if ( (mailcapStyles & wxMAILCAP_NETSCAPE) ||
1498 (mailcapStyles & wxMAILCAP_STANDARD) )
2b813b73
VZ
1499 GetMimeInfo(sExtraDir);
1500
1501 // read GNOME tables
1d529ef7 1502 if (mailcapStyles & wxMAILCAP_GNOME)
2b813b73
VZ
1503 GetGnomeMimeInfo(sExtraDir);
1504
1505 // read KDE tables
1d529ef7 1506 if (mailcapStyles & wxMAILCAP_KDE)
2b813b73
VZ
1507 GetKDEMimeInfo(sExtraDir);
1508
1509 m_mailcapStylesInited |= mailcapStyles;
1510}
1511
1512// clear data so you can read another group of WM files
1513void wxMimeTypesManagerImpl::ClearData()
1514{
dfea7acc
DS
1515 m_aTypes.Clear();
1516 m_aIcons.Clear();
1517 m_aExtensions.Clear();
1518 m_aDescriptions.Clear();
2b813b73 1519
2b5f62a0 1520 WX_CLEAR_ARRAY(m_aEntries);
678ebfcd
VZ
1521 m_aEntries.Empty();
1522
2b813b73
VZ
1523 m_mailcapStylesInited = 0;
1524}
1525
1526wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
1527{
678ebfcd 1528 ClearData();
2b813b73
VZ
1529}
1530
dfea7acc 1531void wxMimeTypesManagerImpl::GetMimeInfo(const wxString& sExtraDir)
2b813b73
VZ
1532{
1533 // read this for netscape or Metamail formats
1534
1535 // directories where we look for mailcap and mime.types by default
1536 // used by netscape and pine and other mailers, using 2 different formats!
1537
1538 // (taken from metamail(1) sources)
1539 //
1540 // although RFC 1524 specifies the search path of
1541 // /etc/:/usr/etc:/usr/local/etc only, it doesn't hurt to search in more
1542 // places - OTOH, the RFC also says that this path can be changed with
1543 // MAILCAPS environment variable (containing the colon separated full
1544 // filenames to try) which is not done yet (TODO?)
1545
1546 wxString strHome = wxGetenv(wxT("HOME"));
1547
1548 wxArrayString dirs;
dfea7acc
DS
1549 dirs.Add( strHome + wxT("/.") );
1550 dirs.Add( wxT("/etc/") );
1551 dirs.Add( wxT("/usr/etc/") );
1552 dirs.Add( wxT("/usr/local/etc/") );
1553 dirs.Add( wxT("/etc/mail/") );
1554 dirs.Add( wxT("/usr/public/lib/") );
1555 if (!sExtraDir.empty())
1556 dirs.Add( sExtraDir + wxT("/") );
2b813b73 1557
15d4b8cd 1558 wxString file;
2b813b73
VZ
1559 size_t nDirs = dirs.GetCount();
1560 for ( size_t nDir = 0; nDir < nDirs; nDir++ )
1561 {
15d4b8cd
RR
1562 file = dirs[nDir];
1563 file += wxT("mailcap");
dfea7acc
DS
1564 if ( wxFile::Exists(file) )
1565 {
2b813b73
VZ
1566 ReadMailcap(file);
1567 }
1568
15d4b8cd
RR
1569 file = dirs[nDir];
1570 file += wxT("mime.types");
dfea7acc 1571 if ( wxFile::Exists(file) )
2b813b73 1572 ReadMimeTypes(file);
2b813b73 1573 }
2b813b73
VZ
1574}
1575
dfea7acc 1576bool wxMimeTypesManagerImpl::WriteToMimeTypes(int index, bool delete_index)
2b813b73
VZ
1577{
1578 // check we have the right manager
a06c1b9b 1579 if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD) )
d0ee33f5 1580 return false;
2b813b73
VZ
1581
1582 bool bTemp;
1583 wxString strHome = wxGetenv(wxT("HOME"));
1584
1585 // and now the users mailcap
1586 wxString strUserMailcap = strHome + wxT("/.mime.types");
1587
1588 wxMimeTextFile file;
1589 if ( wxFile::Exists(strUserMailcap) )
1590 {
7c2e5dec 1591 bTemp = file.Open(strUserMailcap);
2b813b73
VZ
1592 }
1593 else
1594 {
dfea7acc
DS
1595 if (delete_index)
1596 return false;
1597
2b813b73
VZ
1598 bTemp = file.Create(strUserMailcap);
1599 }
dfea7acc 1600
2b813b73
VZ
1601 if (bTemp)
1602 {
1603 int nIndex;
d0ee33f5 1604 // test for netscape's header and return false if its found
dfea7acc 1605 nIndex = file.pIndexOf(wxT("#--Netscape"));
2b813b73
VZ
1606 if (nIndex != wxNOT_FOUND)
1607 {
4362c705 1608 wxFAIL_MSG(wxT("Error in .mime.types\nTrying to mix Netscape and Metamail formats\nFile not modified"));
d0ee33f5 1609 return false;
2b813b73 1610 }
dfea7acc 1611
2b813b73
VZ
1612 // write it in alternative format
1613 // get rid of unwanted entries
1614 wxString strType = m_aTypes[index];
dfea7acc
DS
1615 nIndex = file.pIndexOf(strType);
1616
2b813b73 1617 // get rid of all the unwanted entries...
dfea7acc
DS
1618 if (nIndex != wxNOT_FOUND)
1619 file.CommentLine(nIndex);
2b813b73
VZ
1620
1621 if (!delete_index)
1622 {
1623 // add the new entries in
7c2e5dec 1624 wxString sTmp = strType.Append( wxT(' '), 40 - strType.Len() );
15d4b8cd 1625 sTmp += m_aExtensions[index];
dfea7acc 1626 file.AddLine(sTmp);
2b813b73
VZ
1627 }
1628
dfea7acc
DS
1629 bTemp = file.Write();
1630 file.Close();
2b813b73 1631 }
dfea7acc 1632
2b813b73
VZ
1633 return bTemp;
1634}
1635
dfea7acc 1636bool wxMimeTypesManagerImpl::WriteToNSMimeTypes(int index, bool delete_index)
2b813b73
VZ
1637{
1638 //check we have the right managers
1639 if (! ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) )
d0ee33f5 1640 return false;
2b813b73
VZ
1641
1642 bool bTemp;
1643 wxString strHome = wxGetenv(wxT("HOME"));
1644
1645 // and now the users mailcap
1646 wxString strUserMailcap = strHome + wxT("/.mime.types");
1647
1648 wxMimeTextFile file;
1649 if ( wxFile::Exists(strUserMailcap) )
1650 {
7c2e5dec 1651 bTemp = file.Open(strUserMailcap);
2b813b73
VZ
1652 }
1653 else
1654 {
dfea7acc
DS
1655 if (delete_index)
1656 return false;
1657
2b813b73
VZ
1658 bTemp = file.Create(strUserMailcap);
1659 }
dfea7acc 1660
2b813b73
VZ
1661 if (bTemp)
1662 {
2b813b73
VZ
1663 // write it in the format that Netscape uses
1664 int nIndex;
1665 // test for netscape's header and insert if required...
d0ee33f5 1666 // this is a comment so use true
dfea7acc 1667 nIndex = file.pIndexOf(wxT("#--Netscape"), true);
2b813b73
VZ
1668 if (nIndex == wxNOT_FOUND)
1669 {
1670 // either empty file or metamail format
1671 // at present we can't cope with mixed formats, so exit to preseve
1672 // metamail entreies
dfea7acc 1673 if (file.GetLineCount() > 0)
2b813b73 1674 {
4362c705 1675 wxFAIL_MSG(wxT(".mime.types File not in Netscape format\nNo entries written to\n.mime.types or to .mailcap"));
d0ee33f5 1676 return false;
2b813b73 1677 }
dfea7acc
DS
1678
1679 file.InsertLine(wxT( "#--Netscape Communications Corporation MIME Information" ), 0);
2b813b73
VZ
1680 nIndex = 0;
1681 }
1682
1683 wxString strType = wxT("type=") + m_aTypes[index];
dfea7acc
DS
1684 nIndex = file.pIndexOf(strType);
1685
2b813b73
VZ
1686 // get rid of all the unwanted entries...
1687 if (nIndex != wxNOT_FOUND)
1688 {
1689 wxString sOld = file[nIndex];
1690 while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
1691 {
1692 file.CommentLine(nIndex);
1693 sOld = file[nIndex];
dfea7acc 1694
2b813b73 1695 wxLogTrace(TRACE_MIME, wxT("--- Deleting from mime.types line '%d %s' ---"), nIndex, sOld.c_str());
dfea7acc
DS
1696
1697 nIndex++;
2b813b73 1698 }
dfea7acc
DS
1699
1700 if (nIndex < (int) file.GetLineCount())
1701 file.CommentLine(nIndex);
2b813b73 1702 }
dfea7acc
DS
1703 else
1704 nIndex = (int) file.GetLineCount();
2b813b73
VZ
1705
1706 wxString sTmp = strType + wxT(" \\");
dfea7acc
DS
1707 if (!delete_index)
1708 file.InsertLine(sTmp, nIndex);
1709
678ebfcd 1710 if ( ! m_aDescriptions.Item(index).empty() )
2b813b73 1711 {
dfea7acc 1712 sTmp = wxT("desc=\"") + m_aDescriptions[index]+ wxT("\" \\"); //.trim ??
2b813b73
VZ
1713 if (!delete_index)
1714 {
7c2e5dec 1715 nIndex++;
dfea7acc 1716 file.InsertLine(sTmp, nIndex);
2b813b73
VZ
1717 }
1718 }
dfea7acc 1719
7c2e5dec 1720 wxString sExts = m_aExtensions.Item(index);
dfea7acc 1721 sTmp = wxT("exts=\"") + sExts.Trim(false).Trim() + wxT("\"");
2b813b73
VZ
1722 if (!delete_index)
1723 {
7c2e5dec 1724 nIndex++;
dfea7acc 1725 file.InsertLine(sTmp, nIndex);
2b813b73
VZ
1726 }
1727
dfea7acc
DS
1728 bTemp = file.Write();
1729 file.Close();
2b813b73 1730 }
dfea7acc 1731
2b813b73
VZ
1732 return bTemp;
1733}
1734
dfea7acc 1735bool wxMimeTypesManagerImpl::WriteToMailCap(int index, bool delete_index)
2b813b73
VZ
1736{
1737 //check we have the right managers
1738 if ( !( ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE) ||
a06c1b9b 1739 ( m_mailcapStylesInited & wxMAILCAP_STANDARD) ) )
d0ee33f5 1740 return false;
2b813b73 1741
7c2e5dec 1742 bool bTemp = false;
2b813b73
VZ
1743 wxString strHome = wxGetenv(wxT("HOME"));
1744
1745 // and now the users mailcap
1746 wxString strUserMailcap = strHome + wxT("/.mailcap");
1747
1748 wxMimeTextFile file;
1749 if ( wxFile::Exists(strUserMailcap) )
1750 {
7c2e5dec 1751 bTemp = file.Open(strUserMailcap);
2b813b73 1752 }
be0a33fb 1753 else
2b813b73 1754 {
dfea7acc
DS
1755 if (delete_index)
1756 return false;
1757
2b813b73
VZ
1758 bTemp = file.Create(strUserMailcap);
1759 }
dfea7acc 1760
2b813b73
VZ
1761 if (bTemp)
1762 {
1763 // now got a file we can write to ....
678ebfcd
VZ
1764 wxMimeTypeCommands * entries = m_aEntries[index];
1765 size_t iOpen;
dfea7acc 1766 wxString sCmd = entries->GetCommandForVerb(wxT("open"), &iOpen);
2b813b73
VZ
1767 wxString sTmp;
1768
1769 sTmp = m_aTypes[index];
1770 wxString sOld;
1771 int nIndex = file.pIndexOf(sTmp);
7c2e5dec 1772
2b813b73
VZ
1773 // get rid of all the unwanted entries...
1774 if (nIndex == wxNOT_FOUND)
1775 {
1776 nIndex = (int) file.GetLineCount();
1777 }
1778 else
1779 {
1780 sOld = file[nIndex];
1781 wxLogTrace(TRACE_MIME, wxT("--- Deleting from mailcap line '%d' ---"), nIndex);
6dc6fda6 1782
2b813b73
VZ
1783 while ( (sOld.Contains(wxT("\\"))) && (nIndex < (int) file.GetLineCount()) )
1784 {
1785 file.CommentLine(nIndex);
dfea7acc
DS
1786 if (nIndex < (int) file.GetLineCount())
1787 sOld = sOld + file[nIndex];
2b813b73 1788 }
7c2e5dec 1789
dfea7acc
DS
1790 if (nIndex < (int)
1791 file.GetLineCount()) file.CommentLine(nIndex);
2b813b73 1792 }
6dc6fda6 1793
15d4b8cd 1794 sTmp += wxT(";") + sCmd; //includes wxT(" %s ");
b9517a0a 1795
2b813b73 1796 // write it in the format that Netscape uses (default)
7c2e5dec 1797 if (! ( m_mailcapStylesInited & wxMAILCAP_STANDARD ) )
2b813b73 1798 {
dfea7acc
DS
1799 if (! delete_index)
1800 file.InsertLine(sTmp, nIndex);
1801 nIndex++;
2b813b73 1802 }
2b813b73
VZ
1803 else
1804 {
dfea7acc
DS
1805 // write extended format
1806
1807 // TODO - FIX this code:
2b813b73
VZ
1808 // ii) lost entries
1809 // sOld holds all the entries, but our data store only has some
1810 // eg test= is not stored
1811
1812 // so far we have written the mimetype and command out
dfea7acc
DS
1813 wxStringTokenizer sT(sOld, wxT(";\\"));
1814 if (sT.CountTokens() > 2)
2b813b73
VZ
1815 {
1816 // first one mimetype; second one command, rest unknown...
1817 wxString s;
1818 s = sT.GetNextToken();
1819 s = sT.GetNextToken();
1820
1821 // first unknown
1822 s = sT.GetNextToken();
678ebfcd 1823 while ( ! s.empty() )
2b813b73 1824 {
d0ee33f5 1825 bool bKnownToken = false;
dfea7acc
DS
1826 if (s.Contains(wxT("description=")))
1827 bKnownToken = true;
1828 if (s.Contains(wxT("x11-bitmap=")))
1829 bKnownToken = true;
7c2e5dec 1830
2b813b73 1831 size_t i;
15d4b8cd
RR
1832 size_t nCount = entries->GetCount();
1833 for (i=0; i < nCount; i++)
2b813b73 1834 {
dfea7acc
DS
1835 if (s.Contains(entries->GetVerb(i)))
1836 bKnownToken = true;
2b813b73 1837 }
dfea7acc 1838
2b813b73
VZ
1839 if (!bKnownToken)
1840 {
15d4b8cd 1841 sTmp += wxT("; \\");
dfea7acc 1842 file.InsertLine(sTmp, nIndex);
2b813b73
VZ
1843 sTmp = s;
1844 }
cdf339c9 1845
dfea7acc
DS
1846 s = sT.GetNextToken();
1847 }
2b813b73 1848 }
5bd3a2da 1849
678ebfcd 1850 if (! m_aDescriptions[index].empty() )
2b813b73 1851 {
15d4b8cd 1852 sTmp += wxT("; \\");
dfea7acc 1853 file.InsertLine(sTmp, nIndex);
7c2e5dec 1854 nIndex++;
2b813b73
VZ
1855 sTmp = wxT(" description=\"") + m_aDescriptions[index] + wxT("\"");
1856 }
cdf339c9 1857
678ebfcd 1858 if (! m_aIcons[index].empty() )
2b813b73 1859 {
15d4b8cd 1860 sTmp += wxT("; \\");
dfea7acc 1861 file.InsertLine(sTmp, nIndex);
7c2e5dec 1862 nIndex++;
2b813b73
VZ
1863 sTmp = wxT(" x11-bitmap=\"") + m_aIcons[index] + wxT("\"");
1864 }
cdf339c9 1865
dfea7acc 1866 if ( entries->GetCount() > 1 )
2b813b73
VZ
1867 {
1868 size_t i;
1869 for (i=0; i < entries->GetCount(); i++)
1870 if ( i != iOpen )
1871 {
15d4b8cd 1872 sTmp += wxT("; \\");
dfea7acc 1873 file.InsertLine(sTmp, nIndex);
7c2e5dec 1874 nIndex++;
678ebfcd 1875 sTmp = wxT(" ") + entries->GetVerbCmd(i);
2b813b73
VZ
1876 }
1877 }
b9517a0a 1878
dfea7acc
DS
1879 file.InsertLine(sTmp, nIndex);
1880 nIndex++;
b13d92d1 1881 }
dfea7acc
DS
1882
1883 bTemp = file.Write();
1884 file.Close();
b13d92d1 1885 }
dfea7acc 1886
2b813b73 1887 return bTemp;
b13d92d1
VZ
1888}
1889
dfea7acc 1890wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
b9517a0a 1891{
2b813b73 1892 InitIfNeeded();
b9517a0a 1893
dfea7acc
DS
1894 wxString strType = ftInfo.GetMimeType();
1895 wxString strDesc = ftInfo.GetDescription();
1896 wxString strIcon = ftInfo.GetIconFile();
2b813b73 1897
dfea7acc 1898 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2b813b73 1899
678ebfcd 1900 if ( ! ftInfo.GetOpenCommand().empty())
dfea7acc
DS
1901 entry->Add(wxT("open=") + ftInfo.GetOpenCommand() + wxT(" %s "));
1902 if ( ! ftInfo.GetPrintCommand().empty())
1903 entry->Add(wxT("print=") + ftInfo.GetPrintCommand() + wxT(" %s "));
2b813b73
VZ
1904
1905 // now find where these extensions are in the data store and remove them
dfea7acc 1906 wxArrayString sA_Exts = ftInfo.GetExtensions();
2b813b73
VZ
1907 wxString sExt, sExtStore;
1908 size_t i, nIndex;
15d4b8cd
RR
1909 size_t nExtCount = sA_Exts.GetCount();
1910 for (i=0; i < nExtCount; i++)
dfea7acc 1911 {
2b813b73 1912 sExt = sA_Exts.Item(i);
dfea7acc
DS
1913
1914 // clean up to just a space before and after
d0ee33f5 1915 sExt.Trim().Trim(false);
2b813b73 1916 sExt = wxT(' ') + sExt + wxT(' ');
15d4b8cd
RR
1917 size_t nCount = m_aExtensions.GetCount();
1918 for (nIndex = 0; nIndex < nCount; nIndex++)
dfea7acc 1919 {
2b813b73 1920 sExtStore = m_aExtensions.Item(nIndex);
dfea7acc
DS
1921 if (sExtStore.Replace(sExt, wxT(" ") ) > 0)
1922 m_aExtensions.Item(nIndex) = sExtStore;
1923 }
2b813b73 1924 }
b9517a0a 1925
dfea7acc 1926 if ( !DoAssociation(strType, strIcon, entry, sA_Exts, strDesc) )
2b813b73 1927 return NULL;
4d2976ad 1928
2b813b73 1929 return GetFileTypeFromMimeType(strType);
4d2976ad
VS
1930}
1931
2b813b73
VZ
1932bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
1933 const wxString& strIcon,
678ebfcd 1934 wxMimeTypeCommands *entry,
2b813b73
VZ
1935 const wxArrayString& strExtensions,
1936 const wxString& strDesc)
b13d92d1 1937{
d0ee33f5 1938 int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
7520f3da 1939
2b813b73 1940 if ( nIndex == wxNOT_FOUND )
d0ee33f5 1941 return false;
b13d92d1 1942
dfea7acc 1943 return WriteMimeInfo(nIndex, false);
b13d92d1
VZ
1944}
1945
2b813b73 1946bool wxMimeTypesManagerImpl::WriteMimeInfo(int nIndex, bool delete_mime )
b13d92d1 1947{
d0ee33f5 1948 bool ok = true;
b13d92d1 1949
a06c1b9b 1950 if ( m_mailcapStylesInited & wxMAILCAP_STANDARD )
2b813b73
VZ
1951 {
1952 // write in metamail format;
dfea7acc
DS
1953 if (WriteToMimeTypes(nIndex, delete_mime) )
1954 if ( WriteToMailCap(nIndex, delete_mime) )
d0ee33f5 1955 ok = false;
b13d92d1 1956 }
dfea7acc 1957
2b813b73 1958 if ( m_mailcapStylesInited & wxMAILCAP_NETSCAPE )
b9517a0a 1959 {
2b813b73 1960 // write in netsacpe format;
dfea7acc
DS
1961 if (WriteToNSMimeTypes(nIndex, delete_mime) )
1962 if ( WriteToMailCap(nIndex, delete_mime) )
d0ee33f5 1963 ok = false;
2b813b73 1964 }
dfea7acc 1965
2b850ae1
RR
1966 // Don't write GNOME files here as this is not
1967 // allowed and simply doesn't work
dfea7acc 1968
2b813b73
VZ
1969 if (m_mailcapStylesInited & wxMAILCAP_KDE)
1970 {
1971 // write in KDE format;
dfea7acc 1972 if (WriteKDEMimeFile(nIndex, delete_mime) )
d0ee33f5 1973 ok = false;
b9517a0a
VZ
1974 }
1975
2b813b73 1976 return ok;
a6c65e88
VZ
1977}
1978
2b813b73
VZ
1979int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
1980 const wxString& strIcon,
678ebfcd 1981 wxMimeTypeCommands *entry,
2b813b73
VZ
1982 const wxArrayString& strExtensions,
1983 const wxString& strDesc,
678ebfcd 1984 bool replaceExisting)
b13d92d1 1985{
2b813b73 1986 InitIfNeeded();
b13d92d1 1987
2b813b73 1988 // ensure mimetype is always lower case
678ebfcd
VZ
1989 wxString mimeType = strType.Lower();
1990
1991 // is this a known MIME type?
2b813b73
VZ
1992 int nIndex = m_aTypes.Index(mimeType);
1993 if ( nIndex == wxNOT_FOUND )
1994 {
1995 // new file type
1996 m_aTypes.Add(mimeType);
1997 m_aIcons.Add(strIcon);
678ebfcd 1998 m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
b13d92d1 1999
678ebfcd 2000 // change nIndex so we can use it below to add the extensions
df5168c4 2001 m_aExtensions.Add(wxEmptyString);
b8d61021 2002 nIndex = m_aExtensions.size() - 1;
678ebfcd
VZ
2003
2004 m_aDescriptions.Add(strDesc);
b13d92d1 2005 }
678ebfcd 2006 else // yes, we already have it
2b813b73 2007 {
678ebfcd 2008 if ( replaceExisting )
2b813b73
VZ
2009 {
2010 // if new description change it
678ebfcd 2011 if ( !strDesc.empty())
2b813b73
VZ
2012 m_aDescriptions[nIndex] = strDesc;
2013
2014 // if new icon change it
678ebfcd 2015 if ( !strIcon.empty())
2b813b73
VZ
2016 m_aIcons[nIndex] = strIcon;
2017
678ebfcd
VZ
2018 if ( entry )
2019 {
2020 delete m_aEntries[nIndex];
2021 m_aEntries[nIndex] = entry;
2022 }
2b813b73 2023 }
678ebfcd 2024 else // add data we don't already have ...
2b813b73 2025 {
2b813b73 2026 // if new description add only if none
678ebfcd 2027 if ( m_aDescriptions[nIndex].empty() )
2b813b73
VZ
2028 m_aDescriptions[nIndex] = strDesc;
2029
2030 // if new icon and no existing icon
dfea7acc 2031 if ( m_aIcons[nIndex].empty() )
2b813b73
VZ
2032 m_aIcons[nIndex] = strIcon;
2033
2b813b73 2034 // add any new entries...
678ebfcd 2035 if ( entry )
2b813b73 2036 {
7c2e5dec 2037 wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
678ebfcd
VZ
2038
2039 size_t count = entry->GetCount();
2040 for ( size_t i = 0; i < count; i++ )
2041 {
2042 const wxString& verb = entry->GetVerb(i);
2043 if ( !entryOld->HasVerb(verb) )
2044 {
2045 entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
2046 }
2047 }
2b293fc7
VZ
2048
2049 // as we don't store it anywhere, it won't be deleted later as
2050 // usual -- do it immediately instead
2051 delete entry;
2b813b73
VZ
2052 }
2053 }
b13d92d1 2054 }
4d2976ad 2055
678ebfcd
VZ
2056 // always add the extensions to this mimetype
2057 wxString& exts = m_aExtensions[nIndex];
2058
2059 // add all extensions we don't have yet
15d4b8cd 2060 wxString ext;
678ebfcd
VZ
2061 size_t count = strExtensions.GetCount();
2062 for ( size_t i = 0; i < count; i++ )
2063 {
15d4b8cd
RR
2064 ext = strExtensions[i];
2065 ext += wxT(' ');
678ebfcd
VZ
2066
2067 if ( exts.Find(ext) == wxNOT_FOUND )
2068 {
2069 exts += ext;
2070 }
2071 }
2072
2b813b73
VZ
2073 // check data integrity
2074 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
678ebfcd
VZ
2075 m_aTypes.Count() == m_aExtensions.Count() &&
2076 m_aTypes.Count() == m_aIcons.Count() &&
2077 m_aTypes.Count() == m_aDescriptions.Count() );
cf471cab 2078
2b813b73 2079 return nIndex;
cf471cab
VS
2080}
2081
dfea7acc 2082wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
b13d92d1 2083{
678ebfcd 2084 if (ext.empty() )
2b813b73
VZ
2085 return NULL;
2086
a6c65e88
VZ
2087 InitIfNeeded();
2088
1ee17e1c 2089 size_t count = m_aExtensions.GetCount();
2b813b73 2090 for ( size_t n = 0; n < count; n++ )
678ebfcd 2091 {
dfea7acc 2092 wxStringTokenizer tk(m_aExtensions[n], wxT(' '));
1ee17e1c 2093
678ebfcd
VZ
2094 while ( tk.HasMoreTokens() )
2095 {
1ee17e1c 2096 // consider extensions as not being case-sensitive
d0ee33f5 2097 if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
678ebfcd 2098 {
1ee17e1c 2099 // found
678ebfcd 2100 wxFileType *fileType = new wxFileType;
1ee17e1c 2101 fileType->m_impl->Init(this, n);
678ebfcd
VZ
2102
2103 return fileType;
1ee17e1c
VZ
2104 }
2105 }
2106 }
b13d92d1 2107
678ebfcd 2108 return NULL;
b13d92d1
VZ
2109}
2110
7c2e5dec 2111wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
b13d92d1 2112{
a6c65e88
VZ
2113 InitIfNeeded();
2114
2b813b73 2115 wxFileType * fileType = NULL;
b13d92d1
VZ
2116 // mime types are not case-sensitive
2117 wxString mimetype(mimeType);
2118 mimetype.MakeLower();
2119
2120 // first look for an exact match
2121 int index = m_aTypes.Index(mimetype);
2b813b73
VZ
2122 if ( index != wxNOT_FOUND )
2123 {
2124 fileType = new wxFileType;
2125 fileType->m_impl->Init(this, index);
2126 }
2127
2128 // then try to find "text/*" as match for "text/plain" (for example)
2129 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
2130 // the whole string - ok.
2131
2132 index = wxNOT_FOUND;
2133 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
2134
2135 size_t nCount = m_aTypes.Count();
dfea7acc
DS
2136 for ( size_t n = 0; n < nCount; n++ )
2137 {
2b813b73 2138 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
dfea7acc
DS
2139 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") )
2140 {
2b813b73
VZ
2141 index = n;
2142 break;
b13d92d1
VZ
2143 }
2144 }
2145
2b813b73 2146 if ( index != wxNOT_FOUND )
dfea7acc
DS
2147 {
2148 // don't throw away fileType that was already found
2149 if (!fileType)
7bc5ddc6 2150 fileType = new wxFileType;
b13d92d1 2151 fileType->m_impl->Init(this, index);
b13d92d1 2152 }
dfea7acc 2153
2b813b73
VZ
2154 return fileType;
2155}
2156
2b813b73
VZ
2157wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
2158{
2159 wxString command, testcmd, sV, sTmp;
2160 sV = verb + wxT("=");
dfea7acc 2161
2b813b73 2162 // list of verb = command pairs for this mimetype
678ebfcd 2163 wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
2b813b73
VZ
2164
2165 size_t i;
15d4b8cd
RR
2166 size_t nCount = sPairs->GetCount();
2167 for ( i = 0; i < nCount; i++ )
2b813b73 2168 {
678ebfcd
VZ
2169 sTmp = sPairs->GetVerbCmd (i);
2170 if ( sTmp.Contains(sV) )
2171 command = sTmp.AfterFirst(wxT('='));
b13d92d1 2172 }
dfea7acc 2173
2b813b73 2174 return command;
b13d92d1
VZ
2175}
2176
8e124873
VZ
2177void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
2178{
a6c65e88
VZ
2179 InitIfNeeded();
2180
3f1aaa16 2181 wxString extensions;
8e124873
VZ
2182 const wxArrayString& exts = filetype.GetExtensions();
2183 size_t nExts = exts.GetCount();
dfea7acc
DS
2184 for ( size_t nExt = 0; nExt < nExts; nExt++ )
2185 {
2186 if ( nExt > 0 )
223d09f6 2187 extensions += wxT(' ');
dfea7acc 2188
8e124873
VZ
2189 extensions += exts[nExt];
2190 }
2191
2192 AddMimeTypeInfo(filetype.GetMimeType(),
2193 extensions,
2194 filetype.GetDescription());
2195
2196 AddMailcapInfo(filetype.GetMimeType(),
2197 filetype.GetOpenCommand(),
2198 filetype.GetPrintCommand(),
223d09f6 2199 wxT(""),
8e124873
VZ
2200 filetype.GetDescription());
2201}
2202
2203void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
2204 const wxString& strExtensions,
2205 const wxString& strDesc)
2206{
2b813b73
VZ
2207 // reading mailcap may find image/* , while
2208 // reading mime.types finds image/gif and no match is made
2209 // this means all the get functions don't work fix this
2210 wxString strIcon;
2211 wxString sTmp = strExtensions;
a6c65e88 2212
2b813b73 2213 wxArrayString sExts;
d0ee33f5 2214 sTmp.Trim().Trim(false);
2b813b73 2215
678ebfcd 2216 while (!sTmp.empty())
2b813b73 2217 {
dfea7acc 2218 sExts.Add(sTmp.AfterLast(wxT(' ')));
2b813b73 2219 sTmp = sTmp.BeforeLast(wxT(' '));
8e124873 2220 }
2b813b73 2221
dfea7acc 2222 AddToMimeData(strMimeType, strIcon, NULL, sExts, strDesc, true);
8e124873
VZ
2223}
2224
2225void wxMimeTypesManagerImpl::AddMailcapInfo(const wxString& strType,
2226 const wxString& strOpenCmd,
2227 const wxString& strPrintCmd,
2228 const wxString& strTest,
2229 const wxString& strDesc)
2230{
a6c65e88
VZ
2231 InitIfNeeded();
2232
678ebfcd 2233 wxMimeTypeCommands *entry = new wxMimeTypeCommands;
2b813b73
VZ
2234 entry->Add(wxT("open=") + strOpenCmd);
2235 entry->Add(wxT("print=") + strPrintCmd);
2236 entry->Add(wxT("test=") + strTest);
8e124873 2237
2b813b73
VZ
2238 wxString strIcon;
2239 wxArrayString strExtensions;
2240
dfea7acc 2241 AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
8e124873
VZ
2242}
2243
cc385968 2244bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
b13d92d1 2245{
f6bcfd97
BP
2246 wxLogTrace(TRACE_MIME, wxT("--- Parsing mime.types file '%s' ---"),
2247 strFileName.c_str());
b13d92d1
VZ
2248
2249 wxTextFile file(strFileName);
2b5f62a0 2250#if defined(__WXGTK20__) && wxUSE_UNICODE
7c2e5dec 2251 if ( !file.Open(wxConvUTF8) )
2b5f62a0 2252#else
b13d92d1 2253 if ( !file.Open() )
2b5f62a0 2254#endif
d0ee33f5 2255 return false;
b13d92d1
VZ
2256
2257 // the information we extract
2258 wxString strMimeType, strDesc, strExtensions;
2259
2260 size_t nLineCount = file.GetLineCount();
50920146 2261 const wxChar *pc = NULL;
2b5f62a0
VZ
2262 for ( size_t nLine = 0; nLine < nLineCount; nLine++ )
2263 {
dfea7acc
DS
2264 if ( pc == NULL )
2265 {
22b4634c
VZ
2266 // now we're at the start of the line
2267 pc = file[nLine].c_str();
2268 }
dfea7acc
DS
2269 else
2270 {
22b4634c
VZ
2271 // we didn't finish with the previous line yet
2272 nLine--;
2273 }
b13d92d1
VZ
2274
2275 // skip whitespace
50920146 2276 while ( wxIsspace(*pc) )
b13d92d1
VZ
2277 pc++;
2278
54acce90 2279 // comment or blank line?
dfea7acc
DS
2280 if ( *pc == wxT('#') || !*pc )
2281 {
22b4634c
VZ
2282 // skip the whole line
2283 pc = NULL;
b13d92d1 2284 continue;
22b4634c 2285 }
b13d92d1
VZ
2286
2287 // detect file format
223d09f6 2288 const wxChar *pEqualSign = wxStrchr(pc, wxT('='));
dfea7acc
DS
2289 if ( pEqualSign == NULL )
2290 {
b13d92d1
VZ
2291 // brief format
2292 // ------------
2293
2294 // first field is mime type
dfea7acc
DS
2295 for ( strMimeType.Empty(); !wxIsspace(*pc) && *pc != wxT('\0'); pc++ )
2296 {
b13d92d1
VZ
2297 strMimeType += *pc;
2298 }
2299
2300 // skip whitespace
50920146 2301 while ( wxIsspace(*pc) )
b13d92d1
VZ
2302 pc++;
2303
2304 // take all the rest of the string
2305 strExtensions = pc;
2306
2307 // no description...
2308 strDesc.Empty();
2309 }
dfea7acc
DS
2310 else
2311 {
b13d92d1
VZ
2312 // expanded format
2313 // ---------------
2314
2315 // the string on the left of '=' is the field name
2316 wxString strLHS(pc, pEqualSign - pc);
2317
2318 // eat whitespace
50920146 2319 for ( pc = pEqualSign + 1; wxIsspace(*pc); pc++ )
678ebfcd 2320 ;
b13d92d1 2321
50920146 2322 const wxChar *pEnd;
dfea7acc
DS
2323 if ( *pc == wxT('"') )
2324 {
b13d92d1 2325 // the string is quoted and ends at the matching quote
223d09f6 2326 pEnd = wxStrchr(++pc, wxT('"'));
dfea7acc
DS
2327 if ( pEnd == NULL )
2328 {
5d0d3e6d 2329 wxLogWarning(wxT("Mime.types file %s, line %lu: unterminated quoted string."),
985f824c 2330 strFileName.c_str(), nLine + 1L);
b13d92d1
VZ
2331 }
2332 }
dfea7acc
DS
2333 else
2334 {
8862e11b
VZ
2335 // unquoted string ends at the first space or at the end of
2336 // line
2337 for ( pEnd = pc; *pEnd && !wxIsspace(*pEnd); pEnd++ )
678ebfcd 2338 ;
b13d92d1
VZ
2339 }
2340
2341 // now we have the RHS (field value)
2342 wxString strRHS(pc, pEnd - pc);
2343
22b4634c 2344 // check what follows this entry
dfea7acc
DS
2345 if ( *pEnd == wxT('"') )
2346 {
b13d92d1
VZ
2347 // skip this quote
2348 pEnd++;
2349 }
2350
50920146 2351 for ( pc = pEnd; wxIsspace(*pc); pc++ )
678ebfcd 2352 ;
b13d92d1 2353
22b4634c
VZ
2354 // if there is something left, it may be either a '\\' to continue
2355 // the line or the next field of the same entry
dfea7acc
DS
2356 bool entryEnded = *pc == wxT('\0');
2357 bool nextFieldOnSameLine = false;
2358 if ( !entryEnded )
2359 {
223d09f6 2360 nextFieldOnSameLine = ((*pc != wxT('\\')) || (pc[1] != wxT('\0')));
b13d92d1 2361 }
b13d92d1
VZ
2362
2363 // now see what we got
dfea7acc
DS
2364 if ( strLHS == wxT("type") )
2365 {
b13d92d1
VZ
2366 strMimeType = strRHS;
2367 }
dfea7acc
DS
2368 else if ( strLHS.StartsWith(wxT("desc")) )
2369 {
b13d92d1
VZ
2370 strDesc = strRHS;
2371 }
dfea7acc
DS
2372 else if ( strLHS == wxT("exts") )
2373 {
b13d92d1
VZ
2374 strExtensions = strRHS;
2375 }
dfea7acc 2376 else if ( strLHS == wxT("icon") )
70687b63 2377 {
a2717c3d
VZ
2378 // this one is simply ignored: it usually refers to Netscape
2379 // built in icons which are useless for us anyhow
70687b63 2380 }
dfea7acc 2381 else if ( !strLHS.StartsWith(wxT("x-")) )
70687b63
VZ
2382 {
2383 // we suppose that all fields starting with "X-" are
2384 // unregistered extensions according to the standard practice,
2385 // but it may be worth telling the user about other junk in
2386 // his mime.types file
5d0d3e6d 2387 wxLogWarning(wxT("Unknown field in file %s, line %lu: '%s'."),
985f824c 2388 strFileName.c_str(), nLine + 1L, strLHS.c_str());
b13d92d1
VZ
2389 }
2390
dfea7acc
DS
2391 if ( !entryEnded )
2392 {
22b4634c
VZ
2393 if ( !nextFieldOnSameLine )
2394 pc = NULL;
2395 //else: don't reset it
2396
2397 // as we don't reset strMimeType, the next field in this entry
b13d92d1 2398 // will be interpreted correctly.
22b4634c 2399
b13d92d1
VZ
2400 continue;
2401 }
2402 }
2403
a6c65e88
VZ
2404 // depending on the format (Mosaic or Netscape) either space or comma
2405 // is used to separate the extensions
223d09f6 2406 strExtensions.Replace(wxT(","), wxT(" "));
a1d8eaf7
VZ
2407
2408 // also deal with the leading dot
678ebfcd 2409 if ( !strExtensions.empty() && strExtensions[0u] == wxT('.') )
1b986aef 2410 {
a1d8eaf7
VZ
2411 strExtensions.erase(0, 1);
2412 }
2413
678ebfcd
VZ
2414 wxLogTrace(TRACE_MIME, wxT("mime.types: '%s' => '%s' (%s)"),
2415 strExtensions.c_str(),
2416 strMimeType.c_str(),
2417 strDesc.c_str());
2b813b73 2418
8e124873 2419 AddMimeTypeInfo(strMimeType, strExtensions, strDesc);
22b4634c
VZ
2420
2421 // finished with this line
2422 pc = NULL;
b13d92d1
VZ
2423 }
2424
d0ee33f5 2425 return true;
b13d92d1
VZ
2426}
2427
678ebfcd
VZ
2428// ----------------------------------------------------------------------------
2429// UNIX mailcap files parsing
2430// ----------------------------------------------------------------------------
2431
2432// the data for a single MIME type
2433struct MailcapLineData
2434{
2435 // field values
2436 wxString type,
2437 cmdOpen,
2438 test,
2439 icon,
2440 desc;
2441
2442 wxArrayString verbs,
2443 commands;
2444
2445 // flags
2446 bool testfailed,
2447 needsterminal,
2448 copiousoutput;
2449
d0ee33f5 2450 MailcapLineData() { testfailed = needsterminal = copiousoutput = false; }
678ebfcd
VZ
2451};
2452
2453// process a non-standard (i.e. not the first or second one) mailcap field
2454bool
2455wxMimeTypesManagerImpl::ProcessOtherMailcapField(MailcapLineData& data,
2456 const wxString& curField)
2457{
2458 if ( curField.empty() )
2459 {
2460 // we don't care
d0ee33f5 2461 return true;
678ebfcd
VZ
2462 }
2463
2464 // is this something of the form foo=bar?
2465 const wxChar *pEq = wxStrchr(curField, wxT('='));
2466 if ( pEq != NULL )
2467 {
2468 // split "LHS = RHS" in 2
2469 wxString lhs = curField.BeforeFirst(wxT('=')),
2470 rhs = curField.AfterFirst(wxT('='));
2471
d0ee33f5
WS
2472 lhs.Trim(true); // from right
2473 rhs.Trim(false); // from left
678ebfcd
VZ
2474
2475 // it might be quoted
2476 if ( !rhs.empty() && rhs[0u] == wxT('"') && rhs.Last() == wxT('"') )
2477 {
2478 rhs = rhs.Mid(1, rhs.length() - 2);
2479 }
2480
2481 // is it a command verb or something else?
2482 if ( lhs == wxT("test") )
2483 {
2484 if ( wxSystem(rhs) == 0 )
2485 {
2486 // ok, test passed
2487 wxLogTrace(TRACE_MIME_TEST,
2488 wxT("Test '%s' for mime type '%s' succeeded."),
2489 rhs.c_str(), data.type.c_str());
678ebfcd
VZ
2490 }
2491 else
2492 {
2493 wxLogTrace(TRACE_MIME_TEST,
2494 wxT("Test '%s' for mime type '%s' failed, skipping."),
2495 rhs.c_str(), data.type.c_str());
2496
d0ee33f5 2497 data.testfailed = true;
678ebfcd
VZ
2498 }
2499 }
2500 else if ( lhs == wxT("desc") )
2501 {
2502 data.desc = rhs;
2503 }
2504 else if ( lhs == wxT("x11-bitmap") )
2505 {
2506 data.icon = rhs;
2507 }
2508 else if ( lhs == wxT("notes") )
2509 {
2510 // ignore
2511 }
2512 else // not a (recognized) special case, must be a verb (e.g. "print")
2513 {
2514 data.verbs.Add(lhs);
2515 data.commands.Add(rhs);
2516 }
2517 }
2518 else // '=' not found
2519 {
2520 // so it must be a simple flag
2521 if ( curField == wxT("needsterminal") )
2522 {
d0ee33f5 2523 data.needsterminal = true;
678ebfcd
VZ
2524 }
2525 else if ( curField == wxT("copiousoutput"))
2526 {
2527 // copiousoutput impies that the viewer is a console program
2528 data.needsterminal =
d0ee33f5 2529 data.copiousoutput = true;
678ebfcd
VZ
2530 }
2531 else if ( !IsKnownUnimportantField(curField) )
2532 {
d0ee33f5 2533 return false;
678ebfcd
VZ
2534 }
2535 }
2536
d0ee33f5 2537 return true;
678ebfcd
VZ
2538}
2539
cc385968
VZ
2540bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
2541 bool fallback)
b13d92d1 2542{
f6bcfd97
BP
2543 wxLogTrace(TRACE_MIME, wxT("--- Parsing mailcap file '%s' ---"),
2544 strFileName.c_str());
b13d92d1
VZ
2545
2546 wxTextFile file(strFileName);
2b5f62a0 2547#if defined(__WXGTK20__) && wxUSE_UNICODE
7c2e5dec 2548 if ( !file.Open(wxConvUTF8) )
2b5f62a0 2549#else
b13d92d1 2550 if ( !file.Open() )
2b5f62a0 2551#endif
d0ee33f5 2552 return false;
b13d92d1 2553
678ebfcd
VZ
2554 // indices of MIME types (in m_aTypes) we already found in this file
2555 //
2556 // (see the comments near the end of function for the reason we need this)
2557 wxArrayInt aIndicesSeenHere;
2558
2559 // accumulator for the current field
2560 wxString curField;
2561 curField.reserve(1024);
b13d92d1 2562
15d4b8cd
RR
2563 const wxChar *pPagerEnv = wxGetenv(wxT("PAGER"));
2564
2565 const wxArrayString empty_extensions_list;
2566
b13d92d1 2567 size_t nLineCount = file.GetLineCount();
678ebfcd
VZ
2568 for ( size_t nLine = 0; nLine < nLineCount; nLine++ )
2569 {
b13d92d1 2570 // now we're at the start of the line
50920146 2571 const wxChar *pc = file[nLine].c_str();
b13d92d1
VZ
2572
2573 // skip whitespace
50920146 2574 while ( wxIsspace(*pc) )
b13d92d1
VZ
2575 pc++;
2576
2577 // comment or empty string?
223d09f6 2578 if ( *pc == wxT('#') || *pc == wxT('\0') )
b13d92d1
VZ
2579 continue;
2580
2581 // no, do parse
678ebfcd 2582 // ------------
b13d92d1
VZ
2583
2584 // what field are we currently in? The first 2 are fixed and there may
678ebfcd
VZ
2585 // be an arbitrary number of other fields parsed by
2586 // ProcessOtherMailcapField()
2587 //
2588 // the first field is the MIME type
b13d92d1
VZ
2589 enum
2590 {
2591 Field_Type,
2592 Field_OpenCmd,
2593 Field_Other
dfea7acc
DS
2594 }
2595 currentToken = Field_Type;
b13d92d1
VZ
2596
2597 // the flags and field values on the current line
678ebfcd
VZ
2598 MailcapLineData data;
2599
d0ee33f5 2600 bool cont = true;
678ebfcd
VZ
2601 while ( cont )
2602 {
2603 switch ( *pc )
2604 {
223d09f6 2605 case wxT('\\'):
b13d92d1
VZ
2606 // interpret the next character literally (notice that
2607 // backslash can be used for line continuation)
678ebfcd
VZ
2608 if ( *++pc == wxT('\0') )
2609 {
6e358ae7 2610 // fetch the next line if there is one
678ebfcd
VZ
2611 if ( nLine == nLineCount - 1 )
2612 {
6e358ae7 2613 // something is wrong, bail out
d0ee33f5 2614 cont = false;
6e358ae7 2615
76a6e803 2616 wxLogDebug(wxT("Mailcap file %s, line %lu: '\\' on the end of the last line ignored."),
6e358ae7 2617 strFileName.c_str(),
985f824c 2618 nLine + 1L);
6e358ae7 2619 }
678ebfcd
VZ
2620 else
2621 {
6e358ae7
VZ
2622 // pass to the beginning of the next line
2623 pc = file[++nLine].c_str();
2624
2625 // skip pc++ at the end of the loop
2626 continue;
2627 }
b13d92d1 2628 }
678ebfcd
VZ
2629 else
2630 {
b13d92d1
VZ
2631 // just a normal character
2632 curField += *pc;
2633 }
2634 break;
2635
223d09f6 2636 case wxT('\0'):
d0ee33f5 2637 cont = false; // end of line reached, exit the loop
b13d92d1 2638
678ebfcd 2639 // fall through to still process this field
b13d92d1 2640
223d09f6 2641 case wxT(';'):
b13d92d1 2642 // trim whitespaces from both sides
d0ee33f5 2643 curField.Trim(true).Trim(false);
b13d92d1 2644
678ebfcd
VZ
2645 switch ( currentToken )
2646 {
b13d92d1 2647 case Field_Type:
678ebfcd
VZ
2648 data.type = curField.Lower();
2649 if ( data.type.empty() )
2650 {
6e358ae7
VZ
2651 // I don't think that this is a valid mailcap
2652 // entry, but try to interpret it somehow
dfea7acc 2653 data.type = wxT('*');
6e358ae7
VZ
2654 }
2655
678ebfcd
VZ
2656 if ( data.type.Find(wxT('/')) == wxNOT_FOUND )
2657 {
b13d92d1 2658 // we interpret "type" as "type/*"
678ebfcd 2659 data.type += wxT("/*");
b13d92d1
VZ
2660 }
2661
2662 currentToken = Field_OpenCmd;
2663 break;
2664
2665 case Field_OpenCmd:
678ebfcd 2666 data.cmdOpen = curField;
b13d92d1
VZ
2667
2668 currentToken = Field_Other;
2669 break;
2670
2671 case Field_Other:
678ebfcd
VZ
2672 if ( !ProcessOtherMailcapField(data, curField) )
2673 {
2674 // don't flood the user with error messages if
2675 // we don't understand something in his
2676 // mailcap, but give them in debug mode because
2677 // this might be useful for the programmer
2678 wxLogDebug
2679 (
76a6e803 2680 wxT("Mailcap file %s, line %lu: unknown field '%s' for the MIME type '%s' ignored."),
678ebfcd 2681 strFileName.c_str(),
985f824c 2682 nLine + 1L,
678ebfcd
VZ
2683 curField.c_str(),
2684 data.type.c_str()
2685 );
2686 }
2687 else if ( data.testfailed )
2688 {
2689 // skip this entry entirely
d0ee33f5 2690 cont = false;
b13d92d1
VZ
2691 }
2692
2693 // it already has this value
2694 //currentToken = Field_Other;
2695 break;
2696
2697 default:
223d09f6 2698 wxFAIL_MSG(wxT("unknown field type in mailcap"));
b13d92d1
VZ
2699 }
2700
2701 // next token starts immediately after ';'
2702 curField.Empty();
2703 break;
2704
2705 default:
2706 curField += *pc;
2707 }
6e358ae7
VZ
2708
2709 // continue in the same line
2710 pc++;
b13d92d1
VZ
2711 }
2712
678ebfcd
VZ
2713 // we read the entire entry, check what have we got
2714 // ------------------------------------------------
2715
b13d92d1 2716 // check that we really read something reasonable
678ebfcd
VZ
2717 if ( currentToken < Field_Other )
2718 {
5d0d3e6d 2719 wxLogWarning(wxT("Mailcap file %s, line %lu: incomplete entry ignored."),
985f824c 2720 strFileName.c_str(), nLine + 1L);
678ebfcd
VZ
2721
2722 continue;
b13d92d1 2723 }
e1e9ea40 2724
7c2e5dec 2725 // if the test command failed, it's as if the entry were not there at all
678ebfcd
VZ
2726 if ( data.testfailed )
2727 {
2728 continue;
2729 }
2b813b73 2730
678ebfcd
VZ
2731 // support for flags:
2732 // 1. create an xterm for 'needsterminal'
2733 // 2. append "| $PAGER" for 'copiousoutput'
2734 //
2735 // Note that the RFC says that having both needsterminal and
2736 // copiousoutput is probably a mistake, so it seems that running
2737 // programs with copiousoutput inside an xterm as it is done now
2738 // is a bad idea (FIXME)
2739 if ( data.copiousoutput )
2740 {
15d4b8cd 2741 data.cmdOpen << wxT(" | ") << (pPagerEnv ? pPagerEnv : wxT("more"));
678ebfcd 2742 }
b13d92d1 2743
678ebfcd
VZ
2744 if ( data.needsterminal )
2745 {
15d4b8cd 2746 data.cmdOpen.Printf(wxT("xterm -e sh -c '%s'"),
2b5f62a0 2747 data.cmdOpen.c_str());
678ebfcd 2748 }
b13d92d1 2749
678ebfcd
VZ
2750 if ( !data.cmdOpen.empty() )
2751 {
dfea7acc 2752 data.verbs.Insert(wxT("open"), 0);
678ebfcd
VZ
2753 data.commands.Insert(data.cmdOpen, 0);
2754 }
2b813b73 2755
678ebfcd
VZ
2756 // we have to decide whether the new entry should replace any entries
2757 // for the same MIME type we had previously found or not
2758 bool overwrite;
2759
2760 // the fall back entries have the lowest priority, by definition
2761 if ( fallback )
2762 {
d0ee33f5 2763 overwrite = false;
678ebfcd
VZ
2764 }
2765 else
2766 {
2767 // have we seen this one before?
2768 int nIndex = m_aTypes.Index(data.type);
2769
a9a76b2f
VZ
2770 // and if we have, was it in this file? if not, we should
2771 // overwrite the previously seen one
678ebfcd 2772 overwrite = nIndex == wxNOT_FOUND ||
a9a76b2f 2773 aIndicesSeenHere.Index(nIndex) == wxNOT_FOUND;
b13d92d1
VZ
2774 }
2775
dfea7acc 2776 wxLogTrace(TRACE_MIME, wxT("mailcap %s: %s [%s]"),
678ebfcd 2777 data.type.c_str(), data.cmdOpen.c_str(),
dfea7acc 2778 overwrite ? wxT("replace") : wxT("add"));
678ebfcd
VZ
2779
2780 int n = AddToMimeData
2781 (
2782 data.type,
2783 data.icon,
2784 new wxMimeTypeCommands(data.verbs, data.commands),
15d4b8cd 2785 empty_extensions_list,
678ebfcd
VZ
2786 data.desc,
2787 overwrite
2788 );
2789
2790 if ( overwrite )
2791 {
2792 aIndicesSeenHere.Add(n);
2793 }
b13d92d1 2794 }
cc385968 2795
d0ee33f5 2796 return true;
b13d92d1
VZ
2797}
2798
696e1ea0 2799size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
1b986aef 2800{
a6c65e88
VZ
2801 InitIfNeeded();
2802
54acce90
VZ
2803 mimetypes.Empty();
2804
54acce90
VZ
2805 size_t count = m_aTypes.GetCount();
2806 for ( size_t n = 0; n < count; n++ )
2807 {
2808 // don't return template types from here (i.e. anything containg '*')
15d4b8cd 2809 const wxString &type = m_aTypes[n];
dfea7acc 2810 if ( type.Find(wxT('*')) == wxNOT_FOUND )
54acce90
VZ
2811 {
2812 mimetypes.Add(type);
2813 }
2814 }
1b986aef 2815
54acce90 2816 return mimetypes.GetCount();
1b986aef
VZ
2817}
2818
a6c65e88
VZ
2819// ----------------------------------------------------------------------------
2820// writing to MIME type files
2821// ----------------------------------------------------------------------------
2822
2b813b73 2823bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft)
a6c65e88 2824{
9413e1e3
VZ
2825 InitIfNeeded();
2826
2b813b73 2827 wxArrayString sMimeTypes;
dfea7acc 2828 ft->GetMimeTypes(sMimeTypes);
a6c65e88 2829
2b813b73 2830 size_t i;
15d4b8cd
RR
2831 size_t nCount = sMimeTypes.GetCount();
2832 for (i = 0; i < nCount; i ++)
2b813b73 2833 {
15d4b8cd 2834 const wxString &sMime = sMimeTypes.Item(i);
dfea7acc 2835 int nIndex = m_aTypes.Index(sMime);
2b813b73
VZ
2836 if ( nIndex == wxNOT_FOUND)
2837 {
2838 // error if we get here ??
d0ee33f5 2839 return false;
2b813b73
VZ
2840 }
2841 else
2842 {
dfea7acc 2843 WriteMimeInfo(nIndex, true);
e9d9f136
VZ
2844 m_aTypes.RemoveAt(nIndex);
2845 m_aEntries.RemoveAt(nIndex);
2846 m_aExtensions.RemoveAt(nIndex);
2847 m_aDescriptions.RemoveAt(nIndex);
2848 m_aIcons.RemoveAt(nIndex);
2b813b73
VZ
2849 }
2850 }
2851 // check data integrity
2852 wxASSERT( m_aTypes.Count() == m_aEntries.Count() &&
2853 m_aTypes.Count() == m_aExtensions.Count() &&
2854 m_aTypes.Count() == m_aIcons.Count() &&
2855 m_aTypes.Count() == m_aDescriptions.Count() );
2856
d0ee33f5 2857 return true;
a6c65e88
VZ
2858}
2859
f6bcfd97
BP
2860// ----------------------------------------------------------------------------
2861// private functions
2862// ----------------------------------------------------------------------------
2863
2864static bool IsKnownUnimportantField(const wxString& fieldAll)
2865{
15d4b8cd 2866 static const wxChar * const knownFields[] =
f6bcfd97 2867 {
dfea7acc
DS
2868 wxT("x-mozilla-flags"),
2869 wxT("nametemplate"),
2870 wxT("textualnewlines"),
f6bcfd97
BP
2871 };
2872
dfea7acc 2873 wxString field = fieldAll.BeforeFirst(wxT('='));
f6bcfd97
BP
2874 for ( size_t n = 0; n < WXSIZEOF(knownFields); n++ )
2875 {
2876 if ( field.CmpNoCase(knownFields[n]) == 0 )
d0ee33f5 2877 return true;
f6bcfd97
BP
2878 }
2879
d0ee33f5 2880 return false;
f6bcfd97
BP
2881}
2882
8e124873 2883#endif
b79e32cc 2884 // wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE