Class: Gtk2AppLib::Widgets::CachedImage

Inherits:
Image
  • Object
show all
Defined in:
lib/gtk2applib/widgets/cached_image.rb

Overview

Gtk2AppLib::Image sub classes Gtk::Image to add uri, a way to get the image from the web. Furthermore, it automatically caches itself.

Constant Summary

DEFAULTS =
{
:TIMEOUT => SysConfig::TIMEOUT[:CACHED_IMAGE],
:UTIME => false }

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CachedImage) initialize(*parameters, &block)

Returns a new instance of CachedImage

Parameters:

  • cache (String)

    directory



19
20
21
22
23
# File 'lib/gtk2applib/widgets/cached_image.rb', line 19

def initialize(*parameters, &block)
  @cache, *parameters = Parameters.map(parameters)
  parameters.push(DEFAULTS)
  super(CachedImage.pixbuf(:DEFAULT), *parameters, &block)
end

Class Method Details

+ (Object) _uri_pixbufloader_open(url, loader)

Parameters:

  • url (String)
  • loader (Gdk::PisbufLoader)


33
34
35
36
37
# File 'lib/gtk2applib/widgets/cached_image.rb', line 33

def self._uri_pixbufloader_open(url, loader)
  require 'open-uri' # defined: open
  open(url) { |io| loader.last_write(io.read) }
  loader.pixbuf
end

+ (Object) _uri_utime(dumpfile)

Parameters:



81
82
83
84
# File 'lib/gtk2applib/widgets/cached_image.rb', line 81

def self._uri_utime(dumpfile)
  now = Time.now
  File.utime(now, now, dumpfile)
end

+ (Object) pixbuf(key)

Parameters:

  • key (Symbol)


10
11
12
# File 'lib/gtk2applib/widgets/cached_image.rb', line 10

def self.pixbuf(key)
  Images[key] || Images[:DEFAULT] || Images[:LOGO]
end

Instance Method Details

- (Object) _uri(url, dumpfile)

Parameters:



95
96
97
98
99
# File 'lib/gtk2applib/widgets/cached_image.rb', line 95

def _uri(url, dumpfile)
  (buffer = uri_pixbufloader(url))?
    _uri_save(buffer, dumpfile):
    (self.pixbuf = CachedImage.pixbuf(:BROKEN))
end

- (Object) _uri_pixbufloader(url)

Parameters:



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gtk2applib/widgets/cached_image.rb', line 50

def _uri_pixbufloader(url)
  pixbuf = nil
  loader = Gdk::PixbufLoader.new
  begin
    pixbuf = _uri_pixbufloader_timeout(url, loader)
  rescue Exception
    $!.puts_bang!
  ensure
    loader.close
  end
  pixbuf
end

- (Object) _uri_pixbufloader_timeout(url, loader)

Parameters:

  • url (String)
  • loader (Gdk::PisbufLoader)


41
42
43
44
45
46
47
# File 'lib/gtk2applib/widgets/cached_image.rb', line 41

def _uri_pixbufloader_timeout(url, loader)
  pixbuf = nil
  Timeout.timeout(self.options[:TIMEOUT]) {
    pixbuf = CachedImage._uri_pixbufloader_open(url, loader)
  }
  pixbuf
end

- (Object) _uri_save(buffer, dumpfile)

Parameters:

  • buffer (Pixbuf)
  • dumpfile (String)


88
89
90
91
# File 'lib/gtk2applib/widgets/cached_image.rb', line 88

def _uri_save(buffer, dumpfile)
  self.pixbuf = buffer
  self.pixbuf.save(dumpfile, 'png')
end

- (Object) clear

Sets the image to :EMPTY



27
28
29
# File 'lib/gtk2applib/widgets/cached_image.rb', line 27

def clear
  self.pixbuf = CachedImage.pixbuf(:EMPTY)
end

- (Object) uri(url, name = Digest::MD5.hexdigest(url))

Get the image from uri and cache it

Parameters:

  • url (String)
  • name (String) (defaults to: Digest::MD5.hexdigest(url))


105
106
107
108
109
110
111
112
113
114
# File 'lib/gtk2applib/widgets/cached_image.rb', line 105

def uri(url, name=Digest::MD5.hexdigest(url))
  options = self.options
  if File.exist?(dumpfile = "#{File.join(@cache, name)}.png") then
    self.pixbuf = Gdk::Pixbuf.new(dumpfile)
    CachedImage._uri_utime(dumpfile) if options[:UTIME]
  elsif !options[:OFF_LINE] then
    self.pixbuf = CachedImage.pixbuf(:LOADING)
    _uri(url, dumpfile)
  end
end

- (Object) uri_pixbufloader(url)

Parameters:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gtk2applib/widgets/cached_image.rb', line 64

def uri_pixbufloader(url)
  pixbuff = nil
  Exception.puts_bang! do
    if pixbuff = _uri_pixbufloader(url) then
      options = self.options
      if (width = options[:IMAGE_WIDTH]) &&
        (height = options[:IMAGE_HEIGHT]) &&
        (pixbuff.width > width) &&
        (pixbuff.height > height) then
        pixbuff = pixbuff.scale(width, height)
      end
    end
  end
  pixbuff
end