[recipes] Add an api to create the user chef



commit 0090b93cf486491eb88dfe21d628c7f749dd3577
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Feb 12 19:09:24 2017 -0500

    Add an api to create the user chef
    
    This is just moving some code from the edit page to a place
    where it can be reused.

 src/gr-account.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gr-account.h |    9 +++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-account.c b/src/gr-account.c
index 343742f..6cf1695 100644
--- a/src/gr-account.c
+++ b/src/gr-account.c
@@ -21,6 +21,8 @@
 #include "config.h"
 
 #include "gr-account.h"
+#include "gr-recipe-store.h"
+#include "gr-app.h"
 
 #include <glib/gi18n.h>
 
@@ -258,3 +260,68 @@ gr_account_get_information (GtkWindow                  *window,
 
         gtk_window_export_handle (window, window_handle_exported, cbdata);
 }
+
+typedef struct {
+        UserChefCallback callback;
+        gpointer data;
+} UserChefData;
+
+static void
+got_account_info (const char  *id,
+                  const char  *name,
+                  const char  *image_path,
+                  gpointer     data,
+                  GError      *error)
+{
+        UserChefData *cbdata = data;
+        GrRecipeStore *store;
+        g_autoptr(GrChef) chef = NULL;
+        g_autoptr(GError) local_error = NULL;
+
+        store = gr_app_get_recipe_store (GR_APP (g_application_get_default ()));
+
+        if (error) {
+                g_message ("Failed to get account information: %s", error->message);
+                g_free (cbdata);
+                return;
+        }
+
+        chef = gr_chef_new ();
+        g_object_set (chef,
+                      "id", id,
+                      "fullname", name,
+                      "image-path", image_path,
+                      NULL);
+
+        if (!gr_recipe_store_update_user (store, chef, &local_error))
+                g_warning ("Failed to update chef for user: %s", local_error->message);
+
+        if (cbdata->callback)
+                cbdata->callback (chef, cbdata->data);
+        g_free (cbdata);
+}
+
+void
+gr_ensure_user_chef (GtkWindow        *window,
+                     UserChefCallback  callback,
+                     gpointer          data)
+{
+        GrRecipeStore *store;
+        g_autoptr(GrChef) chef = NULL;
+        UserChefData *cbdata;
+
+        store = gr_app_get_recipe_store (GR_APP (g_application_get_default ()));
+
+        chef = gr_recipe_store_get_chef (store, gr_recipe_store_get_user_key (store));
+        if (chef) {
+                callback (chef, data);
+                return;
+        }
+
+        cbdata = g_new0 (UserChefData, 1);
+        cbdata->callback = callback;
+        cbdata->data = data;
+
+        gr_account_get_information (window, got_account_info, cbdata, NULL);
+}
+
diff --git a/src/gr-account.h b/src/gr-account.h
index 854d934..de354f0 100644
--- a/src/gr-account.h
+++ b/src/gr-account.h
@@ -22,6 +22,8 @@
 
 #include <gtk/gtk.h>
 
+#include "gr-chef.h"
+
 G_BEGIN_DECLS
 
 typedef void (* AccountInformationCallback) (const char  *id,
@@ -35,4 +37,11 @@ void    gr_account_get_information (GtkWindow                  *window,
                                     gpointer                    data,
                                     GDestroyNotify              destroy);
 
+typedef void (*UserChefCallback) (GrChef   *chef,
+                                  gpointer  data);
+
+void    gr_ensure_user_chef (GtkWindow        *window,
+                             UserChefCallback  callback,
+                             gpointer          data);
+
 G_END_DECLS


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