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