]> git.saurik.com Git - wxWidgets.git/blame - src/unix/mimetype.cpp
Optical improvements under OS X
[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
b13d92d1
VZ
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
7520f3da 16 #pragma hdrstop
ce4169a4
RR
17#endif
18
05f616ef 19#if wxUSE_MIMETYPE && wxUSE_FILE
ce4169a4 20
88a7a4e1
WS
21#include "wx/unix/mimetype.h"
22
ce4169a4 23#ifndef WX_PRECOMP
ad9835c9 24 #include "wx/dynarray.h"
7520f3da 25 #include "wx/string.h"
88a7a4e1 26 #include "wx/intl.h"
e4db172a 27 #include "wx/log.h"
de6185e2 28 #include "wx/utils.h"
7c2e5dec 29#endif
3d05544e 30
ce4169a4 31#include "wx/file.h"
2432b92d 32#include "wx/confbase.h"
b13d92d1 33
7dc3cc31 34#include "wx/ffile.h"
7dc3cc31 35#include "wx/dir.h"
7dc3cc31 36#include "wx/tokenzr.h"
c41dbc20 37#include "wx/iconloc.h"
1d529ef7 38#include "wx/filename.h"
88bbc332
RR
39#include "wx/app.h"
40#include "wx/apptrait.h"
b13d92d1 41
b13d92d1
VZ
42// other standard headers
43#include <ctype.h>
44
29886d1b 45class wxMimeTextFile
2b813b73
VZ
46{
47public:
4010aae1 48 wxMimeTextFile()
29886d1b
RR
49 {
50 }
4010aae1 51
29886d1b
RR
52 wxMimeTextFile(const wxString& fname)
53 {
54 m_fname = fname;
55 }
4010aae1 56
d39e2bbc 57 bool Open()
29886d1b 58 {
d39e2bbc 59 wxFFile file( m_fname );
29886d1b
RR
60 if (!file.IsOpened())
61 return false;
4010aae1 62
29886d1b
RR
63 size_t size = file.Length();
64 wxCharBuffer buffer( size );
65 file.Read( (void*) (const char*) buffer, size );
4010aae1 66
d39e2bbc 67 // Check for valid UTF-8 here?
29886d1b 68 wxString all = wxString::FromUTF8( buffer, size );
4010aae1 69
29886d1b
RR
70 wxStringTokenizer tok( all, "\n" );
71 while (tok.HasMoreTokens())
72 {
73 wxString t = tok.GetNextToken();
74 t.MakeLower();
05f616ef 75 if ((!!t) && (t.Find( "comment" ) != 0) && (t.Find( "#" ) != 0) && (t.Find( "generic" ) != 0))
29886d1b
RR
76 m_text.Add( t );
77 }
78 return true;
79 }
4010aae1 80
29886d1b
RR
81 unsigned int GetLineCount() const { return m_text.GetCount(); }
82 wxString &GetLine( unsigned int line ) { return m_text[line]; }
2b813b73 83
d9bbb2d8
VZ
84 int pIndexOf(const wxString& sSearch,
85 bool bIncludeComments = false,
86 int iStart = 0)
2b813b73 87 {
2b813b73
VZ
88 wxString sTest = sSearch;
89 sTest.MakeLower();
31383ebc 90 for(size_t i = iStart; i < GetLineCount(); i++)
2b813b73 91 {
29886d1b 92 wxString sLine = GetLine(i);
31383ebc 93 if(bIncludeComments || ! sLine.StartsWith(wxT("#")))
2b813b73 94 {
31383ebc
RR
95 if(sLine.StartsWith(sTest))
96 return (int)i;
2b813b73
VZ
97 }
98 }
31383ebc 99 return wxNOT_FOUND;
2b813b73
VZ
100 }
101
dfea7acc 102 wxString GetVerb(size_t i)
2b813b73 103 {
dfea7acc
DS
104 if (i > GetLineCount() )
105 return wxEmptyString;
106
2b813b73
VZ
107 wxString sTmp = GetLine(i).BeforeFirst(wxT('='));
108 return sTmp;
109 }
110
dfea7acc 111 wxString GetCmd(size_t i)
2b813b73 112 {
dfea7acc
DS
113 if (i > GetLineCount() )
114 return wxEmptyString;
115
2b813b73
VZ
116 wxString sTmp = GetLine(i).AfterFirst(wxT('='));
117 return sTmp;
118 }
4010aae1 119
29886d1b
RR
120private:
121 wxArrayString m_text;
122 wxString m_fname;
2b813b73
VZ
123};
124
f6bcfd97
BP
125// ----------------------------------------------------------------------------
126// constants
127// ----------------------------------------------------------------------------
128
129// MIME code tracing mask
dfea7acc 130#define TRACE_MIME wxT("mime")
f6bcfd97 131
2b813b73 132
d39e2bbc
RR
133// Read a XDG *.desktop file of type 'Application'
134void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename)
2b813b73 135{
d39e2bbc 136 wxLogTrace(TRACE_MIME, wxT("loading XDG file %s"), filename.c_str());
2b813b73 137
d39e2bbc
RR
138 wxMimeTextFile file(filename);
139 if ( !file.Open() )
d9bbb2d8 140 return;
4010aae1 141
31383ebc 142 // Here, only type 'Application' should be considered.
29886d1b
RR
143 int nIndex = file.pIndexOf( "Type=" );
144 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex) != "application")
31383ebc 145 return;
dfea7acc 146
31383ebc 147 // The hidden entry specifies a file to be ignored.
29886d1b
RR
148 nIndex = file.pIndexOf( "Hidden=" );
149 if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex) == "true")
31383ebc 150 return;
dfea7acc 151
31383ebc
RR
152 // Semicolon separated list of mime types handled by the application.
153 nIndex = file.pIndexOf( wxT("MimeType=") );
154 if (nIndex == wxNOT_FOUND)
155 return;
156 wxString mimetypes = file.GetCmd (nIndex);
dfea7acc 157
31383ebc
RR
158 // Name of the application
159 wxString nameapp;
160 nIndex = wxNOT_FOUND;
161#if wxUSE_INTL // try "Name[locale name]" first
162 wxLocale *locale = wxGetLocale();
163 if ( locale )
164 nIndex = file.pIndexOf(_T("Name[")+locale->GetName()+_T("]="));
165#endif // wxUSE_INTL
166 if(nIndex == wxNOT_FOUND)
167 nIndex = file.pIndexOf( wxT("Name=") );
168 if(nIndex != wxNOT_FOUND)
169 nameapp = file.GetCmd(nIndex);
170
171 // Icon of the application.
172 wxString nameicon, namemini;
173 nIndex = wxNOT_FOUND;
174#if wxUSE_INTL // try "Icon[locale name]" first
175 if ( locale )
176 nIndex = file.pIndexOf(_T("Icon[")+locale->GetName()+_T("]="));
177#endif // wxUSE_INTL
178 if(nIndex == wxNOT_FOUND)
179 nIndex = file.pIndexOf( wxT("Icon=") );
180 if(nIndex != wxNOT_FOUND) {
181 nameicon = wxString(wxT("--icon ")) + file.GetCmd(nIndex);
182 namemini = wxString(wxT("--miniicon ")) + file.GetCmd(nIndex);
183 }
184
185 // Replace some of the field code in the 'Exec' entry.
186 // TODO: deal with %d, %D, %n, %N, %k and %v (but last one is deprecated)
187 nIndex = file.pIndexOf( wxT("Exec=") );
188 if (nIndex == wxNOT_FOUND)
189 return;
190 wxString sCmd = file.GetCmd(nIndex);
191 // we expect %f; others including %F and %U and %u are possible
192 sCmd.Replace(wxT("%F"), wxT("%f"));
193 sCmd.Replace(wxT("%U"), wxT("%f"));
194 sCmd.Replace(wxT("%u"), wxT("%f"));
195 if (0 == sCmd.Replace ( wxT("%f"), wxT("%s") ))
196 sCmd = sCmd + wxT(" %s");
197 sCmd.Replace(wxT("%c"), nameapp);
198 sCmd.Replace(wxT("%i"), nameicon);
199 sCmd.Replace(wxT("%m"), namemini);
200
201 wxStringTokenizer tokenizer(mimetypes, _T(";"));
202 while(tokenizer.HasMoreTokens()) {
203 wxString mimetype = tokenizer.GetNextToken().Lower();
309aefbd 204 nIndex = m_aTypes.Index(mimetype);
31383ebc
RR
205 if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
206 wxMimeTypeCommands* entry = m_aEntries[nIndex];
207 entry->AddOrReplaceVerb(wxT("open"), sCmd);
10274336 208 }
31383ebc
RR
209 }
210}
d0ee33f5 211
d39e2bbc 212void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString& dirname)
31383ebc 213{
6fc93e9b
JS
214 // Don't complain if we don't have permissions to read - it confuses users
215 wxLogNull logNull;
216
31383ebc
RR
217 if(! wxDir::Exists(dirname))
218 return;
219 wxDir dir(dirname);
220 if ( !dir.IsOpened() )
221 return;
d0ee33f5 222
31383ebc
RR
223 wxString filename;
224 // Look into .desktop files
225 bool cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES);
29886d1b
RR
226 while (cont)
227 {
31383ebc 228 wxFileName p(dirname, filename);
d39e2bbc 229 LoadXDGApp( p.GetFullPath() );
31383ebc
RR
230 cont = dir.GetNext(&filename);
231 }
d39e2bbc 232
4010aae1 233#if 0
d39e2bbc
RR
234 // RR: I'm not sure this makes any sense. On my system we'll just
235 // scan the YAST2 and other useless directories
4010aae1 236
31383ebc
RR
237 // Look recursively into subdirs
238 cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
29886d1b
RR
239 while (cont)
240 {
31383ebc
RR
241 wxFileName p(dirname, wxEmptyString);
242 p.AppendDir(filename);
d39e2bbc 243 LoadXDGAppsFilesFromDir( p.GetPath() );
31383ebc
RR
244 cont = dir.GetNext(&filename);
245 }
d39e2bbc 246#endif
31383ebc 247}
d0ee33f5 248
b9517a0a 249
05f616ef
RR
250void wxMimeTypesManagerImpl::LoadXDGGlobs(const wxString& filename)
251{
4010aae1
VZ
252 if ( !wxFileName::FileExists(filename) )
253 return;
254
05f616ef
RR
255 wxLogTrace(TRACE_MIME, wxT("loading XDG globs file from %s"), filename.c_str());
256
257 wxMimeTextFile file(filename);
258 if ( !file.Open() )
259 return;
260
d8a57f5e
RR
261 size_t i;
262 for (i = 0; i < file.GetLineCount(); i++)
263 {
264 wxStringTokenizer tok( file.GetLine(i), ":" );
265 wxString mime = tok.GetNextToken();
266 wxString ext = tok.GetNextToken();
267 ext.Remove( 0, 2 );
268 wxArrayString exts;
269 exts.Add( ext );
4010aae1 270
e675d6d8
RR
271 if (mime.Find( "application" ) != 0)
272 AddToMimeData(mime, wxEmptyString, NULL, exts, wxEmptyString, true );
d8a57f5e 273 }
05f616ef
RR
274}
275
2b813b73
VZ
276// ----------------------------------------------------------------------------
277// wxFileTypeImpl (Unix)
278// ----------------------------------------------------------------------------
279
2b813b73 280wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const
b9517a0a 281{
2b813b73
VZ
282 wxString sTmp;
283 size_t i = 0;
678ebfcd 284 while ( (i < m_index.GetCount() ) && sTmp.empty() )
b9517a0a 285 {
dfea7acc
DS
286 sTmp = m_manager->GetCommand( verb, m_index[i] );
287 i++;
b9517a0a
VZ
288 }
289
2b813b73
VZ
290 return wxFileType::ExpandCommand(sTmp, params);
291}
b9517a0a 292
da0766ab 293bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
2b813b73
VZ
294{
295 wxString sTmp;
296 size_t i = 0;
678ebfcd 297 while ( (i < m_index.GetCount() ) && sTmp.empty() )
c786f763
VZ
298 {
299 sTmp = m_manager->m_aIcons[m_index[i]];
dfea7acc 300 i++;
c786f763 301 }
dfea7acc
DS
302
303 if ( sTmp.empty() )
d0ee33f5 304 return false;
b9517a0a 305
da0766ab 306 if ( iconLoc )
c786f763 307 {
a49686b4 308 iconLoc->SetFileName(sTmp);
2b813b73 309 }
c786f763 310
d0ee33f5 311 return true;
c786f763 312}
2b813b73 313
dfea7acc 314bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
2b813b73
VZ
315{
316 mimeTypes.Clear();
15d4b8cd
RR
317 size_t nCount = m_index.GetCount();
318 for (size_t i = 0; i < nCount; i++)
2b813b73 319 mimeTypes.Add(m_manager->m_aTypes[m_index[i]]);
dfea7acc 320
d0ee33f5 321 return true;
2b813b73
VZ
322}
323
2b813b73
VZ
324size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
325 wxArrayString *commands,
326 const wxFileType::MessageParameters& params) const
327{
2b813b73
VZ
328 wxString vrb, cmd, sTmp;
329 size_t count = 0;
678ebfcd 330 wxMimeTypeCommands * sPairs;
2b813b73
VZ
331
332 // verbs and commands have been cleared already in mimecmn.cpp...
333 // if we find no entries in the exact match, try the inexact match
7c2e5dec 334 for (size_t n = 0; ((count == 0) && (n < m_index.GetCount())); n++)
2b813b73
VZ
335 {
336 // list of verb = command pairs for this mimetype
337 sPairs = m_manager->m_aEntries [m_index[n]];
338 size_t i;
dfea7acc
DS
339 for ( i = 0; i < sPairs->GetCount(); i++ )
340 {
341 vrb = sPairs->GetVerb(i);
7c2e5dec 342 // some gnome entries have "." inside
dfea7acc
DS
343 vrb = vrb.AfterLast(wxT('.'));
344 cmd = sPairs->GetCmd(i);
345 if (! cmd.empty() )
2b813b73 346 {
dfea7acc 347 cmd = wxFileType::ExpandCommand(cmd, params);
7c2e5dec 348 count++;
dfea7acc
DS
349 if ( vrb.IsSameAs(wxT("open")))
350 {
d6a7ca31
VZ
351 if ( verbs )
352 verbs->Insert(vrb, 0u);
353 if ( commands )
354 commands ->Insert(cmd, 0u);
dfea7acc
DS
355 }
356 else
357 {
d6a7ca31
VZ
358 if ( verbs )
359 verbs->Add(vrb);
360 if ( commands )
361 commands->Add(cmd);
dfea7acc
DS
362 }
363 }
2b813b73 364 }
2b813b73 365 }
2b813b73 366
dfea7acc 367 return count;
2b813b73
VZ
368}
369
370bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
371{
86501081 372 const wxString strExtensions = m_manager->GetExtension(m_index[0]);
2b813b73
VZ
373 extensions.Empty();
374
1c4cd9e0 375 // one extension in the space or comma-delimited list
2b813b73 376 wxString strExt;
86501081
VS
377 wxString::const_iterator end = strExtensions.end();
378 for ( wxString::const_iterator p = strExtensions.begin(); /* nothing */; ++p )
dfea7acc 379 {
86501081 380 if ( p == end || *p == wxT(' ') || *p == wxT(',') )
dfea7acc
DS
381 {
382 if ( !strExt.empty() )
383 {
2b813b73
VZ
384 extensions.Add(strExt);
385 strExt.Empty();
386 }
7c2e5dec
DS
387 //else: repeated spaces
388 // (shouldn't happen, but it's not that important if it does happen)
2b813b73 389
86501081 390 if ( p == end )
2b813b73
VZ
391 break;
392 }
dfea7acc
DS
393 else if ( *p == wxT('.') )
394 {
2b813b73 395 // remove the dot from extension (but only if it's the first char)
dfea7acc
DS
396 if ( !strExt.empty() )
397 {
2b813b73
VZ
398 strExt += wxT('.');
399 }
400 //else: no, don't append it
401 }
dfea7acc
DS
402 else
403 {
2b813b73
VZ
404 strExt += *p;
405 }
406 }
407
d0ee33f5 408 return true;
2b813b73
VZ
409}
410
7c2e5dec 411// set an arbitrary command:
2b813b73 412// could adjust the code to ask confirmation if it already exists and
d0ee33f5 413// overwriteprompt is true, but this is currently ignored as *Associate* has
2b813b73 414// no overwrite prompt
17a1ebd1
VZ
415bool
416wxFileTypeImpl::SetCommand(const wxString& cmd,
417 const wxString& verb,
418 bool WXUNUSED(overwriteprompt))
d84afea9 419{
2b813b73 420 wxArrayString strExtensions;
678ebfcd 421 wxString strDesc, strIcon;
2b813b73 422
2b813b73 423 wxArrayString strTypes;
dfea7acc 424 GetMimeTypes(strTypes);
926ce9e3 425 if ( strTypes.IsEmpty() )
dfea7acc 426 return false;
2b813b73 427
926ce9e3
VZ
428 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
429 entry->Add(verb + wxT("=") + cmd + wxT(" %s "));
430
dda522bf 431 bool ok = false;
15d4b8cd
RR
432 size_t nCount = strTypes.GetCount();
433 for ( size_t i = 0; i < nCount; i++ )
d84afea9 434 {
dda522bf
VZ
435 if ( m_manager->DoAssociation
436 (
437 strTypes[i],
438 strIcon,
439 entry,
440 strExtensions,
441 strDesc
442 ) )
443 {
444 // DoAssociation() took ownership of entry, don't delete it below
445 ok = true;
446 }
d84afea9 447 }
2b813b73 448
dda522bf
VZ
449 if ( !ok )
450 delete entry;
451
dfea7acc 452 return ok;
d84afea9 453}
2b813b73 454
15d4b8cd 455// ignore index on the grounds that we only have one icon in a Unix file
17a1ebd1 456bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int WXUNUSED(index))
d84afea9 457{
dfea7acc
DS
458 if (strIcon.empty())
459 return false;
460
2b813b73
VZ
461 wxArrayString strExtensions;
462 wxString strDesc;
463
2b813b73 464 wxArrayString strTypes;
dfea7acc 465 GetMimeTypes(strTypes);
cb0b7b7d 466 if ( strTypes.IsEmpty() )
dfea7acc 467 return false;
2b813b73 468
cb0b7b7d 469 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2f769cb9 470 bool ok = false;
15d4b8cd
RR
471 size_t nCount = strTypes.GetCount();
472 for ( size_t i = 0; i < nCount; i++ )
d84afea9 473 {
2f769cb9
VZ
474 if ( m_manager->DoAssociation
475 (
cb0b7b7d
VZ
476 strTypes[i],
477 strIcon,
478 entry,
479 strExtensions,
480 strDesc
2f769cb9 481 ) )
cb0b7b7d 482 {
2f769cb9
VZ
483 // we don't need to free entry now, DoAssociation() took ownership
484 // of it
485 ok = true;
cb0b7b7d 486 }
d84afea9 487 }
2b813b73 488
2f769cb9
VZ
489 if ( !ok )
490 delete entry;
491
dfea7acc 492 return ok;
d84afea9
GD
493}
494
2b813b73
VZ
495// ----------------------------------------------------------------------------
496// wxMimeTypesManagerImpl (Unix)
497// ----------------------------------------------------------------------------
498
2b813b73
VZ
499wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
500{
d0ee33f5 501 m_initialized = false;
2b813b73
VZ
502}
503
1d529ef7
RR
504void wxMimeTypesManagerImpl::InitIfNeeded()
505{
506 if ( !m_initialized )
507 {
508 // set the flag first to prevent recursion
d0ee33f5 509 m_initialized = true;
4010aae1 510
88bbc332 511 wxString wm = wxTheApp->GetTraits()->GetDesktopEnvironment();
4010aae1 512
88bbc332
RR
513 if (wm == wxT("KDE"))
514 Initialize( wxMAILCAP_KDE );
515 else if (wm == wxT("GNOME"))
516 Initialize( wxMAILCAP_GNOME );
517 else
518 Initialize();
1d529ef7
RR
519 }
520}
521
05f616ef
RR
522
523
2b813b73
VZ
524// read system and user mailcaps and other files
525void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
526 const wxString& sExtraDir)
527{
29886d1b
RR
528#ifdef __VMS
529 // XDG tables are never installed on OpenVMS
530 return;
9bc0aaae 531#else
2b813b73 532
05f616ef
RR
533 // Read MIME type - extension associations
534 LoadXDGGlobs( "/usr/share/mime/globs" );
397e7f98 535 LoadXDGGlobs( "/usr/local/share/mime/globs" );
05f616ef
RR
536
537 // Load desktop files for XDG, and then override them with the defaults.
84513dcb
JS
538 // We will override them one desktop file at a time, rather
539 // than one mime type at a time, but it should be a reasonable
540 // heuristic.
84513dcb 541 {
d39e2bbc 542 wxString xdgDataHome = wxGetenv("XDG_DATA_HOME");
84513dcb 543 if ( xdgDataHome.empty() )
d39e2bbc
RR
544 xdgDataHome = wxGetHomeDir() + "/.local/share";
545 wxString xdgDataDirs = wxGetenv("XDG_DATA_DIRS");
84513dcb 546 if ( xdgDataDirs.empty() )
d39e2bbc
RR
547 {
548 xdgDataDirs = "/usr/local/share:/usr/share";
549 if (mailcapStyles & wxMAILCAP_GNOME)
550 xdgDataDirs += ":/usr/share/gnome:/opt/gnome/share";
551 if (mailcapStyles & wxMAILCAP_KDE)
552 xdgDataDirs += ":/usr/share/kde3:/opt/kde3/share";
553 }
554 if ( !sExtraDir.empty() )
555 {
556 xdgDataDirs += ':';
557 xdgDataDirs += sExtraDir;
558 }
4010aae1 559
84513dcb 560 wxArrayString dirs;
d39e2bbc 561 wxStringTokenizer tokenizer(xdgDataDirs, ":");
84513dcb
JS
562 while ( tokenizer.HasMoreTokens() )
563 {
564 wxString p = tokenizer.GetNextToken();
565 dirs.Add(p);
566 }
567 dirs.insert(dirs.begin(), xdgDataHome);
568
569 wxString defaultsList;
570 size_t i;
571 for (i = 0; i < dirs.GetCount(); i++)
572 {
29886d1b
RR
573 wxString f = dirs[i];
574 if (f.Last() != '/') f += '/';
575 f += "applications/defaults.list";
84513dcb
JS
576 if (wxFileExists(f))
577 {
578 defaultsList = f;
579 break;
580 }
581 }
582
583 // Load application files and associate them to corresponding mime types.
584 size_t nDirs = dirs.GetCount();
585 for (size_t nDir = 0; nDir < nDirs; nDir++)
586 {
29886d1b
RR
587 wxString dirStr = dirs[nDir];
588 if (dirStr.Last() != '/') dirStr += '/';
589 dirStr += "applications";
d39e2bbc 590 LoadXDGAppsFilesFromDir(dirStr);
84513dcb
JS
591 }
592
593 if (!defaultsList.IsEmpty())
594 {
595 wxArrayString deskTopFilesSeen;
596
597 wxMimeTextFile textfile(defaultsList);
598 if ( textfile.Open() )
599 {
600 int nIndex = textfile.pIndexOf( wxT("[Default Applications]") );
601 if (nIndex != wxNOT_FOUND)
602 {
603 for (i = nIndex+1; i < textfile.GetLineCount(); i++)
604 {
29886d1b 605 if (textfile.GetLine(i).Find(wxT("=")) != wxNOT_FOUND)
84513dcb 606 {
84513dcb 607 wxString desktopFile = textfile.GetCmd(i);
4010aae1 608
84513dcb
JS
609 if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND)
610 {
611 deskTopFilesSeen.Add(desktopFile);
612 size_t j;
29886d1b
RR
613 for (j = 0; j < dirs.GetCount(); j++)
614 {
615 wxString desktopPath = dirs[j];
d39e2bbc
RR
616 if (desktopPath.Last() != '/') desktopPath += '/';
617 desktopPath += "applications/";
29886d1b 618 desktopPath += desktopFile;
4010aae1 619
29886d1b 620 if (wxFileExists(desktopPath))
d39e2bbc 621 LoadXDGApp(desktopPath);
29886d1b
RR
622 }
623 }
624 }
2b813b73 625 }
29886d1b 626 }
2b813b73 627 }
b13d92d1 628 }
b13d92d1 629 }
9bc0aaae 630#endif
29886d1b 631}
dfea7acc 632
29886d1b
RR
633// clear data so you can read another group of WM files
634void wxMimeTypesManagerImpl::ClearData()
635{
636 m_aTypes.Clear();
637 m_aIcons.Clear();
638 m_aExtensions.Clear();
639 m_aDescriptions.Clear();
640
641 WX_CLEAR_ARRAY(m_aEntries);
642 m_aEntries.Empty();
643}
644
645wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
646{
647 ClearData();
b13d92d1
VZ
648}
649
dfea7acc 650wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
b9517a0a 651{
2b813b73 652 InitIfNeeded();
b9517a0a 653
dfea7acc
DS
654 wxString strType = ftInfo.GetMimeType();
655 wxString strDesc = ftInfo.GetDescription();
656 wxString strIcon = ftInfo.GetIconFile();
2b813b73 657
dfea7acc 658 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2b813b73 659
678ebfcd 660 if ( ! ftInfo.GetOpenCommand().empty())
dfea7acc
DS
661 entry->Add(wxT("open=") + ftInfo.GetOpenCommand() + wxT(" %s "));
662 if ( ! ftInfo.GetPrintCommand().empty())
663 entry->Add(wxT("print=") + ftInfo.GetPrintCommand() + wxT(" %s "));
2b813b73
VZ
664
665 // now find where these extensions are in the data store and remove them
dfea7acc 666 wxArrayString sA_Exts = ftInfo.GetExtensions();
2b813b73
VZ
667 wxString sExt, sExtStore;
668 size_t i, nIndex;
15d4b8cd
RR
669 size_t nExtCount = sA_Exts.GetCount();
670 for (i=0; i < nExtCount; i++)
dfea7acc 671 {
2b813b73 672 sExt = sA_Exts.Item(i);
dfea7acc
DS
673
674 // clean up to just a space before and after
d0ee33f5 675 sExt.Trim().Trim(false);
2b813b73 676 sExt = wxT(' ') + sExt + wxT(' ');
15d4b8cd
RR
677 size_t nCount = m_aExtensions.GetCount();
678 for (nIndex = 0; nIndex < nCount; nIndex++)
dfea7acc 679 {
2b813b73 680 sExtStore = m_aExtensions.Item(nIndex);
dfea7acc
DS
681 if (sExtStore.Replace(sExt, wxT(" ") ) > 0)
682 m_aExtensions.Item(nIndex) = sExtStore;
683 }
2b813b73 684 }
b9517a0a 685
dfea7acc 686 if ( !DoAssociation(strType, strIcon, entry, sA_Exts, strDesc) )
2b813b73 687 return NULL;
4d2976ad 688
2b813b73 689 return GetFileTypeFromMimeType(strType);
4d2976ad
VS
690}
691
2b813b73
VZ
692bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
693 const wxString& strIcon,
678ebfcd 694 wxMimeTypeCommands *entry,
2b813b73
VZ
695 const wxArrayString& strExtensions,
696 const wxString& strDesc)
b13d92d1 697{
d0ee33f5 698 int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
7520f3da 699
2b813b73 700 if ( nIndex == wxNOT_FOUND )
d0ee33f5 701 return false;
b13d92d1 702
d39e2bbc 703 return true;
a6c65e88
VZ
704}
705
2b813b73
VZ
706int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
707 const wxString& strIcon,
678ebfcd 708 wxMimeTypeCommands *entry,
2b813b73
VZ
709 const wxArrayString& strExtensions,
710 const wxString& strDesc,
678ebfcd 711 bool replaceExisting)
b13d92d1 712{
2b813b73 713 InitIfNeeded();
b13d92d1 714
2b813b73 715 // ensure mimetype is always lower case
678ebfcd
VZ
716 wxString mimeType = strType.Lower();
717
718 // is this a known MIME type?
2b813b73
VZ
719 int nIndex = m_aTypes.Index(mimeType);
720 if ( nIndex == wxNOT_FOUND )
721 {
722 // new file type
723 m_aTypes.Add(mimeType);
724 m_aIcons.Add(strIcon);
678ebfcd 725 m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
b13d92d1 726
678ebfcd 727 // change nIndex so we can use it below to add the extensions
df5168c4 728 m_aExtensions.Add(wxEmptyString);
b8d61021 729 nIndex = m_aExtensions.size() - 1;
678ebfcd
VZ
730
731 m_aDescriptions.Add(strDesc);
b13d92d1 732 }
678ebfcd 733 else // yes, we already have it
2b813b73 734 {
678ebfcd 735 if ( replaceExisting )
2b813b73
VZ
736 {
737 // if new description change it
678ebfcd 738 if ( !strDesc.empty())
2b813b73
VZ
739 m_aDescriptions[nIndex] = strDesc;
740
741 // if new icon change it
678ebfcd 742 if ( !strIcon.empty())
2b813b73
VZ
743 m_aIcons[nIndex] = strIcon;
744
678ebfcd
VZ
745 if ( entry )
746 {
747 delete m_aEntries[nIndex];
748 m_aEntries[nIndex] = entry;
749 }
2b813b73 750 }
678ebfcd 751 else // add data we don't already have ...
2b813b73 752 {
2b813b73 753 // if new description add only if none
678ebfcd 754 if ( m_aDescriptions[nIndex].empty() )
2b813b73
VZ
755 m_aDescriptions[nIndex] = strDesc;
756
757 // if new icon and no existing icon
dfea7acc 758 if ( m_aIcons[nIndex].empty() )
2b813b73
VZ
759 m_aIcons[nIndex] = strIcon;
760
2b813b73 761 // add any new entries...
678ebfcd 762 if ( entry )
2b813b73 763 {
7c2e5dec 764 wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
678ebfcd
VZ
765
766 size_t count = entry->GetCount();
767 for ( size_t i = 0; i < count; i++ )
768 {
769 const wxString& verb = entry->GetVerb(i);
770 if ( !entryOld->HasVerb(verb) )
771 {
772 entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
773 }
774 }
2b293fc7
VZ
775
776 // as we don't store it anywhere, it won't be deleted later as
777 // usual -- do it immediately instead
778 delete entry;
2b813b73
VZ
779 }
780 }
b13d92d1 781 }
4d2976ad 782
678ebfcd
VZ
783 // always add the extensions to this mimetype
784 wxString& exts = m_aExtensions[nIndex];
785
786 // add all extensions we don't have yet
15d4b8cd 787 wxString ext;
678ebfcd
VZ
788 size_t count = strExtensions.GetCount();
789 for ( size_t i = 0; i < count; i++ )
790 {
15d4b8cd
RR
791 ext = strExtensions[i];
792 ext += wxT(' ');
678ebfcd
VZ
793
794 if ( exts.Find(ext) == wxNOT_FOUND )
795 {
796 exts += ext;
797 }
798 }
799
2b813b73 800 // check data integrity
b4a980f4
VZ
801 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
802 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
803 m_aTypes.GetCount() == m_aIcons.GetCount() &&
804 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
cf471cab 805
2b813b73 806 return nIndex;
cf471cab
VS
807}
808
dfea7acc 809wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
b13d92d1 810{
678ebfcd 811 if (ext.empty() )
2b813b73
VZ
812 return NULL;
813
a6c65e88
VZ
814 InitIfNeeded();
815
1ee17e1c 816 size_t count = m_aExtensions.GetCount();
2b813b73 817 for ( size_t n = 0; n < count; n++ )
678ebfcd 818 {
dfea7acc 819 wxStringTokenizer tk(m_aExtensions[n], wxT(' '));
1ee17e1c 820
678ebfcd
VZ
821 while ( tk.HasMoreTokens() )
822 {
1ee17e1c 823 // consider extensions as not being case-sensitive
d0ee33f5 824 if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
678ebfcd 825 {
1ee17e1c 826 // found
678ebfcd 827 wxFileType *fileType = new wxFileType;
1ee17e1c 828 fileType->m_impl->Init(this, n);
678ebfcd
VZ
829
830 return fileType;
1ee17e1c
VZ
831 }
832 }
833 }
b13d92d1 834
678ebfcd 835 return NULL;
b13d92d1
VZ
836}
837
7c2e5dec 838wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
b13d92d1 839{
a6c65e88
VZ
840 InitIfNeeded();
841
2b813b73 842 wxFileType * fileType = NULL;
b13d92d1
VZ
843 // mime types are not case-sensitive
844 wxString mimetype(mimeType);
845 mimetype.MakeLower();
846
847 // first look for an exact match
848 int index = m_aTypes.Index(mimetype);
4010aae1 849
2b813b73
VZ
850 if ( index != wxNOT_FOUND )
851 {
852 fileType = new wxFileType;
853 fileType->m_impl->Init(this, index);
854 }
855
856 // then try to find "text/*" as match for "text/plain" (for example)
857 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
858 // the whole string - ok.
859
860 index = wxNOT_FOUND;
861 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
862
b4a980f4 863 size_t nCount = m_aTypes.GetCount();
dfea7acc
DS
864 for ( size_t n = 0; n < nCount; n++ )
865 {
2b813b73 866 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
dfea7acc
DS
867 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") )
868 {
2b813b73
VZ
869 index = n;
870 break;
b13d92d1
VZ
871 }
872 }
873
2b813b73 874 if ( index != wxNOT_FOUND )
dfea7acc
DS
875 {
876 // don't throw away fileType that was already found
877 if (!fileType)
7bc5ddc6 878 fileType = new wxFileType;
b13d92d1 879 fileType->m_impl->Init(this, index);
b13d92d1 880 }
dfea7acc 881
2b813b73
VZ
882 return fileType;
883}
884
2b813b73
VZ
885wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
886{
887 wxString command, testcmd, sV, sTmp;
888 sV = verb + wxT("=");
dfea7acc 889
2b813b73 890 // list of verb = command pairs for this mimetype
678ebfcd 891 wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
2b813b73
VZ
892
893 size_t i;
15d4b8cd
RR
894 size_t nCount = sPairs->GetCount();
895 for ( i = 0; i < nCount; i++ )
2b813b73 896 {
678ebfcd
VZ
897 sTmp = sPairs->GetVerbCmd (i);
898 if ( sTmp.Contains(sV) )
899 command = sTmp.AfterFirst(wxT('='));
b13d92d1 900 }
dfea7acc 901
2b813b73 902 return command;
b13d92d1
VZ
903}
904
8e124873
VZ
905void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
906{
a6c65e88
VZ
907 InitIfNeeded();
908
3f1aaa16 909 wxString extensions;
8e124873
VZ
910 const wxArrayString& exts = filetype.GetExtensions();
911 size_t nExts = exts.GetCount();
dfea7acc
DS
912 for ( size_t nExt = 0; nExt < nExts; nExt++ )
913 {
914 if ( nExt > 0 )
223d09f6 915 extensions += wxT(' ');
dfea7acc 916
8e124873
VZ
917 extensions += exts[nExt];
918 }
919
920 AddMimeTypeInfo(filetype.GetMimeType(),
921 extensions,
922 filetype.GetDescription());
8e124873
VZ
923}
924
925void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
926 const wxString& strExtensions,
927 const wxString& strDesc)
928{
2b813b73
VZ
929 // reading mailcap may find image/* , while
930 // reading mime.types finds image/gif and no match is made
931 // this means all the get functions don't work fix this
932 wxString strIcon;
933 wxString sTmp = strExtensions;
a6c65e88 934
2b813b73 935 wxArrayString sExts;
d0ee33f5 936 sTmp.Trim().Trim(false);
2b813b73 937
678ebfcd 938 while (!sTmp.empty())
2b813b73 939 {
dfea7acc 940 sExts.Add(sTmp.AfterLast(wxT(' ')));
2b813b73 941 sTmp = sTmp.BeforeLast(wxT(' '));
8e124873 942 }
2b813b73 943
dfea7acc 944 AddToMimeData(strMimeType, strIcon, NULL, sExts, strDesc, true);
8e124873
VZ
945}
946
696e1ea0 947size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
1b986aef 948{
a6c65e88
VZ
949 InitIfNeeded();
950
54acce90
VZ
951 mimetypes.Empty();
952
54acce90
VZ
953 size_t count = m_aTypes.GetCount();
954 for ( size_t n = 0; n < count; n++ )
955 {
956 // don't return template types from here (i.e. anything containg '*')
15d4b8cd 957 const wxString &type = m_aTypes[n];
dfea7acc 958 if ( type.Find(wxT('*')) == wxNOT_FOUND )
54acce90
VZ
959 {
960 mimetypes.Add(type);
961 }
962 }
1b986aef 963
54acce90 964 return mimetypes.GetCount();
1b986aef
VZ
965}
966
a6c65e88
VZ
967// ----------------------------------------------------------------------------
968// writing to MIME type files
969// ----------------------------------------------------------------------------
970
2b813b73 971bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft)
a6c65e88 972{
9413e1e3
VZ
973 InitIfNeeded();
974
2b813b73 975 wxArrayString sMimeTypes;
dfea7acc 976 ft->GetMimeTypes(sMimeTypes);
a6c65e88 977
2b813b73 978 size_t i;
15d4b8cd
RR
979 size_t nCount = sMimeTypes.GetCount();
980 for (i = 0; i < nCount; i ++)
2b813b73 981 {
15d4b8cd 982 const wxString &sMime = sMimeTypes.Item(i);
dfea7acc 983 int nIndex = m_aTypes.Index(sMime);
2b813b73
VZ
984 if ( nIndex == wxNOT_FOUND)
985 {
986 // error if we get here ??
d0ee33f5 987 return false;
2b813b73
VZ
988 }
989 else
990 {
e9d9f136
VZ
991 m_aTypes.RemoveAt(nIndex);
992 m_aEntries.RemoveAt(nIndex);
993 m_aExtensions.RemoveAt(nIndex);
994 m_aDescriptions.RemoveAt(nIndex);
995 m_aIcons.RemoveAt(nIndex);
2b813b73
VZ
996 }
997 }
998 // check data integrity
b4a980f4
VZ
999 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
1000 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
1001 m_aTypes.GetCount() == m_aIcons.GetCount() &&
1002 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
2b813b73 1003
d0ee33f5 1004 return true;
a6c65e88
VZ
1005}
1006
8e124873 1007#endif
05f616ef 1008 // wxUSE_MIMETYPE && wxUSE_FILE