]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/mimetype.cpp
don't treat filenames starting with dot as having empty name and extension equal...
[wxWidgets.git] / src / unix / mimetype.cpp
index a8c0329c914cef4e15a442a4bdb1d65c7c6e4e85..e38775b6718748e1676099cd70bd6c3e6f823379 100644 (file)
@@ -40,7 +40,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
 // headers
 // ----------------------------------------------------------------------------
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "mimetype.h"
 #endif
 
     #pragma implementation "mimetype.h"
 #endif
 
 
 #ifndef WX_PRECOMP
   #include "wx/string.h"
 
 #ifndef WX_PRECOMP
   #include "wx/string.h"
-  #if wxUSE_GUI
-    #include "wx/icon.h"
-
-  #endif
 #endif //WX_PRECOMP
 
 
 #endif //WX_PRECOMP
 
 
@@ -78,6 +74,7 @@
 #include "wx/utils.h"
 #include "wx/tokenzr.h"
 #include "wx/iconloc.h"
 #include "wx/utils.h"
 #include "wx/tokenzr.h"
 #include "wx/iconloc.h"
+#include "wx/filename.h"
 
 #include "wx/unix/mimetype.h"
 
 
 #include "wx/unix/mimetype.h"
 
@@ -575,17 +572,19 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename,
                 else if (sTmp.Contains( wxT("icon_filename=") ) )
                 {
                     curIconFile = sTmp.AfterFirst(wxT('='));
                 else if (sTmp.Contains( wxT("icon_filename=") ) )
                 {
                     curIconFile = sTmp.AfterFirst(wxT('='));
+            
                     if (!wxFileExists(curIconFile))
                     {
                         size_t nDirs = dirs.GetCount();
                         for (size_t nDir = 0; nDir < nDirs; nDir++)
                         {
                     if (!wxFileExists(curIconFile))
                     {
                         size_t nDirs = dirs.GetCount();
                         for (size_t nDir = 0; nDir < nDirs; nDir++)
                         {
-                            wxString newFile;
-                            newFile.Printf(wxT("%s/pixmaps/document-icons/%s.png"),
-                                           dirs[nDir].c_str(),
-                                           curIconFile.c_str());
-                            if (wxFileExists(newFile))
-                                curIconFile = newFile;
+                            wxFileName newFile( curIconFile );
+                            newFile.SetPath( dirs[nDir] );
+                            newFile.AppendDir( wxT("pixmaps") );
+                            newFile.AppendDir( wxT("document-icons") );
+                            newFile.SetExt( wxT("png") );
+                            if (newFile.FileExists())
+                                curIconFile = newFile.GetFullPath();
                         }
                     }
                 }
                         }
                     }
                 }
@@ -632,11 +631,9 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename,
         // ignore blank lines
         nLine ++;
     } // end of while, save any data
         // ignore blank lines
         nLine ++;
     } // end of while, save any data
+    
     if (! curMimeType.empty())
     if (! curMimeType.empty())
-    {
         AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc);
         AddToMimeData ( curMimeType, curIconFile, entry, strExtensions, strDesc);
-    }
-
 }
 
 
 }
 
 
@@ -681,7 +678,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file
                  wxLogTrace(TRACE_MIME,
                             wxT("--- At end of Gnome file  finding mimetype %s  ---"),
                             curMimeType.c_str());
                  wxLogTrace(TRACE_MIME,
                             wxT("--- At end of Gnome file  finding mimetype %s  ---"),
                             curMimeType.c_str());
-
+                
                  AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString);
             }
 
                  AddMimeTypeInfo(curMimeType, curExtList, wxEmptyString);
             }
 
@@ -739,7 +736,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
 
     wxString dirname = dirbase;
     dirname << wxT("/mime-info");
 
     wxString dirname = dirbase;
     dirname << wxT("/mime-info");
-
+    
     if ( !wxDir::Exists(dirname) )
         return;
 
     if ( !wxDir::Exists(dirname) )
         return;
 
@@ -751,7 +748,8 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
     dirname += wxT('/');
 
     wxString filename;
     dirname += wxT('/');
 
     wxString filename;
-    bool cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES);
+    bool cont;
+    cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES);
     while ( cont )
     {
         LoadGnomeMimeTypesFromMimeFile(dirname + filename);
     while ( cont )
     {
         LoadGnomeMimeTypesFromMimeFile(dirname + filename);
@@ -766,21 +764,65 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
 
         cont = dir.GetNext(&filename);
     }
 
         cont = dir.GetNext(&filename);
     }
