add upload of web-albums with lftp
authorGuillaume Cottenceau <gcottenc@gmail.com>
Wed, 6 Jan 2010 16:45:28 +0000 (17:45 +0100)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Wed, 6 Jan 2010 16:45:28 +0000 (17:45 +0100)
INSTALL
bin/booh
data/booh/images/stock-upload-16.png [new file with mode: 0644]

diff --git a/INSTALL b/INSTALL
index a259b81..f4ba891 100644 (file)
--- a/INSTALL
+++ b/INSTALL
@@ -29,7 +29,7 @@ And for runtime:
 - gimp - for photos editing (may not be a hard require)
 - mozilla-firefox - for showing albums in browser, and going on the
   web for tutorials (may not be a hard require)
-
+- lftp - for uploading web-albums
 
         Compiling and Installing Quick Start
 
index 2795f93..85d6951 100644 (file)
--- a/bin/booh
+++ b/bin/booh
@@ -3025,7 +3025,7 @@ def open_file(filename)
 
     populate_subalbums_treeview(true)
 
-    $save.sensitive = $save_as.sensitive = $merge_current.sensitive = $merge_newsubs.sensitive = $merge.sensitive = $extend.sensitive = $generate.sensitive = $view_wa.sensitive = $properties.sensitive = $remove_all_captions.sensitive = $sort_by_exif_date.sensitive = true
+    $save.sensitive = $save_as.sensitive = $merge_current.sensitive = $merge_newsubs.sensitive = $merge.sensitive = $extend.sensitive = $generate.sensitive = $view_wa.sensitive = $upload.sensitive = $properties.sensitive = $remove_all_captions.sensitive = $sort_by_exif_date.sensitive = true
     return nil
 end
 
@@ -4230,6 +4230,161 @@ Click the <span foreground='darkblue'>None</span> icon when you're finished with
 ") % intro), { :pos_centered => true })
 end
 
