#!/usr/bin/env ruby
# Copyright (C) 2005 Paul Chavard
#
# This file is part of Gnome Splash Proprieties.
#
# Gnome Splash Proprieties 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.
#
# Gnome Splash Proprieties 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 Gnome Splash Proprieties; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

require 'gnome2'
require 'gconf2'
require 'gnomevfs'

class Splash
	NAME = "Gnome Splash Properties"
	VERSION = "0.3.0"
	@splash = nil
	@last_splash = nil
	@cwd = nil
	
	def initialize
		gconf_init
		gui_init
	end
	
	def gconf_init
		@gconf_client = GConf::Client.default
		@gconf_client.add_dir("/apps/gnome-session/options")
		
		@splash = @gconf_client["/apps/gnome-session/options/splash_image"]
		
		@gconf_client.notify_add("/apps/gnome-session/options/splash_image") do | c, e |
			@last_splash = @splash
			@splash = e.value
			set_widgets
		end
	end
	
	def gui_init
		# Initialize window.
		@window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
		@window.type_hint = Gdk::Window::TYPE_HINT_DIALOG
		@window.resizable = true
		@window.border_width = 8
		@window.icon_name = "gnome-session"
		@window.signal_connect('destroy') { Gtk.main_quit }
		
		@tooltips = Gtk::Tooltips.new
		@image = Gtk::Image.new
		@event_box = Gtk::EventBox.new.add(@image)
		@event_box.signal_connect("drag-data-received") do |w,d_c,x,y,d,i,t|
			d.data =~ /\n/
			uri = $`.chop
			my_uri = GnomeVFS::URI.new(uri)
			if my_uri.exists? and my_uri.local?
				@cur_dir = []
				path = get_path(my_uri.path)
				@gconf_client["/apps/gnome-session/options/splash_image"] = path
			end
		end
		
		Gtk::Drag.dest_set(@event_box, Gtk::Drag::DEST_DEFAULT_ALL, [['text/uri-list', nil, 0]], Gdk::DragContext::ACTION_COPY)
		
		button_open = Gtk::Button.new("Set New Splash")
		button_open.signal_connect("clicked") { open_file_dialog }
		
		button_default = Gtk::Button.new("Set Default Splash")
		button_default.signal_connect("clicked") { @gconf_client["/apps/gnome-session/options/splash_image"] =
		@gconf_client.default_from_schema("/apps/gnome-session/options/splash_image") }
		
		button_close = Gtk::Button.new(Gtk::Stock::CLOSE)
		button_close.signal_connect("clicked") { @window.destroy }
		
		set_widgets
		
		vbox = Gtk::VBox.new
		vbox.pack_start(@event_box,true,false,0)
		hbox = Gtk::HBox.new
		hbox.pack_start(button_default,true,true,4)
		hbox.pack_start(button_open,true,true,4)
		vbox.pack_start(hbox,false,false,8)
		hbox = Gtk::HBox.new
		hbox.pack_end(button_close,false,false,4)
		vbox.pack_start(hbox,false,false,4)
		
		@window.add(vbox)
		@window.show_all
	end
	
	def get_path(uri)
		dir = GnomeVFS::URI.new(uri)
		if dir.extract_dirname != "/"
			@cur_dir << dir.extract_short_name
			get_path(dir.extract_dirname)
		else
			@cur_dir << dir.extract_short_name
			tmp = ""
			@cur_dir.reverse.each { |d| tmp += "/" + d }
			return tmp
		end
	end
	
	def open_file_dialog
		# Create Dialog
		@dialog = Gtk::FileChooserDialog.new("Select a Splash Image", @window,
		Gtk::FileChooser::ACTION_OPEN, nil,
		[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
		[Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
		@dialog.select_multiple = false
		filter = Gtk::FileFilter.new
		filter.add_pixbuf_formats
		@dialog.filter = filter
		vbox = Gtk::VBox.new.show
		img = Gtk::Image.new.show
		w = Gtk::Label.new.show
		w.set_alignment(0.02,0.5)
		h = Gtk::Label.new.show
		h.set_alignment(0.02,0.5)
		t = Gtk::Label.new.show
		t.set_alignment(0.02,0.5)
		vbox.pack_start(img,false,false,0)
		vbox.pack_start(t,false,false,4)
		vbox.pack_start(w,false,false,4)
		vbox.pack_start(h,false,false,4)
		preview = Gtk::Frame.new
		preview.shadow_type = Gtk::SHADOW_IN
		preview.add(vbox)
		@dialog.preview_widget = preview
		@dialog.use_preview_label = false
		@dialog.signal_connect("update-preview") {
		if @dialog.preview_filename != nil
			filename = @dialog.preview_filename
			info = Gdk::Pixbuf.get_file_info(filename)
		end
		if info != nil
			prev_pixbuf = Gdk::Pixbuf.new(filename, 256, 256)
			t.markup = "<b>Image Type:</b> " + info[0].name
			w.markup = "<b>Width:</b> " + info[1].to_s + " px"
			h.markup = "<b>Height:</b> " + info[2].to_s + " px"
			img.pixbuf = prev_pixbuf
			@dialog.preview_widget_active = true
		else
			@dialog.preview_widget_active = false
		end
		}
			# Run Dialog
			if @dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
				#@cwd = @dialog.current_folder
				@gconf_client["/apps/gnome-session/options/splash_image"] = @dialog.filename
			end
		@dialog.destroy
   	end
    	
	def set_widgets
		if @splash =~ /^\//
			path = @splash
		else
			path = "/usr/share/pixmaps/" + @splash
		end
		info = Gdk::Pixbuf.get_file_info(path)
		if info != nil
			@cwd = GnomeVFS::URI.new(path).extract_dirname
			w = info[1]
			h = info[2]
			if w < 400 and h < 400
				my_pixbuf = Gdk::Pixbuf.new(path,width = w, height = h)
			else
				my_pixbuf = Gdk::Pixbuf.new(path, width = 400, height = 400)
			end
			tip = info[0].name + " Image [ " + w.to_s + " x " + h.to_s + " ]\n"
			tip += path
			@tooltips.set_tip(@event_box,tip,nil)
			@image.clear
			@image.pixbuf = my_pixbuf
		else
			if @last_splash != nil
				@gconf_client["/apps/gnome-session/options/splash_image"] = @last_splash
			else
				@gconf_client["/apps/gnome-session/options/splash_image"] =
				@gconf_client.default_from_schema("/apps/gnome-session/options/splash_image")
			end
		end
	end
	
end

Gnome::Program.new(Splash::NAME,Splash::VERSION)

Gtk.init
Splash.new
Gtk.main
		
