lib/mireru/navigator.rb in mireru-0.9.0 vs lib/mireru/navigator.rb in mireru-0.9.1

- old
+ new

@@ -1,16 +1,34 @@ +# Copyright (C) 2013-2014 Masafumi Yokoyama <myokoym@gmail.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + require "gtk3" +require "gio2" module Mireru class Navigator < Gtk::ScrolledWindow PATH_COLUMN, FILENAME_COLUMN, ICON_COLUMN = 0, 1, 2 def initialize(window, files) super() @window = window @files = files @dir_iters = {} + @icons = {} set_policy(:automatic, :automatic) set_size_request(200, -1) @model = Gtk::TreeStore.new(String, String, Gdk::Pixbuf) @tree_view = create_tree(@model) add(@tree_view) @@ -143,24 +161,46 @@ load_dir(model, file, iter, recursive) if recursive else file_icon = select_icon(file) iter.set_value(ICON_COLUMN, file_icon) end - # TODO: too slow... - #icon_width, icon_height = Gtk::IconSize.lookup(:menu) - #begin - # pixbuf = Gdk::Pixbuf.new(file_path, icon_width, icon_height) - #rescue Gdk::PixbufError - # pixbuf = file_icon - #end - #iter.set_value(ICON_COLUMN, pixbuf) end def select_icon(file) - if Widget.video?(file) or Widget.music?(file) - self.render_icon_pixbuf(Gtk::Stock::CDROM, :menu) + content_type = guess_content_type(file) + return @icons[content_type] if @icons[content_type] + icon_path = lookup_icon_path(content_type) + + if icon_path + @icons[content_type] = Gdk::Pixbuf.new(icon_path) else - self.render_icon_pixbuf(Gtk::Stock::FILE, :menu) + if Widget.video?(file) or Widget.music?(file) + @icons[content_type] = self.render_icon_pixbuf(Gtk::Stock::CDROM, :menu) + else + @icons[content_type] = self.render_icon_pixbuf(Gtk::Stock::FILE, :menu) + end end + @icons[content_type] + end + + def lookup_icon_path(mime_type) + content_type = Gio::ContentType.new(mime_type) + icon = content_type.icon + + icon_theme = Gtk::IconTheme.new + icon_info = icon_theme.lookup_icon(icon, + 16, + Gtk::IconTheme::LookupFlags::GENERIC_FALLBACK) + + if icon_info + icon_info.filename + else + nil + end + end + + def guess_content_type(file) + content_type, _uncertain = Gio::ContentType.guess(file) + content_type end end end