X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d39e2bbcf77228d8d81507d317c969d2dbf346d2..cbbb1f548962b30c43849e29fd64eceb4d8720e8:/src/unix/mimetype.cpp diff --git a/src/unix/mimetype.cpp b/src/unix/mimetype.cpp index 5c7ba44abb..caa9459875 100644 --- a/src/unix/mimetype.cpp +++ b/src/unix/mimetype.cpp @@ -16,7 +16,7 @@ #pragma hdrstop #endif -#if wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE +#if wxUSE_MIMETYPE && wxUSE_FILE #include "wx/unix/mimetype.h" @@ -32,7 +32,6 @@ #include "wx/confbase.h" #include "wx/ffile.h" -#include "wx/textfile.h" #include "wx/dir.h" #include "wx/tokenzr.h" #include "wx/iconloc.h" @@ -46,39 +45,39 @@ class wxMimeTextFile { public: - wxMimeTextFile() + wxMimeTextFile() { } - + wxMimeTextFile(const wxString& fname) { m_fname = fname; } - + bool Open() { wxFFile file( m_fname ); if (!file.IsOpened()) return false; - + size_t size = file.Length(); wxCharBuffer buffer( size ); file.Read( (void*) (const char*) buffer, size ); - + // Check for valid UTF-8 here? wxString all = wxString::FromUTF8( buffer, size ); - + wxStringTokenizer tok( all, "\n" ); while (tok.HasMoreTokens()) { wxString t = tok.GetNextToken(); t.MakeLower(); - if ((!!t) && (t.Find( "comment" ) != 0) && (t.Find( "generic" ) != 0)) + if ((!!t) && (t.Find( "comment" ) != 0) && (t.Find( "#" ) != 0) && (t.Find( "generic" ) != 0)) m_text.Add( t ); } return true; } - + unsigned int GetLineCount() const { return m_text.GetCount(); } wxString &GetLine( unsigned int line ) { return m_text[line]; } @@ -117,7 +116,7 @@ public: wxString sTmp = GetLine(i).AfterFirst(wxT('=')); return sTmp; } - + private: wxArrayString m_text; wxString m_fname; @@ -139,7 +138,7 @@ void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename) wxMimeTextFile file(filename); if ( !file.Open() ) return; - + // Here, only type 'Application' should be considered. int nIndex = file.pIndexOf( "Type=" ); if (nIndex != wxNOT_FOUND && file.GetCmd(nIndex) != "application") @@ -162,7 +161,7 @@ void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename) #if wxUSE_INTL // try "Name[locale name]" first wxLocale *locale = wxGetLocale(); if ( locale ) - nIndex = file.pIndexOf(_T("Name[")+locale->GetName()+_T("]=")); + nIndex = file.pIndexOf(wxT("Name[")+locale->GetName()+wxT("]=")); #endif // wxUSE_INTL if(nIndex == wxNOT_FOUND) nIndex = file.pIndexOf( wxT("Name=") ); @@ -174,7 +173,7 @@ void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename) nIndex = wxNOT_FOUND; #if wxUSE_INTL // try "Icon[locale name]" first if ( locale ) - nIndex = file.pIndexOf(_T("Icon[")+locale->GetName()+_T("]=")); + nIndex = file.pIndexOf(wxT("Icon[")+locale->GetName()+wxT("]=")); #endif // wxUSE_INTL if(nIndex == wxNOT_FOUND) nIndex = file.pIndexOf( wxT("Icon=") ); @@ -199,7 +198,7 @@ void wxMimeTypesManagerImpl::LoadXDGApp(const wxString& filename) sCmd.Replace(wxT("%i"), nameicon); sCmd.Replace(wxT("%m"), namemini); - wxStringTokenizer tokenizer(mimetypes, _T(";")); + wxStringTokenizer tokenizer(mimetypes, wxT(";")); while(tokenizer.HasMoreTokens()) { wxString mimetype = tokenizer.GetNextToken().Lower(); nIndex = m_aTypes.Index(mimetype); @@ -223,7 +222,7 @@ void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString& dirname) wxString filename; // Look into .desktop files - bool cont = dir.GetFirst(&filename, _T("*.desktop"), wxDIR_FILES); + bool cont = dir.GetFirst(&filename, wxT("*.desktop"), wxDIR_FILES); while (cont) { wxFileName p(dirname, filename); @@ -231,10 +230,10 @@ void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString& dirname) cont = dir.GetNext(&filename); } -#if 0 +#if 0 // RR: I'm not sure this makes any sense. On my system we'll just // scan the YAST2 and other useless directories - + // Look recursively into subdirs cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS); while (cont) @@ -248,6 +247,31 @@ void wxMimeTypesManagerImpl::LoadXDGAppsFilesFromDir(const wxString& dirname) } +void wxMimeTypesManagerImpl::LoadXDGGlobs(const wxString& filename) +{ + if ( !wxFileName::FileExists(filename) ) + return; + + wxLogTrace(TRACE_MIME, wxT("loading XDG globs file from %s"), filename.c_str()); + + wxMimeTextFile file(filename); + if ( !file.Open() ) + return; + + size_t i; + for (i = 0; i < file.GetLineCount(); i++) + { + wxStringTokenizer tok( file.GetLine(i), ":" ); + wxString mime = tok.GetNextToken(); + wxString ext = tok.GetNextToken(); + ext.Remove( 0, 2 ); + wxArrayString exts; + exts.Add( ext ); + + AddToMimeData(mime, wxEmptyString, NULL, exts, wxEmptyString, true ); + } +} + // ---------------------------------------------------------------------------- // wxFileTypeImpl (Unix) // ---------------------------------------------------------------------------- @@ -482,18 +506,25 @@ void wxMimeTypesManagerImpl::InitIfNeeded() { // set the flag first to prevent recursion m_initialized = true; - - wxString wm = wxTheApp->GetTraits()->GetDesktopEnvironment(); - - if (wm == wxT("KDE")) - Initialize( wxMAILCAP_KDE ); - else if (wm == wxT("GNOME")) - Initialize( wxMAILCAP_GNOME ); - else - Initialize(); + + int mailcapStyles = wxMAILCAP_ALL; + if ( wxAppTraits * const traits = wxApp::GetTraitsIfExists() ) + { + wxString wm = traits->GetDesktopEnvironment(); + + if ( wm == "KDE" ) + mailcapStyles = wxMAILCAP_KDE; + else if ( wm == "GNOME" ) + mailcapStyles = wxMAILCAP_GNOME; + //else: unknown, use the default + } + + Initialize(mailcapStyles); } } + + // read system and user mailcaps and other files void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, const wxString& sExtraDir) @@ -501,9 +532,13 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, #ifdef __VMS // XDG tables are never installed on OpenVMS return; -#endif +#else - // Load desktop files for Gnome, and then override them with the Gnome defaults. + // Read MIME type - extension associations + LoadXDGGlobs( "/usr/share/mime/globs" ); + LoadXDGGlobs( "/usr/local/share/mime/globs" ); + + // Load desktop files for XDG, and then override them with the defaults. // We will override them one desktop file at a time, rather // than one mime type at a time, but it should be a reasonable // heuristic. @@ -525,7 +560,7 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, xdgDataDirs += ':'; xdgDataDirs += sExtraDir; } - + wxArrayString dirs; wxStringTokenizer tokenizer(xdgDataDirs, ":"); while ( tokenizer.HasMoreTokens() ) @@ -573,9 +608,8 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, { if (textfile.GetLine(i).Find(wxT("=")) != wxNOT_FOUND) { - wxString mimeType = textfile.GetVerb(i); wxString desktopFile = textfile.GetCmd(i); - + if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND) { deskTopFilesSeen.Add(desktopFile); @@ -586,7 +620,7 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, if (desktopPath.Last() != '/') desktopPath += '/'; desktopPath += "applications/"; desktopPath += desktopFile; - + if (wxFileExists(desktopPath)) LoadXDGApp(desktopPath); } @@ -597,6 +631,7 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles, } } } +#endif } // clear data so you can read another group of WM files @@ -688,16 +723,35 @@ int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType, int nIndex = m_aTypes.Index(mimeType); if ( nIndex == wxNOT_FOUND ) { - // new file type - m_aTypes.Add(mimeType); - m_aIcons.Add(strIcon); - m_aEntries.Add(entry ? entry : new wxMimeTypeCommands); + // We put MIME types containing "application" at the end, so that + // if the MIME type for the extention "htm" is searched for, it will + // rather find "text/html" than "application/x-mozilla-bookmarks". + if (mimeType.Find( "application" ) == 0) + { + // new file type + m_aTypes.Add(mimeType); + m_aIcons.Add(strIcon); + m_aEntries.Add(entry ? entry : new wxMimeTypeCommands); - // change nIndex so we can use it below to add the extensions - m_aExtensions.Add(wxEmptyString); - nIndex = m_aExtensions.size() - 1; + // change nIndex so we can use it below to add the extensions + m_aExtensions.Add(wxEmptyString); + nIndex = m_aExtensions.size() - 1; - m_aDescriptions.Add(strDesc); + m_aDescriptions.Add(strDesc); + } + else + { + // new file type + m_aTypes.Insert(mimeType,0); + m_aIcons.Insert(strIcon,0); + m_aEntries.Insert(entry ? entry : new wxMimeTypeCommands,0); + + // change nIndex so we can use it below to add the extensions + m_aExtensions.Insert(wxEmptyString,0); + nIndex = 0; + + m_aDescriptions.Insert(strDesc,0); + } } else // yes, we already have it { @@ -815,6 +869,7 @@ wxFileType * wxMimeTypesManagerImpl::GetFileTypeFromMimeType(const wxString& mim // first look for an exact match int index = m_aTypes.Index(mimetype); + if ( index != wxNOT_FOUND ) { fileType = new wxFileType; @@ -973,4 +1028,4 @@ bool wxMimeTypesManagerImpl::Unassociate(wxFileType *ft) } #endif - // wxUSE_MIMETYPE && wxUSE_FILE && wxUSE_TEXTFILE + // wxUSE_MIMETYPE && wxUSE_FILE