/* gcc $(pkg-config --cflags --libs libgdata goa-1.0 gio-2.0 glib-2.0)
   gdata-query.c */

#include <gio/gio.h>
#include <glib.h>

#define GOA_API_IS_SUBJECT_TO_CHANGE
#include <gdata/gdata.h>
#include <goa/goa.h>

gint
main (void)
{
  GDataDocumentsFeed *documents_feed = NULL;
  GDataDocumentsQuery *query = NULL;
  GDataDocumentsService *service = NULL;
  GError *error = NULL;
  GList *accounts = NULL;
  GList *l;
  GoaClient *client = NULL;

  client = goa_client_new_sync (NULL, &error);
  if (error != NULL)
    {
      g_warning ("%s", error->message);
      g_error_free (error);
      goto out;
    }

  accounts = goa_client_get_accounts (client);
  for (l = accounts; l != NULL; l = l->next)
    {
      GoaAccount *account;
      GoaObject *object = GOA_OBJECT (l->data);
      const gchar *account_identity;
      const gchar *provider_type;

      account = goa_object_peek_account (object);
      account_identity = goa_account_get_identity (account);
      provider_type = goa_account_get_provider_type (account);

      if (g_strcmp0 (provider_type, "google") == 0)
        {
          GDataGoaAuthorizer *authorizer;

          authorizer = gdata_goa_authorizer_new (object);
          service = gdata_documents_service_new (GDATA_AUTHORIZER (authorizer));
          g_object_unref (authorizer);
        }
    }

  if (service == NULL)
    {
      g_warning ("account not found");
      goto out;
    }

  query = gdata_documents_query_new_with_limits (NULL, 1, 100);
  gdata_documents_query_set_show_folders (query, TRUE);

  while (TRUE)
    {
      GList *document_entries;

      error = NULL;
      documents_feed = gdata_documents_service_query_documents (service, query, NULL, NULL, NULL, &error);
      if (error != NULL)
        {
          g_warning ("query-documents: (%s): %s", g_quark_to_string (error->domain), error->message);
          g_error_free (error);
          goto out;
        }

      document_entries = gdata_feed_get_entries (GDATA_FEED (documents_feed));
      if (document_entries == NULL)
        goto out;

      for (l = document_entries; l != NULL; l = l->next)
        {
          GDataEntry *entry = GDATA_ENTRY (l->data);
          GDataFeed *feed = NULL;
          GDataLink *link;
          GList *authors;
          GList *categories;
          GList *entries;
          GList *k;
          GList *links = NULL;
          const gchar *document_id;
          const gchar *id;
          const gchar *resource_id;
          const gchar *summary;
          const gchar *title;
          const gchar *uri;
          gint64 published;
          gint64 updated;

          id = gdata_entry_get_id (entry);
          g_message ("id: %s", id);

          document_id = gdata_documents_entry_get_document_id (GDATA_DOCUMENTS_ENTRY (entry));
          resource_id = gdata_documents_entry_get_resource_id (GDATA_DOCUMENTS_ENTRY (entry));
          g_message ("document_id: %s, resource_id: %s", document_id, resource_id);

          title = gdata_entry_get_title (entry);
          g_message ("title: %s", title);

          published = gdata_entry_get_published (entry);
          updated = gdata_entry_get_updated (entry);
          g_message ("published: %" G_GINT64_FORMAT ", updated: %" G_GINT64_FORMAT, published, updated);

          link = gdata_entry_look_up_link (entry, GDATA_LINK_SELF);
          uri = gdata_link_get_uri (link);
          g_message ("self: %s", uri);

          link = gdata_entry_look_up_link (entry, GDATA_LINK_ALTERNATE);
          uri = gdata_link_get_uri (link);
          g_message ("alternate: %s", uri);

          links = gdata_entry_look_up_links (entry, "http://schemas.google.com/docs/2007#parent");
          for (k = links; k != NULL; k = k->next)
            {
              link = GDATA_LINK (k->data);
              uri = gdata_link_get_uri (link);
              g_message ("parent: %s", uri);
            }

          categories = gdata_entry_get_categories (entry);
          for (k = categories; k != NULL; k = k->next)
            {
              GDataCategory *category = GDATA_CATEGORY (k->data);
              const gchar *label;
              const gchar *scheme;
              const gchar *term;

              scheme = gdata_category_get_scheme (category);
              g_message ("scheme: %s", scheme);

              term = gdata_category_get_term (category);
              g_message ("term: %s", term);

              label = gdata_category_get_label (category);
              g_message ("label: %s", label);
            }

          summary = gdata_entry_get_summary (GDATA_ENTRY (entry));
          g_message ("summary: %s", summary);

          authors = gdata_entry_get_authors (GDATA_ENTRY (entry));
          for (k = authors; k != NULL; k = k->next)
            {
              GDataAuthor *author = GDATA_AUTHOR (k->data);
              const gchar *email;
              const gchar *name;

              email = gdata_author_get_email_address (author);
              name = gdata_author_get_name (author);
              g_message ("email: %s, name: %s", email, name);

              uri = gdata_author_get_uri (author);
              g_message ("uri: %s", uri);
            }

          error = NULL;
          feed = gdata_access_handler_get_rules (GDATA_ACCESS_HANDLER (entry),
                                                 GDATA_SERVICE (service),
                                                 NULL,
                                                 NULL, NULL, &error);
          if (error != NULL)
            {
              g_warning ("get-rules: (%s): %s", g_quark_to_string (error->domain), error->message);
              g_error_free (error);
              goto continuation;
            }

          entries = gdata_feed_get_entries (feed);
          for (k = entries; k != NULL; k = k->next)
            {
              GDataAccessRule *rule = GDATA_ACCESS_RULE (k->data);
              const gchar *key;
              const gchar *role;
              const gchar *scope_type;
              const gchar *scope_value;
              gint64 edited;

              key = gdata_access_rule_get_key (rule);
              g_message ("key: %s", key);

              role = gdata_access_rule_get_role (rule);
              g_message ("role: %s", role);

              gdata_access_rule_get_scope (rule, &scope_type, &scope_value);
              g_message ("scope_type: %s, scope_value: %s", scope_type, scope_value);

              edited = gdata_access_rule_get_edited (rule);
              g_message ("edited: %" G_GINT64_FORMAT, edited);
            }

        continuation:
          g_message ("...");
          g_clear_object (&feed);
          g_list_free (links);
        }

      g_message ("next page...");
      gdata_query_next_page (GDATA_QUERY (query));
      g_object_unref (documents_feed);
    }

 out:
  g_clear_object (&documents_feed);
  g_clear_object (&query);
  g_clear_object (&service);
  g_clear_object (&client);
  g_list_free_full (accounts, g_object_unref);
  return 0;
}
