]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/mimetype.cpp
Part 2 of 3 commits bringing proper Doxygen path usage without use of @wxheader.
[wxWidgets.git] / src / unix / mimetype.cpp
index 20b96b5c21ba32a35a6ecf1c7d4d668ae6677a41..bf761cccea8962679656283278a608dd81bb9687 100644 (file)
@@ -141,24 +141,8 @@ public:
         wxString sTmp = GetLine(i).AfterFirst(wxT('='));
         return sTmp;
     }
         wxString sTmp = GetLine(i).AfterFirst(wxT('='));
         return sTmp;
     }
-
-protected:
-    // we override this virtual method because we want to always use UTF-8
-    // conversion allowing for invalid characters as MIME information files
-    // often contain lines in different encodings and can't be read using any
-    // single conversion in Unicode build, so we just try to read what we can
-    // suing the most common encoding (UTF-8 is almost ubiquitous nowadays) and
-    // ignore the rest
-    virtual bool OnRead(const wxMBConv& WXUNUSED(conv))
-    {
-        return wxTextFile::OnRead(
-                    wxMBConvUTF8(wxMBConvUTF8::MAP_INVALID_UTF8_TO_PUA));
-    }
 };
 
 };
 
-// in case we're compiling in non-GUI mode
-class WXDLLEXPORT wxIcon;
-
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
@@ -511,6 +495,9 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
     wxString dirname = dirbase;
     dirname << wxT("/mime-info");
 
     wxString dirname = dirbase;
     dirname << wxT("/mime-info");
 
+    // Don't complain if we don't have permissions to read - it confuses users
+    wxLogNull logNull;
+
     if ( !wxDir::Exists(dirname) )
         return;
 
     if ( !wxDir::Exists(dirname) )
         return;
 
