#!/usr/bin/env ruby
#
# MozSnapshooter
# Web site thumbnailer
#
# Copyright (C) 2005 Mirko Maischberger
# Released in the Public Domain
#
# From an idea by Andrew McCall - <andrew@textux.com>
# http://www.hackdiary.com/archives/000055.html

require 'gtkmozembed'

class MozSnapshooter < Gtk::Window
  
  def initialize
    super
    self.title="MozSnapshooter"
    self.border_width = 1
    self.resize(780, 570)
    Gtk::MozEmbed.set_profile_path(ENV['HOME'] + '.mozilla', 'RubyGecko')
    self << Gtk::MozEmbed.new
    self.child.chrome_mask = Gtk::MozEmbed::ALLCHROME
    self.child.set_size_request(816,600)
    self.child.signal_connect("net_stop") { on_net_stop }
    self.child.location = "http://www.ruby-lang.org"

    @countdown = 2

    # The user is bored, let's quit.
    self.signal_connect("destroy") do
      $stderr.print "closing...\n"
      Gtk.main_quit
    end

    self.show_all
  end
  
  def on_net_stop
    Gtk::timeout_add(1000) do
      @countdown -= 1
      if(@countdown > 0)
        puts @countdown
        true
      else
        screenshot
        false
      end
    end
  end
  
  def screenshot
    gdkw = self.child.parent_window
    x, y, width, height, depth = gdkw.geometry
    width -= 16
    pixbuf = Gdk::Pixbuf.from_drawable(nil, gdkw, 0, 0, width, height)
    pixbuf = pixbuf.scale(320, 200, Gdk::Pixbuf::INTERP_HYPER)
    pixbuf.save("screenshot-thumb.png","png")
    puts "Wrote screenshot-thumb.png"
    Gtk.main_quit
  end
  
end

Gtk.init
MozSnapshooter.new
Gtk.main


