/* gcc evince-ps-pdf.c $(pkg-config --cflags --libs
   evince-document-3.0 evince-view-3.0 gtk+-3.0) */

#include <evince-document.h>
#include <evince-view.h>
#include <gtk/gtk.h>

static void load_uri (const gchar *uri);

GtkWidget *view;

static gboolean
load_next (gpointer user_data)
{
  load_uri ("file:///home/rishi/Documents/cbs-mr-hicks.pdf");
  return G_SOURCE_REMOVE;
}

static void
load_job_done (EvJob *job)
{
  EvDocumentModel *model;

  if (ev_job_is_failed (job) || job->document == NULL)
    {
      g_warning ("load failed: %s", job->error->message);
      return;
    }

  model = ev_document_model_new_with_document (job->document);
  ev_view_set_model (EV_VIEW (view), model);
  g_object_unref (model);

  g_timeout_add_seconds (5, load_next, NULL);
}

static void
load_uri (const gchar *uri)
{
  EvJob *job;

  job = ev_job_load_new (uri);
  g_signal_connect (job, "finished", G_CALLBACK (load_job_done), NULL);
  ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
  g_object_unref (job);
}

int
main (void)
{
  GtkWidget *sw;
  GtkWidget *window;

  ev_init ();
  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
  g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_container_add (GTK_CONTAINER (window), sw);

  view = ev_view_new ();
  gtk_container_add (GTK_CONTAINER (sw), view);

  load_uri ("file:///home/rishi/Documents/articles-and-slides/chat11.ps");

  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}
