[shotwell/wip/phako/google-photos: 14/15] wip: Make it possible to create an album



commit e3cd020fb4e28b53a235a2f8a7895b759fb01ffb
Author: Jens Georg <mail jensge org>
Date:   Wed Jan 23 16:34:22 2019 +0100

    wip: Make it possible to create an album

 plugins/shotwell-publishing/PhotosPublisher.vala   | 68 ++++++++++++++++++++--
 .../shotwell-publishing/PhotosPublishingPane.vala  | 24 +++++++-
 .../google_photos_publishing_options_pane.ui       |  9 ++-
 3 files changed, 92 insertions(+), 9 deletions(-)
---
diff --git a/plugins/shotwell-publishing/PhotosPublisher.vala 
b/plugins/shotwell-publishing/PhotosPublisher.vala
index 790d5332..122c2ceb 100644
--- a/plugins/shotwell-publishing/PhotosPublisher.vala
+++ b/plugins/shotwell-publishing/PhotosPublisher.vala
@@ -142,7 +142,7 @@ private class MediaCreationTransaction : Publishing.RESTSupport.GooglePublisher.
 }
 
 private class AlbumCreationTransaction : Publishing.RESTSupport.GooglePublisher.AuthenticatedTransaction {
-    private const string ENDPOINT_URL = "https://photoslibrary.googleapis.com/v1/albums.create";;
+    private const string ENDPOINT_URL = "https://photoslibrary.googleapis.com/v1/albums";;
     private string title;
 
     public AlbumCreationTransaction(Publishing.RESTSupport.GoogleSession session,
@@ -188,8 +188,8 @@ private class AlbumDirectoryTransaction : Publishing.RESTSupport.GooglePublisher
             var response_albums = object.get_member ("albums").get_array();
             response_albums.foreach_element( (a, b, element) => {
                 var album = element.get_object();
-                var is_writable = album.get_member("isWritable");
-                if (is_writable != null && is_writable.get_string() != "false")
+                var is_writable = album.get_member("isWriteable");
+                if (is_writable != null && is_writable.get_boolean())
                     albums += new Album(album.get_string_member("title"), album.get_string_member("id"));
             });
 
@@ -329,9 +329,69 @@ public class Publisher : Publishing.RESTSupport.GooglePublisher {
 
         save_parameters_to_configuration_system(publishing_parameters);
 
-        do_upload();
+        if (publishing_parameters.get_target_album_entry_id () != null) {
+            do_upload();
+        } else {
+            do_create_album();
+        }
+    }
+
+    private void do_create_album() {
+        debug("ACTION: Creating album");
+        assert(publishing_parameters.get_target_album_entry_id () == null);
+
+        get_host().set_service_locked(true);
+
+        var txn = new AlbumCreationTransaction(get_session(), publishing_parameters.get_target_album_name());
+        txn.completed.connect(on_album_create_complete);
+        txn.network_error.connect(on_album_create_error);
+
+        try {
+            txn.execute();
+        } catch (Spit.Publishing.PublishingError error) {
+            on_album_create_error(txn, error);
+        }
+    }
+
+    private void on_album_create_complete(Publishing.RESTSupport.Transaction txn) {
+        txn.completed.disconnect(on_album_create_complete);
+        txn.network_error.disconnect(on_album_create_error);
+
+        if (!is_running())
+            return;
+
+        debug("EVENT: finished creating album information: %s", txn.get_response());
+
+        try {
+            var node = Json.from_string(txn.get_response());
+            var object = node.get_object();
+            publishing_parameters.set_target_album_entry_id (object.get_string_member ("id"));
+
+            do_upload();
+        } catch (Error error) {
+        }
     }
 
+    private void on_album_create_error(Publishing.RESTSupport.Transaction txn,
+                                              Spit.Publishing.PublishingError error) {
+        txn.completed.disconnect(on_initial_album_fetch_complete);
+        txn.network_error.disconnect(on_initial_album_fetch_error);
+
+        if (!is_running())
+            return;
+
+        debug("EVENT: creating album failed; response = '%s'.",
+              txn.get_response());
+
+        if (txn.get_status_code() == 403 || txn.get_status_code() == 404) {
+            do_logout();
+        } else {
+            // If we get any other kind of error, we can't recover, so just post it to the user
+            get_host().post_error(error);
+        }
+    }
+
+
 
     protected override void do_logout() {
         debug("ACTION: logging out user.");
diff --git a/plugins/shotwell-publishing/PhotosPublishingPane.vala 
b/plugins/shotwell-publishing/PhotosPublishingPane.vala
index d26fdfc5..af6e5a4f 100644
--- a/plugins/shotwell-publishing/PhotosPublishingPane.vala
+++ b/plugins/shotwell-publishing/PhotosPublishingPane.vala
@@ -19,6 +19,8 @@ internal class PublishingOptionsPane : Gtk.Box, Spit.Publishing.DialogPane {
     [GtkChild]
     private Gtk.Button publish_button;
     [GtkChild]
+    private Gtk.RadioButton existing_album_radio;
+    [GtkChild]
     private Gtk.ComboBoxText existing_albums_combo;
     [GtkChild]
     private Gtk.ComboBoxText size_combo;
@@ -28,6 +30,10 @@ internal class PublishingOptionsPane : Gtk.Box, Spit.Publishing.DialogPane {
     private Gtk.Label login_identity_label;
     [GtkChild]
     private Gtk.CheckButton strip_metadata_check;
+    [GtkChild]
+    private Gtk.RadioButton new_album_radio;
+    [GtkChild]
+    private Gtk.Entry new_album_entry;
 
     public signal void publish();
     public signal void logout();
@@ -62,6 +68,9 @@ internal class PublishingOptionsPane : Gtk.Box, Spit.Publishing.DialogPane {
             size_combo.set_active(parameters.get_major_axis_size_selection_id());
         }
 
+        existing_album_radio.bind_property("active", existing_albums_combo, "sensitive", 
GLib.Binding.SYNC_CREATE);
+        new_album_radio.bind_property("active", new_album_entry, "sensitive", GLib.Binding.SYNC_CREATE);
+
         publish_button.clicked.connect (on_publish_clicked);
         logout_button.clicked.connect (on_logout_clicked);
     }
@@ -92,6 +101,12 @@ internal class PublishingOptionsPane : Gtk.Box, Spit.Publishing.DialogPane {
 
         if (default_album_id >= 0) {
             existing_albums_combo.set_active(default_album_id);
+            existing_album_radio.set_active(true);
+        }
+
+        if (albums.length == 0) {
+            existing_album_radio.set_sensitive(false);
+            new_album_radio.set_active(true);
         }
     }
 
@@ -111,8 +126,13 @@ internal class PublishingOptionsPane : Gtk.Box, Spit.Publishing.DialogPane {
 
         Album[] albums = parameters.get_albums();
 
-        parameters.set_target_album_name(albums[existing_albums_combo.get_active()].name);
-        parameters.set_target_album_entry_id(albums[existing_albums_combo.get_active()].id);
+        if (new_album_radio.get_active()) {
+            parameters.set_target_album_name(new_album_entry.get_text());
+        } else {
+            parameters.set_target_album_name(albums[existing_albums_combo.get_active()].name);
+            parameters.set_target_album_entry_id(albums[existing_albums_combo.get_active()].id);
+        }
+
         publish();
     }
 
diff --git a/plugins/shotwell-publishing/google_photos_publishing_options_pane.ui 
b/plugins/shotwell-publishing/google_photos_publishing_options_pane.ui
index c56d1363..8685b965 100644
--- a/plugins/shotwell-publishing/google_photos_publishing_options_pane.ui
+++ b/plugins/shotwell-publishing/google_photos_publishing_options_pane.ui
@@ -5,8 +5,10 @@
   <template class="PublishingGooglePhotosPublishingOptionsPane" parent="GtkBox">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
-    <property name="margin_start">12</property>
-    <property name="margin_end">12</property>
+    <property name="margin_start">18</property>
+    <property name="margin_end">18</property>
+    <property name="margin_top">18</property>
+    <property name="margin_bottom">18</property>
     <property name="orientation">vertical</property>
     <property name="spacing">1</property>
     <child>
@@ -61,6 +63,7 @@
               <object class="GtkComboBoxText" id="existing_albums_combo">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
+                <property name="tooltip_text" translatable="yes">Shotwell can only publish into albums that 
it created by itself, so this list might be empty despite the fact that you already have albums in your 
Google Photos account</property>
               </object>
               <packing>
                 <property name="left_attach">1</property>
@@ -141,7 +144,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkEntry" id="new_album_label">
+              <object class="GtkEntry" id="new_album_entry">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
               </object>


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