# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/act-icon"
require "qwik/act-thumbnail"
require "qwik/act-ref"

module Qwik
  class Action
    # ============================== file thumb
    def file_thumb_generate(f)
      org_file = @site.files(@req.base).path(f)
      files_dir = @site.files(@req.base).path("")
      thumb_dir = files_dir+".thumb"
      thumb_dir.check_directory
      thumb = thumb_dir+f

      if ! thumb.exist?
	convert = CONVERT_PATH
	return nil if ! convert.path.exist?
	s = THUMB_SIZE
	geom = "#{s}x#{s}"
	cmd = "#{convert} -size #{geom} #{org_file.to_s} #{thumb.to_s}"
#	qp cmd
	system cmd
      end

      return @req.base+".files/.thumb/"+f.to_s
    end

    # ============================== plg_file
    def plg_file(f=nil, alt=f)
      return if f.nil?

      src = @req.base+".files/"+f

      if ! @site.files(@req.base).exist?(f)
	icon = icon_path("broken")
	return ref_html(src, "icon", icon, alt, alt)
      end

      ext = File.extname(f).sub(/\A\./, "")

      if is_image?(ext)
	thumb = file_thumb_generate(f)
	if thumb # else, go through
	  return ref_html(src, "thumb", thumb, alt, alt)
	end
      end

      icon = icon_find(ext)
      if icon
	return ref_html(src, "icon", icon, alt, alt)
      end

      return [:div, {:class=>"ref"}, 
	[:a, {:href=>src}, alt]]
    end
  end
end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActFile < Test::Unit::TestCase
    include TestSession

    def test_all
      assert_wiki("", "")

      if @site.files("1").exist?("1x1.png")
	@site.files("1").delete("1x1.png")
      end

      assert_wiki([], "{{file}}")

      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>"1.files/1x1.png"},
		      [:img, {:alt=>"1x1.png", :class=>"icon",
			  :src=>".theme/icons/broken.gif"}],
		      [:br], "1x1.png"]],
		  "{{file(1x1.png)}}")

      png = "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\001\000\000\000\001\010\002\000\000\000\220wS\336\000\000\000\fIDATx\332c\370\377\377?\000\005\376\002\3763\022\225\024\000\000\000\000IEND\256B`\202"
      @site.files("1").put("1x1.png", png)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>"1.files/1x1.png"},
		      [:img, {:alt=>"1x1.png", :class=>"thumb",
			  :src=>"1.files/.thumb/1x1.png"}],
		      [:br], "1x1.png"]],
		  "{{file(1x1.png)}}")

      @site.files("1").delete("test.pdf") if @site.files("1").exist?("test.pdf")
      pdf = "PDF...dummy"
      @site.files("1").put("test.pdf", pdf)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>"1.files/test.pdf"},
		      [:img, {:alt=>"test.pdf", :class=>"icon",
			  :src=>".theme/icons/pdf.gif"}],
		      [:br], "test.pdf"]],
		  "{{file(test.pdf)}}")

      @site.files("1").delete("test.txt") if @site.files("1").exist?("test.txt")
      file = "text file dummy"
      @site.files("1").put("test.txt", file)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>"1.files/test.txt"},
		      [:img, {:alt=>"test.txt", :class=>"icon",
			  :src=>".theme/icons/text.gif"}],
		      [:br], "test.txt"]],
		  "{{file(test.txt)}}")

      if @site.files("1").exist?("test.nosuchext")
	@site.files("1").delete("test.nosuchext")
      end

      file = "unknown file dummy"
      @site.files("1").put("test.nosuchext", file)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>"1.files/test.nosuchext"},
		      [:img, {:alt=>"test.nosuchext", :class=>"icon", 
			  :src=>".theme/icons/generic.gif"}],
		      [:br], "test.nosuchext"]],
		  "{{file(test.nosuchext)}}")
    end
  end
end
