/* $Id: open-preferred-application.c 183 2006-08-06 19:45:25Z benny $ */
/*-
 * Copyright (c) 2006 Benedikt Meurer <benny@xfce.org>
 *
 * This library 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.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * Sample application to open the preferred application for a given
 * Mime-Type using the ThunarVfsMimeDatabase. Compile with:
 *
 *  cc -o open-preferred-application open-preferred-application.c \
 *     `pkg-config --cflags --libs thunar-vfs-1` -DEXO_API_SUBJECT_TO_CHANGE
 */

#include <stdio.h>
#include <stdlib.h>

#include <thunar-vfs/thunar-vfs.h>



int
main (int argc, char **argv)
{
  ThunarVfsMimeApplication *mime_application;
  ThunarVfsMimeDatabase    *mime_database;
  ThunarVfsMimeInfo        *mime_info;
  GError                   *err = NULL;
  gint                      exit_code = EXIT_FAILURE;

  /* initialize GTK+ */
  gtk_init (&argc, &argv);

  /* make sure that a Mime-Type is specified */
  if (G_UNLIKELY (argc != 2))
    {
      fprintf (stderr, "Usage: open-preferred-application <mime-type>\n");
      return EXIT_FAILURE;
    }

  /* initialize thunar-vfs */
  thunar_vfs_init ();

  /* initialize the mime database */
  mime_database = thunar_vfs_mime_database_get_default ();

  /* determine the mime info for the specified Mime-Type */
  mime_info = thunar_vfs_mime_database_get_info (mime_database, argv[1]);

  /* determine the preferred application for the mime info */
  mime_application = thunar_vfs_mime_database_get_default_application (mime_database, mime_info);
  if (G_UNLIKELY (mime_application == NULL))
    {
      fprintf (stderr, "open-preferred-application: No application registered for \"%s\"\n", thunar_vfs_mime_info_get_comment (mime_info));
    }
  else
    {
      /* try to execute the application */
      if (!thunar_vfs_mime_handler_exec (THUNAR_VFS_MIME_HANDLER (mime_application), NULL, NULL, &err))
        {
          fprintf (stderr, "open-preferred-application: Failed to execute \"%s\": %s\n", thunar_vfs_mime_handler_get_name (THUNAR_VFS_MIME_HANDLER (mime_application)), err->message);
          g_error_free (err);
        }
      else
        {
          /* we did it */
          exit_code = EXIT_SUCCESS;
        }

      /* cleanup */
      g_object_unref (G_OBJECT (mime_application));
    }

  /* cleanup */
  thunar_vfs_mime_info_unref (mime_info);

  /* release the mime database */
  g_object_unref (G_OBJECT (mime_database));

  /* shutdown thunar-vfs */
  thunar_vfs_shutdown ();

  return exit_code;
}