-}
 
 
+    // Hack alert: We scan all icons and deduce the
+    //             mime-type from the file name.
+    dirname = dirbase;
+    dirname << wxT("/pixmaps/document-icons");
+    
+    // these are always empty in this file
+    wxArrayString strExtensions;
+    wxString strDesc;
+    
+    if ( !wxDir::Exists(dirname) )
+    {
+        // Jst test for default GPE dir also
+        dirname = wxT("/usr/share/gpe/pixmaps/default/filemanager/document-icons");
+        
+        if ( !wxDir::Exists(dirname) )
+           return;
+    }
+
+    wxDir dir2( dirname );
 
 
+    cont = dir2.GetFirst(&filename, wxT("gnome-*.png"), wxDIR_FILES);
+    while ( cont )
+    {
+        wxString mimeType = filename;
+        mimeType.Remove( 0, 6 ); // remove "gnome-"
+        mimeType.Remove( mimeType.Len()-4, 4 ); // remove ".png"
+        int pos = mimeType.Find( wxT("-") );
+        if (pos != wxNOT_FOUND)
+        {
+            mimeType.SetChar( pos, wxT('/') );
+            wxString iconFile = dirname;
+            iconFile << wxT("/");
+            iconFile << filename;
+            AddToMimeData ( mimeType, iconFile, NULL, strExtensions, strDesc, TRUE );
+        }
 
 
+        cont = dir2.GetNext(&filename);
+    }
+}
 
 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir)
 {
     wxArrayString dirs;
 
 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir)
 {
     wxArrayString dirs;
+    
+    wxString gnomedir = wxGetenv( wxT("GNOMEDIR") );;
+    if (!gnomedir.empty())
+    {
+        gnomedir << wxT("/share");
+        dirs.Add( gnomedir );
+    }
+
     dirs.Add(wxT("/usr/share"));
     dirs.Add(wxT("/usr/local/share"));
     dirs.Add(wxT("/usr/share"));
     dirs.Add(wxT("/usr/local/share"));
-
-    wxString gnomedir;
-    wxGetHomeDir( &gnomedir );
-    gnomedir += wxT("/.gnome");
+    
+    gnomedir = wxGetHomeDir();
+    gnomedir << wxT("/.gnome");
     dirs.Add( gnomedir );
     dirs.Add( gnomedir );
+    
     if (!sExtraDir.empty()) dirs.Add( sExtraDir );
 
     size_t nDirs = dirs.GetCount();
     if (!sExtraDir.empty()) dirs.Add( sExtraDir );
 
     size_t nDirs = dirs.GetCount();
@@ -808,20 +850,21 @@ void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir)
 // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
 // for now write to .kdelnk but should eventually do .desktop instead (in preference??)
 
 // kde writing; see http://webcvs.kde.org/cgi-bin/cvsweb.cgi/~checkout~/kdelibs/kio/DESKTOP_ENTRY_STANDARD
 // for now write to .kdelnk but should eventually do .desktop instead (in preference??)
 
-bool wxMimeTypesManagerImpl::CheckKDEDirsExist ( const wxString & sOK, const wxString & sTest )
-
-    {
+bool wxMimeTypesManagerImpl::CheckKDEDirsExist ( const wxString &sOK, const wxString &sTest )
+{
     if (sTest.empty())
     if (sTest.empty())
-        {
-            if (wxDir::Exists(sOK)) return TRUE;
-            else return FALSE;
-        }
+    {
+        if (wxDir::Exists(sOK))
+            return TRUE;
+        else
+            return FALSE;
+    }
     else
     else
-        {
-            wxString sStart = sOK + wxT("/") + sTest.BeforeFirst(wxT('/'));
-            if (!wxDir::Exists(sStart))  wxMkdir(sStart);
-            wxString sEnd = sTest.AfterFirst(wxT('/'));
-            return CheckKDEDirsExist(sStart, sEnd);
+    {
+        wxString sStart = sOK + wxT("/") + sTest.BeforeFirst(wxT('/'));
+        if (!wxDir::Exists(sStart))  wxMkdir(sStart);
+        wxString sEnd = sTest.AfterFirst(wxT('/'));
+        return CheckKDEDirsExist(sStart, sEnd);
     }
 }
 
     }
 }
 
