#include "acidlaunch.h"
#include <stdlib.h>
#include <unistd.h>

static GtkTargetEntry target_table[] = {
        { "text/plain", 0, 0 }
};

static void  
drag_data_received  (GtkWidget          *widget,
	GdkDragContext     *context,
	gint                x,
	gint                y,
	GtkSelectionData   *data,
	guint               info,
	guint               time)
{
	AppButton* o = (AppButton*)gtk_object_get_data(GTK_OBJECT(widget), "imagebutton");
 
	o->data_drop(data);
}

AppButton::AppButton(const AppIconNode* app, int xsize, int ysize, int bgcolor, 
	GtkTooltips* tooltips)
: ImageButton(app->icon().c_str(), xsize, ysize, bgcolor), command(app->command()), _name(app->name())
{
	/* set up drag & drop */
	gtk_drag_dest_set (widget(),
		GTK_DEST_DEFAULT_ALL,
		target_table, 1,
		(GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
		
	gtk_signal_connect (GTK_OBJECT (widget()), "drag_data_received",
		GTK_SIGNAL_FUNC (drag_data_received),
		this);
	
	/* add a tool tip */
	if(tooltips)
	{
		gtk_tooltips_set_tip (tooltips, widget(), _name.c_str(), NULL);
	}
}

void AppButton::data_drop(GtkSelectionData* data)
{
	int i=0;
	ArgList args;
	string app;
	
	args.append((char*)data->data);

	/*if(fork() == 0)
	{
		const char** child_argv;
		int i;
		
		child_argv = new const char*[args.size() + 2];
		child_argv[0] = command.c_str();
		
		for(i=0; i < args.size(); i++)
		{
			child_argv[i+1] = args.get(i).c_str();
		}
		child_argv[args.size()+1] = NULL;

		execvp(command.c_str(), (char* const*)child_argv);
		cout << "ERROR!\n";
	}*/
	app = command + " " + args.getArgString() + " &";
	system(app.c_str());
}

void AppButton::clicked()
{
	string app = command + " &";
	system(app.c_str());
	/*if(!fork())
	{
		const char* child_argv[1];
		
		cout << "in child\n";
		child_argv[0]=NULL;

		execvp(command.c_str(), (char* const*)child_argv);
		cout << "ERROR!\n";
		perror("acidlaunch");
	} else {
		cout << "in parent\n";
	}*/
}


syntax highlighted by Code2HTML, v. 0.9.1