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