[glom] ImageGlom: Try to use G_FILE_ATTRIBUTE_THUMBNAIL_PATH for other file types.



commit b0b3b82cd16069871bab3502065482ca256e3196
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jul 14 12:35:01 2011 +0200

    ImageGlom: Try to use G_FILE_ATTRIBUTE_THUMBNAIL_PATH for other file types.
    
    * glom/utility_widgets/imageglom.[h|cc]: show_image_data():
    Get the mime_types supported by GdkPixbuf and only try to use it if we have
    a suitable mime type. Otherwise try to get a thumbnail from GFileInfo.
    That doesn't work now, probably because we should do that async.

 ChangeLog                         |    9 +++
 glom/utility_widgets/imageglom.cc |  118 ++++++++++++++++++++++++++++++-------
 glom/utility_widgets/imageglom.h  |   14 ++++-
 3 files changed, 117 insertions(+), 24 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index da7e794..93edbdc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2011-07-14  Murray Cumming  <murrayc murrayc com>
 
+	ImageGlom: Try to use G_FILE_ATTRIBUTE_THUMBNAIL_PATH for other file types.
+
+	* glom/utility_widgets/imageglom.[h|cc]: show_image_data():
+	Get the mime_types supported by GdkPixbuf and only try to use it if we have 
+	a suitable mime type. Otherwise try to get a thumbnail from GFileInfo.
+	That doesn't work now, probably because we should do that async.
+ 
+2011-07-14  Murray Cumming  <murrayc murrayc com>
+
 	ImageGlom: Remove some unnecessary code.
 
 	* glom/utility_widgets/imageglom.cc: get_value(): Don't use the pixbuf just 
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 8347f02..625e76a 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -33,6 +33,7 @@ namespace Glom
 {
 
 ImageGlom::type_vec_ustrings ImageGlom::m_evince_supported_mime_types;
+ImageGlom::type_vec_ustrings ImageGlom::m_gdkpixbuf_supported_mime_types;
 
 ImageGlom::ImageGlom()
 : m_ev_view(0),
@@ -187,9 +188,10 @@ void ImageGlom::on_size_allocate(Gtk::Allocation& allocation)
 {
   Gtk::EventBox::on_size_allocate(allocation);
 
+  //Resize the GtkImage if necessary:
   if(m_pixbuf_original)
   {
-    Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = get_scaled_image();
+    const Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = get_scaled_image();
     m_image.set(pixbuf_scaled);
   }
 }
@@ -270,33 +272,50 @@ void ImageGlom::fill_evince_supported_mime_types()
 	}  
 }
 
+void ImageGlom::fill_gdkpixbuf_supported_mime_types()
+{
+  //Fill the static list if it has not already been filled:
+  if(!m_gdkpixbuf_supported_mime_types.empty())
+    return;
+    
+  typedef std::vector<Gdk::PixbufFormat> type_vec_formats;
+  const type_vec_formats formats = Gdk::Pixbuf::get_formats();
+  
+  for(type_vec_formats::const_iterator iter = formats.begin();
+    iter != formats.end(); ++iter)
+  {
+    const Gdk::PixbufFormat& format = *iter;
+    const std::vector<Glib::ustring> mime_types = format.get_mime_types();
+    m_gdkpixbuf_supported_mime_types.insert(
+      m_gdkpixbuf_supported_mime_types.end(),
+      mime_types.begin(), mime_types.end());
+  }
+}
+
 void ImageGlom::show_image_data()
 {
   bool use_evince = false;
   
+  const Glib::ustring mime_type = get_mime_type();
+
+  //std::cout << "mime_type=" << mime_type << std::endl; 
   
-  if(!Conversions::value_is_empty(m_original_data))
+  fill_evince_supported_mime_types();
+  const type_vec_ustrings::iterator iterFind = 
+    std::find(m_evince_supported_mime_types.begin(),
+      m_evince_supported_mime_types.end(),
+      mime_type);
+  if(iterFind != m_evince_supported_mime_types.end())
   {
-    const Glib::ustring mime_type = get_mime_type();
-    //std::cout << "mime_type=" << mime_type << std::endl; 
-  
-    fill_evince_supported_mime_types();
-    const type_vec_ustrings::iterator iterFind = 
-      std::find(m_evince_supported_mime_types.begin(),
-        m_evince_supported_mime_types.end(),
-        mime_type);
-    if(iterFind != m_evince_supported_mime_types.end())
-    {
-      use_evince = true;
-    }
-  }
-  
+    use_evince = true;
+  }  
   
   m_frame.remove();
     
   //Clear all possible display widgets:
   m_pixbuf_original.reset();
