Quellcode durchsuchen

Load all notes from server

Matthias Vogelgesang vor 7 Jahren
Ursprung
Commit
0644d6948c
3 geänderte Dateien mit 168 neuen und 93 gelöschten Zeilen
  1. 130 82
      src/iridium-standard-file.c
  2. 5 4
      src/iridium-standard-file.h
  3. 33 7
      src/iridium-window.c

+ 130 - 82
src/iridium-standard-file.c

@@ -57,19 +57,17 @@ typedef struct {
 
 typedef struct {
   IridiumStandardFile *client;
-  JsonParser *parser;
   gchar *password;
-  GCancellable *cancellable;
 } ReadAuthParams;
 
 struct _IridiumStandardFile
 {
   GObject parent_instance;
 
-  gchar *email;
-  gchar *token;
-  SoupSession *session;
-  SoupURI *base_uri;
+  gchar         *email;
+  gchar         *token;
+  SoupSession   *session;
+  SoupURI       *base_uri;
   StandardFileAuthParams auth_params;
 };
 
@@ -215,6 +213,10 @@ decrypt_item (JsonObject *item, StandardFileAuthParams *params)
 
   uuid = json_object_get_string_member (item, "uuid");
   enc_item_key = json_object_get_string_member (item, "enc_item_key");
+
+  if (!g_strcmp0 (enc_item_key, ""))
+    return NULL;
+
   enc_auth_key = decrypt (enc_item_key, uuid, params->keys.master, params->keys.auth, sizeof (params->keys.master));
 
   enc_key = (gchar *) enc_auth_key;
@@ -273,13 +275,14 @@ deserialize_item (JsonObject *meta, const gchar *data)
 }
 
 static gboolean
