factorize
authorgc <gc>
Thu, 14 Apr 2005 21:05:15 +0000 (21:05 +0000)
committergc <gc>
Thu, 14 Apr 2005 21:05:15 +0000 (21:05 +0000)
bin/booh-gui

index 46a6f5cb5bf80e7d22e7aa86a685aa6a2760c55a..5a2f2c87daa0dfe8de9607a5ccb0f8c331a6fb23 100755 (executable)
@@ -256,7 +256,7 @@ def create_editzone(scrolledwindow, pagenum)
     return [ frame, textview ]
 end
 
-def rotate(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
+def rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
     #- update rotate attribute
     xmlelem.add_attribute("#{attributes_prefix}rotate", current_angle = ( xmlelem.attributes["#{attributes_prefix}rotate"].to_i + angle ) % 360)
 
@@ -281,28 +281,72 @@ def rotate(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, des
     end
 end
 
+def rotate(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
+
+    rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
+
+    save_undo(angle == 90 ? _("rotate clockwise") : angle == -90 ? _("rotate counter-clockwise") : _("flip upside-down"),
+              Proc.new { |angle|
+                  rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
+                  $notebook.set_page(attributes_prefix != '' ? 0 : 1)
+                  Proc.new {
+                      rotate_real(-angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
+                      $notebook.set_page(0)
+                      $notebook.set_page(attributes_prefix != '' ? 0 : 1)
+                  }
+              }, -angle)
+end
+
+def color_swap(xmldir, attributes_prefix)
+    if xmldir.attributes["#{attributes_prefix}color-swap"]
+        xmldir.delete_attribute("#{attributes_prefix}color-swap")
+    else
+        xmldir.add_attribute("#{attributes_prefix}color-swap", '1')
+    end
+end
+
+def enhance(xmldir, attributes_prefix)
+    if xmldir.attributes["#{attributes_prefix}enhance"]
+        xmldir.delete_attribute("#{attributes_prefix}enhance")
+    else
+        xmldir.add_attribute("#{attributes_prefix}enhance", '1')
+    end
+end
+
+def gen_real_thumbnail(type, origfile, destfile, xmldir, size, img)
+    Thread.new {
+        push_mousecursor_wait
+        system("rm -f '#{destfile}'")
+        #- type can be 'element' or 'subdir'
+        if type == 'element'
+            gen_thumbnails_element(origfile, xmldir, false, [ { 'filename' => destfile, 'size' => size } ])
+        else
+            gen_thumbnails_subdir(origfile, xmldir, false, [ { 'filename' => destfile, 'size' => size } ], find_subalbum_info_type(xmldir))
+        end
+        img.set(destfile)
+        $rotated_pixbufs[destfile] = { :orig => img.pixbuf, :pixbuf => img.pixbuf, :angle_to_orig => 0 }
+        if entry2type(origfile) == 'video'
+            #- cleanup temp for videos
+            system("rm -f #{current_dest_dir}/screenshot.jpg00000*")
+        end
+        pop_mousecursor
+    }
+end
+
+
 def add_thumbnail(autotable, filename, type, thumbnail_img, caption)
 
     img = nil
     frame1 = Gtk::Frame.new
-    gen_real_thumbnail = Proc.new {
-        Thread.new {
-            push_mousecursor_wait
-            gen_thumbnails_element(from_utf8("#{$current_path}/#{filename}"), $xmldir, false, [ { 'filename' => thumbnail_img, 'size' => $default_size['thumbnails'] } ])
-            img.set(thumbnail_img)
-            $rotated_pixbufs[thumbnail_img] = { :orig => img.pixbuf, :pixbuf => img.pixbuf, :angle_to_orig => 0 }
-            if type == 'video'
-                #- cleanup temp for videos
-                system("rm -f #{current_dest_dir}/screenshot.jpg00000*")
-            end
-            pop_mousecursor
-        }
+
+    my_gen_real_thumbnail = proc {
+        gen_real_thumbnail('element', from_utf8("#{$current_path}/#{filename}"), thumbnail_img, $xmldir, $default_size['thumbnails'], img)
     }
 
     #- generate the thumbnail if missing (if image was rotated but booh was not relaunched)
     if !$rotated_pixbufs[thumbnail_img] && !File.exists?(thumbnail_img)
         frame1.add(img = Gtk::Image.new)
-        gen_real_thumbnail.call
+        my_gen_real_thumbnail.call
     else
         frame1.add(img = Gtk::Image.new($rotated_pixbufs[thumbnail_img] ? $rotated_pixbufs[thumbnail_img][:pixbuf] : thumbnail_img))
     end
@@ -337,34 +381,14 @@ def add_thumbnail(autotable, filename, type, thumbnail_img, caption)
     }
 
     rotate_and_cleanup = Proc.new { |angle|
-        perform_rotate_and_cleanup = Proc.new { |angle|
-            rotate(angle, thumbnail_img, img, $xmldir.elements["[@filename='#{filename}']"], '', $default_thumbnails[:x], $default_thumbnails[:y])
-        }
-        perform_rotate_and_cleanup.call(angle)
-
+        rotate(angle, thumbnail_img, img, $xmldir.elements["[@filename='#{filename}']"], '', $default_thumbnails[:x], $default_thumbnails[:y])
         cleanup_all_thumbnails.call
-
-        save_undo(angle == 90 ? _("rotate clockwise") : angle == -90 ? _("rotate counter-clockwise") : _("flip upside-down"),
-                  Proc.new { |angle|
-                      perform_rotate_and_cleanup.call(angle)
-                      $notebook.set_page(1)
-                      Proc.new {
-                          perform_rotate_and_cleanup.call(-angle)
-                          $notebook.set_page(1)
-                      }
-                  }, -angle)
     }
 
     color_swap_and_cleanup = Proc.new {
         perform_color_swap_and_cleanup = Proc.new {
-            xmlelem = $xmldir.elements["[@filename='#{filename}']"]
-            if xmlelem.attributes['color-swap']
-                xmlelem.delete_attribute('color-swap')
-            else
-                xmlelem.add_attribute('color-swap', '1')
-            end
-            system("rm -f '#{thumbnail_img}'")
-            gen_real_thumbnail.call
+            color_swap($xmldir.elements["[@filename='#{filename}']"], '')
+            my_gen_real_thumbnail.call
         }
 
         cleanup_all_thumbnails.call
@@ -383,20 +407,13 @@ def add_thumbnail(autotable, filename, type, thumbnail_img, caption)
 
     enhance_and_cleanup = Proc.new {
         perform_enhance_and_cleanup = Proc.new {
-            xmlelem = $xmldir.elements["[@filename='#{filename}']"]
-            if xmlelem.attributes['enhance']
-                xmlelem.delete_attribute('enhance')
-            else
-                xmlelem.add_attribute('enhance', '1')
-            end
-            system("rm -f '#{thumbnail_img}'")
-            gen_real_thumbnail.call
+            enhance($xmldir.elements["[@filename='#{filename}']"], '')
+            my_gen_real_thumbnail.call
         }
 
         cleanup_all_thumbnails.call
         perform_enhance_and_cleanup.call
 
-        #- remove out of sync images
         save_undo(_("enhance"),
                   Proc.new {
                       perform_enhance_and_cleanup.call
@@ -728,22 +745,13 @@ def change_dir
         f.set_shadow_type(Gtk::SHADOW_ETCHED_OUT)
 
         img = nil
-        gen_real_thumbnail = Proc.new {
-            Thread.new {
-                system("rm -f '#{thumbnail_file}'")
-                gen_thumbnails_subdir(from_utf8(captionfile), xmldir, false,
-                                      [ { 'filename' => thumbnail_file, 'size' => $albums_thumbnail_size } ], infotype)
-                img.set(thumbnail_file)
-                $rotated_pixbufs[thumbnail_file] = { :orig => img.pixbuf, :pixbuf => img.pixbuf, :angle_to_orig => 0 }
-                if entry2type(captionfile) == 'video'
-                    system("rm -f #{current_dest_dir}/screenshot.jpg00000*")
-                end
-            }
+        my_gen_real_thumbnail = proc {
+            gen_real_thumbnail('subdir', from_utf8(captionfile), thumbnail_file, xmldir, $albums_thumbnail_size, img)
         }
 
         if !$rotated_pixbufs[thumbnail_file] && !File.exists?(thumbnail_file)
             f.add(img = Gtk::Image.new)
-            gen_real_thumbnail.call
+            my_gen_real_thumbnail.call
         else
             f.add(img = Gtk::Image.new($rotated_pixbufs[thumbnail_file] ? $rotated_pixbufs[thumbnail_file][:pixbuf] : thumbnail_file))
         end
@@ -779,38 +787,20 @@ def change_dir
                 xmldir.delete_attribute("#{infotype}-rotate")
                 xmldir.delete_attribute("#{infotype}-color-swap")
                 xmldir.delete_attribute("#{infotype}-enhance")
-                gen_real_thumbnail.call
+                my_gen_real_thumbnail.call
             end
             fc.destroy
         }
 
         rotate_and_cleanup = Proc.new { |angle|
-            perform_rotate_and_cleanup = Proc.new { |angle|
-                rotate(angle, thumbnail_file, img, xmldir, "#{infotype}-",
-                       $default_albums_thumbnails[:x], $default_albums_thumbnails[:y])
-            }
-            perform_rotate_and_cleanup.call(angle)
+            rotate(angle, thumbnail_file, img, xmldir, "#{infotype}-", $default_albums_thumbnails[:x], $default_albums_thumbnails[:y])
             system("rm -f '#{thumbnail_file}'")
-
-            save_undo(angle == 90 ? _("rotate clockwise") : angle == -90 ? _("rotate counter-clockwise") : _("flip upside-down"),
-                      Proc.new { |angle|
-                          perform_rotate_and_cleanup.call(angle)
-                          $notebook.set_page(0)
-                          Proc.new {
-                              perform_rotate_and_cleanup.call(-angle)
-                              $notebook.set_page(0)
-                          }
-                      }, -angle)
         }
 
         color_swap_and_cleanup = Proc.new {
             perform_color_swap_and_cleanup = Proc.new {
-                if xmldir.attributes["#{infotype}-color-swap"]
-                    xmldir.delete_attribute("#{infotype}-color-swap")
-                else
-                    xmldir.add_attribute("#{infotype}-color-swap", '1')
-                end
-                gen_real_thumbnail.call
+                color_swap(xmldir, "#{infotype}-")
+                my_gen_real_thumbnail.call
             }
             perform_color_swap_and_cleanup.call
 
@@ -827,17 +817,12 @@ def change_dir
 
         enhance_and_cleanup = Proc.new {
             perform_enhance_and_cleanup = Proc.new {
-                if xmldir.attributes["#{infotype}-enhance"]
-                    xmldir.delete_attribute("#{infotype}-enhance")
-                else
-                    xmldir.add_attribute("#{infotype}-enhance", '1')
-                end
-                gen_real_thumbnail.call
+                enhance(xmldir, "#{infotype}-")
+                my_gen_real_thumbnail.call
             }
             
             perform_enhance_and_cleanup.call
             
-            #- remove out of sync images
             save_undo(_("enhance"),
                       Proc.new {
                           perform_enhance_and_cleanup.call