[gtk+/portal: 16/18] page setup: Add api to serialize to a GVariant
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/portal: 16/18] page setup: Add api to serialize to a GVariant
- Date: Wed, 6 Jul 2016 19:19:46 +0000 (UTC)
commit 4d1f4593d00b586227aed000e570a93b9c6dfb41
Author: Matthias Clasen <mclasen redhat com>
Date: Sat Jul 2 20:29:51 2016 -0400
page setup: Add api to serialize to a GVariant
This will be useful for portalized print support.
docs/reference/gtk/gtk3-sections.txt | 3 +
gtk/gtkpagesetup.c | 109 ++++++++++++++++++++++++++++++++++
gtk/gtkpagesetup.h | 5 ++
3 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 7075cad..b224d83 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -7371,10 +7371,13 @@ gtk_page_setup_get_page_height
<SUBSECTION Serialization>
gtk_page_setup_new_from_file
gtk_page_setup_new_from_key_file
+gtk_page_setup_new_from_gvariant
gtk_page_setup_load_file
gtk_page_setup_load_key_file
gtk_page_setup_to_file
gtk_page_setup_to_key_file
+gtk_page_setup_to_gvariant
+
<SUBSECTION Standard>
GTK_TYPE_PAGE_SETUP
GTK_PAGE_SETUP
diff --git a/gtk/gtkpagesetup.c b/gtk/gtkpagesetup.c
index aa95703..22885ed 100644
--- a/gtk/gtkpagesetup.c
+++ b/gtk/gtkpagesetup.c
@@ -837,3 +837,112 @@ gtk_page_setup_to_key_file (GtkPageSetup *setup,
"Orientation", orientation);
g_free (orientation);
}
+
+/**
+ * gtk_page_setup_to_gvariant:
+ * @setup: a #GtkPageSetup
+ *
+ * Serialize page setup to an a{sv} variant.
+ *
+ * Return: (transfer none): a new, floating, #GVariant
+ *
+ * Since: 3.22
+ */
+GVariant *
+gtk_page_setup_to_gvariant (GtkPageSetup *setup)
+{
+ GtkPaperSize *paper_size;
+ GVariantBuilder builder;
+ const char *name;
+ const char *ppd_name;
+ const char *display_name;
+ char *orientation;
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
+
+ paper_size = gtk_page_setup_get_paper_size (setup);
+ name = gtk_paper_size_get_name (paper_size);
+ ppd_name = gtk_paper_size_get_ppd_name (paper_size);
+ display_name = gtk_paper_size_get_display_name (paper_size);
+
+ if (ppd_name != NULL)
+ g_variant_builder_add (&builder, "{sv}", "PPDName", g_variant_new_string (ppd_name));
+ else
+ g_variant_builder_add (&builder, "{sv}", "Name", g_variant_new_string (name));
+
+ if (display_name != NULL)
+ g_variant_builder_add (&builder, "{sv}", "DisplayName", g_variant_new_string (display_name));
+
+ g_variant_builder_add (&builder, "{sv}", "Width", g_variant_new_double (gtk_paper_size_get_width
(paper_size, GTK_UNIT_MM)));
+ g_variant_builder_add (&builder, "{sv}", "Height", g_variant_new_double (gtk_paper_size_get_height
(paper_size, GTK_UNIT_MM)));
+
+ g_variant_builder_add (&builder, "{sv}", "MarginTop", g_variant_new_double (gtk_page_setup_get_top_margin
(setup, GTK_UNIT_MM)));
+ g_variant_builder_add (&builder, "{sv}", "MarginBottom", g_variant_new_double
(gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM)));
+ g_variant_builder_add (&builder, "{sv}", "MarginLeft", g_variant_new_double
(gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM)));
+ g_variant_builder_add (&builder, "{sv}", "MarginRight", g_variant_new_double
(gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM)));
+
+ orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
+ gtk_page_setup_get_orientation (setup));
+ g_variant_builder_add (&builder, "{sv}", "Orientation", g_variant_new_string (orientation));
+ g_free (orientation);
+
+ return g_variant_builder_end (&builder);
+}
+
+/**
+ * gtk_page_setup_new_from_gvariant:
+ * @variant: an a{sv} #GVariant
+ *
+ * Desrialize a page setup from an a{sv} variant in
+ * the format produced by gtk_page_setup_to_gvariant().
+ *
+ * Returns: (transfer full): a new #GtkPageSetup object
+ *
+ * Since: 3.22
+ */
+GtkPageSetup *
+gtk_page_setup_new_from_gvariant (GVariant *variant)
+{
+ GtkPageSetup *setup;
+ const char *orientation;
+ gdouble margin;
+ gdouble size;
+ GKeyFile *key_file;
+ const char *name;
+ GtkPaperSize *paper_size;
+
+ g_return_val_if_fail (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARDICT), NULL);
+
+ setup = gtk_page_setup_new ();
+
+ key_file = g_key_file_new ();
+ if (g_variant_lookup (variant, "PPDName", "&s", &name))
+ g_key_file_set_string (key_file, "Page Setup", "PPDName", name);
+ else if (g_variant_lookup (variant, "Name", "&s", &name))
+ g_key_file_set_string (key_file, "Page Setup", "Name", name);
+ if (g_variant_lookup (variant, "DisplayName", "&s", &name))
+ g_key_file_set_string (key_file, "Page Setup", "DisplayName", name);
+ if (g_variant_lookup (variant, "Width", "d", &size))
+ g_key_file_set_double (key_file, "Page Setup", "Width", size);
+ if (g_variant_lookup (variant, "Height", "d", &size))
+ g_key_file_set_double (key_file, "Page Setup", "Height", size);
+ paper_size = gtk_paper_size_new_from_key_file (key_file, "Page Setup", NULL);
+ gtk_page_setup_set_paper_size (setup, paper_size);
+ gtk_paper_size_free (paper_size);
+ g_key_file_unref (key_file);
+
+ if (g_variant_lookup (variant, "MarginTop", "d", &margin))
+ gtk_page_setup_set_top_margin (setup, margin, GTK_UNIT_MM);
+ if (g_variant_lookup (variant, "MarginBottom", "d", &margin))
+ gtk_page_setup_set_bottom_margin (setup, margin, GTK_UNIT_MM);
+ if (g_variant_lookup (variant, "MarginLeft", "d", &margin))
+ gtk_page_setup_set_left_margin (setup, margin, GTK_UNIT_MM);
+ if (g_variant_lookup (variant, "MarginRight", "d", &margin))
+ gtk_page_setup_set_right_margin (setup, margin, GTK_UNIT_MM);
+
+ if (g_variant_lookup (variant, "Orientation", "&s", &orientation))
+ gtk_page_setup_set_orientation (setup, string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
+ orientation));
+
+ return setup;
+}
diff --git a/gtk/gtkpagesetup.h b/gtk/gtkpagesetup.h
index 559ae43..f6474f5 100644
--- a/gtk/gtkpagesetup.h
+++ b/gtk/gtkpagesetup.h
@@ -127,6 +127,11 @@ void gtk_page_setup_to_key_file (GtkPageSetup *setup,
GKeyFile *key_file,
const gchar *group_name);
+GDK_AVAILABLE_IN_3_22
+GVariant *gtk_page_setup_to_gvariant (GtkPageSetup *setup);
+GDK_AVAILABLE_IN_3_22
+GtkPageSetup *gtk_page_setup_new_from_gvariant (GVariant *variant);
+
G_END_DECLS
#endif /* __GTK_PAGE_SETUP_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]