#!/usr/local/bin/perl -w

############################################################################
# ed2k_urlslave                                                            #
############################################################################
# Version: 2                                                               #
#                                                                          #
# Author:  Veit Wahlich <cru@ircnet.de>                                    #
#                                                                          #
# Descr.:  Little GTK+ enabled Perl script for adding ed2k:// links to     #
#          ed2k GUI, designed for better ed2k:// URL handling within Gnome #
#          (especially Galeon) but might be used for any other purpose.    #
#                                                                          #
# Syntax:  ed2k_urlslave <ed2k:// url>                                     #
############################################################################
# Setting up ed2k_urlslave and Gnome:                                      #
# 1. Copy ed2k_urlslave to a place in $PATH, I suggest /usr/local/bin/     #
# 2. Start gnomecc and open the URL handler capplet                        #
# 3. Fill 'ed2k' into the protocol field and 'ed2k_urlslave "%s"' to the   #
#    handler field                                                         #
# 4. Hit 'set' to add the new protocol handler to the list                 #
# 5. Take any Gnome program (e.g. Galeon) and click some ed2k:// URLs ;)   #
############################################################################

local $ed2k_gui_socket="/tmp/.ed2k_gui_socket";

my $false=0;
my $true=1;

sub ShowWindow{
	my($title,$msg)=@_;	
	
	init Gtk;
	
	my $window=new Gtk::Window("toplevel");
	my $button=new Gtk::Button("OK");

	$window->signal_connect("delete_event",\&CloseWindow);   
	$button->signal_connect("clicked",\&CloseWindow);

	$window->border_width(15);
	$window->set_title($title);
	$window->set_position("center");

	my $label=new Gtk::Label($msg);
	$label->set_line_wrap($true);

	my $vbox=new Gtk::VBox($false,0);
	$vbox->pack_start($label,$false,$false,$false);
	$vbox->pack_start($button,$false,$false,$false);

	$window->add($vbox);
	$window->show_all();

	main Gtk;
	exit(0);
}

sub ShowError{
    my($msg)=@_;
    &ShowWindow("ed2k_urlslave","An ERROR occured while adding your ed2k:// URL to the ed2k GUI: \n\n".$msg."\n\n");
}

sub ShowSuccess{
    my($msg)=@_;
    &ShowWindow("ed2k_urlslave",$msg."\n\n");
}

sub CloseWindow{
   Gtk->exit(0);
   return($false);
}

sub AppMain{
	if(lc(substr(&args(),0,5)) ne "ed2k:"){
		if(&args()){
			&ShowError("The URL submitted does not look like a valid ed2k URL!");
		}else{
			&ShowError("No URL submitted!");
		}
	}elsif(-e $ed2k_gui_socket){
		open(SOCK,">".$ed2k_gui_socket)||&ShowError("Unable to push URL to ed2k GUI socket:\n".$!);
		print(SOCK &args()."\n");
		close(SOCK);
		&ShowSuccess("The following URL has been added to ed2k GUI:\n\n".&args());
	}else{
		&ShowError("ed2k GUI seems not to be running!");
	}
}

sub args{
	return(join(" ",@ARGV));
}

use Gtk;
use strict;

&AppMain();
exit(0);
