[recipes/todoist: 5/12] Mark shopping list items as done in Todoist on click of 'Done Shopping' in Shopping page



commit 610ee95e8c8d0d218f79bf9030a5ad80364d97d8
Author: Ekta Nandwani <mailnandwaniekta gmail com>
Date:   Fri Aug 18 07:19:52 2017 +0530

    Mark shopping list items as done in Todoist on click of 'Done Shopping' in Shopping page

 src/gr-shopping-list-exporter.c |  158 +++++++++++++++++++++++++++++++++++++++
 src/gr-shopping-list-exporter.h |    3 +-
 src/gr-window.c                 |    9 ++
 3 files changed, 169 insertions(+), 1 deletions(-)
---
diff --git a/src/gr-shopping-list-exporter.c b/src/gr-shopping-list-exporter.c
index e9f34f8..055b909 100644
--- a/src/gr-shopping-list-exporter.c
+++ b/src/gr-shopping-list-exporter.c
@@ -102,6 +102,151 @@ gr_shopping_list_exporter_new (GtkWindow *parent)
         return exporter;
 }
 
+static void
+complete_items_callback (RestProxyCall *call,
+                        GError *error,
+                        GObject *obj,
+                        GrShoppingListExporter *exporter)
+{
+
+       guint status_code;
+
+       status_code = rest_proxy_call_get_status_code (call);
+
+       if (status_code != 200) {
+               g_warning ("Couldn't complete items in todoist");
+       }
+
+}
+
+static void
+complete_items (GrShoppingListExporter *exporter, GList *items)
+{
+       RestProxy *proxy;
+       RestProxyCall *call;
+       GError *error;
+       g_autofree gchar *uuid = g_uuid_string_random ();
+
+       GList *l;
+       GString *commands;
+       commands = g_string_new ("");
+       g_string_append_printf (commands , "[{\"type\": \"item_complete\", \"uuid\": \"%s\", \"args\": 
{\"ids\": [", uuid);
+       for (l = items; l != NULL; l = l->next) {
+               JsonObject *object;
+               double id;
+               glong item_id;
+
+               object = json_node_get_object (l->data);
+               id = json_object_get_double_member (object, "id");
+               item_id = (glong) id;
+               g_string_append_printf (commands, "%ld,", item_id);
+       }
+       commands = g_string_truncate (commands, commands->len-1);
+       g_string_append_printf (commands, "]}}]");
+
+       error = NULL;
+
+       proxy = rest_proxy_new (TODOIST_URL, FALSE);
+       call = rest_proxy_new_call (proxy);
+       rest_proxy_call_set_method (call, "POST");
+       rest_proxy_call_add_header (call, "content-type", "application/x-www-form-urlencoded");
+       rest_proxy_call_add_param (call, "token", exporter->access_token);
+
+       if (!exporter->sync_token)
+               rest_proxy_call_add_param (call, "sync_token", "\'*\'");
+       else
+               rest_proxy_call_add_param (call, "sync_token", exporter->sync_token);
+
+       rest_proxy_call_add_param (call, "commands", commands->str);
+
+       if (!rest_proxy_call_async (call, (RestProxyCallAsyncCallback) complete_items_callback,
+                                   NULL, exporter, &error))
+       {
+           g_warning ("Couldn't execute RestProxyCall");
+           goto out;
+       }
+       out:
+         g_object_unref (proxy);
+         g_object_unref (call);
+}
+
+static void
+get_project_data_callback (RestProxyCall *call,
+                          GError *error,
+                          GObject *obj,
+                          GrShoppingListExporter *exporter)
+{
+       JsonObject *object = NULL;
+       JsonParser *parser = NULL;
+       GError *parse_error;
+       const gchar *payload;
+       guint status_code;
+       gsize payload_length;
+
+       JsonArray *json_items;
+       GList *items;
+
+       status_code = rest_proxy_call_get_status_code (call);
+
+       if (status_code != 200) {
+               g_warning ("status code %d", status_code);
+               goto out;
+       }
+
+       parser = json_parser_new ();
+       payload = rest_proxy_call_get_payload (call);
+       payload_length = rest_proxy_call_get_payload_length (call);
+       if (!json_parser_load_from_data (parser, payload, payload_length, &parse_error)) {
+               g_clear_error (&parse_error);
+               g_warning ("Couldn't load payload");
+               goto out;
+       }
+
+       object = json_node_dup_object (json_parser_get_root (parser));
+
+       if (!object) {
+               g_warning ("No Data found");
+               goto out;
+       }
+
+       json_items = json_object_get_array_member (object, "items");
+       items = json_array_get_elements (json_items);
+       if (items)
+       {
+               complete_items (exporter, items);
+       }
+       out:
+         g_object_unref (parser);
+         g_object_unref (object);
+}
+
+static void
+get_project_data (GrShoppingListExporter *exporter)
+{
+       RestProxy *proxy;
+       RestProxyCall *call;
+       GError *error;
+       const gchar *id;
+       id = g_strdup_printf ("%ld", exporter->project_id);
+
+       proxy = rest_proxy_new ("https://todoist.com/api/v7/projects/get_data";, FALSE);
+       call = rest_proxy_new_call (proxy);
+       rest_proxy_call_set_method (call, "POST");
+       rest_proxy_call_add_header (call, "content-type", "application/x-www-form-urlencoded");
+       rest_proxy_call_add_param (call, "token", exporter->access_token);
+       rest_proxy_call_add_param (call, "project_id", id);
+
+       if (!rest_proxy_call_async (call, (RestProxyCallAsyncCallback) get_project_data_callback,
+                                   NULL, exporter, &error))
+       {
+               g_warning ("Couldn't execute RestProxyCall");
+               goto out;
+       }
+
+       out:
+         g_object_unref (proxy);
+         g_object_unref (call);
+}
 
 static void
 switch_dialog_contents (GrShoppingListExporter *exporter)
@@ -288,6 +433,19 @@ get_access_token (GrShoppingListExporter *exporter)
 
 }
 
