/*
 * This is just meant to test that g_filename_from_uri() can parse a URI
 * that was provided by GtkFileChooser.
 * Build it like so:
 * gcc -o ./test_filename test_filechooser_to_filename.cc `pkg-config gtk+-2.0 --libs --cflags`
 */
 
#include <gtk/gtk.h>
#include <stdlib.h>

int main(int argc, char** argv) {
  gtk_init(&argc, &argv);
  
  GtkWidget *dialog = gtk_file_chooser_dialog_new ("test", NULL,
    GTK_FILE_CHOOSER_ACTION_OPEN,
    "Cancel", GTK_RESPONSE_CANCEL,
    "_Open", GTK_RESPONSE_ACCEPT,
    NULL);
  int res = gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_hide (dialog);
  if (res != GTK_RESPONSE_ACCEPT) {
    return EXIT_SUCCESS;
  }
  
  GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
  char* uri = gtk_file_chooser_get_uri (chooser);
  printf("uri: %s\n", uri);
  
  GError *error = NULL;
  char* filename = g_filename_from_uri (uri, NULL, &error);
  if (error) {
    printf("Error from g_filename_from_uri(): %s\n", error->message);
    return EXIT_FAILURE;
  }
  
  g_free (uri);

  printf("filename: %s\n", filename);
  g_free (filename);
  
  gtk_widget_destroy (dialog);
  
  return EXIT_SUCCESS;
}
