]> git.saurik.com Git - wxWidgets.git/blobdiff - src/unix/mimetype.cpp
fix build for watcom
[wxWidgets.git] / src / unix / mimetype.cpp
index faf0bd2d6c4e2bb1b76140ff17172a2894c923ae..5f6157c61e63052cf71ae0014208e6cbe4ccf501 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     23.09.98
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
-// Licence:     wxWindows license (part of wxExtra library)
+// Licence:     wxWindows licence (part of wxExtra library)
 /////////////////////////////////////////////////////////////////////////////
 
 // known bugs; there may be others!! chris elliott, biol75@york.ac.uk 27 Mar 01
@@ -364,21 +364,21 @@ bool wxMimeTypesManagerImpl::CheckGnomeDirsExist ()
     wxString gnomedir;
     wxGetHomeDir( &gnomedir );
     wxString sTmp = gnomedir;
-    sTmp = sTmp + "/.gnome";
+    sTmp = sTmp + wxT("/.gnome");
     if (! wxDir::Exists ( sTmp ) )
     {
         if (!wxMkdir ( sTmp ))
         {
-            wxFAIL_MSG (wxString ("Failed to create directory\n.gnome in \nCheckGnomeDirsExist") + sTmp );
+            wxLogError(_("Failed to create directory %s/.gnome."), sTmp.c_str());
             return FALSE;
         }
     }
-    sTmp = sTmp + "/mime-info";
+    sTmp = sTmp + wxT("/mime-info");
     if (! wxDir::Exists ( sTmp ) )
     {
         if (!wxMkdir ( sTmp ))
         {
-            wxFAIL_MSG (wxString ("Failed to create directory\nmime-info in \nCheckGnomeDirsExist") + sTmp );
+            wxLogError(_("Failed to create directory %s/mime-info."), sTmp.c_str());
             return FALSE;
         }
     }
@@ -393,11 +393,15 @@ bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index, bool delete_index)
     wxString gnomedir;
     wxGetHomeDir( &gnomedir );
 
-    wxMimeTextFile outfile ( gnomedir + "/.gnome/mime-info/user.keys");
+    wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.keys"));
     // if this fails probably Gnome is not installed ??
     // create it anyway as a private mime store
 
+#if defined(__WXGTK20__) && wxUSE_UNICODE
+    if (! outfile.Open ( wxConvUTF8) )
+#else
     if (! outfile.Open () )
+#endif
     {
         if (delete_index) return FALSE;
         if (!CheckGnomeDirsExist() ) return FALSE;
@@ -462,13 +466,13 @@ bool wxMimeTypesManagerImpl::WriteGnomeKeyFile(int index, bool delete_index)
             }
             //sOld should also contain the icon
             if ( !m_aIcons[index].empty() )
-                sOld.AddOrReplaceVerb ( wxT( "icon-filename"), m_aIcons[index] );
+                sOld.AddOrReplaceVerb ( wxT("icon-filename"), m_aIcons[index] );
 
             for (i=0; i < sOld.GetCount(); i++)
             {
                 sTmp = sOld.GetVerbCmd(i);
                 sTmp.Replace( wxT("%s"), wxT("%f") );
-                sTmp = wxT ( "\t") + sTmp;
+                sTmp = wxT("\t") + sTmp;
                 nIndex ++;
                 outfile.InsertLine ( sTmp, nIndex );
             }
@@ -484,7 +488,7 @@ bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index)
     wxString gnomedir;
     wxGetHomeDir( &gnomedir );
 
-    wxMimeTextFile outfile ( gnomedir + "/.gnome/mime-info/user.mime");
+    wxMimeTextFile outfile ( gnomedir + wxT("/.gnome/mime-info/user.mime"));
     // if this fails probably Gnome is not installed ??
     // create it anyway as a private mime store
     if (! outfile.Open () )
@@ -498,7 +502,7 @@ bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index)
     if ( nIndex == wxNOT_FOUND )
     {
         outfile.AddLine ( strType );
-        outfile.AddLine ( wxT( "\text:") + m_aExtensions.Item(index) );
+        outfile.AddLine ( wxT("\text:") + m_aExtensions.Item(index) );
     }
     else
     {
@@ -510,13 +514,13 @@ bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index)
         else
         {// check for next line being the right one to replace ??
             wxString sOld = outfile.GetLine(nIndex+1);
-            if (sOld.Contains(wxT("\text: ")))
+            if (sOld.Contains( wxT("\text: ")))
             {
-                outfile.GetLine(nIndex+1) = wxT( "\text: ") + m_aExtensions.Item(index);
+                outfile.GetLine(nIndex+1) = wxT("\text: ") + m_aExtensions.Item(index);
             }
             else
             {
-                outfile.InsertLine(wxT( "\text: ") + m_aExtensions.Item(index), nIndex + 1 );
+                outfile.InsertLine( wxT("\text: ") + m_aExtensions.Item(index), nIndex + 1 );
             }
         }
     }
