]> git.saurik.com Git - wxWidgets.git/blame - src/unix/mimetype.cpp
Correctly restore the originally used C locale in wxLocale dtor.
[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 )
9a83f860 164 nIndex = file.pIndexOf(wxT("Name[")+locale->GetName()+wxT("]="));
31383ebc
RR
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 )
9a83f860 176 nIndex = file.pIndexOf(wxT("Icon[")+locale->GetName()+wxT("]="));
31383ebc
RR
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
9a83f860 201 wxStringTokenizer tokenizer(mimetypes, wxT(";"));
31383ebc
RR
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
9a83f860 225 bool cont = dir.GetFirst(&filename, wxT("*.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 );
03647350 270
98359ac7 271 AddToMimeData(mime, wxEmptyString, NULL, exts, wxEmptyString, true );
d8a57f5e 272 }
05f616ef
RR
273}
274
2b813b73
VZ
275// ----------------------------------------------------------------------------
276// wxFileTypeImpl (Unix)
277// ----------------------------------------------------------------------------
278
2b813b73 279wxString wxFileTypeImpl::GetExpandedCommand(const wxString & verb, const wxFileType::MessageParameters& params) const
b9517a0a 280{
2b813b73
VZ
281 wxString sTmp;
282 size_t i = 0;
678ebfcd 283 while ( (i < m_index.GetCount() ) && sTmp.empty() )
b9517a0a 284 {
dfea7acc
DS
285 sTmp = m_manager->GetCommand( verb, m_index[i] );
286 i++;
b9517a0a
VZ
287 }
288
2b813b73
VZ
289 return wxFileType::ExpandCommand(sTmp, params);
290}
b9517a0a 291
da0766ab 292bool wxFileTypeImpl::GetIcon(wxIconLocation *iconLoc) const
2b813b73
VZ
293{
294 wxString sTmp;
295 size_t i = 0;
678ebfcd 296 while ( (i < m_index.GetCount() ) && sTmp.empty() )
c786f763
VZ
297 {
298 sTmp = m_manager->m_aIcons[m_index[i]];
dfea7acc 299 i++;
c786f763 300 }
dfea7acc
DS
301
302 if ( sTmp.empty() )
d0ee33f5 303 return false;
b9517a0a 304
da0766ab 305 if ( iconLoc )
c786f763 306 {
a49686b4 307 iconLoc->SetFileName(sTmp);
2b813b73 308 }
c786f763 309
d0ee33f5 310 return true;
c786f763 311}
2b813b73 312
dfea7acc 313bool wxFileTypeImpl::GetMimeTypes(wxArrayString& mimeTypes) const
2b813b73
VZ
314{
315 mimeTypes.Clear();
15d4b8cd
RR
316 size_t nCount = m_index.GetCount();
317 for (size_t i = 0; i < nCount; i++)
2b813b73 318 mimeTypes.Add(m_manager->m_aTypes[m_index[i]]);
dfea7acc 319
d0ee33f5 320 return true;
2b813b73
VZ
321}
322
2b813b73
VZ
323size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
324 wxArrayString *commands,
325 const wxFileType::MessageParameters& params) const
326{
2b813b73
VZ
327 wxString vrb, cmd, sTmp;
328 size_t count = 0;
678ebfcd 329 wxMimeTypeCommands * sPairs;
2b813b73
VZ
330
331 // verbs and commands have been cleared already in mimecmn.cpp...
332 // if we find no entries in the exact match, try the inexact match
7c2e5dec 333 for (size_t n = 0; ((count == 0) && (n < m_index.GetCount())); n++)
2b813b73
VZ
334 {
335 // list of verb = command pairs for this mimetype
336 sPairs = m_manager->m_aEntries [m_index[n]];
337 size_t i;
dfea7acc
DS
338 for ( i = 0; i < sPairs->GetCount(); i++ )
339 {
340 vrb = sPairs->GetVerb(i);
7c2e5dec 341 // some gnome entries have "." inside
dfea7acc
DS
342 vrb = vrb.AfterLast(wxT('.'));
343 cmd = sPairs->GetCmd(i);
344 if (! cmd.empty() )
2b813b73 345 {
dfea7acc 346 cmd = wxFileType::ExpandCommand(cmd, params);
7c2e5dec 347 count++;
dfea7acc
DS
348 if ( vrb.IsSameAs(wxT("open")))
349 {
d6a7ca31
VZ
350 if ( verbs )
351 verbs->Insert(vrb, 0u);
352 if ( commands )
353 commands ->Insert(cmd, 0u);
dfea7acc
DS
354 }
355 else
356 {
d6a7ca31
VZ
357 if ( verbs )
358 verbs->Add(vrb);
359 if ( commands )
360 commands->Add(cmd);
dfea7acc
DS
361 }
362 }
2b813b73 363 }
2b813b73 364 }
2b813b73 365
dfea7acc 366 return count;
2b813b73
VZ
367}
368
369bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
370{
86501081 371 const wxString strExtensions = m_manager->GetExtension(m_index[0]);
2b813b73
VZ
372 extensions.Empty();
373
1c4cd9e0 374 // one extension in the space or comma-delimited list
2b813b73 375 wxString strExt;
86501081
VS
376 wxString::const_iterator end = strExtensions.end();
377 for ( wxString::const_iterator p = strExtensions.begin(); /* nothing */; ++p )
dfea7acc 378 {
86501081 379 if ( p == end || *p == wxT(' ') || *p == wxT(',') )
dfea7acc
DS
380 {
381 if ( !strExt.empty() )
382 {
2b813b73
VZ
383 extensions.Add(strExt);
384 strExt.Empty();
385 }
7c2e5dec
DS
386 //else: repeated spaces
387 // (shouldn't happen, but it's not that important if it does happen)
2b813b73 388
86501081 389 if ( p == end )
2b813b73
VZ
390 break;
391 }
dfea7acc
DS
392 else if ( *p == wxT('.') )
393 {
2b813b73 394 // remove the dot from extension (but only if it's the first char)
dfea7acc
DS
395 if ( !strExt.empty() )
396 {
2b813b73
VZ
397 strExt += wxT('.');
398 }
399 //else: no, don't append it
400 }
dfea7acc
DS
401 else
402 {
2b813b73
VZ
403 strExt += *p;
404 }
405 }
406
d0ee33f5 407 return true;
2b813b73
VZ
408}
409
7c2e5dec 410// set an arbitrary command:
2b813b73 411// could adjust the code to ask confirmation if it already exists and
d0ee33f5 412// overwriteprompt is true, but this is currently ignored as *Associate* has
2b813b73 413// no overwrite prompt
17a1ebd1
VZ
414bool
415wxFileTypeImpl::SetCommand(const wxString& cmd,
416 const wxString& verb,
417 bool WXUNUSED(overwriteprompt))
d84afea9 418{
2b813b73 419 wxArrayString strExtensions;
678ebfcd 420 wxString strDesc, strIcon;
2b813b73 421
2b813b73 422 wxArrayString strTypes;
dfea7acc 423 GetMimeTypes(strTypes);
926ce9e3 424 if ( strTypes.IsEmpty() )
dfea7acc 425 return false;
2b813b73 426
926ce9e3
VZ
427 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
428 entry->Add(verb + wxT("=") + cmd + wxT(" %s "));
429
dda522bf 430 bool ok = false;
15d4b8cd
RR
431 size_t nCount = strTypes.GetCount();
432 for ( size_t i = 0; i < nCount; i++ )
d84afea9 433 {
dda522bf
VZ
434 if ( m_manager->DoAssociation
435 (
436 strTypes[i],
437 strIcon,
438 entry,
439 strExtensions,
440 strDesc
441 ) )
442 {
443 // DoAssociation() took ownership of entry, don't delete it below
444 ok = true;
445 }
d84afea9 446 }
2b813b73 447
dda522bf
VZ
448 if ( !ok )
449 delete entry;
450
dfea7acc 451 return ok;
d84afea9 452}
2b813b73 453
15d4b8cd 454// ignore index on the grounds that we only have one icon in a Unix file
17a1ebd1 455bool wxFileTypeImpl::SetDefaultIcon(const wxString& strIcon, int WXUNUSED(index))
d84afea9 456{
dfea7acc
DS
457 if (strIcon.empty())
458 return false;
459
2b813b73
VZ
460 wxArrayString strExtensions;
461 wxString strDesc;
462
2b813b73 463 wxArrayString strTypes;
dfea7acc 464 GetMimeTypes(strTypes);
cb0b7b7d 465 if ( strTypes.IsEmpty() )
dfea7acc 466 return false;
2b813b73 467
cb0b7b7d 468 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2f769cb9 469 bool ok = false;
15d4b8cd
RR
470 size_t nCount = strTypes.GetCount();
471 for ( size_t i = 0; i < nCount; i++ )
d84afea9 472 {
2f769cb9
VZ
473 if ( m_manager->DoAssociation
474 (
cb0b7b7d
VZ
475 strTypes[i],
476 strIcon,
477 entry,
478 strExtensions,
479 strDesc
2f769cb9 480 ) )
cb0b7b7d 481 {
2f769cb9
VZ
482 // we don't need to free entry now, DoAssociation() took ownership
483 // of it
484 ok = true;
cb0b7b7d 485 }
d84afea9 486 }
2b813b73 487
2f769cb9
VZ
488 if ( !ok )
489 delete entry;
490
dfea7acc 491 return ok;
d84afea9
GD
492}
493
2b813b73
VZ
494// ----------------------------------------------------------------------------
495// wxMimeTypesManagerImpl (Unix)
496// ----------------------------------------------------------------------------
497
2b813b73
VZ
498wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
499{
d0ee33f5 500 m_initialized = false;
2b813b73
VZ
501}
502
1d529ef7
RR
503void wxMimeTypesManagerImpl::InitIfNeeded()
504{
505 if ( !m_initialized )
506 {
507 // set the flag first to prevent recursion
d0ee33f5 508 m_initialized = true;
4010aae1 509
6e3f8e61
VZ
510 int mailcapStyles = wxMAILCAP_ALL;
511 if ( wxAppTraits * const traits = wxApp::GetTraitsIfExists() )
512 {
513 wxString wm = traits->GetDesktopEnvironment();
4010aae1 514
6e3f8e61
VZ
515 if ( wm == "KDE" )
516 mailcapStyles = wxMAILCAP_KDE;
517 else if ( wm == "GNOME" )
518 mailcapStyles = wxMAILCAP_GNOME;
519 //else: unknown, use the default
520 }
521
522 Initialize(mailcapStyles);
1d529ef7
RR
523 }
524}
525
05f616ef
RR
526
527
2b813b73
VZ
528// read system and user mailcaps and other files
529void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
530 const wxString& sExtraDir)
531{
29886d1b
RR
532#ifdef __VMS
533 // XDG tables are never installed on OpenVMS
534 return;
9bc0aaae 535#else
2b813b73 536
05f616ef
RR
537 // Read MIME type - extension associations
538 LoadXDGGlobs( "/usr/share/mime/globs" );
397e7f98 539 LoadXDGGlobs( "/usr/local/share/mime/globs" );
05f616ef
RR
540
541 // Load desktop files for XDG, and then override them with the defaults.
84513dcb
JS
542 // We will override them one desktop file at a time, rather
543 // than one mime type at a time, but it should be a reasonable
544 // heuristic.
84513dcb 545 {
d39e2bbc 546 wxString xdgDataHome = wxGetenv("XDG_DATA_HOME");
84513dcb 547 if ( xdgDataHome.empty() )
d39e2bbc
RR
548 xdgDataHome = wxGetHomeDir() + "/.local/share";
549 wxString xdgDataDirs = wxGetenv("XDG_DATA_DIRS");
84513dcb 550 if ( xdgDataDirs.empty() )
d39e2bbc
RR
551 {
552 xdgDataDirs = "/usr/local/share:/usr/share";
553 if (mailcapStyles & wxMAILCAP_GNOME)
554 xdgDataDirs += ":/usr/share/gnome:/opt/gnome/share";
555 if (mailcapStyles & wxMAILCAP_KDE)
556 xdgDataDirs += ":/usr/share/kde3:/opt/kde3/share";
557 }
558 if ( !sExtraDir.empty() )
559 {
560 xdgDataDirs += ':';
561 xdgDataDirs += sExtraDir;
562 }
4010aae1 563
84513dcb 564 wxArrayString dirs;
d39e2bbc 565 wxStringTokenizer tokenizer(xdgDataDirs, ":");
84513dcb
JS
566 while ( tokenizer.HasMoreTokens() )
567 {
568 wxString p = tokenizer.GetNextToken();
569 dirs.Add(p);
570 }
571 dirs.insert(dirs.begin(), xdgDataHome);
572
573 wxString defaultsList;
574 size_t i;
575 for (i = 0; i < dirs.GetCount(); i++)
576 {
29886d1b
RR
577 wxString f = dirs[i];
578 if (f.Last() != '/') f += '/';
579 f += "applications/defaults.list";
84513dcb
JS
580 if (wxFileExists(f))
581 {
582 defaultsList = f;
583 break;
584 }
585 }
586
587 // Load application files and associate them to corresponding mime types.
588 size_t nDirs = dirs.GetCount();
589 for (size_t nDir = 0; nDir < nDirs; nDir++)
590 {
29886d1b
RR
591 wxString dirStr = dirs[nDir];
592 if (dirStr.Last() != '/') dirStr += '/';
593 dirStr += "applications";
d39e2bbc 594 LoadXDGAppsFilesFromDir(dirStr);
84513dcb
JS
595 }
596
597 if (!defaultsList.IsEmpty())
598 {
599 wxArrayString deskTopFilesSeen;
600
601 wxMimeTextFile textfile(defaultsList);
602 if ( textfile.Open() )
603 {
604 int nIndex = textfile.pIndexOf( wxT("[Default Applications]") );
605 if (nIndex != wxNOT_FOUND)
606 {
607 for (i = nIndex+1; i < textfile.GetLineCount(); i++)
608 {
29886d1b 609 if (textfile.GetLine(i).Find(wxT("=")) != wxNOT_FOUND)
84513dcb 610 {
84513dcb 611 wxString desktopFile = textfile.GetCmd(i);
4010aae1 612
84513dcb
JS
613 if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND)
614 {
615 deskTopFilesSeen.Add(desktopFile);
616 size_t j;
29886d1b
RR
617 for (j = 0; j < dirs.GetCount(); j++)
618 {
619 wxString desktopPath = dirs[j];
d39e2bbc
RR
620 if (desktopPath.Last() != '/') desktopPath += '/';
621 desktopPath += "applications/";
29886d1b 622 desktopPath += desktopFile;
4010aae1 623
29886d1b 624 if (wxFileExists(desktopPath))
d39e2bbc 625 LoadXDGApp(desktopPath);
29886d1b
RR
626 }
627 }
628 }
2b813b73 629 }
29886d1b 630 }
2b813b73 631 }
b13d92d1 632 }
b13d92d1 633 }
9bc0aaae 634#endif
29886d1b 635}
dfea7acc 636
29886d1b
RR
637// clear data so you can read another group of WM files
638void wxMimeTypesManagerImpl::ClearData()
639{
640 m_aTypes.Clear();
641 m_aIcons.Clear();
642 m_aExtensions.Clear();
643 m_aDescriptions.Clear();
644
645 WX_CLEAR_ARRAY(m_aEntries);
646 m_aEntries.Empty();
647}
648
649wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
650{
651 ClearData();
b13d92d1
VZ
652}
653
dfea7acc 654wxFileType * wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
b9517a0a 655{
2b813b73 656 InitIfNeeded();
b9517a0a 657
dfea7acc
DS
658 wxString strType = ftInfo.GetMimeType();
659 wxString strDesc = ftInfo.GetDescription();
660 wxString strIcon = ftInfo.GetIconFile();
2b813b73 661
dfea7acc 662 wxMimeTypeCommands *entry = new wxMimeTypeCommands();
2b813b73 663
678ebfcd 664 if ( ! ftInfo.GetOpenCommand().empty())
dfea7acc
DS
665 entry->Add(wxT("open=") + ftInfo.GetOpenCommand() + wxT(" %s "));
666 if ( ! ftInfo.GetPrintCommand().empty())
667 entry->Add(wxT("print=") + ftInfo.GetPrintCommand() + wxT(" %s "));
2b813b73
VZ
668
669 // now find where these extensions are in the data store and remove them
dfea7acc 670 wxArrayString sA_Exts = ftInfo.GetExtensions();
2b813b73
VZ
671 wxString sExt, sExtStore;
672 size_t i, nIndex;
15d4b8cd
RR
673 size_t nExtCount = sA_Exts.GetCount();
674 for (i=0; i < nExtCount; i++)
dfea7acc 675 {
2b813b73 676 sExt = sA_Exts.Item(i);
dfea7acc
DS
677
678 // clean up to just a space before and after
d0ee33f5 679 sExt.Trim().Trim(false);
2b813b73 680 sExt = wxT(' ') + sExt + wxT(' ');
15d4b8cd
RR
681 size_t nCount = m_aExtensions.GetCount();
682 for (nIndex = 0; nIndex < nCount; nIndex++)
dfea7acc 683 {
2b813b73 684 sExtStore = m_aExtensions.Item(nIndex);
dfea7acc
DS
685 if (sExtStore.Replace(sExt, wxT(" ") ) > 0)
686 m_aExtensions.Item(nIndex) = sExtStore;
687 }
2b813b73 688 }
b9517a0a 689
dfea7acc 690 if ( !DoAssociation(strType, strIcon, entry, sA_Exts, strDesc) )
2b813b73 691 return NULL;
4d2976ad 692
2b813b73 693 return GetFileTypeFromMimeType(strType);
4d2976ad
VS
694}
695
2b813b73
VZ
696bool wxMimeTypesManagerImpl::DoAssociation(const wxString& strType,
697 const wxString& strIcon,
678ebfcd 698 wxMimeTypeCommands *entry,
2b813b73
VZ
699 const wxArrayString& strExtensions,
700 const wxString& strDesc)
b13d92d1 701{
d0ee33f5 702 int nIndex = AddToMimeData(strType, strIcon, entry, strExtensions, strDesc, true);
7520f3da 703
2b813b73 704 if ( nIndex == wxNOT_FOUND )
d0ee33f5 705 return false;
b13d92d1 706
d39e2bbc 707 return true;
a6c65e88
VZ
708}
709
2b813b73
VZ
710int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
711 const wxString& strIcon,
678ebfcd 712 wxMimeTypeCommands *entry,
2b813b73
VZ
713 const wxArrayString& strExtensions,
714 const wxString& strDesc,
678ebfcd 715 bool replaceExisting)
b13d92d1 716{
2b813b73 717 InitIfNeeded();
b13d92d1 718
2b813b73 719 // ensure mimetype is always lower case
678ebfcd
VZ
720 wxString mimeType = strType.Lower();
721
722 // is this a known MIME type?
2b813b73
VZ
723 int nIndex = m_aTypes.Index(mimeType);
724 if ( nIndex == wxNOT_FOUND )
725 {
98359ac7 726 // We put MIME types containing "application" at the end, so that
e91d8a28 727 // if the MIME type for the extension "htm" is searched for, it will
98359ac7
RR
728 // rather find "text/html" than "application/x-mozilla-bookmarks".
729 if (mimeType.Find( "application" ) == 0)
730 {
731 // new file type
732 m_aTypes.Add(mimeType);
733 m_aIcons.Add(strIcon);
734 m_aEntries.Add(entry ? entry : new wxMimeTypeCommands);
b13d92d1 735
98359ac7
RR
736 // change nIndex so we can use it below to add the extensions
737 m_aExtensions.Add(wxEmptyString);
738 nIndex = m_aExtensions.size() - 1;
678ebfcd 739
98359ac7
RR
740 m_aDescriptions.Add(strDesc);
741 }
742 else
743 {
744 // new file type
745 m_aTypes.Insert(mimeType,0);
746 m_aIcons.Insert(strIcon,0);
747 m_aEntries.Insert(entry ? entry : new wxMimeTypeCommands,0);
748
749 // change nIndex so we can use it below to add the extensions
750 m_aExtensions.Insert(wxEmptyString,0);
751 nIndex = 0;
752
753 m_aDescriptions.Insert(strDesc,0);
754 }
b13d92d1 755 }
678ebfcd 756 else // yes, we already have it
2b813b73 757 {
678ebfcd 758 if ( replaceExisting )
2b813b73
VZ
759 {
760 // if new description change it
678ebfcd 761 if ( !strDesc.empty())
2b813b73
VZ
762 m_aDescriptions[nIndex] = strDesc;
763
764 // if new icon change it
678ebfcd 765 if ( !strIcon.empty())
2b813b73
VZ
766 m_aIcons[nIndex] = strIcon;
767
678ebfcd
VZ
768 if ( entry )
769 {
770 delete m_aEntries[nIndex];
771 m_aEntries[nIndex] = entry;
772 }
2b813b73 773 }
678ebfcd 774 else // add data we don't already have ...
2b813b73 775 {
2b813b73 776 // if new description add only if none
678ebfcd 777 if ( m_aDescriptions[nIndex].empty() )
2b813b73
VZ
778 m_aDescriptions[nIndex] = strDesc;
779
780 // if new icon and no existing icon
dfea7acc 781 if ( m_aIcons[nIndex].empty() )
2b813b73
VZ
782 m_aIcons[nIndex] = strIcon;
783
2b813b73 784 // add any new entries...
678ebfcd 785 if ( entry )
2b813b73 786 {
7c2e5dec 787 wxMimeTypeCommands *entryOld = m_aEntries[nIndex];
678ebfcd
VZ
788
789 size_t count = entry->GetCount();
790 for ( size_t i = 0; i < count; i++ )
791 {
792 const wxString& verb = entry->GetVerb(i);
793 if ( !entryOld->HasVerb(verb) )
794 {
795 entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
796 }
797 }
2b293fc7
VZ
798
799 // as we don't store it anywhere, it won't be deleted later as
800 // usual -- do it immediately instead
801 delete entry;
2b813b73
VZ
802 }
803 }
b13d92d1 804 }
4d2976ad 805
678ebfcd
VZ
806 // always add the extensions to this mimetype
807 wxString& exts = m_aExtensions[nIndex];
808
809 // add all extensions we don't have yet
15d4b8cd 810 wxString ext;
678ebfcd
VZ
811 size_t count = strExtensions.GetCount();
812 for ( size_t i = 0; i < count; i++ )
813 {
15d4b8cd
RR
814 ext = strExtensions[i];
815 ext += wxT(' ');
678ebfcd
VZ
816
817 if ( exts.Find(ext) == wxNOT_FOUND )
818 {
819 exts += ext;
820 }
821 }
822
2b813b73 823 // check data integrity
b4a980f4
VZ
824 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
825 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
826 m_aTypes.GetCount() == m_aIcons.GetCount() &&
827 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
cf471cab 828
2b813b73 829 return nIndex;
cf471cab
VS
830}
831
dfea7acc 832wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromExtension(const wxString& ext)
b13d92d1 833{
678ebfcd 834 if (ext.empty() )
2b813b73
VZ
835 return NULL;
836
a6c65e88
VZ
837 InitIfNeeded();
838
1ee17e1c 839 size_t count = m_aExtensions.GetCount();
2b813b73 840 for ( size_t n = 0; n < count; n++ )
678ebfcd 841 {
dfea7acc 842 wxStringTokenizer tk(m_aExtensions[n], wxT(' '));
1ee17e1c 843
678ebfcd
VZ
844 while ( tk.HasMoreTokens() )
845 {
1ee17e1c 846 // consider extensions as not being case-sensitive
d0ee33f5 847 if ( tk.GetNextToken().IsSameAs(ext, false /* no case */) )
678ebfcd 848 {
1ee17e1c 849 // found
678ebfcd 850 wxFileType *fileType = new wxFileType;
1ee17e1c 851 fileType->m_impl->Init(this, n);
03647350 852
678ebfcd 853 return fileType;
1ee17e1c
VZ
854 }
855 }
856 }
b13d92d1 857
678ebfcd 858 return NULL;
b13d92d1
VZ
859}
860
7c2e5dec 861wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mimeType)
b13d92d1 862{
a6c65e88
VZ
863 InitIfNeeded();
864
2b813b73 865 wxFileType * fileType = NULL;
b13d92d1
VZ
866 // mime types are not case-sensitive
867 wxString mimetype(mimeType);
868 mimetype.MakeLower();
869
870 // first look for an exact match
871 int index = m_aTypes.Index(mimetype);
4010aae1 872
2b813b73
VZ
873 if ( index != wxNOT_FOUND )
874 {
875 fileType = new wxFileType;
876 fileType->m_impl->Init(this, index);
877 }
878
879 // then try to find "text/*" as match for "text/plain" (for example)
880 // NB: if mimeType doesn't contain '/' at all, BeforeFirst() will return
881 // the whole string - ok.
882
883 index = wxNOT_FOUND;
884 wxString strCategory = mimetype.BeforeFirst(wxT('/'));
885
b4a980f4 886 size_t nCount = m_aTypes.GetCount();
dfea7acc
DS
887 for ( size_t n = 0; n < nCount; n++ )
888 {
2b813b73 889 if ( (m_aTypes[n].BeforeFirst(wxT('/')) == strCategory ) &&
dfea7acc
DS
890 m_aTypes[n].AfterFirst(wxT('/')) == wxT("*") )
891 {
2b813b73
VZ
892 index = n;
893 break;
b13d92d1
VZ
894 }
895 }
896
2b813b73 897 if ( index != wxNOT_FOUND )
dfea7acc
DS
898 {
899 // don't throw away fileType that was already found
900 if (!fileType)
7bc5ddc6 901 fileType = new wxFileType;
b13d92d1 902 fileType->m_impl->Init(this, index);
b13d92d1 903 }
dfea7acc 904
2b813b73
VZ
905 return fileType;
906}
907
2b813b73
VZ
908wxString wxMimeTypesManagerImpl::GetCommand(const wxString & verb, size_t nIndex) const
909{
910 wxString command, testcmd, sV, sTmp;
911 sV = verb + wxT("=");
dfea7acc 912
2b813b73 913 // list of verb = command pairs for this mimetype
678ebfcd 914 wxMimeTypeCommands * sPairs = m_aEntries [nIndex];
2b813b73
VZ
915
916 size_t i;
15d4b8cd
RR
917 size_t nCount = sPairs->GetCount();
918 for ( i = 0; i < nCount; i++ )
2b813b73 919 {
678ebfcd
VZ
920 sTmp = sPairs->GetVerbCmd (i);
921 if ( sTmp.Contains(sV) )
922 command = sTmp.AfterFirst(wxT('='));
b13d92d1 923 }
dfea7acc 924
2b813b73 925 return command;
b13d92d1
VZ
926}
927
8e124873
VZ
928void wxMimeTypesManagerImpl::AddFallback(const wxFileTypeInfo& filetype)
929{
a6c65e88
VZ
930 InitIfNeeded();
931
3f1aaa16 932 wxString extensions;
8e124873
VZ
933 const wxArrayString& exts = filetype.GetExtensions();
934 size_t nExts = exts.GetCount();
dfea7acc
DS
935 for ( size_t nExt = 0; nExt < nExts; nExt++ )
936 {
937 if ( nExt > 0 )
223d09f6 938 extensions += wxT(' ');
dfea7acc 939
8e124873
VZ
940 extensions += exts[nExt];
941 }
942
943 AddMimeTypeInfo(filetype.GetMimeType(),
944 extensions,
945 filetype.GetDescription());
8e124873
VZ
946}
947
948void wxMimeTypesManagerImpl::AddMimeTypeInfo(const wxString& strMimeType,
949 const wxString& strExtensions,
950 const wxString& strDesc)
951{
2b813b73
VZ
952 // reading mailcap may find image/* , while
953 // reading mime.types finds image/gif and no match is made
954 // this means all the get functions don't work fix this
955 wxString strIcon;
956 wxString sTmp = strExtensions;
a6c65e88 957
2b813b73 958 wxArrayString sExts;
d0ee33f5 959 sTmp.Trim().Trim(false);
2b813b73 960
678ebfcd 961 while (!sTmp.empty())
2b813b73 962 {
dfea7acc 963 sExts.Add(sTmp.AfterLast(wxT(' ')));
2b813b73 964 sTmp = sTmp.BeforeLast(wxT(' '));
8e124873 965 }
2b813b73 966
dfea7acc 967 AddToMimeData(strMimeType, strIcon, NULL, sExts, strDesc, true);
8e124873
VZ
968}
969
696e1ea0 970size_t wxMimeTypesManagerImpl::EnumAllFileTypes(wxArrayString& mimetypes)
1b986aef 971{
a6c65e88
VZ
972 InitIfNeeded();
973
54acce90
VZ
974 mimetypes.Empty();
975
54acce90
VZ
976 size_t count = m_aTypes.GetCount();
977 for ( size_t n = 0; n < count; n++ )
978 {
979 // don't return template types from here (i.e. anything containg '*')
15d4b8cd 980 const wxString &type = m_aTypes[n];
dfea7acc 981 if ( type.Find(wxT('*')) == wxNOT_FOUND )
54acce90
VZ
982 {
983 mimetypes.Add(type);
984 }
985 }
1b986aef 986
54acce90 987 return mimetypes.GetCount();
1b986aef
VZ
988}
989
a6c65e88
VZ
990// ----------------------------------------------------------------------------
991// writing to MIME type files
992// ----------------------------------------------------------------------------
993
2b813b73 994bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft)
a6c65e88 995{
9413e1e3
VZ
996 InitIfNeeded();
997
2b813b73 998 wxArrayString sMimeTypes;
dfea7acc 999 ft->GetMimeTypes(sMimeTypes);
a6c65e88 1000
2b813b73 1001 size_t i;
15d4b8cd
RR
1002 size_t nCount = sMimeTypes.GetCount();
1003 for (i = 0; i < nCount; i ++)
2b813b73 1004 {
15d4b8cd 1005 const wxString &sMime = sMimeTypes.Item(i);
dfea7acc 1006 int nIndex = m_aTypes.Index(sMime);
2b813b73
VZ
1007 if ( nIndex == wxNOT_FOUND)
1008 {
1009 // error if we get here ??
d0ee33f5 1010 return false;
2b813b73
VZ
1011 }
1012 else
1013 {
e9d9f136
VZ
1014 m_aTypes.RemoveAt(nIndex);
1015 m_aEntries.RemoveAt(nIndex);
1016 m_aExtensions.RemoveAt(nIndex);
1017 m_aDescriptions.RemoveAt(nIndex);
1018 m_aIcons.RemoveAt(nIndex);
2b813b73
VZ
1019 }
1020 }
1021 // check data integrity
b4a980f4
VZ
1022 wxASSERT( m_aTypes.GetCount() == m_aEntries.GetCount() &&
1023 m_aTypes.GetCount() == m_aExtensions.GetCount() &&
1024 m_aTypes.GetCount() == m_aIcons.GetCount() &&
1025 m_aTypes.GetCount() == m_aDescriptions.GetCount() );
2b813b73 1026
d0ee33f5 1027 return true;
a6c65e88
VZ
1028}
1029
8e124873 1030#endif
05f616ef 1031 // wxUSE_MIMETYPE && wxUSE_FILE