/*
 * IsoVolume.c
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library 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.
 * See COPYING file for details.
 */

#include "org_pykix_libburnia_libisofs_IsoVolume.h"

#include "libisofs.h"

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_new
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1new
(
	JNIEnv *env, 
	jclass cls, 
	jstring volumeId, 
	jstring publisherId, 
	jstring dataPreparerId
)
{
	const char *volume_id = NULL;
	const char *publisher_id = NULL;
	const char *data_preparer_id = NULL;
	
	if ( volumeId != NULL ) {
		volume_id = (*env)->GetStringUTFChars(env, volumeId, NULL);
		if ( volume_id == NULL ) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}
	if ( publisherId != NULL ) {
		publisher_id = (*env)->GetStringUTFChars(env, publisherId, NULL);
		if (publisher_id == NULL) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}
	if ( dataPreparerId != NULL ) {
		data_preparer_id = (*env)->GetStringUTFChars(env, dataPreparerId, NULL);
		if ( data_preparer_id == NULL ) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}

	struct iso_volume *iso = iso_volume_new(volume_id, publisher_id, data_preparer_id);
	
	(*env)->ReleaseStringUTFChars(env, volumeId, volume_id);
	(*env)->ReleaseStringUTFChars(env, publisherId, publisher_id);
	(*env)->ReleaseStringUTFChars(env, dataPreparerId, data_preparer_id);

	return (jlong) iso;

}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_new_with_root
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;J)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1new_1with_1root
(
	JNIEnv *env, 
	jclass cls, 
	jstring volumeId, 
	jstring publisherId, 
	jstring dataPreparerId,
	jlong root
)
{
	const char *volume_id = NULL;
	const char *publisher_id = NULL;
	const char *data_preparer_id = NULL;
	struct iso_volume *iso;
	
	if ( volumeId != NULL ) {
		volume_id = (*env)->GetStringUTFChars(env, volumeId, NULL);
		if ( volume_id == NULL ) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}
	if ( publisherId != NULL ) {
		publisher_id = (*env)->GetStringUTFChars(env, publisherId, NULL);
		if (publisher_id == NULL) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}
	if ( dataPreparerId != NULL ) {
		data_preparer_id = (*env)->GetStringUTFChars(env, dataPreparerId, NULL);
		if ( data_preparer_id == NULL ) {
			return (jlong) 0; /* OutOfMemoryError already thrown */
		}
	}

	iso = iso_volume_new_with_root(volume_id, publisher_id, data_preparer_id, 
		(struct iso_tree_node *) root);
	
	(*env)->ReleaseStringUTFChars(env, volumeId, volume_id);
	(*env)->ReleaseStringUTFChars(env, publisherId, publisher_id);
	(*env)->ReleaseStringUTFChars(env, dataPreparerId, data_preparer_id);

	return (jlong) iso;
}


/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_free
 * Signature: (J)V
 */
JNIEXPORT void JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1free
(
	JNIEnv *env, jclass cls, jlong volume
)
{
	iso_volume_free( (struct iso_volume *) volume );
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_get_root
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1get_1root
(
	JNIEnv *env, jclass cls, jlong volume
)
{
	//return (jlong) iso_volume_get_root( (struct iso_volume *) volume );
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_set_volume_id
 * Signature: (JLjava/lang/String;)V
 */
JNIEXPORT void JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1set_1volume_1id
(
	JNIEnv *env, jclass cls, jlong volume, jstring volumeId
)
{
	const char *volume_id;
	volume_id = (*env)->GetStringUTFChars(env, volumeId, NULL);
	if ( volume_id == NULL ) {
		return; /* OutOfMemoryError already thrown */
	}

	//iso_volume_set_volume_id( (struct iso_volume *) volume, volume_id);

	(*env)->ReleaseStringUTFChars(env, volumeId, volume_id);
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_set_publisher_id
 * Signature: (JLjava/lang/String;)V
 */
JNIEXPORT void JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1set_1publisher_1id
(
	JNIEnv *env, jclass cls, jlong volume, jstring publisherId
)
{
	const char *publisher_id;
	publisher_id = (*env)->GetStringUTFChars(env, publisherId, NULL);
	if (publisher_id == NULL) {
		return; /* OutOfMemoryError already thrown */
	}

	//iso_volume_set_publisher_id( (struct iso_volume *) volume, publisher_id);

	(*env)->ReleaseStringUTFChars(env, publisherId, publisher_id);
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_volume_set_data_preparer_id
 * Signature: (JLjava/lang/String;)V
 */
JNIEXPORT void JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1volume_1set_1data_1preparer_1id
(
	JNIEnv *env, jclass cls, jlong volume, jstring dataPreparerId
)
{
	const char *data_preparer_id;
	data_preparer_id = (*env)->GetStringUTFChars(env, dataPreparerId, NULL);
	if ( data_preparer_id == NULL ) {
		return; /* OutOfMemoryError already thrown */
	}

	//iso_volume_set_data_preparer_id( (struct iso_volume *) volume, data_preparer_id);

	(*env)->ReleaseStringUTFChars(env, dataPreparerId, data_preparer_id);
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_tree_volume_path_to_node
 * Signature: (JLjava/lang/String;)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1tree_1volume_1path_1to_1node
(
	JNIEnv *env, jclass cls, jlong volume, jstring path
)
{
	const char *cpath;
	struct iso_tree_node *node;

	cpath = (*env)->GetStringUTFChars(env, path, NULL);
	if ( cpath == NULL ) {
		return 0; /* OutOfMemoryError already thrown */
	}

	node = iso_tree_volume_path_to_node((struct iso_volume *) volume, cpath);

	(*env)->ReleaseStringUTFChars(env, path, cpath);

	return (jlong) node;	
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_tree_volume_add_path
 * Signature: (JLjava/lang/String;Ljava/lang/String;)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1tree_1volume_1add_1path
(
	JNIEnv *env, jclass cls, jlong volume, jstring discPath, jstring path
)
{
	const char *disc_path;
	const char *cpath;
	struct iso_tree_node *node;

	disc_path = (*env)->GetStringUTFChars(env, discPath, NULL);
	if ( disc_path == NULL ) {
		return 0; /* OutOfMemoryError already thrown */
	}
	cpath = (*env)->GetStringUTFChars(env, path, NULL);
	if ( cpath == NULL ) {
		return 0; /* OutOfMemoryError already thrown */
	}

	node = iso_tree_volume_add_path( (struct iso_volume *) volume, disc_path, path);

	(*env)->ReleaseStringUTFChars(env, path, cpath);
	(*env)->ReleaseStringUTFChars(env, discPath, disc_path);

	return (jlong) node;
}

/*
 * Class:     org_pykix_libburnia_libisofs_IsoVolume
 * Method:    iso_tree_volume_add_new_dir
 * Signature: (JLjava/lang/String;)J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libisofs_IsoVolume_iso_1tree_1volume_1add_1new_1dir
(
	JNIEnv *env, jclass cls, jlong volume, jstring discPath
)
{
	const char *disc_path;
	struct iso_tree_node *node;

	disc_path = (*env)->GetStringUTFChars(env, discPath, NULL);
	if ( disc_path == NULL ) {
		return 0; /* OutOfMemoryError already thrown */
	}
	
	node = iso_tree_volume_add_new_dir( (struct iso_volume *) volume, disc_path);

	(*env)->ReleaseStringUTFChars(env, discPath, disc_path);

	return (jlong) node;
}
