[gnome-initial-setup/wip/pwithnall/misc-fixes: 18/70] endless-eula: Add endless eula page




commit b45b95ef0eb69960a370057fdade467a55800975
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Thu Jun 12 19:03:19 2014 -0700

    endless-eula: Add endless eula page
    
    This page displays the Endless terms of service document, and allows
    the user to enable or disable metrics recording. The enabled/disabled
    state is only saved to the metrics daemon at the end of the FBE.
    
    The terms of service document is displayed in whichever language the
    user selected on the language page, if available. This is accomplished
    by reloading the document whenever the locale changes, using the value
    from the GisDriver; the value from the environment may not be correct
    since it's not safe to modify the environment from the locale page. The
    default locale is "C" if there is no document available in the selected
    locale.
    
    It's also possible to have different terms of service for different
    products, if they are added in eos-license-service. The default product
    is called "eos".
    
    (Various features squashed in, in the course of the 3.34 rebase.)
    
    (Rebase 3.38: Fix minor rebase conflicts. Update declaration of
    save_data().)
    
    Reference to related tickets:
        https://phabricator.endlessm.com/T2728
        https://phabricator.endlessm.com/T3594
        https://phabricator.endlessm.com/T3285
        https://phabricator.endlessm.com/T3601
        https://phabricator.endlessm.com/T4832
        https://phabricator.endlessm.com/T4791
        https://phabricator.endlessm.com/T5924
        https://phabricator.endlessm.com/T13890
        https://phabricator.endlessm.com/T13578
        https://phabricator.endlessm.com/T14065
        https://phabricator.endlessm.com/T14217
        https://phabricator.endlessm.com/T17967
        https://phabricator.endlessm.com/T18943#476863

 gnome-initial-setup/gis-driver.c                   |  54 ++++
 gnome-initial-setup/gis-driver.h                   |   2 +
 gnome-initial-setup/gnome-initial-setup.c          |   6 +
 gnome-initial-setup/meson.build                    |   3 +
 .../pages/endless-eula/endless-eula-page.css       |   7 +
 .../pages/endless-eula/endless-eula.gresource.xml  |   8 +
 .../pages/endless-eula/gis-endless-eula-page.c     | 180 +++++++++++
 .../pages/endless-eula/gis-endless-eula-page.h     |  57 ++++
 .../pages/endless-eula/gis-endless-eula-page.ui    | 136 ++++++++
 .../pages/endless-eula/gis-endless-eula-viewer.c   | 343 +++++++++++++++++++++
 .../pages/endless-eula/gis-endless-eula-viewer.h   |  38 +++
 gnome-initial-setup/pages/endless-eula/meson.build |  11 +
 gnome-initial-setup/pages/meson.build              |   1 +
 po/POTFILES.in                                     |   3 +
 14 files changed, 849 insertions(+)
---
diff --git a/gnome-initial-setup/gis-driver.c b/gnome-initial-setup/gis-driver.c
index 4f160bd8..2f546337 100644
--- a/gnome-initial-setup/gis-driver.c
+++ b/gnome-initial-setup/gis-driver.c
@@ -30,6 +30,7 @@
 
 #include "cc-common-language.h"
 #include "gis-assistant.h"
+#include "gis-page-util.h"
 
 #define GIS_TYPE_DRIVER_MODE (gis_driver_mode_get_type ())
 
@@ -102,6 +103,8 @@ struct _GisDriverPrivate {
 
   const gchar *vendor_conf_file_path;
   GKeyFile *vendor_conf_file;
+
+  gchar *product_name;
 };
 typedef struct _GisDriverPrivate GisDriverPrivate;
 
@@ -154,6 +157,52 @@ running_live_session (void)
   return TRUE;
 }
 
