// generated by Fast Light User Interface Designer (fluid) version 2,0003

// Work Panel for EDE is (C) Copyright 2000-2002 by Martin Pekar, 
// this program is provided under the terms of GNU GPL v.2, see file COPYING for more information.
// Improvements by Vedran Ljubovic (c) 2005.


#include "logoutdialog.h"
#include <stdlib.h>
/*#include <efltk/Fl_Util.h>
#include <efltk/fl_ask.h>

#include <efltk/x.h>		// X stuff
#include <signal.h>
#include <efltk/Fl_Image.h>	// icons*/

#include <fltk/Util.h>
#include <fltk/ask.h>

#include <fltk/x.h>		// X stuff
#include <signal.h>
#include <fltk/Image.h>	// icons

// widgets
fltk::Window* windowLogoutDialog;
fltk::Round_Button* logoutRadioItemLogoutDialog;
fltk::Round_Button* restartRadioItemLogoutDialog;
fltk::Round_Button* shutdownRadioItemLogoutDialog;

// graphics
static fltk::Image penguin_pix((const char **)penguin_xpm);

// globals
bool dmAvailable;
char *xdm_fifo;
bool canShutdown;
bool sdForceNow;
bool sdTryNow;


// This function looks what a user can do and sets other
// global variables
void checkPermissions() 
{
	char* xdm_env = getenv("XDM_MANAGED");
	if (strcmp(xdm_env,"") == 0)
		dmAvailable = false;
	else
		dmAvailable = true;

	// shutting down via XDM fifo
	if (dmAvailable) {
//		Fl_String_List xdm_env_list = Fl_String_List(xdm_env,",");
//		xdm_fifo = xdm_env_list.item(0);
		xdm_fifo = strtok(xdm_env, ",");
		if (xdm_fifo[0] != '/') {	// broken config
			dmAvailable = false;
			xdm_fifo = strdup("");
		}

		canShutdown = sdForceNow = sdTryNow = false;
		char* token;
		while (token = strtok(NULL, ",") {
			if (strcmp(token,"maysd") == 0) canShutdown = true;
			if (strcmp(token,"fn") == 0) sdForceNow = true;
			if (strcmp(token,"tn") == 0) sdTryNow = true;
		}
	}

	// the old way
	if (!dmAvailable) {
		// shutdown cmd with no params shouldn't do anything...
		if (fltk::start_child_process("shutdown") == 127)
			canShutdown = false;
	}
}


// Logout using XDM messaging
void newLogoutFunction() {

	if ((logoutRadioItemLogoutDialog->value()==0) && 
		(restartRadioItemLogoutDialog->value()==0) &&
		(shutdownRadioItemLogoutDialog->value() == 0))
		// this shoudn't happen...
		return;

	if (restartRadioItemLogoutDialog->value()==1)
	{
		if (!canShutdown) {
			fltk::alert (_("You are not allowed to restart !"));
			return;
		}
		
		FILE *fd = fopen(xdm_fifo, "w");
		char* method;
		if (sdForceNow)
			method = strdup("shutdown\treboot\tforcenow\n");
		else if (sdTryNow)
			method = strdup("shutdown\treboot\ttrynow\n");
		else
			method = strdup("shutdown\treboot\tschedule\n");
		fputs (method, fd);
		fclose (fd);
	}

	if (shutdownRadioItemLogoutDialog->value() == 1) 
	{
		if (!canShutdown) {
			fltk::alert (_("You are not allowed to shutdown !"));
			return;
		}
		
		FILE *fd = fopen(xdm_fifo, "w");
		char* method;
		if (sdForceNow)
			method = strdup("shutdown\thalt\tforcenow\n");
		else if (sdTryNow)
			method = strdup("shutdown\thalt\ttrynow\n");
		else
			method = strdup("shutdown\thalt\tschedule\n");
		fputs ((char *)method, fd);
		fclose (fd);
	}
	
	XCloseDisplay(fltk::display);
	//XSetCloseDownMode(fl_display, DestroyAll);
	XKillClient(fltk::display, AllTemporary);
	//XUngrabServer(fl_display);
	//fl_close_display();
	exit(0);
}


// "brute force" logout function
// (in case XDM is not available)
void oldLogoutFunction() {
	if (logoutRadioItemLogoutDialog->value()==1) 
	{ 
		XCloseDisplay(fltk::display);
		//XSetCloseDownMode(fl_display, DestroyAll);
		XKillClient(fltk::display, AllTemporary);
		//XUngrabServer(fl_display);
		//fl_close_display();
		exit(0);
	}

	if (restartRadioItemLogoutDialog->value()==1) 
	{
		if(fltk::start_child_process( "shutdown -r now" ) != 0)
			fltk::alert("You are not alowed to reboot !");
	}

	if (shutdownRadioItemLogoutDialog->value() == 1) 
	{
		if(fltk::start_child_process( "shutdown -h now" ) != 0)
			fltk::alert("You are not alowed to shutdown !");
	}
}


// Determine logout type
void LogoutFunction(fltk::Widget *, void *) {
	if (dmAvailable)
		newLogoutFunction();
	else
		oldLogoutFunction();
}



// Main logout UI and control
static void cb_Cancel(fltk::Button*, void*) {
	windowLogoutDialog->hide();
}

void LogoutDialog(fltk::Widget*, void *) {
	// first see what options are available
	checkPermissions();

	// draw GUI
	fltk::Window* w;
	{
		fltk::Window* o = windowLogoutDialog = new fltk::Window(171, 160, 330, 190, _("Logout"));
		w = o;
		{
			fltk::Group* o = new fltk::Group(5, 12, 55, 45);
			o->image(penguin_pix);
			o->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
			o->end();
		}
		{
			fltk::Round_Button* o = logoutRadioItemLogoutDialog = new fltk::Round_Button(80, 67, 225, 20, _("&logout from the current session"));
			o->type(fltk::Round_Button::RADIO);
			o->value(1);
			o->tooltip(_("Logout from the current session."));
		}
		{
			fltk::Round_Button* o = restartRadioItemLogoutDialog = new fltk::Round_Button(80, 88, 225, 20, _("&restart the computer"));
			o->type(fltk::Round_Button::RADIO);
			if (canShutdown)
				o->tooltip(_("Restart the computer."));
			else {
				o->tooltip(_("Restart the computer. You do not have privileges to do that."));
				o->deactivate();
			}
		}
		{
			fltk::Round_Button* o = shutdownRadioItemLogoutDialog = new fltk::Round_Button(80, 110, 225, 20, _("&shut down the computer"));
			o->type(fltk::Round_Button::RADIO);
			if (canShutdown)
				o->tooltip(_("Shut down the computer."));
			else {
				o->tooltip(_("Shut down the computer. You do not have privileges to do that."));
				o->deactivate();
			}
		}
		{
			fltk::Button* o = new fltk::Button(85, 157, 80, 25, _("&OK"));
			o->callback((fltk::Callback*)LogoutFunction);
		}
		{
			fltk::Button* o = new fltk::Button(170, 157, 80, 25, _("&Cancel"));
			o->callback((fltk::Callback*)cb_Cancel);
		}
		{
			fltk::Box* o = new fltk::Box(65, 7, 260, 63, _("Logout, restart or shut down the computer?"));
			o->label_size(18);
			o->align(132|fltk::ALIGN_INSIDE);
		}
		
		new fltk::Divider(60, 130, 210, 20, "");
		o->x( Fl::info().w/2 - (o->w()/2) );
		o->y( (Fl::info().h/2) - (o->h()/2) );
		o->set_modal();
		o->end();
	}
	windowLogoutDialog->end();
	windowLogoutDialog->show();
}