-get_auth_params (ReadAuthParams *params,
+get_auth_params (JsonParser *parser,
+                 ReadAuthParams *params,
                  GError **error)
 {
   JsonObject *object;
   const gchar *s;
 
-  object = json_node_get_object (json_parser_get_root (params->parser));
+  object = json_node_get_object (json_parser_get_root (parser));
 
   params->client->auth_params.func = SF_FUNC_PBKDF2;
   params->client->auth_params.hash = SF_HASH_SHA512;
@@ -314,50 +317,10 @@ get_auth_params (ReadAuthParams *params,
   return TRUE;
 }
 
-GList *
-iridium_standard_file_load (IridiumStandardFile *client,
-                            const gchar *filename)
-{
-  JsonObject *root;
-  JsonArray *array;
-  g_autoptr(JsonParser) parser;
-  GList *result = NULL;
-  GError *error = NULL;
-
-  parser = json_parser_new_immutable ();
-  json_parser_load_from_file (parser, filename, &error);
-
-  if (error != NULL) {
-    g_error ("Error: %s\n", error->message);
-    return NULL;
-  }
-
-  root = json_node_get_object (json_parser_get_root (parser));
-  array = json_object_get_array_member (root, "items");
-
-  for (guint i = 0; i < json_array_get_length (array); i++) {
-    JsonObject *data;
-    GObject *item;
-    gchar *content;
-
-    data = json_array_get_object_element (array, i);
-    content = decrypt_item (data, &client->auth_params);
-    item = deserialize_item (data, content);
-
-    if (item)
-      result = g_list_append (result, item);
-
-    g_free (content);
-  }
-
-  return result;
-}
-
 static void
 read_auth_params_data_free (ReadAuthParams *data)
 {
   secret_password_free (data->password);
-  g_object_unref (data->parser);
 }
 
 static void
@@ -367,23 +330,28 @@ on_signin_response_parsed (GObject *object,
 {
   GTask *task;
   ReadAuthParams *data;
+  JsonParser *parser;
   JsonObject *root_object;
   GError *error = NULL;
 
   task = user_data;
   data = g_task_get_task_data (task);
+  parser = JSON_PARSER (object);
 
-  if (!json_parser_load_from_stream_finish (data->parser, result, &error)) {
+  if (!json_parser_load_from_stream_finish (parser, result, &error)) {
     g_task_return_error (task, error);
+    g_object_unref (task);
     return;
   }
 
-  root_object = json_node_get_object (json_parser_get_root (data->parser));
+  root_object = json_node_get_object (json_parser_get_root (parser));
 
   g_free (data->client->token);
   data->client->token = g_strdup (json_object_get_string_member (root_object, "token"));
 
   g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
+  g_object_unref (parser);
 }
 
 static void
@@ -393,20 +361,22 @@ on_send_signin_message (GObject *object,
 {
   GInputStream *stream;
   GTask *task;
-  ReadAuthParams *data;
+  JsonParser *parser;
   GError *error = NULL;
 
   task = user_data;
-  data = g_task_get_task_data (task);
-  stream = soup_session_send_finish (SOUP_SESSION (object), result, &error);
+  stream = soup_request_send_finish (SOUP_REQUEST (object), result, &error);
 
   if (stream == NULL) {
     g_task_return_error (task, error);
     return;
   }
 
-  /* we re-use the old parser */
-  json_parser_load_from_stream_async (data->parser, stream, data->cancellable, on_signin_response_parsed, task);
+  parser = json_parser_new ();
+  json_parser_load_from_stream_async (parser, stream, g_task_get_cancellable (task),
+                                      on_signin_response_parsed, task);
+
+  g_object_unref (stream);
 }
 
 static void
@@ -415,8 +385,10 @@ on_auth_params_response_parsed (GObject *object,
                                 gpointer user_data)
 {
   GTask *task;
+  JsonParser *parser;
   ReadAuthParams *data;
   SoupURI *uri;
+  SoupRequestHTTP *request;
   SoupMessage *msg;
   gchar *password;
   gchar *body;
@@ -424,13 +396,14 @@ on_auth_params_response_parsed (GObject *object,
 
   task = user_data;
   data = g_task_get_task_data (task);
+  parser = JSON_PARSER (object);
 
-  if (!json_parser_load_from_stream_finish (data->parser, result, &error)) {
+  if (!json_parser_load_from_stream_finish (parser, result, &error)) {
     g_task_return_error (task, error);
     return;
   }
 
-  if (!get_auth_params (data, &error)) {
+  if (!get_auth_params (parser, data, &error)) {
     g_task_return_error (task, error);
     return;
   }
@@ -445,15 +418,24 @@ on_auth_params_response_parsed (GObject *object,
            data->client->auth_params.cost);
 
   uri = soup_uri_new_with_base (data->client->base_uri, "api/auth/sign_in");
-  msg = soup_message_new_from_uri ("POST", uri);
-  password = hexlify (data->client->auth_params.keys.password, sizeof (data->client->auth_params.keys.password));
+  request = soup_session_request_http_uri (data->client->session, "POST", uri, &error);
+
+  if (request == NULL) {
+    g_task_return_error (task, error);
+    return;
+  }
+
+  password = hexlify (data->client->auth_params.keys.password,
+                      sizeof (data->client->auth_params.keys.password));
   body = g_strdup_printf ("{\"email\": \"%s\", \"password\": \"%s\"}", data->client->email, password);
+  msg = soup_request_http_get_message (request);
   soup_message_set_request (msg, "application/json", SOUP_MEMORY_TAKE, body, strlen (body));
-  soup_session_send_async (data->client->session, msg, NULL, on_send_signin_message, task);
+  soup_request_send_async (SOUP_REQUEST (request), g_task_get_cancellable (task), on_send_signin_message, task);
 
   g_free (password);
   soup_uri_free (uri);
   g_object_unref (msg);
+  g_object_unref (parser);
 }
 
 static void
@@ -463,20 +445,69 @@ on_send_auth_params_message (GObject *object,
 {
   GInputStream *stream;
   GTask *task;
-  ReadAuthParams *data;
+  JsonParser *parser;
   GError *error = NULL;
 
   task = user_data;
-  data = g_task_get_task_data (task);
-  stream = soup_session_send_finish (SOUP_SESSION (object), result, &error);
+  stream = soup_request_send_finish (SOUP_REQUEST (object), result, &error);
 
   if (stream == NULL) {
     g_task_return_error (task, error);
     return;
   }
 
-  data->parser = json_parser_new ();
-  json_parser_load_from_stream_async (data->parser, stream, data->cancellable, on_auth_params_response_parsed, task);
+  parser = json_parser_new ();
+  json_parser_load_from_stream_async (parser, stream, g_task_get_cancellable (task),          
+                                      on_auth_params_response_parsed, task);
+  g_object_unref (object);
+  g_object_unref (stream);
+}
+
+static void
+on_sync_response_parsed (GObject *object,
+                         GAsyncResult *result,
+                         gpointer user_data)
+{
+  GTask *task;
+  StandardFileAuthParams *auth_params;
+  JsonParser *parser;
+  JsonObject *root;
+  JsonArray *array;
+  GList *items = NULL;
+  GError *error = NULL;
+
+  task = G_TASK (user_data);
+  auth_params = g_task_get_task_data (task);
+  parser = JSON_PARSER (object);
+
+  if (!json_parser_load_from_stream_finish (parser, result, &error)) {
+    g_task_return_error (task, error);
+    return;
+  }
+
+  root = json_node_get_object (json_parser_get_root (parser));
+  array = json_object_get_array_member (root, "retrieved_items");
+
+  for (guint i = 0; i < json_array_get_length (array); i++) {
+    JsonObject *data;
+    GObject *item;
+    gchar *content;
+
+    data = json_array_get_object_element (array, i);
+    content = decrypt_item (data, auth_params);
+
+    if (content) {
+      item = deserialize_item (data, content);
+
+      if (item)
+        items = g_list_append (items, item);
+    }
+
+    g_free (content);
+  }
+
+  g_task_return_pointer (task, items, NULL);
+  g_object_unref (parser);
 }
 
 static void
@@ -486,6 +517,7 @@ on_send_sync_request (GObject *object,
 {
   GInputStream *stream;
   GTask *task;
+  JsonParser *parser;
   GError *error = NULL;
 
   task = user_data;
@@ -496,31 +528,44 @@ on_send_sync_request (GObject *object,
     return;
   }
 
-  g_task_return_boolean (task, TRUE);
+  parser = json_parser_new ();
+  json_parser_load_from_stream_async (parser, stream, g_task_get_cancellable (task),
+                                      on_sync_response_parsed, task);
+  g_object_unref (stream);
 }
 
-gboolean
+GList *
+iridium_standard_file_load_finish (IridiumStandardFile *client,
+                                   GAsyncResult *result,
+                                   GError **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, client), FALSE);
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+void
 iridium_standard_file_load_async (IridiumStandardFile *client,
                                   GCancellable *cancellable,
                                   GAsyncReadyCallback callback,
                                   gpointer user_data)
 {
   SoupURI *uri;
-  SoupMessage *msg;
   SoupRequestHTTP *request;
+  SoupMessage *msg;
   SoupMessageHeaders *headers;
   gchar *value;
+  const gchar *body;
   GTask *task;
   GError *error = NULL;
 
   task = g_task_new (client, cancellable, callback, user_data);
-
+  g_task_set_task_data (task, &client->auth_params, NULL);
   uri = soup_uri_new_with_base (client->base_uri, "api/items/sync");
   request = soup_session_request_http_uri (client->session, "POST", uri, &error);
 
   if (request == NULL) {
     g_task_return_error (task, error);
-    return FALSE;
+    return;
   }
 
   msg = soup_request_http_get_message (request);
@@ -528,20 +573,20 @@ iridium_standard_file_load_async (IridiumStandardFile *client,
   value = g_strdup_printf ("Bearer %s", client->token);
   soup_message_headers_append (headers, "Authorization", value);
 
+  body = "{\"items\": []}";
+  soup_message_set_request (msg, "application/json", SOUP_MEMORY_STATIC, body, strlen (body));
   soup_request_send_async (SOUP_REQUEST (request), cancellable, on_send_sync_request, task);
 
   g_object_unref (msg);
   soup_uri_free (uri);
-
-  return TRUE;
 }
 
 gboolean
-iridium_standard_file_connect_finish (GObject *object,
+iridium_standard_file_connect_finish (IridiumStandardFile *client,
                                       GAsyncResult *result,
                                       GError **error)
 {
-  g_return_val_if_fail (g_task_is_valid (result, object), FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, client), FALSE);
   return g_task_propagate_boolean (G_TASK (result), error);
 }
 
@@ -555,9 +600,10 @@ iridium_standard_file_connect_async (IridiumStandardFile *client,
                                      gpointer user_data)
 {
   SoupURI *uri;
-  SoupMessage *msg;
+  SoupRequestHTTP *request;
   ReadAuthParams *data;
   GTask *task;
+  GError *error = NULL;
 
   if (client->base_uri)
     soup_uri_free (client->base_uri);
@@ -567,18 +613,20 @@ iridium_standard_file_connect_async (IridiumStandardFile *client,
 
   uri = soup_uri_new_with_base (client->base_uri, "api/auth/params");
   soup_uri_set_query_from_fields (uri, "email", email, NULL);
-  msg = soup_message_new_from_uri ("GET", uri);
+  task = g_task_new (client, cancellable, callback, user_data);
+  request = soup_session_request_http_uri (client->session, "GET", uri, &error);
+
+  if (request == NULL) {
+    g_task_return_error (task, error);
+    return;
+  }
 
   data = g_new0 (ReadAuthParams, 1);
   data->client = client;
   data->password = g_strdup (password);
-  data->cancellable = cancellable;
 
-  task = g_task_new (client, cancellable, callback, user_data);
   g_task_set_task_data (task, data, (GDestroyNotify) read_auth_params_data_free);
-  soup_session_send_async (client->session, msg, NULL, on_send_auth_params_message, task);
-
-  g_object_unref (msg);
+  soup_request_send_async (SOUP_REQUEST (request), cancellable, on_send_auth_params_message, task);
   soup_uri_free (uri);
 }
 
@@ -617,8 +665,8 @@ iridium_standard_file_class_init (IridiumStandardFileClass *klass)
 static void
 iridium_standard_file_init (IridiumStandardFile *self)
 {
-  self->session = soup_session_new ();
   self->base_uri = NULL;
   self->email = NULL;
   self->token = NULL;
+  self->session = soup_session_new ();
 }

+ 5 - 4
src/iridium-standard-file.h

@@ -40,14 +40,15 @@ void                 iridium_standard_file_connect_async    (IridiumStandardFile
                                                              GCancellable           *cancellable,
                                                              GAsyncReadyCallback     callback,
                                                              gpointer                user_data);
-gboolean             iridium_standard_file_connect_finish   (GObject                *object,
+gboolean             iridium_standard_file_connect_finish   (IridiumStandardFile    *client,
                                                              GAsyncResult           *result,
                                                              GError                **error);
-gboolean             iridium_standard_file_load_async       (IridiumStandardFile    *client,
+void                 iridium_standard_file_load_async       (IridiumStandardFile    *client,
                                                              GCancellable           *cancellable,
                                                              GAsyncReadyCallback     callback,
                                                              gpointer                user_data);
-GList               *iridium_standard_file_load             (IridiumStandardFile    *client,
-                                                             const gchar            *filename);
+GList               *iridium_standard_file_load_finish      (IridiumStandardFile    *client,
+                                                             GAsyncResult           *result,
+                                                             GError                **error);
 
 G_END_DECLS

+ 33 - 7
src/iridium-window.c

@@ -163,20 +163,23 @@ note_date_cmp (IridiumNoteRow *row1,
 }
 
 static void
-on_standard_file_connected (GObject *object,
-                            GAsyncResult *result,
-                            gpointer user_data)
+on_load_finished (GObject *object,
+                  GAsyncResult *result,
+                  gpointer user_data)
 {
   IridiumWindow *self;
+  IridiumStandardFile *client;
   GList *notes;
-  IridiumTag *tag_all;
   GError *error = NULL;
 
   self = IRIDIUM_WINDOW (user_data);
-  tag_all = iridium_tag_new ("All");
+  client = IRIDIUM_STANDARD_FILE (object);
+  notes = iridium_standard_file_load_finish (client, result, &error);
+
+  if (notes) {
+    IridiumTag *tag_all;
 
-  if (iridium_standard_file_connect_finish (object, result, &error)) {
-    notes = iridium_standard_file_load (self->client, "sn.json");
+    tag_all = iridium_tag_new ("All");
 
     for (GList *it = g_list_first (notes); it != NULL; it = g_list_next (it)) {
       IridiumNote *note;
@@ -191,11 +194,34 @@ on_standard_file_connected (GObject *object,
 
     gtk_widget_show_all (GTK_WIDGET (self->note_list));
     gtk_widget_show_all (GTK_WIDGET (self->tag_list));
+  }
+  else if (error != NULL) {
+    /* TODO: show message */
+    g_printerr ("Error: %s\n", error->message);
+    g_error_free (error);
+  }
+}
+
+static void
+on_standard_file_connected (GObject *object,
+                            GAsyncResult *result,
+                            gpointer user_data)
+{
+  IridiumWindow *self;
+  IridiumStandardFile *client;
+  GError *error = NULL;
+
+  self = IRIDIUM_WINDOW (user_data);
+  client = IRIDIUM_STANDARD_FILE (object);
 
+  if (iridium_standard_file_connect_finish (client, result, &error)) {
+    iridium_standard_file_load_async (self->client, NULL, on_load_finished, self);
     gtk_revealer_set_reveal_child (self->notification_revealer, FALSE);
   }
   else {
     /* TODO: show message */
+    g_printerr ("Error: %s\n", error->message);
+    g_error_free (error);
   }
 }