@@ -903,6 +890,10 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase,
 {
     wxFileName dirname(dirbase, wxEmptyString);
     dirname.AppendDir(subdir);
 {
     wxFileName dirname(dirbase, wxEmptyString);
     dirname.AppendDir(subdir);
+
+    // Don't complain if we don't have permissions to read - it confuses users
+    wxLogNull logNull;
+
     wxDir dir(dirname.GetPath());
     if(! dir.IsOpened())
         return;
     wxDir dir(dirname.GetPath());
     if(! dir.IsOpened())
         return;
@@ -930,6 +921,9 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase,
 void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString& dirname,
                                             const wxArrayString& icondirs)
 {
 void wxMimeTypesManagerImpl::LoadKDELinkFilesFromDir(const wxString& dirname,
                                             const wxArrayString& icondirs)
 {
+    // Don't complain if we don't have permissions to read - it confuses users
+    wxLogNull logNull;
+
     if(! wxDir::Exists(dirname))
         return;
 
     if(! wxDir::Exists(dirname))
         return;
 
@@ -1019,7 +1013,7 @@ void wxMimeTypesManagerImpl::LoadKDEApp(const wxString& filename)
     wxStringTokenizer tokenizer(mimetypes, _T(";"));
     while(tokenizer.HasMoreTokens()) {
         wxString mimetype = tokenizer.GetNextToken().Lower();
     wxStringTokenizer tokenizer(mimetypes, _T(";"));
     while(tokenizer.HasMoreTokens()) {
         wxString mimetype = tokenizer.GetNextToken().Lower();
-        int nIndex = m_aTypes.Index(mimetype);
+        nIndex = m_aTypes.Index(mimetype);
         if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
             wxMimeTypeCommands* entry = m_aEntries[nIndex];
             entry->AddOrReplaceVerb(wxT("open"), sCmd);
         if(nIndex != wxNOT_FOUND) { // is this a known MIME type?
             wxMimeTypeCommands* entry = m_aEntries[nIndex];
             entry->AddOrReplaceVerb(wxT("open"), sCmd);
@@ -1029,6 +1023,9 @@ void wxMimeTypesManagerImpl::LoadKDEApp(const wxString& filename)
 
 void wxMimeTypesManagerImpl::LoadKDEAppsFilesFromDir(const wxString& dirname)
 {
 
 void wxMimeTypesManagerImpl::LoadKDEAppsFilesFromDir(const wxString& dirname)
 {
+    // Don't complain if we don't have permissions to read - it confuses users
+    wxLogNull logNull;
+
     if(! wxDir::Exists(dirname))
         return;
     wxDir dir(dirname);
     if(! wxDir::Exists(dirname))
         return;
     wxDir dir(dirname);
@@ -1367,14 +1364,15 @@ size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
 
 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
 {
 
 bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
 {
-    wxString strExtensions = m_manager->GetExtension(m_index[0]);
+    const wxString strExtensions = m_manager->GetExtension(m_index[0]);
     extensions.Empty();
 
     // one extension in the space or comma-delimited list
     wxString strExt;
     extensions.Empty();
 
     // one extension in the space or comma-delimited list
     wxString strExt;
-    for ( const wxChar *p = strExtensions; /* nothing */; p++ )
+    wxString::const_iterator end = strExtensions.end();
+    for ( wxString::const_iterator p = strExtensions.begin(); /* nothing */; ++p )
     {
     {
-        if ( *p == wxT(' ') || *p == wxT(',') || *p == wxT('\0') )
+        if ( p == end || *p == wxT(' ') || *p == wxT(',') )
         {
             if ( !strExt.empty() )
             {
         {
             if ( !strExt.empty() )
             {
@@ -1384,7 +1382,7 @@ bool wxFileTypeImpl::GetExtensions(wxArrayString& extensions)
             //else: repeated spaces
             // (shouldn't happen, but it's not that important if it does happen)
 
             //else: repeated spaces
             // (shouldn't happen, but it's not that important if it does happen)
 
-            if ( *p == wxT('\0') )
+            if ( p == end )
                 break;
         }
         else if ( *p == wxT('.') )
                 break;
         }
         else if ( *p == wxT('.') )
@@ -1536,6 +1534,85 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
         GetKDEMimeInfo(sExtraDir);
 #endif
 
         GetKDEMimeInfo(sExtraDir);
 #endif
 
+    // Load desktop files for Gnome, and then override them with the Gnome 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.
+    if (mailcapStyles & wxMAILCAP_GNOME)
+    {
+        wxString xdgDataHome = wxGetenv(wxT("XDG_DATA_HOME"));
+        if ( xdgDataHome.empty() )
+            xdgDataHome = wxGetHomeDir() + wxT("/.local/share");
+        wxString xdgDataDirs = wxGetenv(wxT("XDG_DATA_DIRS"));
+        if ( xdgDataDirs.empty() )
+            xdgDataDirs = wxT("/usr/local/share:/usr/share:/usr/share/gnome");
+        wxArrayString dirs;
+
+        wxStringTokenizer tokenizer(xdgDataDirs, wxT(":"));
+        while ( tokenizer.HasMoreTokens() )
+        {
+            wxString p = tokenizer.GetNextToken();
+            dirs.Add(p);
+        }
+        dirs.insert(dirs.begin(), xdgDataHome);
+
+        wxString defaultsList;
+        size_t i;
+        for (i = 0; i < dirs.GetCount(); i++)
+        {
+            wxString f(dirs[i] + wxT("/applications/defaults.list"));
+            if (wxFileExists(f))
+            {
+                defaultsList = f;
+                break;
+            }
+        }
+
+        // Load application files and associate them to corresponding mime types.
+        size_t nDirs = dirs.GetCount();
+        for (size_t nDir = 0; nDir < nDirs; nDir++)
+        {
+            wxString dirStr(dirs[nDir] + wxT("/applications"));
+            LoadKDEAppsFilesFromDir(dirStr);
+        }
+
+        if (!defaultsList.IsEmpty())
+        {
+            wxArrayString deskTopFilesSeen;
+
+            wxMimeTextFile textfile(defaultsList);
+            if ( textfile.Open() )
+            {
+                int nIndex = textfile.pIndexOf( wxT("[Default Applications]") );
+                if (nIndex != wxNOT_FOUND)
+                {
+                    for (i = nIndex+1; i < textfile.GetLineCount(); i++)
+                    {
+                        if (textfile[i].Find(wxT("=")) != wxNOT_FOUND)
+                        {
+                            wxString mimeType = textfile.GetVerb(i);
+                            wxString desktopFile = textfile.GetCmd(i);
+
+                            if (deskTopFilesSeen.Index(desktopFile) == wxNOT_FOUND)
+                            {
+                                deskTopFilesSeen.Add(desktopFile);
+                                size_t j;
+                                for (j = 0; j < dirs.GetCount(); j++)
+                                {
+                                    wxString desktopPath(dirs[j] + wxT("/applications/") + desktopFile);
+                                    if (wxFileExists(desktopPath))
+                                    {
+                                        LoadKDEApp(desktopPath);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     m_mailcapStylesInited |= mailcapStyles;
 }
 
     m_mailcapStylesInited |= mailcapStyles;
 }
 
@@ -2488,8 +2565,7 @@ wxMimeTypesManagerImpl::ProcessOtherMailcapField(MailcapLineData& data,
     }
 
     // is this something of the form foo=bar?
     }
 
     // is this something of the form foo=bar?
-    const wxChar *pEq = wxStrchr(curField, wxT('='));
-    if ( pEq != NULL )
+    if ( curField.find('=') != wxString::npos )
     {
         // split "LHS = RHS" in 2
         wxString lhs = curField.BeforeFirst(wxT('=')),
     {
         // split "LHS = RHS" in 2
         wxString lhs = curField.BeforeFirst(wxT('=')),
@@ -2613,8 +2689,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
             Field_Type,
             Field_OpenCmd,
             Field_Other
             Field_Type,
             Field_OpenCmd,
             Field_Other
-        }
-        currentToken = Field_Type;
+        } currentToken = Field_Type;
 
         // the flags and field values on the current line
         MailcapLineData data;
 
         // the flags and field values on the current line
         MailcapLineData data;