-  m_image.set(m_pixbuf_original);
+  m_pixbuf_thumbnail.reset();
+  m_image.set(Glib::RefPtr<Gdk::Pixbuf>()); //TODO: Add an unset() to gtkmm.
   
   if(m_ev_document_model)
   {
@@ -332,13 +351,70 @@ void ImageGlom::show_image_data()
   else
   {
     //Use GtkImage instead:
-
-    gtk_widget_hide(GTK_WIDGET(m_ev_view));
-      
+    gtk_widget_hide(GTK_WIDGET(m_ev_view));  
     m_image.show();
     m_frame.add(m_image);
+      
+    bool use_gdkpixbuf = false;
+    fill_gdkpixbuf_supported_mime_types();
+    const type_vec_ustrings::iterator iterFind = 
+      std::find(m_gdkpixbuf_supported_mime_types.begin(),
+        m_gdkpixbuf_supported_mime_types.end(),
+        mime_type);
+    if(iterFind != m_gdkpixbuf_supported_mime_types.end())
+    {
+      use_gdkpixbuf = true;
+    }
+    
+    if(use_gdkpixbuf)
+    {
+      //Try to use GdkPixbuf's loader:
+      m_pixbuf_original = Utils::get_pixbuf_for_gda_value(m_original_data);
+    }
+    else
+    {
+      //Try to use a thumbnail via GFile:
+      //TODO: Do this asynchronously:
+      const Glib::ustring uri = save_to_temp_file(false /* don't show progress */);
+      if(uri.empty())
+      {
+        std::cerr << G_STRFUNC << "Could not save temp file to get a thumbnail." << std::endl;
+      }
+      else
+      {
+        Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);
+        Glib::RefPtr<const Gio::FileInfo> file_info;
+
+        try
+        {
+          file_info = file->query_info(
+            G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
+            G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
+        }
+        catch(const Glib::Error& ex)
+        {
+          std::cerr << G_STRFUNC << ": query_info() failed: " << ex.what() << std::endl;
+        }
+      
+        if(file_info)
+        {
+          const std::string filepath = 
+            file_info->get_attribute_byte_string(G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
+          const bool failed = 
+            file_info->get_attribute_boolean(G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
+          if(filepath.empty())
+          {
+            std::cerr << G_STRFUNC << ": Could not get attribute G_FILE_ATTRIBUTE_THUMBNAIL_PATH. failed=" << failed << std::endl;
+          }
+          else
+          {
+            //std::cout << "DEBUGDEBUG: filepath=" << filepath << std::endl;
+            m_pixbuf_original = Gdk::Pixbuf::create_from_file(filepath);
+          }
+        }
+      }
+    }
     
-    m_pixbuf_original = Utils::get_pixbuf_for_gda_value(m_original_data);
     if(m_pixbuf_original)
     {
       Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = get_scaled_image();
diff --git a/glom/utility_widgets/imageglom.h b/glom/utility_widgets/imageglom.h
index 27b52c2..fab9e28 100644
--- a/glom/utility_widgets/imageglom.h
+++ b/glom/utility_widgets/imageglom.h
@@ -92,16 +92,23 @@ private:
 
   Glib::ustring get_mime_type() const;
   static void fill_evince_supported_mime_types();
+  static void fill_gdkpixbuf_supported_mime_types();
  
-  Gtk::Image m_image;
+  mutable Gnome::Gda::Value m_original_data; // Original file data (mutable so that we can create it in get_value() if it does not exist yet)
 
+  Gtk::Frame m_frame;
+  
+  //For anything supported by Evince:
   EvView* m_ev_view;
   EvDocumentModel* m_ev_document_model;
   
-  Gtk::Frame m_frame;
-  mutable Gnome::Gda::Value m_original_data; // Original file data (mutable so that we can create it in get_value() if it does not exist yet)
+  //For anything supported by GdkPixbuf:
+  Gtk::Image m_image;
   Glib::RefPtr<Gdk::Pixbuf> m_pixbuf_original; //Only stored temporarily, because it could be big.
   Glib::RefPtr<Gdk::Pixbuf> m_pixbuf_clipboard; //When copy is used, store it here until it is pasted.
+  
+  //For anything else:
+  Glib::RefPtr<Gdk::Pixbuf> m_pixbuf_thumbnail;
 
   Gtk::Menu* m_pMenuPopup_UserMode;
   Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup_UserModePopup;
@@ -113,6 +120,7 @@ private:
   
   typedef std::vector<Glib::ustring> type_vec_ustrings;
   static type_vec_ustrings m_evince_supported_mime_types;
+  static type_vec_ustrings m_gdkpixbuf_supported_mime_types;
 };
 
 } //namespace Glom



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]