[gnome-boxes/wip/rishi/rhel: 13/13] foo



commit b9687c633b5f2c37a0cfce0f0a08c0ae790a2a66
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Aug 24 18:19:50 2017 +0200

    foo
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786679

 data/ui/wizard-source.ui |   64 ++++++++++++++++
 src/wizard-source.vala   |  181 +++++++++++++++++++++++++++++++++++++++++++++-
 src/wizard.vala          |   16 ++++-
 3 files changed, 258 insertions(+), 3 deletions(-)
---
diff --git a/data/ui/wizard-source.ui b/data/ui/wizard-source.ui
index 0bd4e58..9a46c29 100644
--- a/data/ui/wizard-source.ui
+++ b/data/ui/wizard-source.ui
@@ -117,6 +117,53 @@
         </child>
 
         <child>
+          <object class="GtkButton" id="install_rhel_button">
+            <property name="visible">True</property>
+            <signal name="clicked" handler="on_install_rhel_button_clicked"/>
+            <style>
+              <class name="boxes-menu-row"/>
+            </style>
+            <child>
+              <object class="GtkBox" id="install_rhel_box">
+                <property name="visible">True</property>
+                <property name="margin-top">10</property>
+                <property name="margin-bottom">10</property>
+                <property name="margin-start">20</property>
+                <property name="margin-end">20</property>
+                <property name="orientation">vertical</property>
+                <child>
+                  <object class="GtkLabel" id="install_rhel_label">
+                    <property name="visible">True</property>
+                    <property name="halign">start</property>
+                    <property name="use-underline">True</property>
+                    <property name="label" translatable="yes">Red Hat Enterprise Linux</property>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="install_rhel_details_label">
+                    <property name="visible">True</property>
+                    <property name="halign">start</property>
+                    <property name="label" translatable="yes">Available with a free Red Hat developer 
account</property>
+                    <style>
+                      <class name="boxes-step-label"/>
+                      <class name="dim-label"/>
+                    </style>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                  </packing>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+
+        <child>
           <object class="GtkButton" id="enter_url_button">
             <property name="visible">True</property>
             <signal name="clicked" handler="on_enter_url_button_clicked"/>
@@ -225,6 +272,23 @@
       </packing>
     </child>
 
+    <!-- RHEL web view page -->
+    <child>
+      <!-- https://bugzilla.gnome.org/show_bug.cgi?id=786932 -->
+      <!-- https://bugzilla.gnome.org/show_bug.cgi?id=787033 -->
+      <!-- https://bugs.webkit.org/show_bug.cgi?id=175937 -->
+      <object class="WebKitWebView" type-func="webkit_web_view_get_type" id="rhel_web_view">
+        <property name="hexpand">True</property>
+        <property name="vexpand">True</property>
+        <property name="visible">True</property>
+        <signal name="decide-policy" handler="on_rhel_web_view_decide_policy"/>
+      </object>
+
+      <packing>
+        <property name="name">rhel-web-view-page</property>
+      </packing>
+    </child>
+
     <!-- URL page -->
     <child>
       <object class="GtkBox" id="url_menubox">
