
/* Lame example to demonstrate that GVfsIcon works. Copyright (c) 2008 Red Hat, Inc.
 *
 * Licensed under LGPL version 2 or later.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 *
 * compile with: gcc -o main main.c `pkg-config --cflags --libs gio-2.0`
 */

#include <gio/gio.h>

static int ret;

static gboolean
load_icon (GInputStream *stream, gboolean sync)
{
  GError *error;
  gboolean ret;
  gsize bytes_read;
  char buf[200 * 1024];
  GFile *f;

  ret = FALSE;

  error = NULL;
  if (!g_input_stream_read_all (stream,
                                &buf,
                                sizeof buf,
                                &bytes_read,
                                NULL,
                                &error))
    {
      g_printerr ("Error reading from buf: %s", error->message);
      g_error_free (error);
      goto out;
    }

  g_print ("Read %d bytes (%s)\n", bytes_read, sync ? "sync" : "async");

  f = g_file_new_for_commandline_arg ("preview-icon.jpg");
  if (!g_file_replace_contents (f,
                                buf,
                                bytes_read,
                                NULL,
                                FALSE,
                                G_FILE_CREATE_NONE,
                                NULL,
                                NULL,
                                &error))
    {
      g_printerr ("Error writing preview-icon.png: %s", error->message);
      g_error_free (error);
      goto out;
    }
  g_object_unref (f);

  ret = TRUE;

 out:
  return ret;
}

static void
got_stream_cb (GObject *source_object,
               GAsyncResult *res,
               gpointer user_data)
{
  GLoadableIcon *loadable_icon = G_LOADABLE_ICON (source_object);
  GInputStream *stream;
  GError *error;
  GMainLoop *loop = (GMainLoop *) user_data;

  stream = NULL;

  error = NULL;
  stream = g_loadable_icon_load_finish (loadable_icon,
                                        res,
                                        NULL,  /* return location for type */
                                        &error);
  if (stream == NULL)
    {
      g_printerr ("Error getting stream: %s", error->message);
      g_error_free (error);
      goto out;
    }

  if (!load_icon (stream, FALSE))
    goto out;

  ret = 0;

 out:
  if (stream != NULL)
    g_object_unref (stream);
  g_main_loop_quit (loop);
}

int
main (int argc, char *argv[])
{
  GFile *file;
  GFileInfo *file_info;
  GInputStream *stream;
  gboolean sync;

  ret = 1;

  file = NULL;
  file_info = NULL;
  stream = NULL;

  g_type_init ();

  if (argc != 3)
    {
      g_printerr ("usage: main (sync|async) URI\n"
                  "\n"
                  "  example: ./main sync gphoto2://[usb:002,005]/DCIM/100APPLE/IMG_0041.JPG\n"
                  "\n");
      goto out;
    }

  sync = (strcmp (argv[1], "sync") == 0);

  file = g_file_new_for_commandline_arg (argv[2]);
  file_info = g_file_query_info (file,
                                 G_FILE_ATTRIBUTE_PREVIEW_ICON,
                                 G_FILE_QUERY_INFO_NONE,
                                 NULL,  /* GCancellable */
                                 NULL); /* return location for GError */
  if (file_info != NULL)
    {
      GObject *object;

      object = g_file_info_get_attribute_object (file_info,
                                                 G_FILE_ATTRIBUTE_PREVIEW_ICON);
      if (object != NULL && G_IS_LOADABLE_ICON (object))
        {
          if (sync)
            {
              GError *error;

              error = NULL;
              stream = g_loadable_icon_load (G_LOADABLE_ICON (object),
                                                   0,     /* size */
                                                   NULL,  /* return location for type */
                                                   NULL,  /* GCancellable */
                                                   &error);
              if (stream == NULL)
                {
                  g_printerr ("Error getting stream: %s", error->message);
                  g_error_free (error);
                  goto out;
                }
              if (!load_icon (stream, TRUE))
                goto out;

              ret = 0;
            }
          else
            {
              GMainLoop *loop;

              loop = g_main_loop_new (NULL, FALSE);

              g_loadable_icon_load_async (G_LOADABLE_ICON (object),
                                          0,     /* size */
                                          NULL,  /* GCancellable */
                                          got_stream_cb,
                                          loop);

              g_main_loop_run (loop);
            }
        }
      else
        {
          g_printerr ("No preview::icon for %s\n", argv[2]);
          goto out;
        }
    }
  else
    {
      g_printerr ("No file info for %s\n", argv[2]);
      goto out;
    }

 out:
  if (file != NULL)
    g_object_unref (file);
  if (file_info != NULL)
    g_object_unref (file_info);
  if (stream != NULL)
    g_object_unref (stream);
  return ret;
}
