/*
 *  Copyright (C) 2000 Marco Pesenti Gritti, the people who
 * wrote the OAF code I lifted from the docs, Jim Bray
 *
 *  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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glib-1.2/glib.h>
#include <gtk/gtk.h>
#include <liboaf/liboaf.h>

/* local function prototypes */
static CORBA_ORB corba_init (int *argc, char *argv[]);
void new_view_on_running_shell ();

/* global variables */

CORBA_Environment corba_env;                 /* corba environment for errors */


int
main (int argc, char *argv[])
{

	CORBA_ORB orb;

	/* initialise CORBA */
	orb = corba_init (&argc, argv);
	new_view_on_running_shell ();
}


/**
 * corba_init: initialise Gnorba (for gtm) and OAF (for remote
 * Galeon startups).
 */
static CORBA_ORB
corba_init (int *argc, char *argv[])
{
	CORBA_ORB orb;

	/* catch exceptions */
	CORBA_exception_init (&corba_env);

	/* initialise OAF */
	orb = oaf_init (*argc, argv);

	/* return handle */
	return orb;
}

/**
 * new_view_on_running_shell: 
 *
 * Create new view on existing shell. 
 * Returns false if no shell is currently running.
 */
void new_view_on_running_shell ()
{
	CORBA_Object client;
	OAF_ActivationID oaf_id;
	const OAFActivationInfo *oaf_info;



	/* try to find an existing shell to run on */
	client = oaf_activate ("repo_ids.has('IDL:galeon/browser:1.0')", NULL,
			       OAF_FLAG_EXISTING_ONLY, &oaf_id, &corba_env);

	/* check if we found one */
	if (CORBA_Object_is_nil (client, &corba_env))
	{
		return;
	}

	oaf_info=oaf_actid_parse (oaf_id);
	g_print("Implementation ID: %s\n User: %s\n Host: %s\n Domain: %s\n", 
		 oaf_info->iid,oaf_info->user,
		 oaf_info->host,oaf_info->domain);

	g_message (("Galeon already running, using existing process"));

}

/*
 * oaf_actid_parse() returns the following:
 */

#if 0

typedef struct
{
	char *iid;		/* Implementation ID */
	char *user;		/* You owe this field to boc */
	char *host;		/* DNS name or IP address */
	char *domain;		/* This is not a DNS domain, but an activation domain */
} OAFActivationInfo;
#endif