+#define EOS_IMAGE_VERSION_PATH "/sysroot"
+#define EOS_IMAGE_VERSION_ALT_PATH "/"
+
+static char *
+get_image_version (void)
+{
+  g_autoptr(GError) error_sysroot = NULL;
+  g_autoptr(GError) error_root = NULL;
+  char *image_version =
+    gis_page_util_get_image_version (EOS_IMAGE_VERSION_PATH, &error_sysroot);
+
+  if (image_version == NULL)
+    image_version =
+      gis_page_util_get_image_version (EOS_IMAGE_VERSION_ALT_PATH, &error_root);
+
+  if (image_version == NULL)
+    {
+      g_warning ("%s", error_sysroot->message);
+      g_warning ("%s", error_root->message);
+    }
+
+  return image_version;
+}
+
+static gchar *
+get_product_from_image_version (const gchar *image_version)
+{
+  gchar *hyphen_index = NULL;
+
+  if (image_version == NULL)
+    return NULL;
+
+  hyphen_index = index (image_version, '-');
+  if (hyphen_index == NULL)
+    return NULL;
+
+  return g_strndup (image_version, hyphen_index - image_version);
+}
+
+const gchar *
+gis_driver_get_product_name (GisDriver *driver)
+{
+  GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
+  return priv->product_name;
+}
+
 static void
 gis_driver_dispose (GObject *object)
 {
@@ -173,6 +222,7 @@ gis_driver_finalize (GObject *object)
   GisDriver *driver = GIS_DRIVER (object);
   GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
 
+  g_free (priv->product_name);
   g_free (priv->lang_id);
   g_free (priv->username);
   g_free (priv->full_name);
@@ -953,6 +1003,7 @@ gis_driver_startup (GApplication *app)
   GisDriver *driver = GIS_DRIVER (app);
   GisDriverPrivate *priv = gis_driver_get_instance_private (driver);
   WebKitWebContext *context = webkit_web_context_get_default ();
+  g_autofree char *image_version = NULL;
 
   G_APPLICATION_CLASS (gis_driver_parent_class)->startup (app);
 
@@ -978,6 +1029,9 @@ gis_driver_startup (GApplication *app)
 
   gtk_widget_show (GTK_WIDGET (priv->assistant));
 
+  image_version = get_image_version ();
+  priv->product_name = get_product_from_image_version (image_version);
+
   priv->is_live_session = running_live_session ();
   g_object_notify_by_pspec (G_OBJECT (driver), obj_props[PROP_LIVE_SESSION]);
 
diff --git a/gnome-initial-setup/gis-driver.h b/gnome-initial-setup/gis-driver.h
index 801d6f60..56227afe 100644
--- a/gnome-initial-setup/gis-driver.h
+++ b/gnome-initial-setup/gis-driver.h
@@ -121,6 +121,8 @@ gboolean gis_driver_is_live_session (GisDriver *driver);
 
 gboolean gis_driver_is_small_screen (GisDriver *driver);
 
+const gchar *gis_driver_get_product_name (GisDriver *driver);
+
 void gis_driver_add_page (GisDriver *driver,
                           GisPage   *page);
 
diff --git a/gnome-initial-setup/gnome-initial-setup.c b/gnome-initial-setup/gnome-initial-setup.c
index 9ea1744c..e238f0a4 100644
--- a/gnome-initial-setup/gnome-initial-setup.c
+++ b/gnome-initial-setup/gnome-initial-setup.c
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <glib/gi18n.h>
+#include <evince-document.h>
 
 #ifdef HAVE_CHEESE
 #include <cheese-gtk.h>
@@ -35,6 +36,7 @@
 #include "pages/welcome/gis-welcome-page.h"
 #include "pages/language/gis-language-page.h"
 #include "pages/keyboard/gis-keyboard-page.h"
+#include "pages/endless-eula/gis-endless-eula-page.h"
 #include "pages/network/gis-network-page.h"
 #include "pages/timezone/gis-timezone-page.h"
 #include "pages/privacy/gis-privacy-page.h"
@@ -68,6 +70,7 @@ static PageData page_table[] = {
   PAGE (welcome, FALSE),
   PAGE (language, FALSE),
   PAGE (keyboard, FALSE),
+  PAGE (endless_eula, TRUE),
   PAGE (network,  FALSE),
   PAGE (privacy,  FALSE),
   PAGE (timezone, TRUE),
@@ -312,6 +315,7 @@ main (int argc, char *argv[])
 #endif
 
   gtk_init (&argc, &argv);
+  ev_init ();
 
   g_message ("Starting gnome-initial-setup");
   if (gis_get_mock_mode ())
@@ -345,6 +349,8 @@ main (int argc, char *argv[])
 
   g_object_unref (driver);
   g_option_context_free (context);
+  ev_shutdown ();
+
   return status;
 }
 
diff --git a/gnome-initial-setup/meson.build b/gnome-initial-setup/meson.build
index 61aa9765..427d61ff 100644
--- a/gnome-initial-setup/meson.build
+++ b/gnome-initial-setup/meson.build
@@ -36,6 +36,9 @@ dependencies = [
     dependency ('accountsservice'),
     dependency ('gnome-desktop-3.0', version: '>= 3.7.5'),
     dependency ('gsettings-desktop-schemas', version: '>= 3.37.1'),
+    dependency ('eosmetrics-0', version: '>= 0.5.0'),
+    dependency ('evince-document-3.0'),
+    dependency ('evince-view-3.0'),
     dependency ('fontconfig'),
     dependency ('gweather-3.0'),
     dependency ('goa-1.0'),
diff --git a/gnome-initial-setup/pages/endless-eula/endless-eula-page.css 
b/gnome-initial-setup/pages/endless-eula/endless-eula-page.css
new file mode 100644
index 00000000..deef1afd
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/endless-eula-page.css
@@ -0,0 +1,7 @@
+EvView.document-page {
+    border-width: 0;
+    padding: 0;
+    border-image: none;
+    background-color: white;
+    background-image: none;
+}
diff --git a/gnome-initial-setup/pages/endless-eula/endless-eula.gresource.xml 
b/gnome-initial-setup/pages/endless-eula/endless-eula.gresource.xml
new file mode 100644
index 00000000..1705d902
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/endless-eula.gresource.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/initial-setup">
+    <file alias="endless-eula-page.css">endless-eula-page.css</file>
+    <file preprocess="xml-stripblanks" alias="gis-endless-eula-page.ui">gis-endless-eula-page.ui</file>
+  </gresource>
+</gresources>
+
diff --git a/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.c 
b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.c
new file mode 100644
index 00000000..7ac6c745
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.c
@@ -0,0 +1,180 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2014 Endless Mobile, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Cosimo Cecchi <cosimo endlessm com>
+ */
+
+/* Endless EULA page {{{1 */
+
+#define PAGE_ID "endless-eula"
+
+#include "config.h"
+#include "gis-endless-eula-page.h"
+#include "gis-endless-eula-viewer.h"
+
+#include "endless-eula-resources.h"
+
+#include <eosmetrics/eosmetrics.h>
+#include <evince-view.h>
+#include <evince-document.h>
+#include <gio/gio.h>
+#include <glib/gi18n.h>
+
+typedef struct {
+  GtkWidget *eula_viewer_container;
+  GtkWidget *metrics_separator;
+  GtkWidget *metrics_label;
+  GtkWidget *metrics_checkbutton;
+  GtkWidget *metrics_privacy_label;
+
+  GisEndlessEulaViewer *eula_viewer;
+  GDBusProxy *metrics_proxy;
+} GisEndlessEulaPagePrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GisEndlessEulaPage, gis_endless_eula_page, GIS_TYPE_PAGE);
+
+static void
+sync_metrics_active_state (GisEndlessEulaPage *page)
+{
+  GisEndlessEulaPagePrivate *priv = gis_endless_eula_page_get_instance_private (page);
+  GError *error = NULL;
+  GtkWidget *widget;
+  gboolean metrics_active;
+
+  widget = priv->metrics_checkbutton;
+  metrics_active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
+
+  if (!priv->metrics_proxy)
+    return;
+
+  g_dbus_proxy_call_sync (priv->metrics_proxy,
+                          "SetEnabled",
+                          g_variant_new ("(b)", metrics_active),
+                          G_DBUS_CALL_FLAGS_NONE, -1,
+                          NULL, &error);
+
+  if (error != NULL)
+    {
+      g_critical ("Unable to set the enabled state of metrics daemon: %s", error->message);
+      g_error_free (error);
+    }
+}
+
+static void
+gis_endless_eula_page_finalize (GObject *object)
+{
+  GisEndlessEulaPage *page = GIS_ENDLESS_EULA_PAGE (object);
+  GisEndlessEulaPagePrivate *priv = gis_endless_eula_page_get_instance_private (page);
+
+  g_clear_object (&priv->metrics_proxy);
+
+  G_OBJECT_CLASS (gis_endless_eula_page_parent_class)->finalize (object);
+}
+
+static void
+gis_endless_eula_page_constructed (GObject *object)
+{
+  GisEndlessEulaPage *page = GIS_ENDLESS_EULA_PAGE (object);
+  GisEndlessEulaPagePrivate *priv = gis_endless_eula_page_get_instance_private (page);
+  GError *error = NULL;
+
+  G_OBJECT_CLASS (gis_endless_eula_page_parent_class)->constructed (object);
+
+  priv->metrics_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                       G_DBUS_PROXY_FLAGS_NONE,
+                                                       NULL,
+                                                       "com.endlessm.Metrics",
+                                                       "/com/endlessm/Metrics",
+                                                       "com.endlessm.Metrics.EventRecorderServer",
+                                                       NULL, &error);
+
+  if (error != NULL)
+    {
+      g_critical ("Unable to create a DBus proxy for the metrics daemon: %s", error->message);
+      g_error_free (error);
+    }
+
+  gtk_widget_show (GTK_WIDGET (page));
+
+  gis_page_set_needs_accept (GIS_PAGE (page), TRUE);
+  gis_page_set_complete (GIS_PAGE (page), TRUE);
+
+  gtk_container_add (GTK_CONTAINER (priv->eula_viewer_container), GTK_WIDGET (priv->eula_viewer));
+}
+
+static void
+gis_endless_eula_page_locale_changed (GisPage *page)
+{
+  GisEndlessEulaPage *self = GIS_ENDLESS_EULA_PAGE (page);
+  GisEndlessEulaPagePrivate *priv = gis_endless_eula_page_get_instance_private (self);
+
+  gis_page_set_title (page, _("Terms of Use"));
+  gis_endless_eula_viewer_reload (priv->eula_viewer);
+}
+
+static gboolean
+gis_endless_eula_page_save_data (GisPage  *page,
+                                 GError  **error)
+{
+  sync_metrics_active_state (GIS_ENDLESS_EULA_PAGE (page));
+
+  return TRUE;
+}
+
+static void
+gis_endless_eula_page_class_init (GisEndlessEulaPageClass *klass)
+{
+  GisPageClass *page_class = GIS_PAGE_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), 
"/org/gnome/initial-setup/gis-endless-eula-page.ui");
+
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisEndlessEulaPage, 
eula_viewer_container);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisEndlessEulaPage, 
metrics_separator);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisEndlessEulaPage, metrics_label);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisEndlessEulaPage, 
metrics_checkbutton);
+  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (klass), GisEndlessEulaPage, 
metrics_privacy_label);
+
+  page_class->page_id = PAGE_ID;
+  page_class->locale_changed = gis_endless_eula_page_locale_changed;
+  page_class->save_data = gis_endless_eula_page_save_data;
+  object_class->constructed = gis_endless_eula_page_constructed;
+  object_class->finalize = gis_endless_eula_page_finalize;
+}
+
+static void
+gis_endless_eula_page_init (GisEndlessEulaPage *page)
+{
+  GisEndlessEulaPagePrivate *priv = gis_endless_eula_page_get_instance_private (page);
+
+  g_resources_register (endless_eula_get_resource ());
+
+  gtk_widget_init_template (GTK_WIDGET (page));
+
+  priv->eula_viewer = g_object_new (GIS_TYPE_ENDLESS_EULA_VIEWER, NULL);
+}
+
+GisPage *
+gis_prepare_endless_eula_page (GisDriver *driver)
+{
+  return g_object_new (GIS_TYPE_ENDLESS_EULA_PAGE,
+                       "driver", driver,
+                       NULL);
+}
diff --git a/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.h 
b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.h
new file mode 100644
index 00000000..05593fde
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.h
@@ -0,0 +1,57 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2014 Endless Mobile, Inc
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Cosimo Cecchi <cosimo endlessm com>
+ */
+
+#ifndef __GIS_ENDLESS_EULA_PAGE_H__
+#define __GIS_ENDLESS_EULA_PAGE_H__
+
+#include "gnome-initial-setup.h"
+
+G_BEGIN_DECLS
+
+#define GIS_TYPE_ENDLESS_EULA_PAGE               (gis_endless_eula_page_get_type ())
+#define GIS_ENDLESS_EULA_PAGE(obj)               (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GIS_TYPE_ENDLESS_EULA_PAGE, GisEndlessEulaPage))
+#define GIS_ENDLESS_EULA_PAGE_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST ((klass),  
GIS_TYPE_ENDLESS_EULA_PAGE, GisEndlessEulaPageClass))
+#define GIS_IS_ENDLESS_EULA_PAGE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GIS_TYPE_ENDLESS_EULA_PAGE))
+#define GIS_IS_ENDLESS_EULA_PAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  
GIS_TYPE_ENDLESS_EULA_PAGE))
+#define GIS_ENDLESS_EULA_PAGE_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj),  
GIS_TYPE_ENDLESS_EULA_PAGE, GisEndlessEulaPageClass))
+
+typedef struct _GisEndlessEulaPage        GisEndlessEulaPage;
+typedef struct _GisEndlessEulaPageClass   GisEndlessEulaPageClass;
+
+struct _GisEndlessEulaPage
+{
+  GisPage parent;
+};
+
+struct _GisEndlessEulaPageClass
+{
+  GisPageClass parent_class;
+};
+
+GType gis_endless_eula_page_get_type (void);
+
+GisPage *gis_prepare_endless_eula_page (GisDriver *driver);
+
+G_END_DECLS
+
+#endif /* __GIS_ENDLESS_EULA_PAGE_H__ */
diff --git a/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.ui 
b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.ui
new file mode 100644
index 00000000..464fc7dc
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.ui
@@ -0,0 +1,136 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <template class="GisEndlessEulaPage" parent="GisPage">
+    <child>
+      <object class="GtkGrid" id="endless_eula_page">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="halign">center</property>
+        <property name="margin_start">24</property>
+        <property name="margin_end">24</property>
+        <property name="vexpand">True</property>
+        <property name="orientation">vertical</property>
+        <property name="row_spacing">12</property>
+        <child>
+          <object class="GtkLabel" id="terms_label">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="halign">start</property>
+            <property name="label" translatable="yes">Please read the following terms of use 
carefully</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+              <attribute name="scale" value="1.2"/>
+            </attributes>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkLabel" id="terms_text">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes">By clicking "Accept," you acknowledge that you have 
read, understood, and agree to be bound by the following terms and conditions.</property>
+            <property name="wrap">True</property>
+            <property name="hexpand">False</property>
+            <property name="halign">start</property>
+            <property name="valign">start</property>
+            <property name="xalign">0</property>
+            <property name="yalign">0</property>
+            <property name="wrap">True</property>
+            <property name="max_width_chars">70</property>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">1</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkAlignment" id="eula_viewer_container">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hexpand">True</property>
+            <property name="vexpand">True</property>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">2</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkSeparator" id="metrics_separator">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">3</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkLabel" id="metrics_label">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="halign">start</property>
+            <property name="label" translatable="yes">Help make Endless better for everyone</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+              <attribute name="scale" value="1.2"/>
+            </attributes>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">4</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkCheckButton" id="metrics_checkbutton">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="valign">start</property>
+            <property name="use_stock">True</property>
+            <property name="xalign">0</property>
+            <property name="yalign">0</property>
+            <property name="active">True</property>
+            <property name="draw_indicator">True</property>
+            <child>
+              <object class="GtkLabel" id="metrics_privacy_label">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="valign">start</property>
+                <property name="yalign">0</property>
+                <property name="margin_start">6</property>
+                <property name="label" translatable="yes">Automatically save and send usage statistics and 
problem reports to Endless. All data is anonymous.</property>
+                <property name="use_markup">True</property>
+                <property name="wrap">True</property>
+                <property name="max_width_chars">70</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">5</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.c 
b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.c
new file mode 100644
index 00000000..7d2888d9
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.c
@@ -0,0 +1,343 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2018 Endless Mobile, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Damián Nohales <damian endlessm com>
+ */
+
+#include "config.h"
+#include "gis-endless-eula-viewer.h"
+
+#include <evince-document.h>
+#include <evince-view.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+#include <webkit2/webkit2.h>
+
+struct _GisEndlessEulaViewer {
+  GtkScrolledWindow parent;
+};
+
+G_DEFINE_TYPE (GisEndlessEulaViewer, gis_endless_eula_viewer, GTK_TYPE_SCROLLED_WINDOW)
+
+#define LICENSE_SERVICE_URI "http://localhost:3010";
+
+static void
+present_view_in_modal (GisEndlessEulaViewer *viewer,
+                       GtkWidget *view,
+                       const gchar *title,
+                       gboolean needs_scrolled_window)
+{
+  GtkWidget *widget, *modal, *content_area, *toplevel;
+
+  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (viewer));
+  modal = gtk_dialog_new_with_buttons (title, GTK_WINDOW (toplevel),
+                                       GTK_DIALOG_MODAL |
+                                       GTK_DIALOG_USE_HEADER_BAR,
+                                       NULL,
+                                       NULL);
+  gtk_window_set_default_size (GTK_WINDOW (modal), 600, 500);
+
+  if (needs_scrolled_window)
+    {
+      widget = gtk_scrolled_window_new (NULL, NULL);
+      gtk_container_add (GTK_CONTAINER (widget), view);
+    }
+  else
+    {
+      widget = view;
+    }
+
+  content_area = gtk_dialog_get_content_area (GTK_DIALOG (modal));
+  gtk_container_add (GTK_CONTAINER (content_area), widget);
+  gtk_widget_show_all (modal);
+
+  g_signal_connect (modal, "response",
+                    G_CALLBACK (gtk_widget_destroy), NULL);
+}
+
+static char *
+find_terms_document_for_language (const gchar *product_name,
+                                  const gchar *language)
+{
+  const gchar * const * data_dirs;
+  gchar *path = NULL;
+  gint i;
+
+  data_dirs = g_get_system_data_dirs ();
+
+  for (i = 0; data_dirs[i] != NULL; i++)
+    {
+      path = g_build_filename (data_dirs[i],
+                               "eos-license-service",
+                               "terms",
+                               product_name,
+                               language,
+                               "Terms-of-Use.pdf",
+                               NULL);
+
+      if (g_file_test (path, G_FILE_TEST_EXISTS))
+        return path;
+
+      g_free (path);
+    }
+
+  return NULL;
+}
+
+static char *
+find_terms_document_for_languages (const gchar *product_name,
+                                   const gchar * const *languages)
+{
+  int i;
+  gchar *path = NULL;
+
+  if (product_name == NULL)
+    return NULL;
+
+  for (i = 0; languages[i] != NULL; i++)
+    {
+      path = find_terms_document_for_language (product_name, languages[i]);
+
+      if (path != NULL)
+        return path;
+    }
+
+  /* Use "C" as a fallback. */
+  return find_terms_document_for_language (product_name, "C");
+}
+
+static GFile *
+get_terms_document (void)
+{
+  GisDriver *driver;
+  gchar **locale_variants;
+  const gchar *product_name;
+  const gchar *language;
+  gchar *path = NULL;
+  GFile *file;
+
+  driver = GIS_DRIVER (g_application_get_default ());
+  language = gis_driver_get_user_language (driver);
+  locale_variants = g_get_locale_variants (language);
+  product_name = gis_driver_get_product_name (driver);
+
+  path = find_terms_document_for_languages (product_name, (const gchar * const *)locale_variants);
+  if (path == NULL)
+    path = find_terms_document_for_languages ("eos", (const gchar * const *)locale_variants);
+
+  g_strfreev (locale_variants);
+
+  if (path == NULL)
+    {
+      g_critical ("Unable to find terms and conditions PDF on the system");
+      return NULL;
+    }
+
+  file = g_file_new_for_path (path);
+  g_free (path);
+
+  return file;
+}
+
+static GtkWidget *
+load_license_service_view (void)
+{
+  GtkWidget *view;
+
+  view = webkit_web_view_new ();
+  webkit_web_view_load_uri (WEBKIT_WEB_VIEW (view), LICENSE_SERVICE_URI);
+  gtk_widget_set_hexpand (view, TRUE);
+  gtk_widget_set_vexpand (view, TRUE);
+
+  return view;
+}
+
+static EvDocument *
+load_evince_document_for_file (GFile *file)
+{
+  EvDocument *document;
+  GError *error = NULL;
+  gchar *path;
+
+  document = ev_document_factory_get_document_for_gfile (file,
+                                                         EV_DOCUMENT_LOAD_FLAG_NONE,
+                                                         NULL,
+                                                         &error);
+  if (error != NULL)
+    {
+      path = g_file_get_path (file);
+      g_critical ("Unable to load EvDocument for file %s: %s", path, error->message);
+      g_error_free (error);
+      g_free (path);
+
+      return NULL;
+    }
+
+  return document;
+}
+
+static GtkWidget *
+load_evince_view_for_file (GFile *file)
+{
+  EvDocument *document;
+  EvDocumentModel *model;
+  GtkWidget *view;
+
+  document = load_evince_document_for_file (file);
+  g_return_val_if_fail (document != NULL, NULL);
+
+  model = ev_document_model_new_with_document (document);
+  view = ev_view_new ();
+  ev_view_set_model (EV_VIEW (view), model);
+
+  g_object_unref (document);
+  g_object_unref (model);
+
+  gtk_widget_set_hexpand (view, TRUE);
+  gtk_widget_set_vexpand (view, TRUE);
+
+  return view;
+}
+
+static void
+evince_view_external_link_cb (EvView *ev_view,
+                              EvLinkAction *action,
+                              GisEndlessEulaViewer *viewer)
+{
+  const gchar *uri;
+  GFile *file;
+  GtkWidget *view;
+
+  uri = ev_link_action_get_uri (action);
+  file = g_file_new_for_uri (uri);
+
+  if (g_file_has_uri_scheme (file, "file"))
+    {
+      view = load_evince_view_for_file (file);
+      if (view != NULL)
+        present_view_in_modal (viewer, view, _("Terms of Use"), TRUE);
+    }
+  else if (g_str_has_prefix (uri, LICENSE_SERVICE_URI))
+    {
+      view = load_license_service_view ();
+      present_view_in_modal (viewer, view, _("Third Party Software"), FALSE);
+    }
+
+  g_object_unref (file);
+}
+
+static void
+load_terms_view (GisEndlessEulaViewer *viewer)
+{
+  GtkWidget *evince_view;
+  GFile *file;
+
+  file = get_terms_document ();
+  if (file == NULL)
+    return;
+
+  evince_view = load_evince_view_for_file (file);
+  g_object_unref (file);
+
+  if (evince_view == NULL)
+    return;
+
+  gtk_container_add (GTK_CONTAINER (viewer), evince_view);
+
+  g_signal_connect (evince_view, "external-link",
+                    G_CALLBACK (evince_view_external_link_cb), viewer);
+}
+
+void
+gis_endless_eula_viewer_reload (GisEndlessEulaViewer *viewer)
+{
+  GtkWidget *evince_view;
+  EvDocument *document;
+  EvDocumentModel *model;
+  GFile *file;
+
+  evince_view = gtk_bin_get_child (GTK_BIN (viewer));
+
+  if (evince_view == NULL)
+    return;
+
+  file = get_terms_document ();
+  if (file == NULL)
+    return;
+
+  document = load_evince_document_for_file (file);
+  g_return_if_fail (document != NULL);
+
+  model = ev_document_model_new_with_document (document);
+  ev_view_set_model (EV_VIEW (evince_view), model);
+
+  g_object_unref (document);
+  g_object_unref (model);
+}
+
+static void
+load_css_overrides (GisEndlessEulaViewer *viewer)
+{
+  GtkCssProvider *provider;
+  GError *error = NULL;
+
+  provider = gtk_css_provider_new ();
+  gtk_css_provider_load_from_resource (provider, "/org/gnome/initial-setup/endless-eula-page.css");
+
+  if (error != NULL)
+    {
+      g_warning ("Unable to load CSS overrides for the endless-eula page: %s",
+                 error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (GTK_WIDGET (viewer)),
+                                                 GTK_STYLE_PROVIDER (provider),
+                                                 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
+  g_object_unref (provider);
+}
+
+static void
+gis_endless_eula_viewer_constructed (GObject *object)
+{
+  GisEndlessEulaViewer *viewer = GIS_ENDLESS_EULA_VIEWER (object);
+
+  G_OBJECT_CLASS (gis_endless_eula_viewer_parent_class)->constructed (object);
+
+  load_css_overrides (viewer);
+  load_terms_view (viewer);
+  gtk_widget_show_all (GTK_WIDGET (viewer));
+}
+
+static void
+gis_endless_eula_viewer_class_init (GisEndlessEulaViewerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  object_class->constructed = gis_endless_eula_viewer_constructed;
+}
+
+static void
+gis_endless_eula_viewer_init (GisEndlessEulaViewer *viewer)
+{
+  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (viewer), GTK_SHADOW_IN);
+}
diff --git a/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.h 
b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.h
new file mode 100644
index 00000000..f936a6b5
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.h
@@ -0,0 +1,38 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2018 Endless Mobile, Inc
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Damián Nohales <damian endlessm com>
+ */
+
+#ifndef __GIS_ENDLESS_EULA_VIEWER_H__
+#define __GIS_ENDLESS_EULA_VIEWER_H__
+
+#include "gnome-initial-setup.h"
+
+G_BEGIN_DECLS
+
+#define GIS_TYPE_ENDLESS_EULA_VIEWER (gis_endless_eula_viewer_get_type ())
+G_DECLARE_FINAL_TYPE (GisEndlessEulaViewer, gis_endless_eula_viewer, GIS, ENDLESS_EULA_VIEWER, 
GtkScrolledWindow)
+
+void gis_endless_eula_viewer_reload (GisEndlessEulaViewer *viewer);
+
+G_END_DECLS
+
+#endif /* __GIS_ENDLESS_EULA_VIEWER_H__ */
diff --git a/gnome-initial-setup/pages/endless-eula/meson.build 
b/gnome-initial-setup/pages/endless-eula/meson.build
new file mode 100644
index 00000000..c47d6399
--- /dev/null
+++ b/gnome-initial-setup/pages/endless-eula/meson.build
@@ -0,0 +1,11 @@
+sources += gnome.compile_resources(
+    'endless-eula-resources',
+    files('endless-eula.gresource.xml'),
+    c_name: 'endless_eula'
+)
+
+sources += files(
+    'gis-endless-eula-page.h',
+    'gis-endless-eula-page.c',
+    'gis-endless-eula-viewer.c',
+)
diff --git a/gnome-initial-setup/pages/meson.build b/gnome-initial-setup/pages/meson.build
index 45f77d72..be3f4b46 100644
--- a/gnome-initial-setup/pages/meson.build
+++ b/gnome-initial-setup/pages/meson.build
@@ -2,6 +2,7 @@ pages = [
    'account',
    'language',
    'keyboard',
+   'endless-eula',
    'network',
    'timezone',
    'privacy',
diff --git a/po/POTFILES.in b/po/POTFILES.in
index cda56736..07f4617e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -13,6 +13,9 @@ gnome-initial-setup/pages/account/gis-account-page.ui
 gnome-initial-setup/pages/account/um-photo-dialog.c
 gnome-initial-setup/pages/account/um-realm-manager.c
 gnome-initial-setup/pages/account/um-utils.c
+gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.c
+gnome-initial-setup/pages/endless-eula/gis-endless-eula-page.ui
+gnome-initial-setup/pages/endless-eula/gis-endless-eula-viewer.c
 gnome-initial-setup/pages/goa/gis-goa-page.c
 gnome-initial-setup/pages/goa/gis-goa-page.ui
 gnome-initial-setup/pages/keyboard/cc-input-chooser.c


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