@@ -525,10 +529,15 @@ bool wxMimeTypesManagerImpl::WriteGnomeMimeFile(int index, bool delete_index)
 }
 
 
-void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename)
+void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename,
+                                                      const wxArrayString& dirs)
 {
     wxTextFile textfile(filename);
+#if defined(__WXGTK20__) && wxUSE_UNICODE
+    if ( !textfile.Open( wxConvUTF8) )
+#else
     if ( !textfile.Open() )
+#endif
         return;
     wxLogTrace(TRACE_MIME, wxT("--- Opened Gnome file %s  ---"),
             filename.c_str());
@@ -556,10 +565,29 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename)
             wxString sTmp(pc);
             if (sTmp.Contains(wxT("=")) )
             {
+                // GNOME 1:
                 if (sTmp.Contains( wxT("icon-filename=") ) )
                 {
                     curIconFile = sTmp.AfterFirst(wxT('='));
                 }
+                // GNOME 2:
+                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++)
+                        {
+                            wxString newFile;
+                            newFile.Printf(wxT("%s/pixmaps/document-icons/%s.png"),
+                                           dirs[nDir].c_str(),
+                                           curIconFile.c_str());
+                            if (wxFileExists(newFile))
+                                curIconFile = newFile;
+                        }
+                    }
+                }
                 else //: some other field,
                 {
                     //may contain lines like this (RH7)
@@ -615,7 +643,11 @@ void wxMimeTypesManagerImpl::LoadGnomeDataFromKeyFile(const wxString& filename)
 void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& filename)
 {
     wxTextFile textfile(filename);
+#if defined(__WXGTK20__) && wxUSE_UNICODE
+    if ( !textfile.Open( wxConvUTF8) )
+#else
     if ( !textfile.Open() )
+#endif
         return;
     wxLogTrace(TRACE_MIME, wxT("--- Opened Gnome file %s  ---"),
                  filename.c_str());
@@ -630,7 +662,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file
         if ( nLine < nLineCount )
         {
             pc = textfile[nLine].c_str();
-            if ( *pc == _T('#') )
+            if ( *pc == wxT('#') )
             {
                 // skip comments
                 continue;
@@ -665,13 +697,13 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file
         }
 
         // what do we have here?
-        if ( *pc == _T('\t') )
+        if ( *pc == wxT('\t') )
         {
             // this is a field=value ling
             pc++; // skip leading TAB
 
             static const int lenField = 4; // strlen("ext:")
-            if ( wxStrncmp(pc, _T("ext:"), lenField) == 0 )
+            if ( wxStrncmp(pc, wxT("ext:"), lenField) == 0 )
             {
                 // skip ' ' which follows and take everything left until the end
                 // of line
@@ -690,7 +722,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file
 
             curMimeType.Empty();
 
-            while ( *pc != _T(':') && *pc != _T('\0') )
+            while ( *pc != wxT(':') && *pc != wxT('\0') )
             {
                 curMimeType += *pc++;
             }
@@ -699,13 +731,14 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeTypesFromMimeFile(const wxString& file
 }
 
 
-void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(const wxString& dirbase)
+void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(
+                      const wxString& dirbase, const wxArrayString& dirs)
 {
     wxASSERT_MSG( !!dirbase && !wxEndsWithPathSeparator(dirbase),
                   _T("base directory shouldn't end with a slash") );
 
     wxString dirname = dirbase;
-    dirname << _T("/mime-info");
+    dirname << wxT("/mime-info");
 
     if ( !wxDir::Exists(dirname) )
         return;
@@ -715,7 +748,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(const wxString& dirbase)
         return;
 
     // we will concatenate it with filename to get the full path below
-    dirname += _T('/');
+    dirname += wxT('/');
 
     wxString filename;
     bool cont = dir.GetFirst(&filename, _T("*.mime"), wxDIR_FILES);
@@ -729,7 +762,7 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(const wxString& dirbase)
     cont = dir.GetFirst(&filename, _T("*.keys"), wxDIR_FILES);
     while ( cont )
     {
-        LoadGnomeDataFromKeyFile(dirname + filename);
+        LoadGnomeDataFromKeyFile(dirname + filename, dirs);
 
         cont = dir.GetNext(&filename);
     }
@@ -740,21 +773,20 @@ void wxMimeTypesManagerImpl::LoadGnomeMimeFilesFromDir(const wxString& dirbase)
 
 void wxMimeTypesManagerImpl::GetGnomeMimeInfo(const wxString& sExtraDir)
 {
-
     wxArrayString dirs;
-    dirs.Add(_T("/usr/share"));
-    dirs.Add(_T("/usr/local/share"));
+    dirs.Add(wxT("/usr/share"));
+    dirs.Add(wxT("/usr/local/share"));
 
     wxString gnomedir;
     wxGetHomeDir( &gnomedir );
-    gnomedir += _T("/.gnome");
+    gnomedir += wxT("/.gnome");
     dirs.Add( gnomedir );
     if (!sExtraDir.empty()) dirs.Add( sExtraDir );
 
     size_t nDirs = dirs.GetCount();
     for ( size_t nDir = 0; nDir < nDirs; nDir++ )
     {
-        LoadGnomeMimeFilesFromDir(dirs[nDir]);
+        LoadGnomeMimeFilesFromDir(dirs[nDir], dirs);
     }
 }
 
@@ -909,16 +941,19 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
     wxMimeTextFile file;
     if ( !file.Open(dirbase + filename) ) return;
 
+    wxLogTrace(TRACE_MIME, wxT("loading KDE file %s"),
+                           (dirbase+filename).c_str());
+    
     wxMimeTypeCommands * entry = new wxMimeTypeCommands;
     wxArrayString sExts;
     wxString mimetype, mime_desc, strIcon;
 
-    int nIndex = file.pIndexOf ("MimeType=");
+    int nIndex = file.pIndexOf( wxT("MimeType=") );
     if (nIndex == wxNOT_FOUND)
     {
         // construct mimetype from the directory name and the basename of the
         // file (it always has .kdelnk extension)
-        mimetype << subdir << _T('/') << filename.BeforeLast(_T('.'));
+        mimetype << subdir << wxT('/') << filename.BeforeLast( wxT('.') );
     }
     else mimetype = file.GetCmd (nIndex);
 
@@ -979,6 +1014,7 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
     if ( nIndex != wxNOT_FOUND )
     {
         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))
         {
@@ -992,6 +1028,7 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeSubtype(const wxString& dirbase,
                     if (wxFileExists(icondirs[nDir] + strIcon))
                     {
                         strIcon.Prepend(icondirs[nDir]);
+                        wxLogTrace(TRACE_MIME, wxT("  iconfile %s"), strIcon.c_str());
                         break;
                     }
             }
@@ -1030,6 +1067,9 @@ void wxMimeTypesManagerImpl::LoadKDELinksForMimeType(const wxString& dirbase,
     if ( !dir.IsOpened() )
         return;
 
+    wxLogTrace(TRACE_MIME, wxT("--- Loading from KDE directory %s  ---"),
+                           dirname.c_str());
+    
     dirname += _T('/');
 
     wxString filename;
@@ -1085,15 +1125,15 @@ void wxMimeTypesManagerImpl::GetKDEMimeInfo(const wxString& sExtraDir)
     wxArrayString icondirs;
 
     // settings in ~/.kde have maximal priority
-    dirs.Add(wxGetHomeDir() + _T("/.kde/share"));
-    icondirs.Add(wxGetHomeDir() + _T("/.kde/share/icons/"));
+    dirs.Add(wxGetHomeDir() + wxT("/.kde/share"));
+    icondirs.Add(wxGetHomeDir() + wxT("/.kde/share/icons/"));
 
     // the variable KDEDIR is set when KDE is running
-    const char *kdedir = getenv("KDEDIR");
+    const wxChar *kdedir = wxGetenv( wxT("KDEDIR") );
     if ( kdedir )
     {
-        dirs.Add(wxString(kdedir) + _T("/share"));
-        icondirs.Add(wxString(kdedir) + _T("/share/icons/"));
+        dirs.Add( wxString(kdedir) + wxT("/share") );
+        icondirs.Add( wxString(kdedir) + wxT("/share/icons/") );
     }
     else
     {
@@ -1346,6 +1386,7 @@ void wxMimeTypesManagerImpl::ClearData()
     m_aExtensions.Clear ();
     m_aDescriptions.Clear ();
 
+    WX_CLEAR_ARRAY(m_aEntries);
     m_aEntries.Empty();
 
     m_mailcapStylesInited = 0;
@@ -1354,8 +1395,6 @@ void wxMimeTypesManagerImpl::ClearData()
 wxMimeTypesManagerImpl::~wxMimeTypesManagerImpl()
 {
     ClearData();
-
-    WX_CLEAR_ARRAY(m_aEntries);
 }
 
 
@@ -1840,6 +1879,10 @@ int wxMimeTypesManagerImpl::AddToMimeData(const wxString& strType,
                         entryOld->AddOrReplaceVerb(verb, entry->GetCmd(i));
                     }
                 }
+
+                // as we don't store it anywhere, it won't be deleted later as
+                // usual -- do it immediately instead
+                delete entry;
             }
         }
     }
@@ -2033,7 +2076,11 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
                strFileName.c_str());
 
     wxTextFile file(strFileName);
+#if defined(__WXGTK20__) && wxUSE_UNICODE
+    if ( !file.Open( wxConvUTF8) )
+#else
     if ( !file.Open() )
+#endif
         return FALSE;
 
     // the information we extract
@@ -2041,7 +2088,8 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
 
     size_t nLineCount = file.GetLineCount();
     const wxChar *pc = NULL;
-    for ( size_t nLine = 0; nLine < nLineCount; nLine++ ) {
+    for ( size_t nLine = 0; nLine < nLineCount; nLine++ )
+    {
         if ( pc == NULL ) {
             // now we're at the start of the line
             pc = file[nLine].c_str();
@@ -2141,14 +2189,19 @@ bool wxMimeTypesManagerImpl::ReadMimeTypes(const wxString& strFileName)
             else if ( strLHS == wxT("exts") ) {
                 strExtensions = strRHS;
             }
-            else {
+            else if ( strLHS != _T("icon") )
+            {
                 // this one is simply ignored: it usually refers to Netscape
                 // built in icons which are useless for us anyhow
-                if ( strLHS != _T("icon") )
-                {
-                    wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
-                                 strFileName.c_str(), nLine + 1, strLHS.c_str());
-                }
+            }
+            else if ( !strLHS.StartsWith(_T("x-")) )
+            {
+                // we suppose that all fields starting with "X-" are
+                // unregistered extensions according to the standard practice,
+                // but it may be worth telling the user about other junk in
+                // his mime.types file
+                wxLogWarning(_("Unknown field in file %s, line %d: '%s'."),
+                             strFileName.c_str(), nLine + 1, strLHS.c_str());
             }
 
             if ( !entryEnded ) {
@@ -2307,7 +2360,11 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                strFileName.c_str());
 
     wxTextFile file(strFileName);
+#if defined(__WXGTK20__) && wxUSE_UNICODE
+    if ( !file.Open( wxConvUTF8) )
+#else
     if ( !file.Open() )
+#endif
         return FALSE;
 
     // indices of MIME types (in m_aTypes) we already found in this file
@@ -2367,11 +2424,11 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                             // something is wrong, bail out
                             cont = FALSE;
 
-                            wxLogDebug(wxT("Mailcap file %s, line %d: "
+                            wxLogDebug(wxT("Mailcap file %s, line %lu: "
                                            "'\\' on the end of the last line "
                                            "ignored."),
                                        strFileName.c_str(),
-                                       nLine + 1);
+                                       (unsigned long)nLine + 1);
                         }
                         else
                         {
@@ -2433,11 +2490,11 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
                                 // this might be useful for the programmer
                                 wxLogDebug
                                 (
-                                    wxT("Mailcap file %s, line %d: "
+                                    wxT("Mailcap file %s, line %lu: "
                                         "unknown field '%s' for the "
                                         "MIME type '%s' ignored."),
                                     strFileName.c_str(),
-                                    nLine + 1,
+                                    (unsigned long)nLine + 1,
                                     curField.c_str(),
                                     data.type.c_str()
                                 );
@@ -2504,7 +2561,8 @@ bool wxMimeTypesManagerImpl::ReadMailcap(const wxString& strFileName,
 
         if ( data.needsterminal )
         {
-            data.cmdOpen.Printf(_T("xterm -e sh -c '%s'"), data.cmdOpen.c_str());
+            data.cmdOpen = wxString::Format(_T("xterm -e sh -c '%s'"),
+                                            data.cmdOpen.c_str());
         }
 
         if ( !data.cmdOpen.empty() )