diff --git a/src/wizard-source.vala b/src/wizard-source.vala
index 35bd250..2143695 100644
--- a/src/wizard-source.vala
+++ b/src/wizard-source.vala
@@ -2,6 +2,7 @@
 
 private enum Boxes.SourcePage {
     MAIN,
+    RHEL_WEB_VIEW,
     URL,
 
     LAST,
@@ -96,7 +97,7 @@ private class Boxes.WizardMediaEntry : Gtk.Button {
 
 [GtkTemplate (ui = "/org/gnome/Boxes/ui/wizard-source.ui")]
 private class Boxes.WizardSource: Gtk.Stack {
-    private const string[] page_names = { "main-page", "url-page" };
+    private const string[] page_names = { "main-page", "rhel-web-view-page", "url-page" };
 
     public Gtk.Widget? selected { get; set; }
     public string uri {
@@ -123,6 +124,10 @@ private class Boxes.WizardSource: Gtk.Stack {
     private Gtk.Button libvirt_sys_import_button;
     [GtkChild]
     private Gtk.Label libvirt_sys_import_label;
+    [GtkChild]
+    private Gtk.Button install_rhel_button;
+    [GtkChild]
+    private WebKit.WebView rhel_web_view;
 
     private AppWindow window;
 
@@ -132,6 +137,8 @@ private class Boxes.WizardSource: Gtk.Stack {
 
     public MediaManager media_manager;
 
+    public string basename { get; set; }
+
     public bool download_required {
         get {
             const string[] supported_schemes = { "http", "https" };
@@ -158,6 +165,8 @@ private class Boxes.WizardSource: Gtk.Stack {
                 // FIXME: grab first element in the menu list
                 main_vbox.grab_focus ();
                 break;
+            case SourcePage.RHEL_WEB_VIEW:
+                break;
             case SourcePage.URL:
                 url_entry.changed ();
                 url_entry.grab_focus ();
@@ -211,6 +220,7 @@ private class Boxes.WizardSource: Gtk.Stack {
     }
 
     public void cleanup () {
+        basename = null;
         install_media = null;
         libvirt_sys_import = false;
         selected = null;
@@ -320,4 +330,173 @@ private class Boxes.WizardSource: Gtk.Stack {
             warning ("Failed to setup installation media '%s': %s", media.device_file, error.message);
         }
     }
+
+    private string rhel_get_authentication_uri_from_json (string contents) throws GLib.Error
+        requires (contents.length > 0) {
+
+        var parser = new Json.Parser ();
+        parser.load_from_data (contents, -1);
+
+        Json.NodeType node_type = Json.NodeType.NULL;
+
+        var root_node = parser.get_root ();
+        node_type = root_node.get_node_type ();
+        if (node_type != Json.NodeType.ARRAY)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: couldn’t find root array");
+
+        var root_array = root_node.get_array ();
+        if (root_array == null)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: couldn’t find root array");
+        if (root_array.get_length () == 0)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: root array is empty");
+
+        var root_array_node_0 = root_array.get_element (0);
+        node_type = root_array_node_0.get_node_type ();
+        if (node_type != Json.NodeType.OBJECT)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: root array doesn’t have an object");
+
+        var root_array_object_0 = root_array_node_0.get_object ();
+
+        var product_code_node = root_array_object_0.get_member ("productCode");
+        if (product_code_node == null)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: couldn’t find productCode");
+        node_type = product_code_node.get_node_type ();
+        if (node_type != Json.NodeType.VALUE)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: productCode is not a VALUE");
+
+        var product_code = product_code_node.get_string ();
+        if (product_code != "rhel")
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: productCode is not rhel");
+
+        var featured_artifact_node = root_array_object_0.get_member ("featuredArtifact");
+        if (featured_artifact_node == null)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: couldn’t find featuredArtifact");
+        node_type = featured_artifact_node.get_node_type ();
+        if (node_type != Json.NodeType.OBJECT)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: featuredArtifact is not an OBJECT");
+
+        var featured_artifact_object = featured_artifact_node.get_object ();
+
+        var url_node = featured_artifact_object.get_member ("url");
+        if (url_node == null)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: couldn’t find featuredArtifact.url");
+        node_type = url_node.get_node_type ();
+        if (node_type != Json.NodeType.VALUE)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: featuredArtifact.url is not a VALUE");
+
+        var url = url_node.get_string ();
+        if (url == null || url.length == 0)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: featuredArtifact.url is empty");
+
+        return url;
+    }
+
+    private string rhel_get_authentication_uri_from_xml (string contents) throws GLib.Error
+        requires (contents.length > 0) {
+
+        var product_code = extract_xpath (contents, "string(/products/product/productCode)", true);
+        if (product_code != "rhel")
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: productCode is not rhel");
+
+        var url = extract_xpath (contents, "string(/products/product/featuredArtifact/url)", true);
+        if (url.length == 0)
+            throw new Boxes.Error.INVALID ("Failed to parse JSON: featuredArtifact.url is empty");
+
+        return url;
+    }
+
+    [GtkCallback]
+    private void on_install_rhel_button_clicked () {
+        var cached_path = get_cache ("developers.redhat.com", "rhel");
+        GLib.FileUtils.unlink (cached_path);
+
+        var downloader = Downloader.get_instance ();
+        var file = GLib.File.new_for_uri 
("https://developers.redhat.com/download-manager/rest/available/rhel";);
+        string[] cached_paths = { cached_path };
+        var progress = new ActivityProgress ();
+        downloader.download.begin (file, cached_paths, progress, null, (obj, res) => {
+            try {
+                file = downloader.download.end (res);
+            } catch (GLib.Error error) {
+                window.notificationbar.display_error (_("Failed to get authentication URI"));
+                warning (error.message);
+                return;
+            }
+
+            file.load_contents_async.begin (null, (obj, res) => {
+                uint8[] contents;
+                try {
+                    file.load_contents_async.end (res, out contents, null);
+                } catch (GLib.Error error) {
+                    window.notificationbar.display_error (_("Failed to parse response from redhat.com"));
+                    warning (error.message);
+                    return;
+                }
+
+                if (contents.length <= 0) {
+                    window.notificationbar.display_error (_("Failed to parse response from redhat.com"));
+                    warning ("Empty response from redhat.com");
+                    return;
+                }
+
+                string authentication_uri = null;
+                try {
+                    authentication_uri = rhel_get_authentication_uri_from_json ((string) contents);
+                } catch (Boxes.Error error) {
+                    window.notificationbar.display_error (_("Failed to parse response from redhat.com"));
+                    warning (error.message);
+                    return;
+                } catch (GLib.Error json_error) {
+                    debug ("Failed to parse as JSON, could be XML: %s", json_error.message);
+                    try {
+                        authentication_uri = rhel_get_authentication_uri_from_xml ((string) contents);
+                    } catch (GLib.Error xml_error) {
+                        window.notificationbar.display_error (_("Failed to parse response from redhat.com"));
+                        warning (xml_error.message);
+                        return;
+                    }
+                }
+
+                debug ("RHEL ISO authentication URI: %s", authentication_uri);
+
+                rhel_web_view.load_uri (authentication_uri);
+                basename = GLib.Path.get_basename (authentication_uri);
+                page = SourcePage.RHEL_WEB_VIEW;
+            });
+        });
+    }
+
+    [GtkCallback]
+    private bool on_rhel_web_view_decide_policy (WebKit.WebView web_view,
+                                                 WebKit.PolicyDecision decision,
+                                                 WebKit.PolicyDecisionType decision_type) {
+        if (decision_type != WebKit.PolicyDecisionType.NAVIGATION_ACTION)
+            return false;
+
+        var action = (decision as WebKit.NavigationPolicyDecision).get_navigation_action ();
+        var request = action.get_request ();
+        var request_uri = request.get_uri ();
+        if (!request_uri.has_prefix ("https://developers.redhat.com/products/rhel";))
+            return false;
+
+        var soup_uri = new Soup.URI (request_uri);
+        var query = soup_uri.get_query ();
+        if (query == null)
+            return false;
+
+        var key_value_pairs = Soup.Form.decode (query);
+        var download_uri = key_value_pairs.lookup ("tcDownloadURL");
+        if (download_uri == null)
+            return false;
+
+        debug ("RHEL ISO download URI: %s", download_uri);
+
+        uri = download_uri;
+        activated ();
+
+        selected = install_rhel_button;
+
+        decision.ignore ();
+        return true;
+    }
 }
diff --git a/src/wizard.vala b/src/wizard.vala
index bea0ddf..1733f1a 100644
--- a/src/wizard.vala
+++ b/src/wizard.vala
@@ -83,6 +83,8 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
                     break;
 
                 case WizardPage.PREPARATION:
+                    //next_button.sensitive = true;
+                    //next_button.visible = true;
                     if (!prepare (create_preparation_progress ()))
                         return;
                     break;
@@ -119,6 +121,11 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
                 }
             } else {
                 switch (page) {
+                case WizardPage.PREPARATION:
+                    if (wizard_source.page == SourcePage.RHEL_WEB_VIEW)
+                        wizard_source.page = SourcePage.MAIN;
+                    break;
+
                 case WizardPage.REVIEW:
                     create_button.visible = false;
                     continue_button.visible = true;
@@ -156,6 +163,7 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
         if (page != WizardPage.SOURCE)
             return;
 
+        //next_button.visible = true;
         next_button.sensitive = false;
 
         switch (wizard_source.page) {
@@ -164,6 +172,10 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
             source = null;
             break;
 
+        case Boxes.SourcePage.RHEL_WEB_VIEW:
+            //next_button.visible = false;
+            break;
+
         case Boxes.SourcePage.URL:
             next_button.sensitive = false;
             if (wizard_source.uri.length == 0)
@@ -396,7 +408,7 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
 
         try {
             // Validate URI
-            prepare_for_location (wizard_source.uri, null, true);
+            prepare_for_location (wizard_source.uri, wizard_source.basename, true);
         } catch (GLib.Error error) {
             window.notificationbar.display_error (error.message);
 
@@ -407,7 +419,7 @@ private class Boxes.Wizard: Gtk.Stack, Boxes.UI {
 
         if (wizard_source.download_required) {
             continue_button.sensitive = false;
-            download_media.begin (wizard_source.uri, null, progress);
+            download_media.begin (wizard_source.uri, wizard_source.basename, progress);
 
             return true;
         }


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