#include <gtk/gtk.h>

#include <string.h>
#include <unistd.h>

int
main (int   argc,
      char *argv[])
{
  gchar *filename = NULL;
  GtkWidget *dialog;
  gint fd;

  gtk_init (&argc, &argv);

  fd = g_file_open_tmp ("fc-504323-XXXXXX.desktop", &filename, NULL);

  dialog = gtk_file_chooser_dialog_new_with_backend (NULL, NULL,
                                                     GTK_FILE_CHOOSER_ACTION_SAVE,
                                                     argc > 1 ? argv[1] : NULL,
                                                     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
                                                     GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
                                                     NULL);

  if (-1 != fd)
    {
      gchar *contents, *uri;

      gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), filename);

      uri = g_filename_to_uri (g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP),
                               NULL, NULL);

      contents = g_strconcat ("[Desktop Entry]\n"
                              "Version=1.0\n"
                              "Encoding=UTF-8\n"
                              "Name=foo_desktop\n"
                              "Comment=Open \"", uri, "\"\n"
                              "Icon=gnome-fs-directory\n"
                              "URL=", uri, "\n"
                              "Type=Link\n",
                              NULL);

      write (fd, contents, strlen (contents));
      close (fd);

      g_free (contents);
      g_free (uri);
    }

  if (GTK_RESPONSE_ACCEPT == gtk_dialog_run (GTK_DIALOG (dialog)))
    {
      g_print ("%s\n", gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)));
      g_print ("%s\n", gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog)));
    }

  if (-1 != fd && filename)
    unlink (filename);

  return 0;
}
