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