+def perform_remote_synchronization(url, detail_label, progressbar_window, dialog, read, write)
+    begin
+        gtk_thread_protect {
+            detail_label.set_markup("<i>" + utf8(_("Login into remote site...")) + "</i>")
+        }
+        write.puts("set net:max-retries 1")
+        write.puts("set cmd:fail-exit true")
+        write.puts("open " + url)
+        write.puts("lcd " + File.dirname($xmldoc.root.attributes['destination']))
+        write.puts("ls")  #- force connection and fail on exit, in order to detect problems with host or path
+        write.puts("echo __ls_EOF_1234567890abcdefghijk")  #- detect end
+
+        ok_to_mirror = false
+        while line = read.gets
+            msg 3, "received from lftp (login stage): #{line}"
+            if line == "__ls_EOF_1234567890abcdefghijk\n"
+                ok_to_mirror = true
+                break
+            end
+        end
+        if ! ok_to_mirror
+            gtk_thread_protect {
+                progressbar_window.destroy
+                show_popup(dialog, utf8(_("Specified URL incorrect, please check your input.")), { :pos_centered => true })
+            }
+            return
+        end
+
+        msg 3, "lftp login and ls ok, mirroring..."
+        gtk_thread_protect {
+            detail_label.set_markup("<i>" + utf8(_("Mirroring data...")) + "</i>")
+        }
+        mirrored_successfully = false
+        write.puts("set net:max-retries 5")
+        write.puts("mirror -R " + File.basename($xmldoc.root.attributes['destination']))
+        write.puts("echo __finished___ls_EOF_1234567890abcdefghijk")  #- detect end
+        while line = read.gets
+            msg 3, "received from lftp (mirror stage): #{line}"
+            if line == "__finished___ls_EOF_1234567890abcdefghijk\n"
+                mirrored_successfully = true
+                break
+            end
+        end
+
+        write.close
+        read.close
+
+        gtk_thread_protect {
+            progressbar_window.destroy
+            if mirrored_successfully
+                show_popup(dialog, utf8(_("Successfully mirrored into remote repository.")), { :pos_centered => true })
+            else
+                show_popup(dialog, utf8(_("Failed to mirror into remote repository.")), { :pos_centered => true })
+            end
+        }
+
+    rescue
+        msg 3, "failed lftp dialog: #{$!}"
+    end
+end
+
+def remote_synchronization
+
+    remote_synchro = Gtk::Dialog.new(utf8(_("Change seek time")),
+                                     $main_window,
+                                     Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
+                                     [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK])
+    remote_synchro.vbox.add(Gtk::Label.new.set_markup(utf8(_("<b>Upload web-album.</b>
+
+Mirror web-album into remote repository (lftp URL):
+<i>The destination directory '%s' will be created/updated there.</i>") % File.basename($xmldoc.root.attributes['destination']))).set_alignment(0, 0))
+    remote_synchro.vbox.add(Gtk::HBox.new(false, 0).pack_start(repo = Gtk::Entry.new.set_text($xmldoc.root.attributes['remote_synchronization_url'] || ''), true, true).
+                                                    pack_start(mirror = Gtk::Button.new(utf8(_("Upload"))).set_image(Gtk::Image.new("#{$FPATH}/images/stock-upload-16.png")), false, false))
+
+    mirror.signal_connect('clicked') {
+        if $xmldoc.root.attributes['remote_synchronization_url'] != repo.text
+            $modified = true
+            $xmldoc.root.add_attribute('remote_synchronization_url', repo.text)
+        end
+        w = create_window
+        w.set_transient_for(remote_synchro)
+        w.modal = true
+        vb = Gtk::VBox.new(false, 5).set_border_width(5)
+        vb.pack_start(Gtk::Label.new(utf8(_("Please wait, mirroring..."))), false, false)
+        vb.pack_start(detail = Gtk::Label.new.set_markup("<i>" + utf8(_("Initialization...")) + "</i>"), false, false)
+        vb.pack_start(pb = Gtk::ProgressBar.new, false, false)
+        bottom = Gtk::Alignment.new(0.5, 0.5, 0, 0).add(b = Gtk::Button.new(utf8(_("_Abort"))))
+        b.image = Gtk::Image.new("#{$FPATH}/images/stock-close-24.png")
+        vb.pack_end(bottom, false, false)
+        refresh_thread = Thread.new {
+            while true
+                gtk_thread_protect { pb.pulse }
+                sleep 0.5
+            end
+        }
+        w.add(vb)
+        w.signal_connect('delete-event') { w.destroy }
+        w.signal_connect('destroy') {
+            Thread.kill(refresh_thread)
+            gtk_thread_flush  #- needed because we're about to destroy widgets in w, for which they may be some pending gtk calls
+        }
+        w.window_position = Gtk::Window::POS_CENTER
+
+        pid = nil
+        cmd = 'lftp'
+        rd1, wr1 = IO.pipe
+        rd2, wr2 = IO.pipe
+
+        if ! pid = fork
+            rd2.close
+            wr1.close
+            $stdin.reopen(rd1)
+            $stdout.reopen(wr2)
+            $stderr.reopen(wr2)
+            begin
+                exec(cmd)
+            rescue
+                Process.exit!(66)  #- _exit
+            end
+        end
+
+        rd1.close
+        wr2.close
+
+        remote_synchronization_thread = Thread.new {
+            perform_remote_synchronization(repo.text, detail, w, remote_synchro, rd2, wr1)
+            begin
+                Process.kill('SIGTERM', pid)
+            rescue
+            end
+        }
+
+        b.signal_connect('clicked') {
+            Thread.kill(remote_synchronization_thread)
+            Process.kill('SIGTERM', pid)
+            w.destroy
+        }
+        w.show_all
+
+        Thread.new {
+            id, exitstatus = Process.waitpid2(pid)
+            if exitstatus >> 8 == 66
+                gtk_thread_protect {
+                    w.destroy
+                    show_popup(remote_synchro, utf8(_("Failed to execute 'lftp' program.")), { :pos_centered => true })
+                }
+            end
+        }
+    }
+
+    remote_synchro.window_position = Gtk::Window::POS_CENTER
+    remote_synchro.show_all
+    remote_synchro.run { remote_synchro.destroy }
+end
+
 def create_menu_and_toolbar
     
     #- menu
@@ -4262,6 +4417,8 @@ def create_menu_and_toolbar
     tooltips.set_tip($generate, utf8(_("(Re)generate web-album from latest changes into the destination directory")), nil)
     filesubmenu.append($view_wa = Gtk::ImageMenuItem.new(utf8(_("View web-album with browser"))).set_sensitive(false))
     $view_wa.image = Gtk::Image.new("#{$FPATH}/images/stock-view-webalbum-16.png")
+    filesubmenu.append($upload = Gtk::ImageMenuItem.new(utf8(_("Upload web-album"))).set_sensitive(false))
+    $upload.image = Gtk::Image.new("#{$FPATH}/images/stock-upload-16.png")
     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
     filesubmenu.append($properties = Gtk::ImageMenuItem.new(Gtk::Stock::PROPERTIES).set_sensitive(false))
     tooltips.set_tip($properties, utf8(_("View and modify properties of the web-album")), nil)
@@ -4314,6 +4471,18 @@ Click to view it in your browser:") % [ $xmldoc.root.attributes['destination'],
             show_popup($main_window, utf8(_("Seems like you should generate the web-album first.")))
         end
     }
+    $upload.signal_connect('activate') {
+        indexhtml = $xmldoc.root.attributes['destination'] + '/index.html'
+        if File.exists?(indexhtml)
+            if !system("which lftp >/dev/null 2>/dev/null")
+                show_popup($main_window, utf8(_("The program 'lftp' is needed to upload web-albums. Please install it.")), { :pos_centered => true })
+            else
+                remote_synchronization
+            end
+        else
+            show_popup($main_window, utf8(_("Seems like you should generate the web-album first.")))
+        end
+    }
     $properties.signal_connect('activate') { properties }
 
     quit.signal_connect('activate') { try_quit }
diff --git a/data/booh/images/stock-upload-16.png b/data/booh/images/stock-upload-16.png
new file mode 100644 (file)
index 0000000..e2aa8cc
Binary files /dev/null and b/data/booh/images/stock-upload-16.png differ