/***************************************************************************
                            Invocation.java -  description
                             -------------------
    begin                : Tue Oct 31 06:12:14 2000
    copyright            : (C) Gert-Jan van der Heiden. All rights reserved.
    written by           : Gert-Jan van der Heiden.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as published by*
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package org.trinitydesktop.koala;

import java.util.*;
import java.lang.reflect.*;
import org.trinitydesktop.qt.qtjava;

/** A utility class for creating a closure to invoke later
*/
class Invocation{
public static ArrayList invokeInterfaces(long qtObject){
	   return invokeArrayOperation(qtObject, "interfaces");
	}
	public static ArrayList invokeFunctions(long qtObject){
	   return invokeArrayOperation(qtObject, "functions");
	}
  public static ArrayList invokeArrayOperation(long qtObject, String operationName){
	  try{
      Object onThis = qtjava.objectForQtKey(qtObject, "java.lang.object", true);
			if(onThis != null){
				Object[] arguments = new Object[0];
	      Class[] objectTypes = new Class[0];
	      Method method = onThis.getClass().getMethod(operationName, objectTypes);
	      return (ArrayList) method.invoke(onThis,arguments);
			}else {
			  System.out.println("Invocation.invokeArrayOperation() : object not found!");
			}
	  }catch(Exception e){
	    e.printStackTrace();
	  }
		return new ArrayList();
	}
  public static DCOPAnswer invokeProcess(long qtObject, String fun, byte[] data){
	  try{
      Object onThis = qtjava.objectForQtKey(qtObject, "java.lang.object", true);
			if(onThis != null){
				Object[] arguments = {fun, data};
	      Class[] objectTypes = { String.class,
				                        data.getClass()};

	      Method method = onThis.getClass().getMethod("javaProcess", objectTypes);
	      return  (DCOPAnswer) method.invoke(onThis,arguments);
			}else {
			  System.out.println("Invocation.invokeArrayOperation() : object not found!");
			}
	  }catch(Exception e){
	    e.printStackTrace();
	  }
		DCOPAnswer answer = new DCOPAnswer();
		answer.setSucces(false);
		return answer;
	}
}
