/*
  Cloned Xunzip
  Copyright (C) 1999 Tero Koskinen

  This program 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.

  This program 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 this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
*/

#include <stdio.h>
#include <config.h>

#include <gtk/gtk.h>
#include <gnome.h>

// #include <popt.h>

/* #include <algorithm> */
/* #include <string> */
/* #include <list> */

#include "prepareapp.hh"
#include "app.hh"

typedef struct {
	App *app;
	GnomeClient *client;
} SessionManagementData;

static gint dont_read_config=0;

struct poptOption options[] = {
        {"noconfig", 'n', POPT_ARG_NONE, &dont_read_config, 0,
         N_("Do not read config"), NULL},
        {NULL, '\0', 0, NULL, 0}
};

GnomeClient *newGnomeClient(const char *name);

static void session_die (App *)
{
	gtk_main_quit ();
}

void save_more(gint reply,gpointer data)
{
	SessionManagementData *d;
	if (reply != GNOME_YES)
		return;

	d=(SessionManagementData*)data;

	gint x,y,w,h;
	
	gdk_window_get_geometry (d->app->app->window, &x, &y, &w, &h, NULL);
	
	gnome_config_push_prefix (gnome_client_get_config_prefix (d->client));
	gnome_config_set_int ("Options/RememberWindowSize", 1);
	gnome_config_set_int ("Geometry/x", x);
	gnome_config_set_int ("Geometry/y", y);
	gnome_config_set_int ("Geometry/w", w);
	gnome_config_set_int ("Geometry/h", h);
	gnome_config_pop_prefix();
	gnome_config_sync();
}

static int save_state (GnomeClient        *client,
		       gint                /* phase */,
		       GnomeRestartStyle   /* save_style */,
		       gint                /* shutdown */ ,
		       GnomeInteractStyle  /* interact_style */,
		       gint                /* fast */,
		       App *app            /* client_data */ )
{
	SessionManagementData data;
/*	gchar *sess_id; */
	gchar *args[4]= { "rm", "-r",NULL,NULL };
	GtkWidget *save_more_dialog;

	// gint argc=0;
	data.app=app;
	data.client=client;
#if 0	
	gdk_window_get_geometry (app->app->window, &x, &y, &w, &h, NULL);
	
	gnome_config_push_prefix (gnome_client_get_config_prefix (client));
	gnome_config_set_int ("Options/RememberWindowSize", 1);
        gnome_config_set_int ("Geometry/x", x);
	gnome_config_set_int ("Geometry/y", y);
	gnome_config_set_int ("Geometry/w", w);
	gnome_config_set_int ("Geometry/h", h);
	
	gnome_config_pop_prefix ();
#endif
	gnome_config_sync();

	args[2] = gnome_config_get_real_path
		(gnome_client_get_config_prefix (client));
	gnome_client_set_discard_command (client, 3, args);

	save_more_dialog = gnome_question_dialog (_("Save window size?"),
                                                      save_more, &data);
	
        gnome_client_save_any_dialog (client, GNOME_DIALOG (save_more_dialog));
	
	return TRUE;
}

GnomeClient *newGnomeClient(const char * /* name */,App *app)
{
	gchar *buf[1024];
	
	GnomeClient *client;
	
	// client = gnome_client_new();
	client=gnome_master_client();
	
	if (!client)
		return NULL;
	
	getcwd((char *)buf,sizeof(buf));
	gnome_client_set_current_directory(client, (char *)buf);
	
	gtk_object_ref(GTK_OBJECT(client));
	gtk_object_sink(GTK_OBJECT(client));
	
	gtk_signal_connect (GTK_OBJECT (client), "save_yourself",
			    GTK_SIGNAL_FUNC (save_state), app);
	gtk_signal_connect (GTK_OBJECT (client), "die",
			    GTK_SIGNAL_FUNC (session_die), app);
	return client;
}

int main(int argc,char **argv)
{
	App *app;
	GnomeClient *client;
	const char **leftovers;
	
	poptContext ctx;
	
	gnome_init_with_popt_table("cxunzip",VERSION,argc,argv,options,0,&ctx);
	app=new App(!dont_read_config);
	
	client=newGnomeClient(g_filename_pointer(argv[0]),app);
	/*  bindtextdomain (PACKAGE, GNOMELOCALEDIR);
	    textdomain (PACKAGE); */
	
	prepare_app("cxunzip",app);
	
	leftovers = poptGetArgs(ctx);
	
	if(leftovers && leftovers[0]) {
		open_file(leftovers[0],app);
	}
	
	poptFreeContext(ctx);
	
	gtk_main();
	return 0;
}
