[gnome-boxes/osinfo-from-server: 50/53] assistant: Fetch recommended downloads from an URL




commit bd499b2a9175bf728d51698cc1d34e5ba6ad8c2a
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Apr 12 14:39:06 2022 +0200

    assistant: Fetch recommended downloads from an URL

 data/ui/assistant/pages/downloads-page.ui | 29 +++++++++++++++++++++--------
 src/assistant/downloads-page.vala         | 17 ++++++++++++++---
 src/assistant/index-page.vala             |  4 +++-
 src/downloader.vala                       |  2 +-
 src/util-app.vala                         | 28 +++++++++++++++++++++++++++-
 5 files changed, 66 insertions(+), 14 deletions(-)
---
diff --git a/data/ui/assistant/pages/downloads-page.ui b/data/ui/assistant/pages/downloads-page.ui
index d49d5149..56257580 100644
--- a/data/ui/assistant/pages/downloads-page.ui
+++ b/data/ui/assistant/pages/downloads-page.ui
@@ -11,15 +11,28 @@
         <property name="margin-end">20</property>
 
         <child>
-          <object class="GtkListBox" id="recommended_listbox">
+          <object class="GtkStack" id="stack">
             <property name="visible">True</property>
-            <property name="vexpand">True</property>
-            <property name="selection-mode">none</property>
-            <property name="margin">20</property>
-            <signal name="row-activated" handler="on_listbox_row_activated"/>
-            <style>
-              <class name="content"/>
-            </style>
+
+            <child>
+              <object class="GtkSpinner">
+                <property name="visible">True</property>
+                <property name="active">True</property>
+              </object>
+            </child>
+
+            <child>
+              <object class="GtkListBox" id="recommended_listbox">
+                <property name="visible">True</property>
+                <property name="vexpand">True</property>
+                <property name="selection-mode">none</property>
+                <property name="margin">20</property>
+                <signal name="row-activated" handler="on_listbox_row_activated"/>
+                <style>
+                  <class name="content"/>
+                </style>
+              </object>
+            </child>
           </object>
         </child>
       </object>
diff --git a/src/assistant/downloads-page.vala b/src/assistant/downloads-page.vala
index ce8fe2c3..1b63b0ae 100644
--- a/src/assistant/downloads-page.vala
+++ b/src/assistant/downloads-page.vala
@@ -16,6 +16,8 @@ public enum AssistantDownloadsPageView {
     [GtkChild]
     private unowned Gtk.ListBox listbox;
     [GtkChild]
+    private unowned Gtk.Stack stack;
+    [GtkChild]
     private unowned Gtk.ListBox recommended_listbox;
 
     public Gtk.SearchEntry search_entry = new Gtk.SearchEntry ();
@@ -59,7 +61,6 @@ public enum AssistantDownloadsPageView {
 
         recommended_model = new GLib.ListStore (typeof (Osinfo.Media));
         recommended_listbox.bind_model (recommended_model, create_downloads_entry);
-        populate_recommended_list.begin ();
 
         show_more_button = new Gtk.Button () {
             visible = true,
@@ -87,12 +88,22 @@ private void set_visible_view () {
         }
     }
 
-    private async void populate_recommended_list () {
-        foreach (var media in yield get_recommended_downloads ()) {
+    public async void populate_recommended_list () {
+        if (recommended_model.get_n_items () != 0)
+            return;
+
+        var os_list = yield fetch_recommended_downloads_from_net ();
+        if (os_list == null)
+            os_list = yield get_recommended_downloads ();
+
+        foreach (var media in os_list) {
             if (media != null) {
                 recommended_model.append (media);
             }
         }
+
+        if (recommended_model.get_n_items () > 0)
+            stack.set_visible_child (recommended_listbox);
     }
 
     private Gtk.Widget create_downloads_entry (Object item) {
diff --git a/src/assistant/index-page.vala b/src/assistant/index-page.vala
index a5b68d6d..43674f4c 100644
--- a/src/assistant/index-page.vala
+++ b/src/assistant/index-page.vala
@@ -180,9 +180,11 @@ private async void on_select_file_button_clicked () {
     }
 
     [GtkCallback]
-    private void on_download_an_os_button_clicked () {
+    private async void on_download_an_os_button_clicked () {
         stack.set_visible_child (recommended_downloads_page);
 
+        yield recommended_downloads_page.populate_recommended_list ();
+
         dialog.previous_button.label = _("Previous");
     }
 }
diff --git a/src/downloader.vala b/src/downloader.vala
index c79f506c..a8b6f339 100644
--- a/src/downloader.vala
+++ b/src/downloader.vala
@@ -125,7 +125,7 @@ public async File download (File             remote_file,
         return cached_file;
     }
 
-    private async void download_from_http (Download download, Cancellable? cancellable = null) throws 
GLib.Error {
+    public async void download_from_http (Download download, Cancellable? cancellable = null) throws 
GLib.Error {
         var msg = new Soup.Message ("GET", download.uri);
         GLib.Error? err = null;
 
diff --git a/src/util-app.vala b/src/util-app.vala
index 65aba759..e11e76b2 100644
--- a/src/util-app.vala
+++ b/src/util-app.vala
@@ -178,10 +178,34 @@ else if (media_url.contains ("dvd"))
     }
 
     public async GLib.List<Osinfo.Media>? get_recommended_downloads () {
+        return yield parse_recommended_downloads_file (
+            "resource:///org/gnome/Boxes/recommended-downloads.xml");
+    }
+
+    private const string GNOME_UPSTREAM_RECOMMENDED_DOWNLOADS = 
"https://gnome.pages.gitlab.gnome.org/gnome-boxes-logos/recommended-downloads.xml";;
+    public async GLib.List<Osinfo.Media>? fetch_recommended_downloads_from_net () {
+        var remote_file = GLib.File.new_for_uri (GNOME_UPSTREAM_RECOMMENDED_DOWNLOADS);
+        string cached_path = get_logo_cache ("recommended-downloads.xml");
+        GLib.File cached_file = GLib.File.new_for_path (cached_path);
+
+        var download = new Download (remote_file, cached_file, new ActivityProgress ()); 
+        try {
+            yield Downloader.get_default ().download_from_http (download);
+        } catch (GLib.Error error) {
+            warning ("Failed to download recommended-downloads file: %s", error.message);
+
+            if (!cached_file.query_exists ())
+                return null;
+        }
+
+        return yield parse_recommended_downloads_file (cached_file.get_uri ());
+    }
+
+    private async GLib.List<Osinfo.Media>? parse_recommended_downloads_file (string uri) {
         uint8[] contents;
 
         try {
-            File file = File.new_for_uri ("resource:///org/gnome/Boxes/recommended-downloads.xml");
+            File file = File.new_for_uri (uri);
 
             file.load_contents (null, out contents, null);
         } catch (GLib.Error e) {
@@ -205,6 +229,8 @@ public async GLib.List<Osinfo.Media>? get_recommended_downloads () {
         var os_db = MediaManager.get_default ().os_db;
         for (Xml.Node* iter = root->children; iter != null; iter = iter->next) {
             var os_id = iter->get_prop ("id");
+            if (os_id == null)
+                continue;
 
             Osinfo.Os? os;
             try {


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