@@ -929,8 +972,6 @@ bool wxMimeTypesManagerImpl::WriteKDEMimeFile(int index, bool delete_index)
     appoutfile.Close ();
 
     return bTemp;
     appoutfile.Close ();
 
     return bTemp;
-
-
 }
 
 void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
 }
 
 void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
@@ -1007,7 +1048,6 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
     }
     sExts.Add(mime_extension);
 
     }
     sExts.Add(mime_extension);
 
-
     // ok, now we can take care of icon:
 
     nIndex = file.pIndexOf(_T("Icon="));
     // ok, now we can take care of icon:
 
     nIndex = file.pIndexOf(_T("Icon="));
@@ -1016,6 +1056,8 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
         strIcon = file.GetCmd(nIndex);
         wxLogTrace(TRACE_MIME, wxT("  icon %s"), strIcon.c_str());
         //it could be the real path, but more often a short name
         strIcon = file.GetCmd(nIndex);
         wxLogTrace(TRACE_MIME, wxT("  icon %s"), strIcon.c_str());
         //it could be the real path, but more often a short name
+    
+    
         if (!wxFileExists(strIcon))
         {
             // icon is just the short name
         if (!wxFileExists(strIcon))
         {
             // icon is just the short name
@@ -1025,12 +1067,17 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
                 // in many locations, at least ~/.kde and $KDEDIR
                 size_t nDir, nDirs = icondirs.GetCount();
                 for ( nDir = 0; nDir < nDirs; nDir++ )
                 // in many locations, at least ~/.kde and $KDEDIR
                 size_t nDir, nDirs = icondirs.GetCount();
                 for ( nDir = 0; nDir < nDirs; nDir++ )
-                    if (wxFileExists(icondirs[nDir] + strIcon))
+                {
+                    wxFileName fnameIcon( strIcon );
+                    wxFileName fname( icondirs[nDir], fnameIcon.GetName() );
+                    fname.SetExt( wxT("png") );
+                    if (fname.FileExists())
                     {
                     {
-                        strIcon.Prepend(icondirs[nDir]);
+                        strIcon = fname.GetFullPath();
                         wxLogTrace(TRACE_MIME, wxT("  iconfile %s"), strIcon.c_str());
                         break;
                     }
                         wxLogTrace(TRACE_MIME, wxT("  iconfile %s"), strIcon.c_str());
                         break;
                     }
+                }
             }
         }
     }
             }
         }
     }
@@ -1124,13 +1171,100 @@ void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir)
     wxArrayString dirs;
     wxArrayString icondirs;
 
     wxArrayString dirs;
     wxArrayString icondirs;
 
