#include <gtk/gtk.h>

static gboolean fullscreen;
static gboolean new_window;
static gchar **uris;

static gboolean startup_called = FALSE;
static GtkWindow *window = NULL;
static GOptionContext *context = NULL;

const GOptionEntry all_options [] =
{
  { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen,
    "Open the application in fullscreen mode", NULL },

  { "new-window", 'n', 0, G_OPTION_ARG_NONE, &new_window,
    "Create a new toplevel window in an existing instance of the app", NULL },

  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &uris,
    NULL, "[uri]" },

  { NULL }
};

static void
app_init (GtkApplication *app)
{
  window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
  gtk_window_set_application (window, app);
  gtk_widget_show (GTK_WIDGET (window));
}

static void
app_startup (GApplication *app,
	     void         *user_data)
{
  /* We don't do anything here, as we need to know the options
   * when we set everything up.
   * Note that this will break D-Bus activation of the application */
  startup_called = TRUE;
}

void
process_command_line (GtkApplication   *app,
		      GtkWindow        *window)
{
  int i;

  if (fullscreen)
    gtk_window_fullscreen (window);

  if (new_window)
    app_init (app);

  if (uris)
    for (i = 0; uris[i] != NULL; i++)
      g_print ("uri: %s\n", uris[i]);
}

static int
app_command_line (GApplication            *app,
		  GApplicationCommandLine *command_line,
		  void                    *user_data)
{
  int argc;
  char **argv;

  argv = g_application_command_line_get_arguments (command_line, &argc);

  /* No need to check for errors here, it's already been called
     on local_command_line handler, if there's an error, it was
     already been raised there */
  g_option_context_parse (context, &argc, &argv, NULL);

  /* Don't create another window if we're remote.
   * We can't use g_application_get_is_remote() because it's not registered yet */
  if (startup_called != FALSE)
    {
      app_init (GTK_APPLICATION (app));
      startup_called = FALSE;
    }
  else
    {
      gtk_window_present_with_time (window, GDK_CURRENT_TIME);
    }

  process_command_line (GTK_APPLICATION (app), window);

  g_strfreev (argv);
  return 0;
}

int main (int argc, char **argv) {
  GtkApplication *app;
  int res;

  g_type_init ();

  /* Setup command line options */
  context = g_option_context_new ("- Remote Desktop Viewer");
  g_option_context_add_main_entries (context, all_options, "vinagre");
  g_option_context_add_group (context, gtk_get_option_group (TRUE));

  app = gtk_application_new ("org.gnome.vinagre", G_APPLICATION_HANDLES_COMMAND_LINE);

  /* With this, GApplication handles --help and related command line stuff (including errors) */
  g_application_set_option_context (G_APPLICATION (app), context);

  /* This only will be called if there's real command line arguments */
  g_signal_connect (app,
		    "command-line",
		    G_CALLBACK (app_command_line),
		    NULL);

  g_signal_connect (app,
		    "startup",
		    G_CALLBACK (app_startup),
		    NULL);

  res = g_application_run (G_APPLICATION (app), argc, argv);

  g_object_unref (app);
  g_option_context_free (context);

  return res;
}
