[evolution-ews/wip/mcrha/office365: 14/50] Add code for easier testing and check whether can get folder listing
- From: Milan Crha <mcrha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-ews/wip/mcrha/office365: 14/50] Add code for easier testing and check whether can get folder listing
- Date: Mon, 3 Aug 2020 15:24:07 +0000 (UTC)
commit 77b7a64aea103c61426da1d0e191a263c37aeff6
Author: Milan Crha <mcrha redhat com>
Date: Tue Jun 9 17:45:59 2020 +0200
Add code for easier testing and check whether can get folder listing
src/Office365/common/e-o365-connection.c | 14 +-
src/Office365/common/e-o365-json-utils.c | 152 ++++++++++++++++++++-
src/Office365/common/e-o365-json-utils.h | 20 +++
src/Office365/common/e-o365-soup-logger.c | 2 +-
.../evolution/e-mail-config-o365-backend.c | 57 ++++++++
5 files changed, 232 insertions(+), 13 deletions(-)
---
diff --git a/src/Office365/common/e-o365-connection.c b/src/Office365/common/e-o365-connection.c
index 4d333125..adda8909 100644
--- a/src/Office365/common/e-o365-connection.c
+++ b/src/Office365/common/e-o365-connection.c
@@ -22,6 +22,7 @@
#include <json-glib/json-glib.h>
#include "camel-o365-settings.h"
+#include "e-o365-json-utils.h"
#include "e-o365-soup-logger.h"
#include "e-o365-connection.h"
@@ -1015,8 +1016,12 @@ o365_connection_send_request_sync (EO365Connection *cnc,
}
g_clear_object (&input_stream);
- } else if (!message->status_code) {
- soup_message_set_status (message, SOUP_STATUS_CANCELLED);
+ } else {
+ if (!message->status_code)
+ soup_message_set_status (message, SOUP_STATUS_CANCELLED);
+
+ g_set_error_literal (error, SOUP_HTTP_ERROR, message->status_code,
+ message->reason_phrase ? message->reason_phrase : soup_status_get_phrase
(message->status_code));
}
g_clear_object (&soup_session);
@@ -1172,7 +1177,6 @@ e_o365_list_folders_response_cb (EO365Connection *cnc,
GSList **out_folders = user_data;
guint ii, len;
- g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (node, JSON_TYPE_NODE), FALSE);
g_return_val_if_fail (out_folders != NULL, FALSE);
g_return_val_if_fail (out_next_link != NULL, FALSE);
g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), FALSE);
@@ -1180,9 +1184,9 @@ e_o365_list_folders_response_cb (EO365Connection *cnc,
object = json_node_get_object (node);
g_return_val_if_fail (object != NULL, FALSE);
- *out_next_link = g_strdup (json_object_get_string_member (object, "@odata.nextLink"));
+ *out_next_link = g_strdup (e_o365_json_get_string_member (object, "@odata.nextLink", NULL));
- value = json_object_get_array_member (object, "value");
+ value = e_o365_json_get_array_member (object, "value");
g_return_val_if_fail (value != NULL, FALSE);
len = json_array_get_length (value);
diff --git a/src/Office365/common/e-o365-json-utils.c b/src/Office365/common/e-o365-json-utils.c
index 9faede3b..0a02b15f 100644
--- a/src/Office365/common/e-o365-json-utils.c
+++ b/src/Office365/common/e-o365-json-utils.c
@@ -21,40 +21,178 @@
#include "e-o365-json-utils.h"
+JsonArray *
+e_o365_json_get_array_member (JsonObject *object,
+ const gchar *member_name)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, NULL);
+ g_return_val_if_fail (member_name != NULL, NULL);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return NULL;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_ARRAY (node), NULL);
+
+ return json_node_get_array (node);
+}
+
+gboolean
+e_o365_json_get_boolean_member (JsonObject *object,
+ const gchar *member_name,
+ gboolean default_value)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, default_value);
+ g_return_val_if_fail (member_name != NULL, default_value);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return default_value;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
+
+ return json_node_get_boolean (node);
+}
+
+gdouble
+e_o365_json_get_double_member (JsonObject *object,
+ const gchar *member_name,
+ gdouble default_value)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, default_value);
+ g_return_val_if_fail (member_name != NULL, default_value);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return default_value;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
+
+ return json_node_get_double (node);
+}
+
+gint64
+e_o365_json_get_int_member (JsonObject *object,
+ const gchar *member_name,
+ gint64 default_value)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, default_value);
+ g_return_val_if_fail (member_name != NULL, default_value);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return default_value;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
+
+ return json_node_get_int (node);
+}
+
+gboolean
+e_o365_json_get_null_member (JsonObject *object,
+ const gchar *member_name,
+ gboolean default_value)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, default_value);
+ g_return_val_if_fail (member_name != NULL, default_value);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return default_value;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_NULL (node), default_value);
+
+ return json_node_is_null (node);
+}
+
+JsonObject *
+e_o365_json_get_object_member (JsonObject *object,
+ const gchar *member_name)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, NULL);
+ g_return_val_if_fail (member_name != NULL, NULL);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return NULL;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_OBJECT (node), NULL);
+
+ return json_node_get_object (node);
+}
+
+const gchar *
+e_o365_json_get_string_member (JsonObject *object,
+ const gchar *member_name,
+ const gchar *default_value)
+{
+ JsonNode *node;
+
+ g_return_val_if_fail (object != NULL, default_value);
+ g_return_val_if_fail (member_name != NULL, default_value);
+
+ node = json_object_get_member (object, member_name);
+
+ if (!node)
+ return default_value;
+
+ g_return_val_if_fail (JSON_NODE_HOLDS_VALUE (node), default_value);
+
+ return json_node_get_string (node);
+}
+
/* https://docs.microsoft.com/en-us/graph/api/resources/mailfolder?view=graph-rest-1.0 */
-s
+
const gchar *
e_o365_mail_folder_get_display_name (JsonObject *object)
{
- return json_object_get_string_member (object, "displayName");
+ return e_o365_json_get_string_member (object, "displayName", NULL);
}
const gchar *
e_o365_mail_folder_get_id (JsonObject *object)
{
- return json_object_get_string_member (object, "id");
+ return e_o365_json_get_string_member (object, "id", NULL);
}
const gchar *
e_o365_mail_folder_get_parent_folder_id (JsonObject *object)
{
- return json_object_get_string_member (object, "parentFolderId");
+ return e_o365_json_get_string_member (object, "parentFolderId", NULL);
}
gint32
e_o365_mail_folder_get_child_folder_count (JsonObject *object)
{
- return (gint32) json_object_get_int_member (object, "childFolderCount");
+ return (gint32) e_o365_json_get_int_member (object, "childFolderCount", 0);
}
gint32
e_o365_mail_folder_get_total_item_count (JsonObject *object)
{
- return (gint32) json_object_get_int_member (object, "totalItemCount");
+ return (gint32) e_o365_json_get_int_member (object, "totalItemCount", 0);
}
gint32
e_o365_mail_folder_get_unread_item_count (JsonObject *object)
{
- return (gint32) json_object_get_int_member (object, "unreadItemCount");
+ return (gint32) e_o365_json_get_int_member (object, "unreadItemCount", 0);
}
diff --git a/src/Office365/common/e-o365-json-utils.h b/src/Office365/common/e-o365-json-utils.h
index dd8bd573..5cc2e886 100644
--- a/src/Office365/common/e-o365-json-utils.h
+++ b/src/Office365/common/e-o365-json-utils.h
@@ -22,6 +22,26 @@
G_BEGIN_DECLS
+JsonArray * e_o365_json_get_array_member (JsonObject *object,
+ const gchar *member_name);
+gboolean e_o365_json_get_boolean_member (JsonObject *object,
+ const gchar *member_name,
+ gboolean default_value);
+gdouble e_o365_json_get_double_member (JsonObject *object,
+ const gchar *member_name,
+ gdouble default_value);
+gint64 e_o365_json_get_int_member (JsonObject *object,
+ const gchar *member_name,
+ gint64 default_value);
+gboolean e_o365_json_get_null_member (JsonObject *object,
+ const gchar *member_name,
+ gboolean default_value);
+JsonObject * e_o365_json_get_object_member (JsonObject *object,
+ const gchar *member_name);
+const gchar * e_o365_json_get_string_member (JsonObject *object,
+ const gchar *member_name,
+ const gchar *default_value);
+
const gchar * e_o365_mail_folder_get_display_name (JsonObject *object);
const gchar * e_o365_mail_folder_get_id (JsonObject *object);
const gchar * e_o365_mail_folder_get_parent_folder_id (JsonObject *object);
diff --git a/src/Office365/common/e-o365-soup-logger.c b/src/Office365/common/e-o365-soup-logger.c
index 18d8a817..f0472cc6 100644
--- a/src/Office365/common/e-o365-soup-logger.c
+++ b/src/Office365/common/e-o365-soup-logger.c
@@ -109,7 +109,7 @@ static void
e_o365_soup_logger_print_data (EO365SoupLogger *logger)
{
if (logger->data) {
- g_print ("%s\n", logger->data->str);
+ g_print ("%s\n\n", logger->data->str);
g_string_free (logger->data, TRUE);
logger->data = NULL;
}
diff --git a/src/Office365/evolution/e-mail-config-o365-backend.c
b/src/Office365/evolution/e-mail-config-o365-backend.c
index d7bcc1a6..e677fd8c 100644
--- a/src/Office365/evolution/e-mail-config-o365-backend.c
+++ b/src/Office365/evolution/e-mail-config-o365-backend.c
@@ -26,6 +26,8 @@
#include <mail/e-mail-config-receiving-page.h>
#include "common/camel-o365-settings.h"
+#include "common/e-o365-connection.h"
+#include "common/e-o365-json-utils.h"
#include "e-mail-config-o365-backend.h"
@@ -81,6 +83,49 @@ mail_config_o365_backend_set_oauth2_tooltip (GtkWidget *widget,
g_free (when_value_filled);
}
+static void
+test_clicked_cb (GtkButton *button,
+ EMailConfigServiceBackend *backend)
+{
+ CamelSettings *settings;
+ ESource *source;
+ EO365Connection *cnc;
+ GSList *folders = NULL, *link;
+ GError *error = NULL;
+
+ settings = e_mail_config_service_backend_get_settings (backend);
+ source = e_mail_config_service_backend_get_collection (backend);
+
+ cnc = e_o365_connection_new (source, CAMEL_O365_SETTINGS (settings));
+ g_return_if_fail (cnc != NULL);
+
+ if (!e_o365_connection_list_folders_sync (cnc, NULL, NULL, NULL, &folders, NULL, &error)) {
+ printf ("%s: failed with error: %s\n", __FUNCTION__, error ? error->message : "none");
+ } else {
+ if (error) {
+ printf ("%s: succeeded, but has set error: '%s'\n", __FUNCTION__, error->message);
+ }
+
+ printf ("%s: returned %d objects:\n", __FUNCTION__, g_slist_length (folders));
+
+ for (link = folders; link; link = g_slist_next (link)) {
+ JsonObject *folder = link->data;
+
+ printf (" %p: '%s' childCount:%d total:%d unread:%d id:'%s' parent:'%s'\n", folder,
+ e_o365_mail_folder_get_display_name (folder),
+ e_o365_mail_folder_get_child_folder_count (folder),
+ e_o365_mail_folder_get_total_item_count (folder),
+ e_o365_mail_folder_get_unread_item_count (folder),
+ e_o365_mail_folder_get_id (folder),
+ e_o365_mail_folder_get_parent_folder_id (folder));
+ }
+ }
+
+ g_slist_free_full (folders, (GDestroyNotify) json_object_unref);
+ g_clear_error (&error);
+ g_clear_object (&cnc);
+}
+
static void
mail_config_o365_backend_insert_widgets (EMailConfigServiceBackend *backend,
GtkBox *parent)
@@ -89,6 +134,7 @@ mail_config_o365_backend_insert_widgets (EMailConfigServiceBackend *backend,
EMailConfigServicePage *page;
ESource *source;
ESourceExtension *extension;
+ ESourceAuthentication *auth_extension;
CamelSettings *settings;
GtkLabel *label;
GtkWidget *widget;
@@ -304,6 +350,17 @@ mail_config_o365_backend_insert_widgets (EMailConfigServiceBackend *backend,
extension, "identity",
G_BINDING_BIDIRECTIONAL |
G_BINDING_SYNC_CREATE);
+
+ auth_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
+ e_source_authentication_set_host (auth_extension, "graph.microsoft.com");
+ e_source_authentication_set_port (auth_extension, 442);
+ e_source_authentication_set_method (auth_extension, "Office365");
+
+ /* The following is for easier debugging only */
+ widget = gtk_button_new_with_mnemonic ("_Test");
+ g_signal_connect (widget, "clicked", G_CALLBACK (test_clicked_cb), o365_backend);
+ gtk_widget_show (widget);
+ gtk_box_pack_start (GTK_BOX (parent), widget, FALSE, FALSE, 0);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]