+void
+done_shopping_in_todoist (GrShoppingListExporter *exporter)
+{
+       gboolean project_present = FALSE;
+       if (get_todoist_account (exporter)) {
+               get_access_token (exporter);
+               if (!exporter->project_id)
+                       project_present = get_project_id (exporter);
+               if (project_present)
+                       get_project_data (exporter);
+       }
+}
+
 static void
 add_project_id (GrShoppingListExporter *exporter)
 {
diff --git a/src/gr-shopping-list-exporter.h b/src/gr-shopping-list-exporter.h
index 8af5ea6..6fc44b2 100644
--- a/src/gr-shopping-list-exporter.h
+++ b/src/gr-shopping-list-exporter.h
@@ -32,6 +32,7 @@ G_DECLARE_FINAL_TYPE (GrShoppingListExporter, gr_shopping_list_exporter, GR, SHO
 
 GrShoppingListExporter *gr_shopping_list_exporter_new    (GtkWindow        *window);
 
-void              gr_shopping_list_exporter_export (GrShoppingListExporter *exporter, GList *items);
+void                                   gr_shopping_list_exporter_export (GrShoppingListExporter *exporter, 
GList *items);
 
+void                                   done_shopping_in_todoist (GrShoppingListExporter *exporter);
 G_END_DECLS
\ No newline at end of file
diff --git a/src/gr-window.c b/src/gr-window.c
index fb507f9..0520d76 100644
--- a/src/gr-window.c
+++ b/src/gr-window.c
@@ -47,6 +47,7 @@
 #include "gr-utils.h"
 #include "gr-appdata.h"
 #include "gr-settings.h"
+#include "gr-shopping-list-exporter.h"
 
 
 struct _GrWindow
@@ -824,6 +825,7 @@ done_shopping (GrWindow *window)
         GList *recipes, *l;
         GrRecipeStore *store;
         const char **removed_ingredients;
+        GrShoppingListExporter *exporter = NULL;
 
         store = gr_recipe_store_get ();
 
@@ -855,6 +857,13 @@ done_shopping (GrWindow *window)
         window->removed_ingredients = g_strdupv ((char **)removed_ingredients);
 
         gr_recipe_store_clear_shopping_list (store);
+        if (!exporter) {
+                GtkWidget *shopping_window;
+
+                shopping_window = gtk_widget_get_ancestor (GTK_WIDGET (window->shopping_page), 
GTK_TYPE_APPLICATION_WINDOW);
+                exporter = gr_shopping_list_exporter_new (GTK_WINDOW (shopping_window));
+                done_shopping_in_todoist (exporter);
+        }
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]