dont save the master file in the back of the user when generating an album
authorgc <gc>
Sun, 3 Jul 2005 16:05:10 +0000 (16:05 +0000)
committergc <gc>
Sun, 3 Jul 2005 16:05:10 +0000 (16:05 +0000)
bin/booh

index af6e7c32becc509920e688a24585601cffbeb3e6..d907bea2c8b87ef66ae28f44cc024ec3b0bd66f0 100755 (executable)
--- a/bin/booh
+++ b/bin/booh
@@ -100,6 +100,8 @@ def read_config
     if !FileTest.directory?(File.expand_path('~/.booh'))
         system("mkdir ~/.booh")
     end
+
+    $tempfiles = []
 end
 
 def write_config
@@ -129,6 +131,10 @@ def write_config
     }
     $xmldoc.write(ios, 0)
     ios.close
+
+    $tempfiles.each { |f|
+        system("rm -f #{f}")
+    }
 end
 
 def set_mousecursor(what, *widget)
@@ -1083,10 +1089,16 @@ def save_current_file
         ios = File.open($filename, "w")
         $xmldoc.write(ios, 0)
         ios.close
-        $modified = false
     end
 end
 
+def save_current_file_user
+    $filename = $orig_filename
+    save_current_file
+    $modified = false
+    $generated_outofline = false
+end
+
 #- ret: true => ok  false => cancel
 def ask_save_modifications(msg1, msg2, *options)
     ret = true
@@ -1114,8 +1126,24 @@ def ask_save_modifications(msg1, msg2, *options)
         dialog.run { |response|
             dialog.destroy
             if response == Gtk::Dialog::RESPONSE_YES
-                save_current_file
-            elsif response == Gtk::Dialog::RESPONSE_CANCEL
+                save_current_file_user
+            else
+                #- if we have generated an album but won't save modifications, we must remove 
+                #- already-generated markers in original file
+                if $generated_outofline
+                    begin
+                        $xmldoc = REXML::Document.new File.new($orig_filename)
+                        $xmldoc.elements.each('//dir') { |elem|
+                            elem.delete_attribute('already-generated')
+                        }
+                        ios = File.open($orig_filename, "w")
+                        $xmldoc.write(ios, 0)
+                        ios.close
+                    rescue Exception
+                    end
+                end
+            end
+            if response == Gtk::Dialog::RESPONSE_CANCEL
                 ret = false
             end
         }
@@ -1819,14 +1847,30 @@ def open_file(filename)
 
     populate_subalbums_treeview
 
-    $config['last-opens'] ||= []
-    if $config['last-opens'][-1] != utf8(filename)
-        $config['last-opens'] << utf8(filename)
-    end
     $save.sensitive = $save_as.sensitive = $merge_current.sensitive = $merge.sensitive = $generate.sensitive = $properties.sensitive = $remove_all_captions.sensitive = true
     return nil
 end
 
+def open_file_user(filename)
+    result = open_file(filename)
+    if !result
+        $config['last-opens'] ||= []
+        if $config['last-opens'][-1] != utf8(filename)
+            $config['last-opens'] << utf8(filename)
+        end
+        $orig_filename = $filename
+        tmp = Tempfile.new("boohtemp")
+        tmp.close!
+        #- for security
+        ios = File.open($filename = tmp.path, File::RDWR|File::CREAT|File::EXCL)
+        ios.close
+        $tempfiles << $filename
+    else
+        $orig_filename = nil
+    end
+    return result
+end
+
 def open_file_popup
     if !ask_save_modifications(utf8(_("Save this album?")),
                                utf8(_("Do you want to save the changes to this album?")),
@@ -1845,7 +1889,7 @@ def open_file_popup
     while !ok
         if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
             push_mousecursor_wait(fc)
-            msg = open_file(fc.filename)
+            msg = open_file_user(fc.filename)
             pop_mousecursor(fc)
             if msg
                 show_popup(fc, msg)
@@ -2242,8 +2286,8 @@ def save_as_do
     fc.set_current_folder(File.expand_path("~/.booh"))
     fc.filename = $filename
     if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
-        $filename = fc.filename
-        save_current_file
+        $orig_filename = fc.filename
+        save_current_file_user
     end
     fc.destroy
 end
@@ -2375,7 +2419,7 @@ def create_menu_and_toolbar
 
     new.signal_connect('activate') { new_album }
     open.signal_connect('activate') { open_file_popup }
-    $save.signal_connect('activate') { save_current_file }
+    $save.signal_connect('activate') { save_current_file_user }
     $save_as.signal_connect('activate') { save_as_do }
     $merge_current.signal_connect('activate') { merge_current }
     $merge.signal_connect('activate') { merge }
@@ -2394,6 +2438,7 @@ def create_menu_and_toolbar
                              $undo_tb.sensitive = $undo_mb.sensitive = false
                              $redo_tb.sensitive = $redo_mb.sensitive = false
                              save_current_file
+                             $generated_outofline = true
                          }})
     }
     $properties.signal_connect('activate') { properties }
@@ -2490,7 +2535,7 @@ French: Guillaume Cottenceau") % $VERSION), { :centered => true, :pos_centered =
                                               utf8(_("Do you want to save the changes to this album?")),
                                               { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
                         push_mousecursor_wait
-                        msg = open_file(from_utf8(e))
+                        msg = open_file_user(from_utf8(e))
                         pop_mousecursor
                         if msg
                             show_popup($main_window, msg)
@@ -2716,7 +2761,7 @@ read_config
 Gtk.init
 create_main_window
 if ARGV[0]
-    open_file(ARGV[0])
+    open_file_user(ARGV[0])
 end
 Gtk.main