+    // FIXME: This code is heavily broken. There are three bugs in it:
+    //        1) it uses only KDEDIR, which is deprecated, instead of using
+    //           list of paths from KDEDIRS and using KDEDIR only if KDEDIRS
+    //           is not set
+    //        2) it doesn't look into ~/.kde/share/config/kdeglobals where
+    //           user's settings are stored and thus *ignores* user's settings
+    //           instead of respecting them
+    //        3) it "tries to guess KDEDIR" and "tries a few likely theme
+    //           names", both of which is completely arbitrary; instead, the
+    //           code should give up if KDEDIR(S) is not set and/or the icon
+    //           theme cannot be determined, because it means that the user is
+    //           not using KDE (and thus is not interested in KDE icons anyway)
+    
+    // the variable $KDEDIR is set when KDE is running
+    wxString kdedir = wxGetenv( wxT("KDEDIR") );
+    
+    if (!kdedir.empty())
+    {
+        // $(KDEDIR)/share/config/kdeglobals holds info
+        // the current icons theme
+        wxFileName configFile( kdedir, wxEmptyString );
+        configFile.AppendDir( wxT("share") );
+        configFile.AppendDir( wxT("config") );
+        configFile.SetName( wxT("kdeglobals") );
+           
+        wxTextFile config;
+        if (configFile.FileExists() && config.Open(configFile.GetFullPath()))
+        {
+            // $(KDEDIR)/share/config -> $(KDEDIR)/share
+            configFile.RemoveDir( configFile.GetDirCount()-1 );
+            // $(KDEDIR)/share/ -> $(KDEDIR)/share/icons
+            configFile.AppendDir( wxT("icons") );
+
+            // Check for entry
+            wxString theme(wxT("default.kde"));
+            size_t cnt = config.GetLineCount();
+            for (size_t i = 0; i < cnt; i++)
+            {
+                if (config[i].StartsWith(wxT("Theme="), &theme/*rest*/))
+                    break;
+            }
+            configFile.AppendDir(theme);
+        }
+        else
+        {
+            // $(KDEDIR)/share/config -> $(KDEDIR)/share
+            configFile.RemoveDir( configFile.GetDirCount()-1 );
+            // $(KDEDIR)/share/ -> $(KDEDIR)/share/icons
+            configFile.AppendDir( wxT("icons") );
+            // $(KDEDIR)/share/icons -> $(KDEDIR)/share/icons/default.kde
+            configFile.AppendDir( wxT("default.kde") );
+        }
+        
+        configFile.SetName( wxEmptyString );
+        configFile.AppendDir( wxT("32x32") );
+        configFile.AppendDir( wxT("mimetypes") );
+        
+        // Just try a few likely icons theme names
+        
+        int pos = configFile.GetDirCount()-3;
+        
+        if (!wxDir::Exists(configFile.GetPath()))
+        {
+            configFile.RemoveDir( pos );
+            configFile.InsertDir( pos, wxT("default.kde") );
+        }
+        
+        if (!wxDir::Exists(configFile.GetPath()))
+        {
+            configFile.RemoveDir( pos );
+            configFile.InsertDir( pos, wxT("default") );
+        }
+        
+        if (!wxDir::Exists(configFile.GetPath()))
+        {
+            configFile.RemoveDir( pos );
+            configFile.InsertDir( pos, wxT("crystalsvg") );
+        }
+        
+        if (!wxDir::Exists(configFile.GetPath()))
+        {
+            configFile.RemoveDir( pos );
+            configFile.InsertDir( pos, wxT("crystal") );
+        }
+        
+        if (wxDir::Exists(configFile.GetPath()))
+            icondirs.Add( configFile.GetFullPath() );
+    }
+
     // settings in ~/.kde have maximal priority
     dirs.Add(wxGetHomeDir() + wxT("/.kde/share"));
     icondirs.Add(wxGetHomeDir() + wxT("/.kde/share/icons/"));
 
     // settings in ~/.kde have maximal priority
     dirs.Add(wxGetHomeDir() + wxT("/.kde/share"));
     icondirs.Add(wxGetHomeDir() + wxT("/.kde/share/icons/"));
 
-    // the variable KDEDIR is set when KDE is running
-    const wxChar *kdedir = wxGetenv( wxT("KDEDIR") );
-    if ( kdedir )
+    if (kdedir)
     {
         dirs.Add( wxString(kdedir) + wxT("/share") );
         icondirs.Add( wxString(kdedir) + wxT("/share/icons/") );
     {
         dirs.Add( wxString(kdedir) + wxT("/share") );
         icondirs.Add( wxString(kdedir) + wxT("/share/icons/") );
@@ -1345,6 +1479,26 @@ wxMimeTypesManagerImpl::wxMimeTypesManagerImpl()
     m_mailcapStylesInited = 0;
 }
 
     m_mailcapStylesInited = 0;
 }
 
+void wxMimeTypesManagerImpl::InitIfNeeded()
+{
+    if ( !m_initialized )
+    {
+        // set the flag first to prevent recursion
+        m_initialized = TRUE;
+    
+#if 0
+    wxString wm = wxGetenv( wxT("WINDOWMANAGER") );
+    
+    if (wm.Find( wxT("kde") ) != wxNOT_FOUND)
+        Initialize( wxMAILCAP_KDE|wxMAILCAP_STANDARD );
+    else if (wm.Find( wxT("gnome") ) != wxNOT_FOUND)
+        Initialize( wxMAILCAP_GNOME|wxMAILCAP_STANDARD );
+    else
+#endif
+            Initialize();
+    }
+}
+
 // read system and user mailcaps and other files
 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
                                         const wxString& sExtraDir)
 // read system and user mailcaps and other files
 void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
                                         const wxString& sExtraDir)
