#define DBUS_API_SUBJECT_TO_CHANGE

#include <stdio.h>
#include <dbus/dbus.h>

static DBusHandlerResult signal_filter 
	(DBusConnection *connection, DBusMessage *message, void *user_data);

char *sendToSkype( char *msg);

DBusConnection *connection;
DBusError error;
DBusMessage *message;
DBusMessage *reply;

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

	DBusMessageIter iter, array;
	int reply_timeout, array_type;
	char *tmp;

	dbus_error_init (&error);

	connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (connection == NULL){
		fprintf(stderr, "Failed to open connection to bus: %s\n",
					error.message);
		dbus_error_free (&error);
		exit (1);
	}

	/* Construct the message */
	message = dbus_message_new_method_call (
				DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,    /*service*/
				DBUS_PATH_ORG_FREEDESKTOP_DBUS,       /*path*/
				DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,  /*interface*/
				"ListServices"); 

	/* Call ListServices method */
	reply_timeout = -1;   /*don't timeout*/
	reply = dbus_connection_send_with_reply_and_block (connection,
									message, reply_timeout, 
									&error);

	if (dbus_error_is_set (&error)){
		fprintf (stderr, "Error: %s\n", error.message);
		dbus_error_free (&error);
		exit (1);
	}

	dbus_message_iter_init( reply, &iter );
	if( dbus_message_iter_get_arg_type( &iter ) != DBUS_TYPE_ARRAY ||
		dbus_message_iter_get_array_type( &iter ) != DBUS_TYPE_STRING ){
		fprintf (stderr, "Error: reply is not except format\n");
		exit (1);
	}

	printf ("Services on the message bus:\n");
	dbus_message_iter_init_array_iterator( &iter, &array, &array_type );
	if( array_type != DBUS_TYPE_STRING ){
		fprintf (stderr, "Error: reply is not except format\n");
		exit (1);
	}

	for( ; ; dbus_message_iter_next( &array ) ){
		tmp = dbus_message_iter_get_string( &array );
		printf( "%s\n", tmp );
		if( !dbus_message_iter_has_next(&array) )
			break;
	}

	dbus_message_unref( reply );
	dbus_message_unref( message );

	tmp = sendToSkype("NAME hogehoge");
	puts( tmp );
	tmp = sendToSkype("PROTOCOL 1");
	puts( tmp );

//  /* listening to messages from all objects as no path is specified */
//  dbus_bus_add_match (connection,
//                      "type='signal',interface='com.Skype.API'",
//                      &error );
//  dbus_connection_add_filter (connection, signal_filter, NULL, NULL);

	tmp = sendToSkype("GET USER ryuusei.k ONLINESTATUS");
	puts( tmp );

//  sleep( 3 );
}

char *sendToSkype( char *msg){

	char *tmp;
	int reply_timeout = -1;   /*don't timeout*/

	/* Construct the message */
	message = dbus_message_new_method_call (
						"com.Skype.API",    /*service*/
						"/com/Skype",       /*path*/
						"com.Skype.API",  /*interface*/
						"Invoke"); 

	if( !dbus_message_append_args( message,
						DBUS_TYPE_STRING, msg,
						DBUS_TYPE_INVALID ) ){
		fprintf (stderr, "Error: reply is not except format\n");
		exit (1);
	}

	/* Call ListServices method */
	reply_timeout = -1;   /*don't timeout*/
	reply = dbus_connection_send_with_reply_and_block (connection,
											message, reply_timeout, 
											&error);

	if (dbus_error_is_set (&error)){
		fprintf (stderr, "Error: %s\n", error.message);
		dbus_error_free (&error);
		exit (1);
	}
	dbus_message_get_args( reply, &error,
		DBUS_TYPE_STRING, &tmp,
		DBUS_TYPE_INVALID);
	if (dbus_error_is_set (&error)){
		fprintf (stderr, "Error: %s\n", error.message);
		dbus_error_free (&error);
		exit (1);
	}

	return tmp;
}

static DBusHandlerResult
signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
{
	puts("get message!!");
	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


