#include <string.h>
#include <gtk/gtk.h>

enum {
  ICON_COLUMN,
  MIMETYPE_COLUMN,
  DESCRIPTION_COLUMN,
  N_COLUMNS
};

static GtkTreeModel *
create_model (void)
{
  GtkListStore *store;
  GList *list, *l;

  store = gtk_list_store_new (N_COLUMNS, G_TYPE_ICON, G_TYPE_STRING, G_TYPE_STRING);

  list = g_content_types_get_registered ();
  for (l = list; l != NULL; l = l->next) {
    const char *content_type = (const char *) l->data;
    GIcon *icon, *emblem_icon, *emblemed_icon;
    GEmblem *emblem;
    char *mime_type, *description;
    GtkTreeIter iter;

    mime_type = g_content_type_get_mime_type (content_type);
    description = g_content_type_get_description (content_type);
    icon = g_content_type_get_icon (content_type);

    emblem_icon = g_themed_icon_new ("go-up");
    emblem = g_emblem_new (emblem_icon);
    emblemed_icon = g_emblemed_icon_new (icon, emblem);
    
    gtk_list_store_insert_with_values (store, &iter, -1,
                                       ICON_COLUMN, emblemed_icon,
                                       MIMETYPE_COLUMN, mime_type,
                                       DESCRIPTION_COLUMN, description,
                                       -1);

    g_free (mime_type);
    g_free (description);
    g_object_unref (icon);
    g_object_unref (emblem_icon);
    g_object_unref (emblem);
    g_object_unref (emblemed_icon);
  }

  g_list_foreach (list, (GFunc) g_free, NULL);
  g_list_free (list);

  return GTK_TREE_MODEL (store);
}

int
main (int argc, char **argv)
{
  GtkWidget *window, *sw, *tv;
  GtkTreeModel *model;
  GtkTreeViewColumn *column;
  GtkCellRenderer *icon_cell, *text_cell;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);

  model = create_model ();
  tv = gtk_tree_view_new_with_model (model);
  g_object_unref (model);

  column = gtk_tree_view_column_new ();

  icon_cell = gtk_cell_renderer_pixbuf_new ();
  g_object_set (icon_cell, "stock-size", GTK_ICON_SIZE_DIALOG, NULL);
  gtk_tree_view_column_pack_start (column, icon_cell, FALSE);
  gtk_tree_view_column_add_attribute (column, icon_cell, "gicon", ICON_COLUMN);

  text_cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, text_cell, TRUE);
  gtk_tree_view_column_add_attribute (column, text_cell, "text", MIMETYPE_COLUMN);

  text_cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, text_cell, TRUE);
  gtk_tree_view_column_add_attribute (column, text_cell, "text", DESCRIPTION_COLUMN);

  gtk_tree_view_append_column (GTK_TREE_VIEW (tv), column);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  gtk_container_add (GTK_CONTAINER (sw), tv);
  gtk_container_add (GTK_CONTAINER (window), sw);
  
  gtk_widget_show_all (sw);
  gtk_window_present (GTK_WINDOW (window));
  
  gtk_main ();

  return 0;
}