@@ -1355,11 +1509,11 @@ void wxMimeTypesManagerImpl::Initialize(int mailcapStyles,
         GetMimeInfo(sExtraDir);
 
     // read GNOME tables
         GetMimeInfo(sExtraDir);
 
     // read GNOME tables
-    if ( mailcapStyles & wxMAILCAP_GNOME)
+    if (mailcapStyles & wxMAILCAP_GNOME)
         GetGnomeMimeInfo(sExtraDir);
 
     // read KDE tables
         GetGnomeMimeInfo(sExtraDir);
 
     // read KDE tables
-    if ( mailcapStyles & wxMAILCAP_KDE)
+    if (mailcapStyles & wxMAILCAP_KDE)
         GetKDEMimeInfo(sExtraDir);
 
     m_mailcapStylesInited |= mailcapStyles;
         GetKDEMimeInfo(sExtraDir);
 
     m_mailcapStylesInited |= mailcapStyles;
@@ -2135,8 +2289,7 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
                 // the string is quoted and ends at the matching quote
                 pEnd = wxStrchr(++pc, wxT('"'));
                 if ( pEnd == NULL ) {
                 // the string is quoted and ends at the matching quote
                 pEnd = wxStrchr(++pc, wxT('"'));
                 if ( pEnd == NULL ) {
-                    wxLogWarning(_("Mime.types file %s, line %d: unterminated "
-                                   "quoted string."),
+                    wxLogWarning(_("Mime.types file %s, line %d: unterminated quoted string."),
                                  strFileName.c_str(), nLine + 1);
                 }
             }
                                  strFileName.c_str(), nLine + 1);
                 }
             }
@@ -2177,7 +2330,7 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
             else if ( strLHS == wxT("exts") ) {
                 strExtensions = strRHS;
             }
             else if ( strLHS == wxT("exts") ) {
                 strExtensions = strRHS;
             }
-            else if ( strLHS != _T("icon") )
+            else if ( strLHS == _T("icon") )
             {
                 // this one is simply ignored: it usually refers to Netscape
                 // built in icons which are useless for us anyhow
             {
                 // this one is simply ignored: it usually refers to Netscape
                 // built in icons which are useless for us anyhow
@@ -2412,9 +2565,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                             // something is wrong, bail out
                             cont = FALSE;
 
                             // something is wrong, bail out
                             cont = FALSE;
 
-                            wxLogDebug(wxT("Mailcap file %s, line %lu: "
-                                           "'\\' on the end of the last line "
-                                           "ignored."),
+                            wxLogDebug(wxT("Mailcap file %s, line %lu: '\\' on the end of the last line ignored."),
                                        strFileName.c_str(),
                                        (unsigned long)nLine + 1);
                         }
                                        strFileName.c_str(),
                                        (unsigned long)nLine + 1);
                         }
@@ -2478,9 +2629,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                                 // this might be useful for the programmer
                                 wxLogDebug
                                 (
                                 // this might be useful for the programmer
                                 wxLogDebug
                                 (
-                                    wxT("Mailcap file %s, line %lu: "
-                                        "unknown field '%s' for the "
-                                        "MIME type '%s' ignored."),
+                                    wxT("Mailcap file %s, line %lu: unknown field '%s' for the MIME type '%s' ignored."),
                                     strFileName.c_str(),
                                     (unsigned long)nLine + 1,
                                     curField.c_str(),
                                     strFileName.c_str(),
                                     (unsigned long)nLine + 1,
                                     curField.c_str(),
@@ -2519,8 +2668,7 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
         // check that we really read something reasonable
         if ( currentToken < Field_Other )
         {
         // check that we really read something reasonable
         if ( currentToken < Field_Other )
         {
-            wxLogWarning(_("Mailcap file %s, line %d: incomplete entry "
-                           "ignored."),
+            wxLogWarning(_("Mailcap file %s, line %d: incomplete entry ignored."),
                          strFileName.c_str(), nLine + 1);
 
             continue;
                          strFileName.c_str(), nLine + 1);
 
             continue;
@@ -2573,9 +2721,10 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
             // have we seen this one before?
             int nIndex = m_aTypes.Index(data.type);
 
             // have we seen this one before?
             int nIndex = m_aTypes.Index(data.type);
 
-            // and if we have, was it in this file?
+            // and if we have, was it in this file? if not, we should
+            // overwrite the previously seen one
             overwrite = nIndex == wxNOT_FOUND ||
             overwrite = nIndex == wxNOT_FOUND ||
-                            aIndicesSeenHere.Index(nIndex) != wxNOT_FOUND;
+                            aIndicesSeenHere.Index(nIndex) == wxNOT_FOUND;
         }
 
         wxLogTrace(TRACE_MIME, _T("mailcap %s: %s [%s]"),
         }
 
         wxLogTrace(TRACE_MIME, _T("mailcap %s: %s [%s]"),