support panoramas
[booh] / bin / booh
1 #! /usr/bin/ruby
2 #
3 #                         *  BOOH  *
4 #
5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
6 #
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
11 #
12 #
13 # Copyright (c) 2004 Guillaume Cottenceau <http://zarb.org/~gc/resource/gc_mail.png>
14 #
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 require 'getoptlong'
23 require 'tempfile'
24 require 'thread'
25
26 require 'gtk2'
27 require 'booh/gtkadds'
28 require 'booh/GtkAutoTable'
29
30 require 'gettext'
31 include GetText
32 bindtextdomain("booh")
33
34 require 'rexml/document'
35 include REXML
36
37 require 'booh/booh-lib'
38 include Booh
39 require 'booh/UndoHandler'
40
41
42 #- options
43 $options = [
44     [ '--help',          '-h', GetoptLong::NO_ARGUMENT,       _("Get help message") ],
45
46     [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
47 ]
48
49 #- default values for some globals 
50 $xmldir = nil
51 $modified = false
52 $current_cursor = nil
53 $ignore_videos = false
54 $button1_pressed_autotable = false
55 $generated_outofline = false
56
57 def usage
58     puts _("Usage: %s [OPTION]...") % File.basename($0)
59     $options.each { |ary|
60         printf " %3s, %-15s %s\n", ary[1], ary[0], ary[3]
61     }
62 end
63
64 def handle_options
65     parser = GetoptLong.new
66     parser.set_options(*$options.collect { |ary| ary[0..2] })
67     begin
68         parser.each_option do |name, arg|
69             case name
70             when '--help'
71                 usage
72                 exit(0)
73
74             when '--verbose-level'
75                 $verbose_level = arg.to_i
76
77             end
78         end
79     rescue
80         puts $!
81         usage
82         exit(1)
83     end
84 end
85
86 def read_config
87     $config = {}
88     $config_file = File.expand_path('~/.booh-gui-rc')
89     if File.readable?($config_file)
90         $xmldoc = REXML::Document.new(File.new($config_file))
91         $xmldoc.root.elements.each { |element|
92             txt = element.get_text
93             if txt
94                 if txt.value =~ /~~~/ || element.name == 'last-opens'
95                     $config[element.name] = txt.value.split(/~~~/)
96                 else
97                     $config[element.name] = txt.value
98                 end
99             elsif element.elements.size == 0
100                 $config[element.name] = ''
101             else
102                 $config[element.name] = {}
103                 element.each { |chld|
104                     txt = chld.get_text
105                     $config[element.name][chld.name] = txt ? txt.value : nil
106                 }
107             end
108         }
109     end
110     $config['video-viewer'] ||= '/usr/bin/mplayer %f'
111     $config['browser'] ||= "/usr/bin/mozilla-firefox -remote 'openURL(%f,new-window)' || /usr/bin/mozilla-firefox %f"
112     if !FileTest.directory?(File.expand_path('~/.booh'))
113         system("mkdir ~/.booh")
114     end
115
116     $tempfiles = []
117     $todelete = []
118 end
119
120 def check_config
121     if !system("which convert >/dev/null 2>/dev/null")
122         show_popup($main_window, utf8(_("The program 'convert' is needed. Please install it.
123 It is generally available with the 'ImageMagick' software package.")), { :pos_centered => true })
124         exit 1
125     end
126     if !system("which identify >/dev/null 2>/dev/null")
127         show_popup($main_window, utf8(_("The program 'identify' is needed to get images sizes and EXIF data. Please install it.
128 It is generally available with the 'ImageMagick' software package.")), { :pos_centered => true })
129     end
130     missing = %w(transcode mencoder).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
131     if missing != []
132         show_popup($main_window, utf8(_("The following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')), { :pos_centered => true })
133     end
134
135     viewer_binary = $config['video-viewer'].split.first
136     if viewer_binary && !File.executable?(viewer_binary)
137         show_popup($main_window, utf8(_("The configured video viewer seems to be unavailable.
138 You should fix this in Edit/Preferences so that you can view videos.
139
140 Problem was: '%s' is not an executable file.
141 Hint: don't forget to specify the full path to the executable,
142 e.g. '/usr/bin/mplayer' is correct but 'mplayer' only is not.") % viewer_binary), { :pos_centered => true, :not_transient => true })
143     end
144     browser_binary = $config['browser'].split.first
145     if browser_binary && !File.executable?(browser_binary)
146         show_popup($main_window, utf8(_("The configured browser seems to be unavailable.
147 You should fix this in Edit/Preferences so that you can open URLs.
148
149 Problem was: '%s' is not an executable file.") % browser_binary), { :pos_centered => true, :not_transient => true })
150     end
151 end
152
153 def write_config
154     if $config['last-opens'] && $config['last-opens'].size > 5
155         $config['last-opens'] = $config['last-opens'][-5, 5]
156     end
157
158     ios = File.open($config_file, "w")
159     $xmldoc = Document.new "<booh-gui-rc version='#{$VERSION}'/>"
160     $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
161     $config.each_pair { |key, value|
162         elem = $xmldoc.root.add_element key
163         if value.is_a? Hash
164             $config[key].each_pair { |subkey, subvalue|
165                 subelem = elem.add_element subkey
166                 subelem.add_text subvalue.to_s
167             }
168         elsif value.is_a? Array
169             elem.add_text value.join('~~~')
170         else
171             if !value
172                 elem.remove
173             else
174                 elem.add_text value.to_s
175             end
176         end
177     }
178     $xmldoc.write(ios, 0)
179     ios.close
180
181     $tempfiles.each { |f|
182         system("rm -f #{f}")
183     }
184 end
185
186 def set_mousecursor(what, *widget)
187     cursor = what.nil? ? nil : Gdk::Cursor.new(what)
188     if widget[0] && widget[0].window
189         widget[0].window.cursor = cursor
190     end
191     if $main_window.window
192         $main_window.window.cursor = cursor
193     end
194     $current_cursor = what
195 end
196 def set_mousecursor_wait(*widget)
197     gtk_thread_protect { set_mousecursor(Gdk::Cursor::WATCH, *widget) }
198     if Thread.current == Thread.main
199         Gtk.main_iteration while Gtk.events_pending?
200     end
201 end
202 def set_mousecursor_normal(*widget)
203     gtk_thread_protect { set_mousecursor($save_cursor = nil, *widget) }
204 end
205 def push_mousecursor_wait(*widget)
206     if $current_cursor != Gdk::Cursor::WATCH
207         $save_cursor = $current_cursor
208         gtk_thread_protect { set_mousecursor_wait(*widget) }
209     end
210 end
211 def pop_mousecursor(*widget)
212     gtk_thread_protect { set_mousecursor($save_cursor || nil, *widget) }
213 end
214
215 def current_dest_dir
216     source = $xmldoc.root.attributes['source']
217     dest = $xmldoc.root.attributes['destination']
218     return make_dest_filename(from_utf8($current_path.sub(/^#{Regexp.quote(source)}/, dest)))
219 end
220
221 def full_src_dir_to_rel(path, source)
222     return path.sub(/^#{Regexp.quote(from_utf8(source))}/, '')
223 end
224
225 def build_full_dest_filename(filename)
226     return current_dest_dir + '/' + make_dest_filename(from_utf8(filename))
227 end
228
229 def save_undo(name, closure, *params)
230     UndoHandler.save_undo(name, closure, [ *params ])
231     $undo_tb.sensitive = $undo_mb.sensitive = true
232     $redo_tb.sensitive = $redo_mb.sensitive = false
233 end
234
235 def view_element(filename, closures)
236     if entry2type(filename) == 'video'
237         cmd = from_utf8($config['video-viewer']).gsub('%f', "'#{from_utf8($current_path + '/' + filename)}'") + ' &'
238         msg 2, cmd
239         system(cmd)
240         return
241     end
242
243     w = Gtk::Window.new.set_title(filename)
244
245     msg 3, "filename: #{filename}"
246     dest_img = build_full_dest_filename(filename).sub(/\.[^\.]+$/, '') + "-#{$default_size['fullscreen']}.jpg"
247     #- typically this file won't exist in case of videos; try with the largest thumbnail around
248     if !File.exists?(dest_img)
249         if entry2type(filename) == 'video'
250             alternatives = Dir[build_full_dest_filename(filename).sub(/\.[^\.]+$/, '') + '-*'].sort
251             if not alternatives.empty?
252                 dest_img = alternatives[-1]
253             end
254         else
255             push_mousecursor_wait
256             gen_thumbnails_element(from_utf8("#{$current_path}/#{filename}"), $xmldir, false, [ { 'filename' => dest_img, 'size' => $default_size['fullscreen'] } ])
257             pop_mousecursor
258             if !File.exists?(dest_img)
259                 msg 2, _("Could not generate fullscreen thumbnail!")
260                 return
261                 end
262         end
263     end
264     evt = Gtk::EventBox.new.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(Gtk::Frame.new.add(Gtk::Image.new(dest_img)).set_shadow_type(Gtk::SHADOW_ETCHED_OUT)))
265     evt.signal_connect('button-press-event') { |this, event|
266         if event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 1
267             $config['nogestures'] or $gesture_press = { :x => event.x, :y => event.y }
268         end
269         if event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 3
270             menu = Gtk::Menu.new
271             menu.append(delete_item  = Gtk::ImageMenuItem.new(Gtk::Stock::DELETE))
272             delete_item.signal_connect('activate') {
273                 w.destroy
274                 closures[:delete].call
275             }
276             menu.show_all
277             menu.popup(nil, nil, event.button, event.time)
278         end
279     }
280     evt.signal_connect('button-release-event') { |this, event|
281         if $gesture_press
282             if (($gesture_press[:y]-event.y)/($gesture_press[:x]-event.x)).abs > 2 && event.y-$gesture_press[:y] > 5
283                 msg 3, "gesture delete: click-drag right button to the bottom"
284                 w.destroy
285                 closures[:delete].call
286                 $statusbar.push(0, utf8(_("Mouse gesture: delete.")))
287             end
288         end
289     }
290     tooltips = Gtk::Tooltips.new
291     tooltips.set_tip(evt, File.basename(filename).gsub(/\.jpg/, ''), nil)
292
293     w.signal_connect('key-press-event') { |w,event|
294         if event.state & Gdk::Window::CONTROL_MASK != 0 && event.keyval == Gdk::Keyval::GDK_Delete
295             w.destroy
296             closures[:delete].call
297         end
298     }
299
300     bottom = Gtk::Alignment.new(0.5, 0.5, 0, 0).add(b = Gtk::Button.new(Gtk::Stock::CLOSE))
301     b.signal_connect('clicked') { w.destroy }
302
303     vb = Gtk::VBox.new
304     vb.pack_start(evt, false, false)
305     vb.pack_end(bottom, false, false)
306
307     w.add(vb)
308     w.signal_connect('delete-event') { w.destroy }
309     w.window_position = Gtk::Window::POS_CENTER
310     w.show_all
311 end
312
313 def scroll_upper(scrolledwindow, ypos_top)
314     newval = scrolledwindow.vadjustment.value -
315         ((scrolledwindow.vadjustment.value - ypos_top - 1) / scrolledwindow.vadjustment.step_increment + 1) * scrolledwindow.vadjustment.step_increment
316     if newval < scrolledwindow.vadjustment.lower
317         newval = scrolledwindow.vadjustment.lower
318     end
319     scrolledwindow.vadjustment.value = newval
320 end
321
322 def scroll_lower(scrolledwindow, ypos_bottom)
323     newval = scrolledwindow.vadjustment.value +
324         ((ypos_bottom - (scrolledwindow.vadjustment.value + scrolledwindow.vadjustment.page_size) - 1) / scrolledwindow.vadjustment.step_increment + 1) * scrolledwindow.vadjustment.step_increment
325     if newval > scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.page_size
326         newval = scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.page_size
327     end
328     scrolledwindow.vadjustment.value = newval
329 end
330
331 def autoscroll_if_needed(scrolledwindow, image, textview)
332     #- autoscroll if cursor or image is not visible, if possible
333     if image && image.window || textview.window
334         ypos_top = (image && image.window) ? image.window.position[1] : textview.window.position[1]
335         ypos_bottom = max(textview.window.position[1] + textview.window.size[1], image && image.window ? image.window.position[1] + image.window.size[1] : -1)
336         current_miny_visible = scrolledwindow.vadjustment.value
337         current_maxy_visible = scrolledwindow.vadjustment.value + scrolledwindow.vadjustment.page_size
338         if ypos_top < current_miny_visible
339             scroll_upper(scrolledwindow, ypos_top)
340         elsif ypos_bottom > current_maxy_visible
341             scroll_lower(scrolledwindow, ypos_bottom)
342         end
343     end
344 end
345
346 def create_editzone(scrolledwindow, pagenum, image)
347     frame = Gtk::Frame.new
348     frame.add(textview = Gtk::TextView.new.set_wrap_mode(Gtk::TextTag::WRAP_WORD))
349     frame.set_shadow_type(Gtk::SHADOW_IN)
350     textview.signal_connect('key-press-event') { |w, event|
351         textview.set_editable(event.keyval != Gdk::Keyval::GDK_Tab)
352         if event.keyval == Gdk::Keyval::GDK_Page_Up || event.keyval == Gdk::Keyval::GDK_Page_Down
353             scrolledwindow.signal_emit('key-press-event', event)
354         end
355         if (event.keyval == Gdk::Keyval::GDK_Up || event.keyval == Gdk::Keyval::GDK_Down) &&
356            event.state & (Gdk::Window::CONTROL_MASK | Gdk::Window::SHIFT_MASK | Gdk::Window::MOD1_MASK) == 0
357             if event.keyval == Gdk::Keyval::GDK_Up
358                 if scrolledwindow.vadjustment.value >= scrolledwindow.vadjustment.lower + scrolledwindow.vadjustment.step_increment
359                     scrolledwindow.vadjustment.value -= scrolledwindow.vadjustment.step_increment
360                 else
361                     scrolledwindow.vadjustment.value = scrolledwindow.vadjustment.lower
362                 end
363             else
364                 if scrolledwindow.vadjustment.value <= scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.step_increment - scrolledwindow.vadjustment.page_size
365                     scrolledwindow.vadjustment.value += scrolledwindow.vadjustment.step_increment
366                 else
367                     scrolledwindow.vadjustment.value = scrolledwindow.vadjustment.upper - scrolledwindow.vadjustment.page_size
368                 end
369             end
370         end
371         false  #- propagate
372     }
373     textview.signal_connect('focus-in-event') { |w, event|
374         textview.buffer.select_range(textview.buffer.get_iter_at_offset(0), textview.buffer.get_iter_at_offset(-1))
375         false  #- propagate
376     }
377
378     candidate_undo_text = nil
379     textview.signal_connect('focus-in-event') { |w, event|
380         candidate_undo_text = textview.buffer.text
381         false  #- propagate
382     }
383     textview.signal_connect('key-release-event') { |w, event|
384         if candidate_undo_text && candidate_undo_text != textview.buffer.text
385             $modified = true
386             save_undo(_("text edit"),
387                       Proc.new { |text|
388                           save_text = textview.buffer.text
389                           textview.buffer.text = text
390                           textview.grab_focus
391                           $notebook.set_page(pagenum)
392                           Proc.new {
393                               textview.buffer.text = save_text
394                               textview.grab_focus
395                               $notebook.set_page(pagenum)
396                           }
397                       }, candidate_undo_text)
398             candidate_undo_text = nil
399         end
400
401         if event.state != 0 || ![Gdk::Keyval::GDK_Page_Up, Gdk::Keyval::GDK_Page_Down, Gdk::Keyval::GDK_Up, Gdk::Keyval::GDK_Down].include?(event.keyval)
402             autoscroll_if_needed(scrolledwindow, image, textview)
403         end
404         false  #- propagate
405     }
406
407     return [ frame, textview ]
408 end
409
410 def update_shown_pixbuf(thumbnail_img, img, desired_x, desired_y)
411
412     if !$modified_pixbufs[thumbnail_img]
413         $modified_pixbufs[thumbnail_img] = { :orig => img.pixbuf }
414     elsif !$modified_pixbufs[thumbnail_img][:orig]
415         $modified_pixbufs[thumbnail_img][:orig] = img.pixbuf
416     end
417
418     pixbuf = $modified_pixbufs[thumbnail_img][:orig].dup
419
420     #- rotate
421     if $modified_pixbufs[thumbnail_img][:angle_to_orig] && $modified_pixbufs[thumbnail_img][:angle_to_orig] != 0
422         pixbuf = rotate_pixbuf(pixbuf, $modified_pixbufs[thumbnail_img][:angle_to_orig])
423         msg 3, "sizes: #{pixbuf.width} #{pixbuf.height} - desired #{desired_x}x#{desired_x}"
424         if pixbuf.height > desired_y
425             pixbuf = pixbuf.scale(pixbuf.width * (desired_y.to_f/pixbuf.height), desired_y, Gdk::Pixbuf::INTERP_BILINEAR)
426         elsif pixbuf.width < desired_x && pixbuf.height < desired_y
427             pixbuf = pixbuf.scale(desired_x, pixbuf.height * (desired_x.to_f/pixbuf.width), Gdk::Pixbuf::INTERP_BILINEAR)
428         end
429     end
430
431     #- fix white balance
432     if $modified_pixbufs[thumbnail_img][:whitebalance]
433         pixbuf.whitebalance!($modified_pixbufs[thumbnail_img][:whitebalance])
434     end
435
436     img.pixbuf = $modified_pixbufs[thumbnail_img][:pixbuf] = pixbuf
437 end
438
439 def rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
440     $modified = true
441
442     #- update rotate attribute
443     xmlelem.add_attribute("#{attributes_prefix}rotate", (xmlelem.attributes["#{attributes_prefix}rotate"].to_i + angle) % 360)
444
445     $modified_pixbufs[thumbnail_img] ||= {}
446     $modified_pixbufs[thumbnail_img][:angle_to_orig] = (($modified_pixbufs[thumbnail_img][:angle_to_orig] || 0) + angle) % 360
447     msg 3, "angle: #{angle}, angle to orig: #{$modified_pixbufs[thumbnail_img][:angle_to_orig]}"
448
449     update_shown_pixbuf(thumbnail_img, img, desired_x, desired_y)
450 end
451
452 def rotate(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
453     $modified = true
454
455     rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
456
457     save_undo(angle == 90 ? _("rotate clockwise") : angle == -90 ? _("rotate counter-clockwise") : _("flip upside-down"),
458               Proc.new { |angle|
459                   rotate_real(angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
460                   $notebook.set_page(attributes_prefix != '' ? 0 : 1)
461                   Proc.new {
462                       rotate_real(-angle, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y)
463                       $notebook.set_page(0)
464                       $notebook.set_page(attributes_prefix != '' ? 0 : 1)
465                   }
466               }, -angle)
467 end
468
469 def color_swap(xmldir, attributes_prefix)
470     $modified = true
471     if xmldir.attributes["#{attributes_prefix}color-swap"]
472         xmldir.delete_attribute("#{attributes_prefix}color-swap")
473     else
474         xmldir.add_attribute("#{attributes_prefix}color-swap", '1')
475     end
476 end
477
478 def enhance(xmldir, attributes_prefix)
479     $modified = true
480     if xmldir.attributes["#{attributes_prefix}enhance"]
481         xmldir.delete_attribute("#{attributes_prefix}enhance")
482     else
483         xmldir.add_attribute("#{attributes_prefix}enhance", '1')
484     end
485 end
486
487 def change_frame_offset(xmldir, attributes_prefix, value)
488     $modified = true
489     xmldir.add_attribute("#{attributes_prefix}frame-offset", value)
490 end
491
492 def ask_new_frame_offset(xmldir, attributes_prefix)
493     if xmldir
494         value = xmldir.attributes["#{attributes_prefix}frame-offset"]
495     else
496         value = ''
497     end
498
499     dialog = Gtk::Dialog.new(utf8(_("Change frame offset")),
500                              $main_window,
501                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
502                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
503                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
504
505     lbl = Gtk::Label.new
506     lbl.markup = utf8(
507 _("Please specify the <b>frame offset</b> of the video, to take the thumbnail
508 from. There are approximately 25 frames per second in a video.
509 "))
510     dialog.vbox.add(lbl)
511     dialog.vbox.add(entry = Gtk::Entry.new.set_text(value))
512     entry.signal_connect('key-press-event') { |w, event|
513         if event.keyval == Gdk::Keyval::GDK_Return
514             dialog.response(Gtk::Dialog::RESPONSE_OK)
515             true
516         elsif event.keyval == Gdk::Keyval::GDK_Escape
517             dialog.response(Gtk::Dialog::RESPONSE_CANCEL)
518             true
519         else
520             false  #- propagate if needed
521         end
522     }
523     
524     dialog.window_position = Gtk::Window::POS_MOUSE
525     dialog.show_all
526
527     dialog.run { |response|
528         newval = entry.text
529         dialog.destroy
530         if response == Gtk::Dialog::RESPONSE_OK
531             $modified = true
532             msg 3, "changing frame offset to #{newval}"
533             return { :old => value, :new => newval }
534         else
535             return nil
536         end
537     }
538 end
539
540 def change_pano_amount(xmldir, attributes_prefix, value)
541     $modified = true
542     if value.nil?
543         xmldir.delete_attribute("#{attributes_prefix}pano-amount")
544     else
545         xmldir.add_attribute("#{attributes_prefix}pano-amount", value)
546     end
547 end
548
549 def ask_new_pano_amount(xmldir, attributes_prefix)
550     if xmldir
551         value = xmldir.attributes["#{attributes_prefix}pano-amount"]
552     else
553         value = ''
554     end
555
556     dialog = Gtk::Dialog.new(utf8(_("Specify panorama amount")),
557                              $main_window,
558                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
559                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
560                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
561
562     lbl = Gtk::Label.new
563     lbl.markup = utf8(
564 _("Please specify the <b>panorama 'amount'</b> of the image, which indicates the width
565 of this panorama image compared to other regular images. For example, if the panorama
566 was taken out of four photos on one row, counting the necessary overlap, the width of
567 this panorama image should probably be roughly three times the width of regular images.
568
569 With this information, booh will be able to generate panorama thumbnails looking
570 the right 'size'.
571 "))
572     dialog.vbox.add(lbl)
573     dialog.vbox.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(Gtk::HBox.new.add(rb_no = Gtk::RadioButton.new(utf8(_("none (not a panorama image)")))).
574                                                                          add(rb_yes = Gtk::RadioButton.new(rb_no, utf8(_("amount of: ")))).
575                                                                          add(spin = Gtk::SpinButton.new(1, 8, 0.1)).
576                                                                          add(Gtk::Label.new(utf8(_("times the width of other images"))))))
577     dialog.window_position = Gtk::Window::POS_MOUSE
578     dialog.show_all
579     if value
580         spin.value = value.to_f
581         rb_yes.active = true
582         spin.grab_focus
583     else
584         rb_no.active = true
585     end
586
587     dialog.run { |response|
588         if rb_no.active?
589             newval = nil
590         else
591             newval = spin.value.to_f
592         end
593         dialog.destroy
594         if response == Gtk::Dialog::RESPONSE_OK
595             $modified = true
596             msg 3, "changing panorama amount to #{newval}"
597             return { :old => value, :new => newval }
598         else
599             return nil
600         end
601     }
602 end
603
604 def change_whitebalance(xmlelem, attributes_prefix, value)
605     $modified = true
606     xmlelem.add_attribute("#{attributes_prefix}white-balance", value)
607 end
608
609 def recalc_whitebalance(level, orig, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y, infotype)
610
611     #- in case the white balance was already modified in the config file and thumbnail, we cannot just revert, we need to use original file
612     if (!$modified_pixbufs[thumbnail_img] || !$modified_pixbufs[thumbnail_img][:whitebalance]) && xmlelem.attributes["#{attributes_prefix}white-balance"]
613         save_whitebalance = xmlelem.attributes["#{attributes_prefix}white-balance"]
614         xmlelem.delete_attribute("#{attributes_prefix}white-balance")
615         destfile = "#{thumbnail_img}-orig-whitebalance.jpg"
616         gen_real_thumbnail_core(attributes_prefix == '' ? 'element' : 'subdir', orig, destfile,
617                                 xmlelem, attributes_prefix == '' ? $default_size['thumbnails'] : $albums_thumbnail_size, infotype)
618         $modified_pixbufs[thumbnail_img] ||= {}
619         $modified_pixbufs[thumbnail_img][:orig] = pixbuf_or_nil(destfile)
620         xmlelem.add_attribute("#{attributes_prefix}white-balance", save_whitebalance)
621         $modified_pixbufs[thumbnail_img][:angle_to_orig] = 0
622     end
623
624     $modified_pixbufs[thumbnail_img] ||= {}
625     $modified_pixbufs[thumbnail_img][:whitebalance] = level.to_f
626
627     update_shown_pixbuf(thumbnail_img, img, desired_x, desired_y)
628 end
629
630 def ask_whitebalance(orig, thumbnail_img, img_, xmlelem, attributes_prefix, desired_x, desired_y, infotype)
631     #- init $modified_pixbufs correctly
632 #    update_shown_pixbuf(thumbnail_img, img_, desired_x, desired_y)
633
634     value = xmlelem.attributes["#{attributes_prefix}white-balance"] || "0"
635
636     dialog = Gtk::Dialog.new(utf8(_("Fix white balance")),
637                              $main_window,
638                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
639                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
640                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
641
642     lbl = Gtk::Label.new
643     lbl.markup = utf8(
644 _("You can fix the <b>white balance</b> of the image, if your image is too blue
645 or too yellow because your camera didn't detect the light correctly. Drag the
646 slider below the image to the left for more blue, to the right for more yellow.
647 "))
648     dialog.vbox.add(lbl)
649     dialog.vbox.add(evt = Gtk::EventBox.new.add(img = Gtk::Image.new(img_.pixbuf)))
650     dialog.vbox.add(hs = Gtk::HScale.new(-200, 200, 1).set_value(value.to_i))
651     
652     dialog.window_position = Gtk::Window::POS_MOUSE
653     dialog.show_all
654
655     lastval = nil
656     timeout = Gtk.timeout_add(100) {
657         if hs.value != lastval
658             lastval = hs.value
659             recalc_whitebalance(lastval, orig, thumbnail_img, img, xmlelem, attributes_prefix, desired_x, desired_y, infotype)
660         end
661         true
662     }
663
664     dialog.run { |response|
665         Gtk.timeout_remove(timeout)
666         if response == Gtk::Dialog::RESPONSE_OK
667             $modified = true
668             newval = hs.value.to_s
669             msg 3, "changing white balance to #{newval}"
670             dialog.destroy
671             return { :old => value, :new => newval }
672         else
673             $modified_pixbufs[thumbnail_img] ||= {}
674             $modified_pixbufs[thumbnail_img][:whitebalance] = value.to_f
675             $modified_pixbufs[thumbnail_img][:pixbuf] = img_.pixbuf
676             dialog.destroy
677             return nil
678         end
679     }
680 end
681
682 def gen_real_thumbnail_core(type, origfile, destfile, xmldir, size, infotype)
683     system("rm -f '#{destfile}'")
684     #- type can be 'element' or 'subdir'
685     if type == 'element'
686         gen_thumbnails_element(origfile, xmldir, false, [ { 'filename' => destfile, 'size' => size } ])
687     else
688         gen_thumbnails_subdir(origfile, xmldir, false, [ { 'filename' => destfile, 'size' => size } ], infotype)
689     end
690 end
691
692 def gen_real_thumbnail(type, origfile, destfile, xmldir, size, img, infotype)
693     Thread.new {
694         push_mousecursor_wait
695         gen_real_thumbnail_core(type, origfile, destfile, xmldir, size, infotype)
696         gtk_thread_protect {
697             puts "destroyed: " + img.destroyed?.to_s
698             img.set(destfile)
699             $modified_pixbufs[destfile] = { :orig => img.pixbuf, :pixbuf => img.pixbuf, :angle_to_orig => 0 }
700         }
701         pop_mousecursor
702     }
703 end
704
705 def popup_thumbnail_menu(event, optionals, type, xmldir, attributes_prefix, possible_actions, closures)
706     distribute_multiple_call = Proc.new { |action, arg|
707         $selected_elements.each_key { |path|
708             $name2widgets[path][:img].pixbuf = $selected_elements[path][:pixbuf]
709         }
710         if possible_actions[:can_multiple] && $selected_elements.length > 0
711             UndoHandler.begin_batch
712             $selected_elements.each_key { |k| $name2closures[k][action].call(arg) }
713             UndoHandler.end_batch
714         else
715             closures[action].call(arg)
716         end
717         $selected_elements = {}
718     }
719     menu = Gtk::Menu.new
720     if optionals.include?('change_image')
721         menu.append(changeimg = Gtk::ImageMenuItem.new(utf8(_("Change image"))))
722         changeimg.image = Gtk::Image.new("#{$FPATH}/images/stock-images-16.png")
723         changeimg.signal_connect('activate') { closures[:change].call }
724         menu.append(            Gtk::SeparatorMenuItem.new)
725     end
726     if closures[:view]
727         if type == 'image'
728             menu.append(view = Gtk::ImageMenuItem.new(utf8(_("View larger"))))
729             view.image = Gtk::Image.new("#{$FPATH}/images/stock-view-16.png")
730             view.signal_connect('activate') { closures[:view].call }
731             menu.append(               Gtk::SeparatorMenuItem.new)
732         else
733             menu.append(view = Gtk::ImageMenuItem.new(utf8(_("Play video"))))
734             view.image = Gtk::Image.new("#{$FPATH}/images/stock-video-16.png")
735             view.signal_connect('activate') { closures[:view].call }
736             menu.append(               Gtk::SeparatorMenuItem.new)
737         end
738     end
739     menu.append(r90 = Gtk::ImageMenuItem.new(utf8(_("Rotate clockwise"))))
740     r90.image = Gtk::Image.new("#{$FPATH}/images/stock-rotate-90-16.png")
741     r90.signal_connect('activate') { distribute_multiple_call.call(:rotate, 90) }
742     menu.append(r270 = Gtk::ImageMenuItem.new(utf8(_("Rotate counter-clockwise"))))
743     r270.image = Gtk::Image.new("#{$FPATH}/images/stock-rotate-270-16.png")
744     r270.signal_connect('activate') { distribute_multiple_call.call(:rotate, -90) }
745     if !possible_actions[:can_multiple] || $selected_elements.length == 0
746         menu.append(               Gtk::SeparatorMenuItem.new)
747         if !possible_actions[:forbid_left]
748             menu.append(moveleft = Gtk::ImageMenuItem.new(utf8(_("Move left"))))
749             moveleft.image = Gtk::Image.new("#{$FPATH}/images/stock-move-left.png")
750             moveleft.signal_connect('activate') { closures[:move].call('left') }
751             if !possible_actions[:can_left]
752                 moveleft.sensitive = false
753             end
754         end
755         if !possible_actions[:forbid_right]
756             menu.append(moveright = Gtk::ImageMenuItem.new(utf8(_("Move right"))))
757             moveright.image = Gtk::Image.new("#{$FPATH}/images/stock-move-right.png")
758             moveright.signal_connect('activate') { closures[:move].call('right') }
759             if !possible_actions[:can_right]
760                 moveright.sensitive = false
761             end
762         end
763         menu.append(moveup = Gtk::ImageMenuItem.new(utf8(_("Move up"))))
764         moveup.image = Gtk::Image.new("#{$FPATH}/images/stock-move-up.png")
765         moveup.signal_connect('activate') { closures[:move].call('up') }
766         if !possible_actions[:can_up]
767             moveup.sensitive = false
768         end
769         menu.append(movedown = Gtk::ImageMenuItem.new(utf8(_("Move down"))))
770         movedown.image = Gtk::Image.new("#{$FPATH}/images/stock-move-down.png")
771         movedown.signal_connect('activate') { closures[:move].call('down') }
772         if !possible_actions[:can_down]
773             movedown.sensitive = false
774         end
775     end
776     if type == 'video'
777         if !possible_actions[:can_multiple] || $selected_elements.length == 0 || $selected_elements.reject { |k,v| $name2widgets[k][:type] == 'video' }.empty?
778             menu.append(               Gtk::SeparatorMenuItem.new)
779             menu.append(  color_swap = Gtk::ImageMenuItem.new(utf8(_("Red/blue color swap"))))
780             color_swap.image = Gtk::Image.new("#{$FPATH}/images/stock-color-triangle-16.png")
781             color_swap.signal_connect('activate') { distribute_multiple_call.call(:color_swap) }
782             menu.append(        flip = Gtk::ImageMenuItem.new(utf8(_("Flip upside-down"))))
783             flip.image = Gtk::Image.new("#{$FPATH}/images/stock-rotate-180-16.png")
784             flip.signal_connect('activate') { distribute_multiple_call.call(:rotate, 180) }
785             menu.append(frame_offset = Gtk::ImageMenuItem.new(utf8(_("Specify frame offset"))))
786             frame_offset.image = Gtk::Image.new("#{$FPATH}/images/stock-video-16.png")
787             frame_offset.signal_connect('activate') {
788                 if possible_actions[:can_multiple] && $selected_elements.length > 0
789                     if values = ask_new_frame_offset(nil, '')
790                         distribute_multiple_call.call(:frame_offset, values)
791                     end
792                 else
793                     closures[:frame_offset].call
794                 end
795             }
796         end
797     end
798     menu.append(               Gtk::SeparatorMenuItem.new)
799     if !possible_actions[:can_multiple] || $selected_elements.length == 0
800         menu.append(whitebalance = Gtk::ImageMenuItem.new(utf8(_("Fix white-balance"))))
801         whitebalance.image = Gtk::Image.new("#{$FPATH}/images/stock-tool-color-balance-16.png")
802         whitebalance.signal_connect('activate') { closures[:whitebalance].call }
803     end
804     if !possible_actions[:can_multiple] || $selected_elements.length == 0
805         menu.append(enhance = Gtk::ImageMenuItem.new(utf8(xmldir.attributes["#{attributes_prefix}enhance"] ? _("Original contrast") :
806                                                                                                              _("Enhance constrast"))))
807     else
808         menu.append(enhance = Gtk::ImageMenuItem.new(utf8(_("Toggle contrast enhancement"))))
809     end
810     enhance.image = Gtk::Image.new("#{$FPATH}/images/stock-channels-16.png")
811     enhance.signal_connect('activate') { distribute_multiple_call.call(:enhance) }
812     if type == 'image' && possible_actions[:can_panorama]
813         menu.append(panorama = Gtk::ImageMenuItem.new(utf8(_("Set as panorama"))))
814         panorama.image = Gtk::Image.new("#{$FPATH}/images/stock-images-16.png")
815         panorama.signal_connect('activate') { closures[:pano].call }
816     end
817     if optionals.include?('delete')
818         menu.append(               Gtk::SeparatorMenuItem.new)
819         menu.append(cut_item     = Gtk::ImageMenuItem.new(Gtk::Stock::CUT))
820         cut_item.signal_connect('activate') { distribute_multiple_call.call(:cut) }
821         if !possible_actions[:can_multiple] || $selected_elements.length == 0
822             menu.append(paste_item   = Gtk::ImageMenuItem.new(Gtk::Stock::PASTE))
823             paste_item.signal_connect('activate') { closures[:paste].call }
824             menu.append(clear_item   = Gtk::ImageMenuItem.new(Gtk::Stock::CLEAR))
825             clear_item.signal_connect('activate') { $cuts = [] }
826             if $cuts.size == 0
827                 paste_item.sensitive = clear_item.sensitive = false
828             end
829         end
830         menu.append(               Gtk::SeparatorMenuItem.new)
831         menu.append(delete_item  = Gtk::ImageMenuItem.new(Gtk::Stock::DELETE))
832         delete_item.signal_connect('activate') { distribute_multiple_call.call(:delete) }
833     end
834     menu.show_all
835     menu.popup(nil, nil, event.button, event.time)
836 end
837
838 def add_thumbnail(autotable, filename, type, thumbnail_img, caption)
839
840     img = nil
841     frame1 = Gtk::Frame.new
842     fullpath = from_utf8("#{$current_path}/#{filename}")
843
844     my_gen_real_thumbnail = proc {
845         gen_real_thumbnail('element', fullpath, thumbnail_img, $xmldir, $default_size['thumbnails'], img, '')
846     }
847
848     #- generate the thumbnail if missing (if image was rotated but booh was not relaunched)
849     if !$modified_pixbufs[thumbnail_img] && !File.exists?(thumbnail_img)
850         frame1.add(img = Gtk::Image.new)
851         my_gen_real_thumbnail.call
852     else
853         frame1.add(img = Gtk::Image.new($modified_pixbufs[thumbnail_img] ? $modified_pixbufs[thumbnail_img][:pixbuf] : thumbnail_img))
854     end
855     evtbox = Gtk::EventBox.new.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(frame1.set_shadow_type(Gtk::SHADOW_ETCHED_OUT)))
856
857     tooltips = Gtk::Tooltips.new
858     tipname = from_utf8(File.basename(filename).gsub(/\.[^\.]+$/, ''))
859     tooltips.set_tip(evtbox, utf8(type == 'video' ? (_("%s (video - %s KB)") % [tipname, commify(file_size(fullpath)/1024)]) : tipname), nil)
860
861     frame2, textview = create_editzone($autotable_sw, 1, img)
862     textview.buffer.text = utf8(caption)
863     textview.set_justification(Gtk::Justification::CENTER)
864
865     vbox = Gtk::VBox.new(false, 5)
866     vbox.pack_start(evtbox, false, false)
867     vbox.pack_start(frame2, false, false)
868     autotable.append(vbox, filename)
869
870     #- to be able to grab focus of textview when retrieving vbox's position from AutoTable
871     $vbox2widgets[vbox] = { :textview => textview, :image => img }
872
873     #- to be able to find widgets by name
874     $name2widgets[filename] = { :textview => textview, :evtbox => evtbox, :vbox => vbox, :img => img, :type => type }
875
876     cleanup_all_thumbnails = Proc.new {
877         #- remove out of sync images
878         dest_img_base = build_full_dest_filename(filename).sub(/\.[^\.]+$/, '')
879         for sizeobj in $images_size
880             system("rm -f #{dest_img_base}-#{sizeobj['fullscreen']}.jpg #{dest_img_base}-#{sizeobj['thumbnails']}.jpg")
881         end
882
883     }
884
885     rotate_and_cleanup = Proc.new { |angle|
886         rotate(angle, thumbnail_img, img, $xmldir.elements["*[@filename='#{filename}']"], '', $default_thumbnails[:x], $default_thumbnails[:y])
887         cleanup_all_thumbnails.call
888     }
889
890     move = Proc.new { |direction|
891         do_method = "move_#{direction}"
892         undo_method = "move_" + case direction; when 'left'; 'right'; when 'right'; 'left'; when 'up'; 'down'; when 'down'; 'up' end
893         perform = Proc.new {
894             done = autotable.method(do_method).call(vbox)
895             textview.grab_focus  #- because if moving, focus is stolen
896             done
897         }
898         if perform.call
899             save_undo(_("move %s") % direction,
900                       Proc.new {
901                           autotable.method(undo_method).call(vbox)
902                           textview.grab_focus  #- because if moving, focus is stolen
903                           autoscroll_if_needed($autotable_sw, img, textview)
904                           $notebook.set_page(1)
905                           Proc.new {
906                               autotable.method(do_method).call(vbox)
907                               textview.grab_focus  #- because if moving, focus is stolen
908                               autoscroll_if_needed($autotable_sw, img, textview)
909                               $notebook.set_page(1)
910                           }
911                       })
912         end
913     }
914
915     color_swap_and_cleanup = Proc.new {
916         perform_color_swap_and_cleanup = Proc.new {
917             color_swap($xmldir.elements["*[@filename='#{filename}']"], '')
918             my_gen_real_thumbnail.call
919         }
920
921         cleanup_all_thumbnails.call
922         perform_color_swap_and_cleanup.call
923
924         save_undo(_("color swap"),
925                   Proc.new {
926                       perform_color_swap_and_cleanup.call
927                       textview.grab_focus
928                       autoscroll_if_needed($autotable_sw, img, textview)
929                       $notebook.set_page(1)
930                       Proc.new {
931                           perform_color_swap_and_cleanup.call
932                           textview.grab_focus
933                           autoscroll_if_needed($autotable_sw, img, textview)
934                           $notebook.set_page(1)
935                       }
936                   })
937     }
938
939     change_frame_offset_and_cleanup_real = Proc.new { |values|
940         perform_change_frame_offset_and_cleanup = Proc.new { |val|
941             change_frame_offset($xmldir.elements["*[@filename='#{filename}']"], '', val)
942             my_gen_real_thumbnail.call
943         }
944         perform_change_frame_offset_and_cleanup.call(values[:new])
945         
946         save_undo(_("specify frame offset"),
947                   Proc.new {
948                       perform_change_frame_offset_and_cleanup.call(values[:old])
949                       textview.grab_focus
950                       autoscroll_if_needed($autotable_sw, img, textview)
951                       $notebook.set_page(1)
952                       Proc.new {
953                           perform_change_frame_offset_and_cleanup.call(values[:new])
954                           textview.grab_focus
955                           autoscroll_if_needed($autotable_sw, img, textview)
956                           $notebook.set_page(1)
957                       }
958                   })
959     }
960
961     change_frame_offset_and_cleanup = Proc.new {
962         if values = ask_new_frame_offset($xmldir.elements["*[@filename='#{filename}']"], '')
963             change_frame_offset_and_cleanup_real.call(values)
964         end
965     }
966
967     change_pano_amount_and_cleanup_real = Proc.new { |values|
968         perform_change_pano_amount_and_cleanup = Proc.new { |val|
969             change_pano_amount($xmldir.elements["*[@filename='#{filename}']"], '', val)
970         }
971         perform_change_pano_amount_and_cleanup.call(values[:new])
972         
973         save_undo(_("change panorama amount"),
974                   Proc.new {
975                       perform_change_pano_amount_and_cleanup.call(values[:old])
976                       textview.grab_focus
977                       autoscroll_if_needed($autotable_sw, img, textview)
978                       $notebook.set_page(1)
979                       Proc.new {
980                           perform_change_pano_amount_and_cleanup.call(values[:new])
981                           textview.grab_focus
982                           autoscroll_if_needed($autotable_sw, img, textview)
983                           $notebook.set_page(1)
984                       }
985                   })
986     }
987
988     change_pano_amount_and_cleanup = Proc.new {
989         if values = ask_new_pano_amount($xmldir.elements["*[@filename='#{filename}']"], '')
990             change_pano_amount_and_cleanup_real.call(values)
991         end
992     }
993
994     whitebalance_and_cleanup = Proc.new {
995         if values = ask_whitebalance(fullpath, thumbnail_img, img,
996                                      $xmldir.elements["*[@filename='#{filename}']"], '', $default_thumbnails[:x], $default_thumbnails[:y], '')
997             perform_change_whitebalance_and_cleanup = Proc.new { |val|
998                 change_whitebalance($xmldir.elements["*[@filename='#{filename}']"], '', val)
999                 recalc_whitebalance(val, fullpath, thumbnail_img, img,
1000                                     $xmldir.elements["*[@filename='#{filename}']"], '', $default_thumbnails[:x], $default_thumbnails[:y], '')
1001                 cleanup_all_thumbnails.call
1002             }
1003             perform_change_whitebalance_and_cleanup.call(values[:new])
1004
1005             save_undo(_("fix white balance"),
1006                       Proc.new {
1007                           perform_change_whitebalance_and_cleanup.call(values[:old])
1008                           textview.grab_focus
1009                           autoscroll_if_needed($autotable_sw, img, textview)
1010                           $notebook.set_page(1)
1011                           Proc.new {
1012                               perform_change_whitebalance_and_cleanup.call(values[:new])
1013                               textview.grab_focus
1014                               autoscroll_if_needed($autotable_sw, img, textview)
1015                               $notebook.set_page(1)
1016                           }
1017                       })
1018         end
1019     }
1020
1021     enhance_and_cleanup = Proc.new {
1022         perform_enhance_and_cleanup = Proc.new {
1023             enhance($xmldir.elements["*[@filename='#{filename}']"], '')
1024             my_gen_real_thumbnail.call
1025         }
1026
1027         cleanup_all_thumbnails.call
1028         perform_enhance_and_cleanup.call
1029
1030         save_undo(_("enhance"),
1031                   Proc.new {
1032                       perform_enhance_and_cleanup.call
1033                       textview.grab_focus
1034                       autoscroll_if_needed($autotable_sw, img, textview)
1035                       $notebook.set_page(1)
1036                       Proc.new {
1037                           perform_enhance_and_cleanup.call
1038                           textview.grab_focus
1039                           autoscroll_if_needed($autotable_sw, img, textview)
1040                           $notebook.set_page(1)
1041                       }
1042                   })
1043     }
1044
1045     delete = Proc.new { |isacut|
1046         if autotable.current_order.size > 1 || show_popup($main_window, utf8(_("Do you confirm this subalbum needs to be completely removed?")), { :okcancel => true })
1047             $modified = true
1048             after = nil
1049             perform_delete = Proc.new {
1050                 after = autotable.get_next_widget(vbox)
1051                 if !after
1052                     after = autotable.get_previous_widget(vbox)
1053                 end
1054                 if $config['deleteondisk'] && !isacut
1055                     msg 3, "scheduling for delete: #{fullpath}"
1056                     $todelete << fullpath
1057                 end
1058                 autotable.remove(vbox)
1059                 if after
1060                     $vbox2widgets[after][:textview].grab_focus
1061                     autoscroll_if_needed($autotable_sw, $vbox2widgets[after][:image], $vbox2widgets[after][:textview])
1062                 end
1063             }
1064             
1065             previous_pos = autotable.get_current_number(vbox)
1066             perform_delete.call
1067
1068             if !after
1069                 if $xmldir.child_byname_notattr('dir', 'deleted')
1070                     $xmldir.delete_attribute('thumbnails-caption')
1071                     $xmldir.delete_attribute('thumbnails-captionfile')
1072                 else
1073                     $xmldir.add_attribute('deleted', 'true')
1074                     moveup = $xmldir
1075                     while moveup.parent.name == 'dir'
1076                         moveup = moveup.parent
1077                         if !moveup.child_byname_notattr('dir', 'deleted') && !moveup.child_byname_notattr('image', 'deleted') && !moveup.child_byname_notattr('video', 'deleted')
1078                             moveup.add_attribute('deleted', 'true')
1079                         else
1080                             break
1081                         end
1082                     end
1083                 end
1084                 save_changes('forced')
1085                 populate_subalbums_treeview
1086             else
1087                 save_undo(_("delete"),
1088                           Proc.new { |pos|
1089                               autotable.reinsert(pos, vbox, filename)
1090                               $notebook.set_page(1)
1091                               autotable.queue_draws << proc { textview.grab_focus; autoscroll_if_needed($autotable_sw, img, textview) }
1092                               $cuts = []
1093                               msg 3, "removing deletion schedule of: #{fullpath}"
1094                               $todelete.delete(fullpath)  #- unconditional because deleteondisk option could have been modified
1095                               Proc.new {
1096                                   perform_delete.call
1097                                   $notebook.set_page(1)
1098                               }
1099                           }, previous_pos)
1100             end
1101         end
1102     }
1103
1104     cut = Proc.new {
1105         delete.call(true)
1106         $cuts << { :vbox => vbox, :filename => filename }
1107         $statusbar.push(0, utf8(_("%s elements in the clipboard.") % $cuts.size ))
1108     }
1109     paste = Proc.new {
1110         if $cuts.size > 0
1111             $cuts.each { |elem|
1112                 autotable.reinsert(autotable.get_current_number(vbox), elem[:vbox], elem[:filename])
1113             }
1114             last = $cuts[-1]
1115             autotable.queue_draws << proc {
1116                 $vbox2widgets[last[:vbox]][:textview].grab_focus
1117                 autoscroll_if_needed($autotable_sw, $vbox2widgets[last[:vbox]][:image], $vbox2widgets[last[:vbox]][:textview])
1118             }
1119             save_undo(_("paste"),
1120                       Proc.new { |cuts|
1121                           cuts.each { |elem| autotable.remove(elem[:vbox]) }
1122                           $notebook.set_page(1)
1123                           Proc.new {
1124                               cuts.each { |elem|
1125                                   autotable.reinsert(autotable.get_current_number(vbox), elem[:vbox], elem[:filename])
1126                               }
1127                               $notebook.set_page(1)
1128                           }
1129                       }, $cuts)
1130             $statusbar.push(0, utf8(_("Pasted %s elements.") % $cuts.size ))
1131             $cuts = []
1132         end
1133     }
1134
1135     $name2closures[filename] = { :rotate => rotate_and_cleanup, :enhance => enhance_and_cleanup, :delete => delete, :cut => cut,
1136                                  :color_swap => color_swap_and_cleanup, :frame_offset => change_frame_offset_and_cleanup_real,
1137                                  :pano => change_pano_amount_and_cleanup }
1138
1139     textview.signal_connect('key-press-event') { |w, event|
1140         propagate = true
1141         if event.state != 0
1142             x, y = autotable.get_current_pos(vbox)
1143             control_pressed = event.state & Gdk::Window::CONTROL_MASK != 0
1144             shift_pressed = event.state & Gdk::Window::SHIFT_MASK != 0
1145             alt_pressed = event.state & Gdk::Window::MOD1_MASK != 0
1146             if event.keyval == Gdk::Keyval::GDK_Up && y > 0
1147                 if control_pressed
1148                     if widget_up = autotable.get_widget_at_pos(x, y - 1)
1149                         $vbox2widgets[widget_up][:textview].grab_focus
1150                     end
1151                 end
1152                 if shift_pressed
1153                     move.call('up')
1154                 end
1155             end
1156             if event.keyval == Gdk::Keyval::GDK_Down && y < autotable.get_max_y
1157                 if control_pressed
1158                     if widget_down = autotable.get_widget_at_pos(x, y + 1)
1159                         $vbox2widgets[widget_down][:textview].grab_focus
1160                     end
1161                 end
1162                 if shift_pressed
1163                     move.call('down')
1164                 end
1165             end
1166             if event.keyval == Gdk::Keyval::GDK_Left
1167                 if x > 0
1168                     if control_pressed
1169                         $vbox2widgets[autotable.get_previous_widget(vbox)][:textview].grab_focus
1170                     end
1171                     if shift_pressed
1172                         move.call('left')
1173                     end
1174                 end
1175                 if alt_pressed
1176                     rotate_and_cleanup.call(-90)
1177                 end
1178             end
1179             if event.keyval == Gdk::Keyval::GDK_Right
1180                 next_ = autotable.get_next_widget(vbox)
1181                 if next_ && autotable.get_current_pos(next_)[0] > x
1182                     if control_pressed
1183                         $vbox2widgets[next_][:textview].grab_focus
1184                     end
1185                     if shift_pressed
1186                         move.call('right')
1187                     end
1188                 end
1189                 if alt_pressed
1190                     rotate_and_cleanup.call(90)
1191                 end
1192             end
1193             if event.keyval == Gdk::Keyval::GDK_Delete && control_pressed
1194                 delete.call(false)
1195             end
1196             if event.keyval == Gdk::Keyval::GDK_Return && control_pressed
1197                 view_element(filename, { :delete => delete })
1198                 propagate = false
1199             end
1200             if event.keyval == Gdk::Keyval::GDK_z && control_pressed
1201                 perform_undo
1202             end
1203             if event.keyval == Gdk::Keyval::GDK_r && control_pressed
1204                 perform_redo
1205             end
1206         end
1207         !propagate  #- propagate if needed
1208     }
1209
1210     $ignore_next_release = false
1211     evtbox.signal_connect('button-press-event') { |w, event|
1212         if event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 1
1213             if event.state & Gdk::Window::BUTTON3_MASK != 0
1214                 #- gesture redo: hold right mouse button then click left mouse button
1215                 $config['nogestures'] or perform_redo
1216                 $ignore_next_release = true
1217             else
1218                 shift_or_control = event.state & Gdk::Window::SHIFT_MASK != 0 || event.state & Gdk::Window::CONTROL_MASK != 0
1219                 if $r90.active?
1220                     rotate_and_cleanup.call(shift_or_control ? -90 : 90)
1221                 elsif $r270.active?
1222                     rotate_and_cleanup.call(shift_or_control ? 90 : -90)
1223                 elsif $enhance.active?
1224                     enhance_and_cleanup.call
1225                 elsif $delete.active?
1226                     delete.call(false)
1227                 else
1228                     textview.grab_focus
1229                     $config['nogestures'] or $gesture_press = { :filename => filename, :x => event.x, :y => event.y }
1230                 end
1231             end
1232             $button1_pressed_autotable = true
1233         elsif event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 3
1234             if event.state & Gdk::Window::BUTTON1_MASK != 0
1235                 #- gesture undo: hold left mouse button then click right mouse button
1236                 $config['nogestures'] or perform_undo
1237                 $ignore_next_release = true
1238             end
1239         elsif event.event_type == Gdk::Event::BUTTON2_PRESS && event.button == 1
1240             view_element(filename, { :delete => delete })
1241         end
1242         false   #- propagate
1243     }
1244
1245     evtbox.signal_connect('button-release-event') { |w, event|
1246         if event.event_type == Gdk::Event::BUTTON_RELEASE && event.button == 3
1247             if !$ignore_next_release
1248                 x, y = autotable.get_current_pos(vbox)
1249                 next_ = autotable.get_next_widget(vbox)
1250                 popup_thumbnail_menu(event, ['delete'], type, $xmldir.elements["*[@filename='#{filename}']"], '',
1251                                      { :can_left => x > 0, :can_right => next_ && autotable.get_current_pos(next_)[0] > x,
1252                                        :can_up => y > 0, :can_down => y < autotable.get_max_y, :can_multiple => true, :can_panorama => true },
1253                                      { :rotate => rotate_and_cleanup, :move => move, :color_swap => color_swap_and_cleanup, :enhance => enhance_and_cleanup,
1254                                        :frame_offset => change_frame_offset_and_cleanup, :delete => delete, :whitebalance => whitebalance_and_cleanup,
1255                                        :cut => cut, :paste => paste, :view => proc { view_element(filename, { :delete => delete }) },
1256                                        :pano => change_pano_amount_and_cleanup })
1257             end
1258             $ignore_next_release = false
1259             $gesture_press = nil
1260         end
1261         false   #- propagate
1262     }
1263
1264     #- handle reordering with drag and drop
1265     Gtk::Drag.source_set(vbox, Gdk::Window::BUTTON1_MASK, [['reorder-elements', Gtk::Drag::TARGET_SAME_APP, 1]], Gdk::DragContext::ACTION_MOVE)
1266     Gtk::Drag.dest_set(vbox, Gtk::Drag::DEST_DEFAULT_ALL, [['reorder-elements', Gtk::Drag::TARGET_SAME_APP, 1]], Gdk::DragContext::ACTION_MOVE)
1267     vbox.signal_connect('drag-data-get') { |w, ctxt, selection_data, info, time|
1268         selection_data.set(Gdk::Selection::TYPE_STRING, autotable.get_current_number(vbox).to_s)
1269     }
1270
1271     vbox.signal_connect('drag-data-received') { |w, ctxt, x, y, selection_data, info, time|
1272         done = false
1273         #- mouse gesture first (dnd disables button-release-event)
1274         if $gesture_press && $gesture_press[:filename] == filename
1275             if (($gesture_press[:x]-x)/($gesture_press[:y]-y)).abs > 2 && ($gesture_press[:x]-x).abs > 5
1276                 angle = x-$gesture_press[:x] > 0 ? 90 : -90
1277                 msg 3, "gesture rotate: #{angle}: click-drag right button to the left/right"
1278                 rotate_and_cleanup.call(angle)
1279                 $statusbar.push(0, utf8(_("Mouse gesture: rotate.")))
1280                 done = true
1281             elsif (($gesture_press[:y]-y)/($gesture_press[:x]-x)).abs > 2 && y-$gesture_press[:y] > 5
1282                 msg 3, "gesture delete: click-drag right button to the bottom"
1283                 delete.call(false)
1284                 $statusbar.push(0, utf8(_("Mouse gesture: delete.")))
1285                 done = true
1286             end
1287         end
1288         if !done
1289             ctxt.targets.each { |target|
1290                 if target.name == 'reorder-elements'
1291                     move_dnd = Proc.new { |from,to|
1292                         if from != to
1293                             $modified = true
1294                             autotable.move(from, to)
1295                             save_undo(_("reorder"),
1296                                       Proc.new { |from, to|
1297                                           if to > from
1298                                               autotable.move(to - 1, from)
1299                                           else
1300                                               autotable.move(to, from + 1)
1301                                           end
1302                                           $notebook.set_page(1)
1303                                           Proc.new {
1304                                               autotable.move(from, to)
1305                                               $notebook.set_page(1)
1306                                           }
1307                                       }, from, to)
1308                         end
1309                     }
1310                     if $multiple_dnd.size == 0
1311                         move_dnd.call(selection_data.data.to_i,
1312                                       autotable.get_current_number(vbox))
1313                     else
1314                         UndoHandler.begin_batch
1315                         $multiple_dnd.sort { |a,b| autotable.get_current_number($name2widgets[a][:vbox]) <=> autotable.get_current_number($name2widgets[b][:vbox]) }.
1316                                       each { |path|
1317                             #- need to update current position between each call
1318                             move_dnd.call(autotable.get_current_number($name2widgets[path][:vbox]),
1319                                           autotable.get_current_number(vbox))
1320                         }
1321                         UndoHandler.end_batch
1322                     end
1323                     $multiple_dnd = []
1324                 end
1325             }
1326         end
1327     }
1328
1329     vbox.show_all
1330 end
1331
1332 def create_auto_table
1333
1334     $autotable = Gtk::AutoTable.new(5)
1335
1336     $autotable_sw = Gtk::ScrolledWindow.new(nil, nil)
1337     thumbnails_vb = Gtk::VBox.new(false, 5)
1338
1339     frame, $thumbnails_title = create_editzone($autotable_sw, 0, nil)
1340     $thumbnails_title.set_justification(Gtk::Justification::CENTER)
1341     thumbnails_vb.pack_start(Gtk::Alignment.new(0.5, 0.5, 0.5, 0).add(frame), false, false)
1342     thumbnails_vb.add($autotable)
1343
1344     $autotable_sw.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS)
1345     $autotable_sw.add_with_viewport(thumbnails_vb)
1346
1347     #- follows stuff for handling multiple elements selection
1348     press_x = nil; press_y = nil; pos_x = nil; pos_y = nil; $selected_elements = {}
1349     gc = nil
1350     update_selected = Proc.new {
1351         $autotable.current_order.each { |path|
1352             w = $name2widgets[path][:evtbox].window
1353             xm = w.position[0] + w.size[0]/2
1354             ym = w.position[1] + w.size[1]/2
1355             if ym < press_y && ym > pos_y || ym < pos_y && ym > press_y
1356                 if (xm < press_x && xm > pos_x || xm < pos_x && xm > press_x) && ! $selected_elements[path]
1357                     $selected_elements[path] = { :pixbuf => $name2widgets[path][:img].pixbuf }
1358                     $name2widgets[path][:img].pixbuf = $name2widgets[path][:img].pixbuf.saturate_and_pixelate(1, true)
1359                 end
1360             end
1361             if $selected_elements[path] && ! $selected_elements[path][:keep]
1362                 if ((xm < press_x && xm < pos_x || xm > pos_x && xm > press_x) || (ym < press_y && ym < pos_y || ym > pos_y && ym > press_y))
1363                     $name2widgets[path][:img].pixbuf = $selected_elements[path][:pixbuf]
1364                     $selected_elements.delete(path)
1365                 end
1366             end
1367         }
1368     }
1369     $autotable.signal_connect('realize') { |w,e|
1370         gc = Gdk::GC.new($autotable.window)
1371         gc.set_line_attributes(1, Gdk::GC::LINE_ON_OFF_DASH, Gdk::GC::CAP_PROJECTING, Gdk::GC::JOIN_ROUND)
1372         gc.function = Gdk::GC::INVERT
1373         #- autoscroll handling for DND and multiple selections
1374         Gtk.timeout_add(100) {
1375             w, x, y, mask = $autotable.window.pointer
1376             if mask & Gdk::Window::BUTTON1_MASK != 0
1377                 if y < $autotable_sw.vadjustment.value
1378                     if pos_x
1379                         $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1380                     end
1381                     if $button1_pressed_autotable || press_x
1382                         scroll_upper($autotable_sw, y)
1383                     end
1384                     if not press_x.nil?
1385                         w, pos_x, pos_y = $autotable.window.pointer
1386                         $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1387                         update_selected.call
1388                     end
1389                 end
1390                 if y > $autotable_sw.vadjustment.value + $autotable_sw.vadjustment.page_size
1391                     if pos_x
1392                         $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1393                     end
1394                     if $button1_pressed_autotable || press_x
1395                         scroll_lower($autotable_sw, y)
1396                     end
1397                     if not press_x.nil?
1398                         w, pos_x, pos_y = $autotable.window.pointer
1399                         $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1400                         update_selected.call
1401                     end
1402                 end
1403             end
1404             true
1405         }
1406     }
1407
1408     $autotable.signal_connect('button-press-event') { |w,e|
1409         if e.button == 1
1410             if !$button1_pressed_autotable
1411                 press_x = e.x
1412                 press_y = e.y
1413                 if e.state & Gdk::Window::SHIFT_MASK == 0
1414                     $selected_elements.each_key { |path| $name2widgets[path][:img].pixbuf = $selected_elements[path][:pixbuf] }
1415                     $selected_elements = {}
1416                     $statusbar.push(0, utf8(_("Nothing selected.")))
1417                 else
1418                     $selected_elements.each_key { |path| $selected_elements[path][:keep] = true }
1419                 end
1420                 set_mousecursor(Gdk::Cursor::TCROSS)
1421             end
1422         end
1423     }
1424     $autotable.signal_connect('button-release-event') { |w,e|
1425         if e.button == 1
1426             if $button1_pressed_autotable
1427                 #- unselect all only now
1428                 $multiple_dnd = $selected_elements.keys
1429                 $selected_elements.each_key { |path| $name2widgets[path][:img].pixbuf = $selected_elements[path][:pixbuf] }
1430                 $selected_elements = {}
1431                 $button1_pressed_autotable = false
1432             else
1433                 if pos_x
1434                     $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1435                     if $selected_elements.length > 0
1436                         $statusbar.push(0, utf8(_("%s elements selected.") % $selected_elements.length))
1437                     end
1438                 end
1439                 press_x = press_y = pos_x = pos_y = nil
1440                 set_mousecursor(Gdk::Cursor::LEFT_PTR)
1441             end
1442         end
1443     }
1444     $autotable.signal_connect('motion-notify-event') { |w,e|
1445         if ! press_x.nil?
1446             if pos_x
1447                 $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1448             end
1449             pos_x = e.x
1450             pos_y = e.y
1451             $autotable.window.draw_lines(gc, [[press_x, press_y], [pos_x, press_y], [pos_x, pos_y], [press_x, pos_y], [press_x, press_y]])
1452             update_selected.call
1453         end
1454     }
1455
1456 end
1457
1458 def create_subalbums_page
1459
1460     subalbums_hb = Gtk::HBox.new
1461     $subalbums_vb = Gtk::VBox.new(false, 5)
1462     subalbums_hb.pack_start($subalbums_vb, false, false)
1463     $subalbums_sw = Gtk::ScrolledWindow.new(nil, nil)
1464     $subalbums_sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
1465     $subalbums_sw.add_with_viewport(subalbums_hb)
1466 end
1467
1468 def save_current_file
1469     save_changes
1470
1471     if $filename
1472         ios = File.open($filename, "w")
1473         $xmldoc.write(ios, 0)
1474         ios.close
1475     end
1476 end
1477
1478 def save_current_file_user
1479     save_tempfilename = $filename
1480     $filename = $orig_filename
1481     save_current_file
1482     $modified = false
1483     $generated_outofline = false
1484     $filename = save_tempfilename
1485
1486     msg 3, "performing actual deletion of: " + $todelete.join(', ')
1487     $todelete.each { |f|
1488         system("rm -f #{f}")
1489     }
1490 end
1491
1492 def mark_document_as_dirty
1493     $xmldoc.elements.each('//dir') { |elem|
1494         elem.delete_attribute('already-generated')
1495     }
1496 end
1497
1498 #- ret: true => ok  false => cancel
1499 def ask_save_modifications(msg1, msg2, *options)
1500     ret = true
1501     options = options.size > 0 ? options[0] : {}
1502     if $modified
1503         if options[:disallow_cancel]
1504             dialog = Gtk::Dialog.new(msg1,
1505                                      $main_window,
1506                                      Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
1507                                      [options[:no] || Gtk::Stock::CLOSE, Gtk::Dialog::RESPONSE_NO],
1508                                      [options[:yes] || Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_YES])
1509         else
1510             dialog = Gtk::Dialog.new(msg1,
1511                                      $main_window,
1512                                      Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
1513                                      [options[:cancel] || Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
1514                                      [options[:no] || Gtk::Stock::CLOSE, Gtk::Dialog::RESPONSE_NO],
1515                                      [options[:yes] || Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_YES])
1516         end
1517         dialog.default_response = Gtk::Dialog::RESPONSE_YES
1518         dialog.vbox.add(Gtk::Label.new(msg2))
1519         dialog.window_position = Gtk::Window::POS_CENTER
1520         dialog.show_all
1521         
1522         dialog.run { |response|
1523             dialog.destroy
1524             if response == Gtk::Dialog::RESPONSE_YES
1525                 save_current_file_user
1526             else
1527                 #- if we have generated an album but won't save modifications, we must remove 
1528                 #- already-generated markers in original file
1529                 if $generated_outofline
1530                     begin
1531                         $xmldoc = REXML::Document.new File.new($orig_filename)
1532                         mark_document_as_dirty
1533                         ios = File.open($orig_filename, "w")
1534                         $xmldoc.write(ios, 0)
1535                         ios.close
1536                     rescue Exception
1537                         puts "exception: #{$!}"
1538                     end
1539                 end
1540             end
1541             if response == Gtk::Dialog::RESPONSE_CANCEL
1542                 ret = false
1543             end
1544             $todelete = []  #- unconditionally clear the list of images/videos to delete
1545         }
1546     end
1547     return ret
1548 end
1549
1550 def try_quit(*options)
1551     if ask_save_modifications(utf8(_("Save before quitting?")),
1552                               utf8(_("Do you want to save your changes before quitting?")),
1553                               *options)
1554         Gtk.main_quit
1555     end
1556 end
1557
1558 def show_popup(parent, msg, *options)
1559     dialog = Gtk::Dialog.new
1560     dialog.title = utf8(_("Booh message"))
1561     lbl = Gtk::Label.new
1562     lbl.markup = msg
1563     if options[0] && options[0][:centered]
1564         lbl.set_justify(Gtk::Justification::CENTER)
1565     end
1566     if options[0] && options[0][:topwidget]
1567         dialog.vbox.add(options[0][:topwidget])
1568     end
1569     dialog.vbox.add(lbl)
1570     if options[0] && options[0][:okcancel]
1571         dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL)
1572     end
1573     dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK)
1574
1575     dialog.set_default_size(200, 120)
1576     if options[0] && options[0][:pos_centered]
1577         dialog.window_position = Gtk::Window::POS_CENTER
1578     else
1579         dialog.window_position = Gtk::Window::POS_MOUSE
1580     end
1581
1582     if options[0] && options[0][:linkurl]
1583         linkbut = Gtk::Button.new('')
1584         linkbut.child.markup = "<span foreground=\"#00000000FFFF\" underline=\"single\">#{options[0][:linkurl]}</span>"
1585         linkbut.signal_connect('clicked') { open_url(options[0][:linkurl] + '/index.html' ) }
1586         linkbut.relief = Gtk::RELIEF_NONE
1587         linkbut.signal_connect('enter-notify-event') { set_mousecursor(Gdk::Cursor::HAND2, linkbut); false }
1588         linkbut.signal_connect('leave-notify-event') { set_mousecursor(nil, linkbut); false }
1589         dialog.vbox.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(linkbut))
1590     end
1591
1592     dialog.show_all
1593
1594     if !options[0] || !options[0][:not_transient]
1595         dialog.transient_for = parent
1596         dialog.run { |response|
1597             dialog.destroy
1598             if options[0] && options[0][:okcancel]
1599                 return response == Gtk::Dialog::RESPONSE_OK
1600             end
1601         }
1602     else
1603         dialog.signal_connect('response') { dialog.destroy }
1604     end
1605 end
1606
1607 def backend_wait_message(parent, msg, infopipe_path, mode)
1608     w = Gtk::Window.new
1609     w.set_transient_for(parent)
1610     w.modal = true
1611
1612     vb = Gtk::VBox.new(false, 5).set_border_width(5)
1613     vb.pack_start(Gtk::Label.new(msg), false, false)
1614
1615     vb.pack_start(frame1 = Gtk::Frame.new(utf8(_("Thumbnails"))).add(vb1 = Gtk::VBox.new(false, 5)))
1616     vb1.pack_start(pb1_1 = Gtk::ProgressBar.new.set_text(utf8(_("Scanning images and videos..."))), false, false)
1617     if mode != 'one dir scan'
1618         vb1.pack_start(pb1_2 = Gtk::ProgressBar.new.set_text(utf8(_("not started"))), false, false)
1619     end
1620     if mode == 'web-album'
1621         vb.pack_start(frame2 = Gtk::Frame.new(utf8(_("HTML pages"))).add(vb1 = Gtk::VBox.new(false, 5)))
1622         vb1.pack_start(pb2 = Gtk::ProgressBar.new.set_text(utf8(_("not started"))), false, false)
1623     end
1624     vb.pack_start(Gtk::HSeparator.new, false, false)
1625
1626     bottom = Gtk::Alignment.new(0.5, 0.5, 0, 0).add(b = Gtk::Button.new(utf8(_("_Abort"))))
1627     b.image = Gtk::Image.new("#{$FPATH}/images/stock-close-24.png")
1628     vb.pack_end(bottom, false, false)
1629
1630     infopipe = File.open(infopipe_path, File::RDONLY | File::NONBLOCK)
1631     refresh_thread = Thread.new {
1632         directories_counter = 0
1633         while line = infopipe.gets
1634             if line =~ /^directories: (\d+), sizes: (\d+)/
1635                 directories = $1.to_f + 1
1636                 sizes = $2.to_f
1637             elsif line =~ /^walking: (.+)\|(.+), (\d+) elements$/
1638                 elements = $3.to_f + 1
1639                 if mode == 'web-album'
1640                     elements += sizes
1641                 end
1642                 element_counter = 0
1643                 gtk_thread_protect { puts "destroyed: " + pb1_1.destroyed?.to_s; pb1_1.fraction = 0 }
1644                 if mode != 'one dir scan'
1645                     newtext = utf8(full_src_dir_to_rel($1, $2))
1646                     newtext = '/' if newtext == ''
1647                     gtk_thread_protect { puts "destroyed: " + pb1_2.destroyed?.to_s; pb1_2.text = newtext }
1648                     directories_counter += 1
1649                     gtk_thread_protect { puts "destroyed: " + pb1_2.destroyed?.to_s; pb1_2.fraction = directories_counter / directories }
1650                 end
1651             elsif line =~ /^processing element$/
1652                 element_counter += 1
1653                 gtk_thread_protect { puts "destroyed: " + pb1_1.destroyed?.to_s; pb1_1.fraction = element_counter / elements }
1654             elsif line =~ /^processing size$/
1655                 element_counter += 1
1656                 gtk_thread_protect { puts "destroyed: " + pb1_1.destroyed?.to_s; pb1_1.fraction = element_counter / elements }
1657             elsif line =~ /^finished processing sizes$/
1658                 gtk_thread_protect { puts "destroyed: " + pb1_1.destroyed?.to_s; pb1_1.fraction = 1 }
1659             elsif line =~ /^creating index.html$/
1660                 gtk_thread_protect { puts "destroyed: " + pb1_2.destroyed?.to_s; pb1_2.text = utf8(_("finished")) }
1661                 gtk_thread_protect { puts "destroyed: " + pb1_1.destroyed?.to_s; pb1_1.fraction = pb1_2.fraction = 1 }
1662                 directories_counter = 0
1663             elsif line =~ /^index.html: (.+)\|(.+)/
1664                 newtext = utf8(full_src_dir_to_rel($1, $2))
1665                 newtext = '/' if newtext == ''
1666                 gtk_thread_protect { puts "destroyed: " + pb2.destroyed?.to_s; pb2.text = newtext }
1667                 directories_counter += 1
1668                 gtk_thread_protect { puts "destroyed: " + pb2.destroyed?.to_s; pb2.fraction = directories_counter / directories }
1669             end
1670         end
1671     }
1672
1673     w.add(vb)
1674     w.signal_connect('delete-event') { w.destroy }
1675     w.signal_connect('destroy') {
1676         Thread.kill(refresh_thread)
1677         gtk_thread_abandon  #- needed because we're about to destroy widgets in w, for which they may be some pending gtk calls
1678         if infopipe_path
1679             infopipe.close
1680             system("rm -f #{infopipe_path}")
1681         end
1682     }
1683     w.window_position = Gtk::Window::POS_CENTER
1684     w.show_all
1685
1686     return [ b, w ]
1687 end
1688
1689 def call_backend(cmd, waitmsg, mode, params)
1690     pipe = Tempfile.new("boohpipe")
1691     pipe.close!
1692     system("mkfifo #{pipe.path}")
1693     cmd += " --info-pipe #{pipe.path}"
1694     button, w8 = backend_wait_message($main_window, waitmsg, pipe.path, mode)
1695     pid = nil
1696     Thread.new {
1697         msg 2, cmd
1698         if pid = fork
1699             id, exitstatus = Process.waitpid2(pid)
1700             gtk_thread_protect { puts "destroyed: " + w8.destroyed?.to_s; w8.destroy }
1701             if exitstatus == 0
1702                 if params[:successmsg]
1703                     gtk_thread_protect { show_popup($main_window, params[:successmsg], { :linkurl => params[:successmsg_linkurl] }) }
1704                 end
1705                 if params[:closure_after]
1706                     gtk_thread_protect(&params[:closure_after])
1707                 end
1708             elsif exitstatus == 15
1709                 #- say nothing, user aborted
1710             else
1711                 if params[:failuremsg]
1712                     gtk_thread_protect { show_popup($main_window, params[:failuremsg]) }
1713                 end
1714             end
1715         else
1716             exec(cmd)
1717         end
1718     }
1719     button.signal_connect('clicked') {
1720         Process.kill('SIGTERM', pid)
1721     }
1722 end
1723
1724 def save_changes(*forced)
1725     if forced.empty? && (!$current_path || !$undo_tb.sensitive?)
1726         return
1727     end
1728
1729     $xmldir.delete_attribute('already-generated')
1730
1731     propagate_children = Proc.new { |xmldir|
1732         if xmldir.attributes['subdirs-caption']
1733             xmldir.delete_attribute('already-generated')
1734         end
1735         xmldir.elements.each('dir') { |element|
1736             propagate_children.call(element)
1737         }
1738     }
1739
1740     if $xmldir.child_byname_notattr('dir', 'deleted')
1741         new_title = $subalbums_title.buffer.text
1742         if new_title != $xmldir.attributes['subdirs-caption']
1743             parent = $xmldir.parent
1744             if parent.name == 'dir'
1745                 parent.delete_attribute('already-generated')
1746             end
1747             propagate_children.call($xmldir)
1748         end
1749         $xmldir.add_attribute('subdirs-caption', new_title)
1750         $xmldir.elements.each('dir') { |element|
1751             if !element.attributes['deleted']
1752                 path = element.attributes['path']
1753                 newtext = $subalbums_edits[path][:editzone].buffer.text
1754                 if element.attributes['subdirs-caption']
1755                     if element.attributes['subdirs-caption'] != newtext
1756                         propagate_children.call(element)
1757                     end
1758                     element.add_attribute('subdirs-caption',     newtext)
1759                     element.add_attribute('subdirs-captionfile', utf8($subalbums_edits[path][:captionfile]))
1760                 else
1761                     if element.attributes['thumbnails-caption'] != newtext
1762                         element.delete_attribute('already-generated')
1763                     end
1764                     element.add_attribute('thumbnails-caption',     newtext)
1765                     element.add_attribute('thumbnails-captionfile', utf8($subalbums_edits[path][:captionfile]))
1766                 end
1767             end
1768         }
1769     end
1770
1771     if $notebook.page == 0 && $xmldir.child_byname_notattr('dir', 'deleted')
1772         if $xmldir.attributes['thumbnails-caption']
1773             path = $xmldir.attributes['path']
1774             $xmldir.add_attribute('thumbnails-caption', $subalbums_edits[path][:editzone].buffer.text)
1775         end
1776     elsif $xmldir.attributes['thumbnails-caption']
1777         $xmldir.add_attribute('thumbnails-caption', $thumbnails_title.buffer.text)
1778     end
1779
1780     #- remove and reinsert elements to reflect new ordering
1781     saves = {}
1782     cpt = 0
1783     $xmldir.elements.each { |element|
1784         if element.name == 'image' || element.name == 'video'
1785             saves[element.attributes['filename']] = element.remove
1786             cpt += 1
1787         end
1788     }
1789     $autotable.current_order.each { |path|
1790         chld = $xmldir.add_element(saves[path].name, saves[path].attributes)
1791         chld.add_attribute('caption', $name2widgets[File.basename(path)][:textview].buffer.text)
1792         saves.delete(path)
1793     }
1794     saves.each_key { |path|
1795         chld = $xmldir.add_element(saves[path].name, saves[path].attributes)
1796         chld.add_attribute('deleted', 'true')
1797     }
1798 end
1799
1800 def remove_all_captions
1801     $modified = true
1802     texts = {}
1803     $autotable.current_order.each { |path|
1804         texts[File.basename(path) ] = $name2widgets[File.basename(path)][:textview].buffer.text
1805         $name2widgets[File.basename(path)][:textview].buffer.text = ''
1806     }
1807     save_undo(_("remove all captions"),
1808               Proc.new { |texts|
1809                   texts.each_key { |key|
1810                       $name2widgets[key][:textview].buffer.text = texts[key]
1811                   }
1812                   $notebook.set_page(1)
1813                   Proc.new {
1814                       texts.each_key { |key|
1815                           $name2widgets[key][:textview].buffer.text = ''
1816                       }
1817                       $notebook.set_page(1)
1818                   }
1819               }, texts)
1820 end
1821
1822 def change_dir
1823     $selected_elements.each_key { |path|
1824         $name2widgets[path][:img].pixbuf = $selected_elements[path][:pixbuf]
1825     }
1826     $autotable.clear
1827     $vbox2widgets = {}
1828     $name2widgets = {}
1829     $name2closures = {}
1830     $selected_elements = {}
1831     $cuts = []
1832     $multiple_dnd = []
1833     UndoHandler.cleanup
1834     $undo_tb.sensitive = $undo_mb.sensitive = false
1835     $redo_tb.sensitive = $redo_mb.sensitive = false
1836
1837     if !$current_path
1838         return
1839     end
1840
1841     $subalbums_vb.children.each { |chld|
1842         $subalbums_vb.remove(chld)
1843     }
1844     $subalbums = Gtk::Table.new(0, 0, true)
1845     current_y_sub_albums = 0
1846
1847     $xmldir = $xmldoc.elements["//dir[@path='#{$current_path}']"]
1848     $subalbums_edits = {}
1849     subalbums_counter = 0
1850     subalbums_edits_bypos = {}
1851
1852     add_subalbum = Proc.new { |xmldir, counter|
1853         $subalbums_edits[xmldir.attributes['path']] = { :position => counter }
1854         subalbums_edits_bypos[counter] = $subalbums_edits[xmldir.attributes['path']]
1855         if xmldir == $xmldir
1856             thumbnail_file = "#{current_dest_dir}/thumbnails-thumbnail.jpg"
1857             caption = xmldir.attributes['thumbnails-caption']
1858             captionfile, dummy = find_subalbum_caption_info(xmldir)
1859             infotype = 'thumbnails'
1860         else
1861             thumbnail_file = "#{current_dest_dir}/thumbnails-#{make_dest_filename(from_utf8(File.basename(xmldir.attributes['path'])))}.jpg"
1862             captionfile, caption = find_subalbum_caption_info(xmldir)
1863             infotype = find_subalbum_info_type(xmldir)
1864         end
1865         msg 3, "add subdir: #{xmldir.attributes['path']} with file: #{thumbnail_file}"
1866         hbox = Gtk::HBox.new
1867         hbox.pack_start(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup('<i>' + File.basename(xmldir.attributes['path']) + ':</i>')))
1868         f = Gtk::Frame.new
1869         f.set_shadow_type(Gtk::SHADOW_ETCHED_OUT)
1870
1871         img = nil
1872         my_gen_real_thumbnail = proc {
1873             gen_real_thumbnail('subdir', captionfile, thumbnail_file, xmldir, $albums_thumbnail_size, img, infotype)
1874         }
1875
1876         if !$modified_pixbufs[thumbnail_file] && !File.exists?(thumbnail_file)
1877             f.add(img = Gtk::Image.new)
1878             my_gen_real_thumbnail.call
1879         else
1880             f.add(img = Gtk::Image.new($modified_pixbufs[thumbnail_file] ? $modified_pixbufs[thumbnail_file][:pixbuf] : thumbnail_file))
1881         end
1882         hbox.pack_end(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(evtbox = Gtk::EventBox.new.add(f)), false, false)
1883         $subalbums.attach(hbox,
1884                           0, 1, current_y_sub_albums, current_y_sub_albums + 1, Gtk::FILL, Gtk::FILL, 2, 2)
1885
1886         frame, textview = create_editzone($subalbums_sw, 0, img)
1887         textview.buffer.text = caption
1888         $subalbums.attach(Gtk::Alignment.new(0, 0.5, 0.5, 0).add(frame),
1889                           1, 2, current_y_sub_albums, current_y_sub_albums + 1, Gtk::FILL, Gtk::FILL, 2, 2)
1890
1891         change_image = Proc.new {
1892             fc = Gtk::FileChooserDialog.new(utf8(_("Select image for caption")),
1893                                             nil,
1894                                             Gtk::FileChooser::ACTION_OPEN,
1895                                             nil,
1896                                             [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
1897             fc.set_current_folder(from_utf8(xmldir.attributes['path']))
1898             fc.transient_for = $main_window
1899             fc.preview_widget = preview = Gtk::Alignment.new(0.5, 0.5, 0, 0).add(f = Gtk::Frame.new.set_shadow_type(Gtk::SHADOW_ETCHED_OUT))
1900             f.add(preview_img = Gtk::Image.new)
1901             preview.show_all
1902             fc.signal_connect('update-preview') { |w|
1903                 begin
1904                     if fc.preview_filename
1905                         preview_img.pixbuf = rotate_pixbuf(Gdk::Pixbuf.new(fc.preview_filename, 240, 180), guess_rotate(fc.preview_filename))
1906                         fc.preview_widget_active = true
1907                     end
1908                 rescue Gdk::PixbufError
1909                     fc.preview_widget_active = false
1910                 end
1911             }
1912             if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
1913                 $modified = true
1914                 old_file = captionfile
1915                 old_rotate = xmldir.attributes["#{infotype}-rotate"]
1916                 old_color_swap = xmldir.attributes["#{infotype}-color-swap"]
1917                 old_enhance = xmldir.attributes["#{infotype}-enhance"]
1918                 old_frame_offset = xmldir.attributes["#{infotype}-frame-offset"]
1919
1920                 new_file = fc.filename
1921                 msg 3, "new captionfile is: #{fc.filename}"
1922                 perform_changefile = Proc.new {
1923                     $subalbums_edits[xmldir.attributes['path']][:captionfile] = captionfile = new_file
1924                     $modified_pixbufs.delete(thumbnail_file)
1925                     xmldir.delete_attribute("#{infotype}-rotate")
1926                     xmldir.delete_attribute("#{infotype}-color-swap")
1927                     xmldir.delete_attribute("#{infotype}-enhance")
1928                     xmldir.delete_attribute("#{infotype}-frame-offset")
1929                     my_gen_real_thumbnail.call
1930                 }
1931                 perform_changefile.call
1932
1933                 save_undo(_("change caption file for sub-album"),
1934                           Proc.new {
1935                               $subalbums_edits[xmldir.attributes['path']][:captionfile] = captionfile = old_file
1936                               xmldir.add_attribute("#{infotype}-rotate", old_rotate)
1937                               xmldir.add_attribute("#{infotype}-color-swap", old_color_swap)
1938                               xmldir.add_attribute("#{infotype}-enhance", old_enhance)
1939                               xmldir.add_attribute("#{infotype}-frame-offset", old_frame_offset)
1940                               my_gen_real_thumbnail.call
1941                               $notebook.set_page(0)
1942                               Proc.new {
1943                                   perform_changefile.call
1944                                   $notebook.set_page(0)
1945                               }
1946                           })
1947             end
1948             fc.destroy
1949         }
1950
1951         rotate_and_cleanup = Proc.new { |angle|
1952             rotate(angle, thumbnail_file, img, xmldir, "#{infotype}-", $default_albums_thumbnails[:x], $default_albums_thumbnails[:y])
1953             system("rm -f '#{thumbnail_file}'")
1954         }
1955
1956         move = Proc.new { |direction|
1957             $modified = true
1958
1959             save_changes('forced')
1960             if direction == 'up'
1961                 oldpos = $subalbums_edits[xmldir.attributes['path']][:position]
1962                 $subalbums_edits[xmldir.attributes['path']][:position] -= 1
1963                 subalbums_edits_bypos[oldpos - 1][:position] += 1
1964             else
1965                 oldpos = $subalbums_edits[xmldir.attributes['path']][:position]
1966                 $subalbums_edits[xmldir.attributes['path']][:position] += 1
1967                 subalbums_edits_bypos[oldpos + 1][:position] -= 1
1968             end
1969
1970             elems = []
1971             $xmldir.elements.each('dir') { |element|
1972                 if (!element.attributes['deleted'])
1973                     elems << [ element.attributes['path'], element.remove ]
1974                 end
1975             }
1976             elems.sort { |a,b| $subalbums_edits[a[0]][:position] <=> $subalbums_edits[b[0]][:position] }.
1977                   each { |e| $xmldir.add_element(e[1]) }
1978             #- need to remove the already-generated tag to all subdirs because of "previous/next albums" links completely changed
1979             $xmldir.elements.each('descendant::dir') { |elem|
1980                 elem.delete_attribute('already-generated')
1981             }
1982             change_dir
1983         }
1984
1985         color_swap_and_cleanup = Proc.new {
1986             perform_color_swap_and_cleanup = Proc.new {
1987                 color_swap(xmldir, "#{infotype}-")
1988                 my_gen_real_thumbnail.call
1989             }
1990             perform_color_swap_and_cleanup.call
1991
1992             save_undo(_("color swap"),
1993                       Proc.new {
1994                           perform_color_swap_and_cleanup.call
1995                           $notebook.set_page(0)
1996                           Proc.new {
1997                               perform_color_swap_and_cleanup.call
1998                               $notebook.set_page(0)
1999                           }
2000                       })
2001         }
2002
2003         change_frame_offset_and_cleanup = Proc.new {
2004             if values = ask_new_frame_offset(xmldir, "#{infotype}-")
2005                 perform_change_frame_offset_and_cleanup = Proc.new { |val|
2006                     change_frame_offset(xmldir, "#{infotype}-", val)
2007                     my_gen_real_thumbnail.call
2008                 }
2009                 perform_change_frame_offset_and_cleanup.call(values[:new])
2010
2011                 save_undo(_("specify frame offset"),
2012                           Proc.new {
2013                               perform_change_frame_offset_and_cleanup.call(values[:old])
2014                               $notebook.set_page(0)
2015                               Proc.new {
2016                                   perform_change_frame_offset_and_cleanup.call(values[:new])
2017                                   $notebook.set_page(0)
2018                               }
2019                           })
2020             end
2021         }
2022
2023         whitebalance_and_cleanup = Proc.new {
2024             if values = ask_whitebalance(captionfile, thumbnail_file, img, xmldir, "#{infotype}-",
2025                                          $default_albums_thumbnails[:x], $default_albums_thumbnails[:y], infotype)
2026                 perform_change_whitebalance_and_cleanup = Proc.new { |val|
2027                     change_whitebalance(xmldir, "#{infotype}-", val)
2028                     recalc_whitebalance(val, captionfile, thumbnail_file, img, xmldir, "#{infotype}-",
2029                                         $default_albums_thumbnails[:x], $default_albums_thumbnails[:y], infotype)
2030                     system("rm -f '#{thumbnail_file}'")
2031                 }
2032                 perform_change_whitebalance_and_cleanup.call(values[:new])
2033                 
2034                 save_undo(_("fix white balance"),
2035                           Proc.new {
2036                               perform_change_whitebalance_and_cleanup.call(values[:old])
2037                               $notebook.set_page(0)
2038                               Proc.new {
2039                                   perform_change_whitebalance_and_cleanup.call(values[:new])
2040                                   $notebook.set_page(0)
2041                               }
2042                           })
2043             end
2044         }
2045
2046         enhance_and_cleanup = Proc.new {
2047             perform_enhance_and_cleanup = Proc.new {
2048                 enhance(xmldir, "#{infotype}-")
2049                 my_gen_real_thumbnail.call
2050             }
2051             
2052             perform_enhance_and_cleanup.call
2053             
2054             save_undo(_("enhance"),
2055                       Proc.new {
2056                           perform_enhance_and_cleanup.call
2057                           $notebook.set_page(0)
2058                           Proc.new {
2059                               perform_enhance_and_cleanup.call
2060                               $notebook.set_page(0)
2061                           }
2062                       })
2063         }
2064
2065         evtbox.signal_connect('button-press-event') { |w, event|
2066             if event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 1
2067                 if $r90.active?
2068                     rotate_and_cleanup.call(90)
2069                 elsif $r270.active?
2070                     rotate_and_cleanup.call(-90)
2071                 elsif $enhance.active?
2072                     enhance_and_cleanup.call
2073                 end
2074             end
2075             if event.event_type == Gdk::Event::BUTTON_PRESS && event.button == 3
2076                 popup_thumbnail_menu(event, ['change_image'], entry2type(captionfile), xmldir, "#{infotype}-",
2077                                      { :forbid_left => true, :forbid_right => true,
2078                                        :can_up => counter > 1, :can_down => counter > 0 && counter < subalbums_counter },
2079                                      { :change => change_image, :move => move, :rotate => rotate_and_cleanup, :enhance => enhance_and_cleanup,
2080                                        :color_swap => color_swap_and_cleanup, :frame_offset => change_frame_offset_and_cleanup, :whitebalance => whitebalance_and_cleanup })
2081             end
2082             if event.event_type == Gdk::Event::BUTTON2_PRESS && event.button == 1
2083                 change_image.call
2084                 true   #- handled
2085             end
2086         }
2087         evtbox.signal_connect('button-press-event') { |w, event|
2088             $gesture_press = { :filename => thumbnail_file, :x => event.x, :y => event.y }
2089             false
2090         }
2091
2092         evtbox.signal_connect('button-release-event') { |w, event|
2093             if !$r90.active? && !$r270.active? && $gesture_press && $gesture_press[:filename] == thumbnail_file
2094                 msg 3, "press: #{$gesture_press[:x]} release: #{event.x}"
2095                 if (($gesture_press[:x]-event.x)/($gesture_press[:y]-event.y)).abs > 2 && ($gesture_press[:x]-event.x).abs > 5
2096                     angle = event.x-$gesture_press[:x] > 0 ? 90 : -90
2097                     msg 3, "gesture rotate: #{angle}"
2098                     rotate_and_cleanup.call(angle)
2099                 end
2100             end
2101             $gesture_press = nil
2102         }
2103                 
2104         $subalbums_edits[xmldir.attributes['path']][:editzone] = textview
2105         $subalbums_edits[xmldir.attributes['path']][:captionfile] = captionfile
2106         current_y_sub_albums += 1
2107     }
2108
2109     if $xmldir.child_byname_notattr('dir', 'deleted')
2110         #- title edition
2111         frame, $subalbums_title = create_editzone($subalbums_sw, 0, nil)
2112         $subalbums_title.buffer.text = $xmldir.attributes['subdirs-caption']
2113         $subalbums_title.set_justification(Gtk::Justification::CENTER)
2114         $subalbums_vb.pack_start(Gtk::Alignment.new(0.5, 0.5, 0.5, 0).add(frame), false, false)
2115         #- this album image/caption
2116         if $xmldir.attributes['thumbnails-caption']
2117             add_subalbum.call($xmldir, 0)
2118         end
2119     end
2120     total = { 'image' => 0, 'video' => 0, 'dir' => 0 }
2121     $xmldir.elements.each { |element|
2122         if (element.name == 'image' || element.name == 'video') && !element.attributes['deleted']
2123             #- element (image or video) of this album
2124             dest_img = build_full_dest_filename(element.attributes['filename']).sub(/\.[^\.]+$/, '') + "-#{$default_size['thumbnails']}.jpg"
2125             msg 3, "dest_img: #{dest_img}"
2126             add_thumbnail($autotable, element.attributes['filename'], element.name, dest_img, from_utf8(element.attributes['caption']))
2127             total[element.name] += 1
2128         end
2129         if element.name == 'dir' && !element.attributes['deleted']
2130             #- sub-album image/caption
2131             add_subalbum.call(element, subalbums_counter += 1)
2132             total[element.name] += 1
2133         end
2134     }
2135     $statusbar.push(0, utf8(_("%s: %s images and %s videos, %s sub-albums") % [ File.basename(from_utf8($xmldir.attributes['path'])),
2136                                                                                 total['image'], total['video'], total['dir'] ]))
2137     $subalbums_vb.add($subalbums)
2138     $subalbums_vb.show_all
2139
2140     if !$xmldir.child_byname_notattr('image', 'deleted') && !$xmldir.child_byname_notattr('video', 'deleted')
2141         $notebook.get_tab_label($autotable_sw).sensitive = false
2142         $notebook.set_page(0)
2143         $thumbnails_title.buffer.text = ''
2144     else
2145         $notebook.get_tab_label($autotable_sw).sensitive = true
2146         $thumbnails_title.buffer.text = $xmldir.attributes['thumbnails-caption']
2147     end
2148
2149     if !$xmldir.child_byname_notattr('dir', 'deleted')
2150         $notebook.get_tab_label($subalbums_sw).sensitive = false
2151         $notebook.set_page(1)
2152     else
2153         $notebook.get_tab_label($subalbums_sw).sensitive = true
2154     end
2155 end
2156
2157 def pixbuf_or_nil(filename)
2158     begin
2159         return Gdk::Pixbuf.new(filename)
2160     rescue
2161         return nil
2162     end
2163 end
2164
2165 def theme_choose(current)
2166     dialog = Gtk::Dialog.new(utf8(_("Select your preferred theme")),
2167                              $main_window,
2168                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
2169                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
2170                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2171
2172     model = Gtk::ListStore.new(String, Gdk::Pixbuf, Gdk::Pixbuf, Gdk::Pixbuf)
2173     treeview = Gtk::TreeView.new(model).set_rules_hint(true)
2174     treeview.append_column(Gtk::TreeViewColumn.new(utf8(_("Theme name")), Gtk::CellRendererText.new, { :text => 0 }).set_alignment(0.5).set_spacing(5))
2175     treeview.append_column(Gtk::TreeViewColumn.new(utf8(_("Sub-albums page look")), Gtk::CellRendererPixbuf.new, { :pixbuf => 1 }).set_alignment(0.5).set_spacing(5))
2176     treeview.append_column(Gtk::TreeViewColumn.new(utf8(_("Thumbnails page look")), Gtk::CellRendererPixbuf.new, { :pixbuf => 2 }).set_alignment(0.5).set_spacing(5))
2177     treeview.append_column(Gtk::TreeViewColumn.new(utf8(_("Fullscreen page look")), Gtk::CellRendererPixbuf.new, { :pixbuf => 3 }).set_alignment(0.5).set_spacing(5))
2178     treeview.append_column(Gtk::TreeViewColumn.new('', Gtk::CellRendererText.new, {}))
2179     treeview.signal_connect('button-press-event') { |w, event|
2180         if event.event_type == Gdk::Event::BUTTON2_PRESS && event.button == 1
2181             dialog.response(Gtk::Dialog::RESPONSE_OK)
2182         end
2183     }
2184
2185     dialog.vbox.add(sw = Gtk::ScrolledWindow.new(nil, nil).add(treeview).set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC))
2186
2187     `find '#{$FPATH}/themes' -mindepth 1 -maxdepth 1 -type d`.each { |dir|
2188         dir.chomp!
2189         iter = model.append
2190         iter[0] = File.basename(dir)
2191         iter[1] = pixbuf_or_nil("#{dir}/metadata/screenshot-1.png")
2192         iter[2] = pixbuf_or_nil("#{dir}/metadata/screenshot-2.png")
2193         iter[3] = pixbuf_or_nil("#{dir}/metadata/screenshot-3.png")
2194         if File.basename(dir) == current
2195             treeview.selection.select_iter(iter)
2196         end
2197     }
2198
2199     dialog.set_default_size(700, 400)
2200     dialog.vbox.show_all
2201     dialog.run { |response|
2202         iter = treeview.selection.selected
2203         dialog.destroy
2204         if response == Gtk::Dialog::RESPONSE_OK && iter
2205             return model.get_value(iter, 0)
2206         end
2207     }
2208     return nil
2209 end
2210
2211 def populate_subalbums_treeview
2212     $albums_ts.clear
2213     $autotable.clear
2214     $subalbums_vb.children.each { |chld|
2215         $subalbums_vb.remove(chld)
2216     }
2217
2218     source = $xmldoc.root.attributes['source']
2219     msg 3, "source: #{source}"
2220
2221     xmldir = $xmldoc.elements['//dir']
2222     if !xmldir || xmldir.attributes['path'] != source
2223         msg 1, _("Corrupted booh file...")
2224         return
2225     end
2226
2227     append_dir_elem = Proc.new { |parent_iter, xmldir|
2228         child_iter = $albums_ts.append(parent_iter)
2229         child_iter[0] = File.basename(xmldir.attributes['path'])
2230         child_iter[1] = xmldir.attributes['path']
2231         msg 3, "puttin location: #{xmldir.attributes['path']}"
2232         xmldir.elements.each('dir') { |elem|
2233             if !elem.attributes['deleted']
2234                 append_dir_elem.call(child_iter, elem)
2235             end
2236         }
2237     }
2238     append_dir_elem.call(nil, xmldir)
2239
2240     $albums_tv.expand_all
2241     $albums_tv.selection.select_iter($albums_ts.iter_first)
2242 end
2243
2244 def open_file(filename)
2245
2246     $filename = nil
2247     $modified = false
2248     $current_path = nil   #- invalidate
2249     $modified_pixbufs = {}
2250     $albums_ts.clear
2251     $autotable.clear
2252     $subalbums_vb.children.each { |chld|
2253         $subalbums_vb.remove(chld)
2254     }
2255
2256     if !File.exists?(filename)
2257         return utf8(_("File not found."))
2258     end
2259
2260     begin
2261         $xmldoc = REXML::Document.new File.new(filename)
2262     rescue Exception
2263         $xmldoc = nil
2264     end
2265
2266     if !$xmldoc || !$xmldoc.root || $xmldoc.root.name != 'booh'
2267         if entry2type(filename).nil?
2268             return utf8(_("Not a booh file!"))
2269         else
2270             return utf8(_("Not a booh file!\n\nHint: you cannot import directly an image or video with File/Open.\nUse File/New to create a new album."))
2271         end
2272     end
2273
2274     if !source = $xmldoc.root.attributes['source']
2275         return utf8(_("Corrupted booh file..."))
2276     end
2277
2278     if !dest = $xmldoc.root.attributes['destination']
2279         return utf8(_("Corrupted booh file..."))
2280     end
2281
2282     if !theme = $xmldoc.root.attributes['theme']
2283         return utf8(_("Corrupted booh file..."))
2284     end
2285
2286     if $xmldoc.root.attributes['version'] != $VERSION
2287         msg 2, _("File's version %s, booh version now %s, marking dirty") % [ $xmldoc.root.attributes['version'], $VERSION ]
2288         mark_document_as_dirty
2289         $xmldoc.root.add_attribute('version', $VERSION)
2290     end
2291
2292     limit_sizes = $xmldoc.root.attributes['limit-sizes']
2293     optimizefor32 = !$xmldoc.root.attributes['optimize-for-32'].nil?
2294     nperrow = $xmldoc.root.attributes['thumbnails-per-row']
2295
2296     $filename = filename
2297     select_theme(theme, limit_sizes, optimizefor32, nperrow)
2298     $default_size['thumbnails'] =~ /(.*)x(.*)/
2299     $default_thumbnails = { :x => $1.to_i, :y => $2.to_i }
2300     $albums_thumbnail_size =~ /(.*)x(.*)/
2301     $default_albums_thumbnails = { :x => $1.to_i, :y => $2.to_i }
2302
2303     populate_subalbums_treeview
2304
2305     $save.sensitive = $save_as.sensitive = $merge_current.sensitive = $merge_newsubs.sensitive = $merge.sensitive = $generate.sensitive = $view_wa.sensitive = $properties.sensitive = $remove_all_captions.sensitive = true
2306     return nil
2307 end
2308
2309 def open_file_user(filename)
2310     result = open_file(filename)
2311     if !result
2312         $config['last-opens'] ||= []
2313         if $config['last-opens'][-1] != utf8(filename)
2314             $config['last-opens'] << utf8(filename)
2315         end
2316         $orig_filename = $filename
2317         tmp = Tempfile.new("boohtemp")
2318         tmp.close!
2319         #- for security
2320         ios = File.open($filename = tmp.path, File::RDWR|File::CREAT|File::EXCL)
2321         ios.close
2322         $tempfiles << $filename << "#{$filename}.backup"
2323     else
2324         $orig_filename = nil
2325     end
2326     return result
2327 end
2328
2329 def open_file_popup
2330     if !ask_save_modifications(utf8(_("Save this album?")),
2331                                utf8(_("Do you want to save the changes to this album?")),
2332                                { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
2333         return
2334     end
2335     fc = Gtk::FileChooserDialog.new(utf8(_("Open file")),
2336                                     nil,
2337                                     Gtk::FileChooser::ACTION_OPEN,
2338                                     nil,
2339                                     [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2340     fc.add_shortcut_folder(File.expand_path("~/.booh"))
2341     fc.set_current_folder(File.expand_path("~/.booh"))
2342     fc.transient_for = $main_window
2343     ok = false
2344     while !ok
2345         if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
2346             push_mousecursor_wait(fc)
2347             msg = open_file_user(fc.filename)
2348             pop_mousecursor(fc)
2349             if msg
2350                 show_popup(fc, msg)
2351                 ok = false
2352             else
2353                 ok = true
2354             end
2355         else
2356             ok = true
2357         end
2358     end
2359     fc.destroy
2360 end
2361
2362 def open_url(url)
2363     cmd = $config['browser'].gsub('%f', "'#{url}'") + ' &'
2364     msg 2, cmd
2365     system(cmd)
2366 end
2367
2368 def additional_booh_options
2369     options = ''
2370     if $config['mproc']
2371         options += "--mproc #{$config['mproc'].to_i} "
2372     end
2373     if $config['emptycomments']
2374         options += "--empty-comments "
2375     end
2376     return options
2377 end
2378
2379 def new_album
2380     if !ask_save_modifications(utf8(_("Save this album?")),
2381                                utf8(_("Do you want to save the changes to this album?")),
2382                                { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
2383         return
2384     end
2385     dialog = Gtk::Dialog.new(utf8(_("Create a new album")),
2386                              $main_window,
2387                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
2388                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
2389                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2390     
2391     frame1 = Gtk::Frame.new(utf8(_("Locations"))).add(tbl = Gtk::Table.new(0, 0, false))
2392     tbl.attach(Gtk::Label.new(utf8(_("Directory of images/videos: "))),
2393                0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2394     tbl.attach(src = Gtk::Entry.new,
2395                1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
2396     tbl.attach(src_browse = Gtk::Button.new(utf8(_("browse..."))),
2397                2, 3, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2398     tbl.attach(Gtk::Label.new.set_markup(utf8(_("<span size='small'><i>number of images/videos down this directory:</i></span> "))),
2399                0, 1, 1, 2, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2400     tbl.attach(src_nb = Gtk::Label.new.set_markup(utf8(_("<span size='small'><i>N/A</i></span>"))),
2401                1, 2, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 2)
2402     tbl.attach(Gtk::Label.new(utf8(_("Directory where to put the web-album: "))),
2403                0, 1, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2404     tbl.attach(dest = Gtk::Entry.new,
2405                1, 2, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2)
2406     tbl.attach(dest_browse = Gtk::Button.new(utf8(_("browse..."))),
2407                2, 3, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2408     tbl.attach(Gtk::Label.new(utf8(_("Filename to store this album's properties: "))),
2409                0, 1, 3, 4, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2410     tbl.attach(conf = Gtk::Entry.new.set_size_request(250, -1),
2411                1, 2, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 2)
2412     tbl.attach(conf_browse = Gtk::Button.new(utf8(_("browse..."))),
2413                2, 3, 3, 4, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2414
2415     tooltips = Gtk::Tooltips.new
2416     frame2 = Gtk::Frame.new(utf8(_("Configuration"))).add(vb = Gtk::VBox.new)
2417     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Theme: "))), false, false, 0).
2418                          pack_start(theme_button = Gtk::Button.new($config['default-theme'] || 'simple'), false, false, 0))
2419     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Sizes of images to generate: "))), false, false, 0).
2420                                    pack_start(sizes = Gtk::HBox.new, false, false, 0))
2421     vb.add(optimize432 = Gtk::CheckButton.new(utf8(_("Optimize for 3/2 aspect ratio"))))
2422     tooltips.set_tip(optimize432, utf8(_("Resize images with optimized sizes for 3/2 aspect ratio rather than 4/3 (typical aspect ratio of pictures from non digital cameras are 3/2 when pictures from digital cameras are 4/3)")), nil)
2423     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Number of thumbnails per row: "))), false, false, 0).
2424                                    pack_start(nperrowradios = Gtk::HBox.new, false, false, 0))
2425
2426     src_nb_calculated_for = ''
2427     src_nb_thread = nil
2428     process_src_nb = Proc.new {
2429         if src.text != src_nb_calculated_for
2430             src_nb_calculated_for = src.text
2431             if src_nb_thread
2432                 Thread.kill(src_nb_thread)
2433                 src_nb_thread = nil
2434             end
2435             if File.directory?(from_utf8(src_nb_calculated_for)) && src_nb_calculated_for != '/'
2436                 if File.readable?(from_utf8(src_nb_calculated_for))
2437                     src_nb_thread = Thread.new {
2438                         gtk_thread_protect { puts "destroyed: " + src_nb.destroyed?.to_s; src_nb.set_markup(utf8(_("<span size='small'><i>processing...</i></span>"))) }
2439                         total = { 'image' => 0, 'video' => 0, nil => 0 }
2440                         `find '#{from_utf8(src_nb_calculated_for)}' -type d -follow`.each { |dir|
2441                             if File.basename(dir) =~ /^\./
2442                                 next
2443                             else
2444                                 begin
2445                                     Dir.entries(dir.chomp).each { |file|
2446                                         total[entry2type(file)] += 1
2447                                     }
2448                                 rescue Errno::EACCES, Errno::ENOENT
2449                                 end
2450                             end
2451                         }
2452                         gtk_thread_protect { puts "destroyed: " + src_nb.destroyed?.to_s; src_nb.set_markup(utf8(_("<span size='small'><i>%s images and %s videos</i></span>") % [ total['image'], total['video'] ])) }
2453                         src_nb_thread = nil
2454                     }
2455                 else
2456                     src_nb.set_markup(utf8(_("<span size='small'><i>permission denied</i></span>")))
2457                 end
2458             else
2459                 src_nb.set_markup(utf8(_("<span size='small'><i>N/A</i></span>")))
2460             end
2461         end
2462         true
2463     }
2464     timeout_src_nb = Gtk.timeout_add(100) {
2465         process_src_nb.call
2466     }
2467
2468     src_browse.signal_connect('clicked') {
2469         fc = Gtk::FileChooserDialog.new(utf8(_("Select the directory of images/videos")),
2470                                         nil,
2471                                         Gtk::FileChooser::ACTION_SELECT_FOLDER,
2472                                         nil,
2473                                         [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2474         fc.transient_for = $main_window
2475         if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
2476             src.text = utf8(fc.filename)
2477             process_src_nb.call
2478             conf.text = File.expand_path("~/.booh/#{File.basename(src.text)}")
2479         end
2480         fc.destroy
2481     }
2482
2483     dest_browse.signal_connect('clicked') {
2484         fc = Gtk::FileChooserDialog.new(utf8(_("Select a new directory where to put the web-album")),
2485                                         nil,
2486                                         Gtk::FileChooser::ACTION_CREATE_FOLDER,
2487                                         nil,
2488                                         [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2489         fc.transient_for = $main_window
2490         if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
2491             dest.text = utf8(fc.filename)
2492         end
2493         fc.destroy
2494     }
2495
2496     conf_browse.signal_connect('clicked') {
2497         fc = Gtk::FileChooserDialog.new(utf8(_("Select a new file to store this album's properties")),
2498                                         nil,
2499                                         Gtk::FileChooser::ACTION_SAVE,
2500                                         nil,
2501                                         [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2502         fc.transient_for = $main_window
2503         fc.add_shortcut_folder(File.expand_path("~/.booh"))
2504         fc.set_current_folder(File.expand_path("~/.booh"))
2505         if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
2506             conf.text = utf8(fc.filename)
2507         end
2508         fc.destroy
2509     }
2510
2511     theme_sizes = []
2512     nperrows = []
2513     recreate_theme_config = proc {
2514         theme_sizes.each { |e| sizes.remove(e[:widget]) }
2515         theme_sizes = []
2516         select_theme(theme_button.label, 'all', optimize432.active?, nil)
2517         $images_size.each { |s|
2518             sizes.add(cb = Gtk::CheckButton.new(sizename(s['name'])))
2519             if !s['optional']
2520                 cb.active = true
2521             end
2522             tooltips.set_tip(cb, utf8(s['description']), nil)
2523             theme_sizes << { :widget => cb, :value => s['name'] }
2524         }
2525         sizes.add(cb = Gtk::CheckButton.new(utf8(_('original'))))
2526         tooltips = Gtk::Tooltips.new
2527         tooltips.set_tip(cb, utf8(_("Include original image in web-album")), nil)
2528         theme_sizes << { :widget => cb, :value => 'original' }
2529         sizes.show_all
2530
2531         nperrows.each { |e| nperrowradios.remove(e[:widget]) }
2532         nperrow_group = nil
2533         nperrows = []
2534         $allowed_N_values.each { |n|
2535             if nperrow_group
2536                 nperrowradios.add(rb = Gtk::RadioButton.new(nperrow_group, n.to_s, false))
2537             else
2538                 nperrowradios.add(nperrow_group = rb = Gtk::RadioButton.new(n.to_s))
2539             end
2540             if $default_N == n
2541                 rb.active = true
2542             end
2543             nperrows << { :widget => rb, :value => n }
2544         }
2545         nperrowradios.show_all
2546     }
2547     recreate_theme_config.call
2548
2549     theme_button.signal_connect('clicked') {
2550         if newtheme = theme_choose(theme_button.label)
2551             theme_button.label = newtheme
2552             recreate_theme_config.call
2553         end
2554     }
2555
2556     dialog.vbox.add(frame1)
2557     dialog.vbox.add(frame2)
2558     dialog.window_position = Gtk::Window::POS_MOUSE
2559     dialog.show_all
2560
2561     keepon = true
2562     ok = true
2563     while keepon
2564         dialog.run { |response|
2565             if response == Gtk::Dialog::RESPONSE_OK
2566                 srcdir = from_utf8(src.text)
2567                 destdir = from_utf8(dest.text)
2568                 if !File.directory?(srcdir)
2569                     show_popup(dialog, utf8(_("The directory of images/videos doesn't exist. Please check your input.")))
2570                     src.grab_focus
2571                 elsif conf.text == ''
2572                     show_popup(dialog, utf8(_("Please specify a filename to store the album's properties.")))
2573                     conf.grab_focus
2574                 elsif File.directory?(from_utf8(conf.text))
2575                     show_popup(dialog, utf8(_("Sorry, the filename specified to store the album's properties is an existing directory. Please choose another one.")))
2576                     conf.grab_focus
2577                 elsif destdir != make_dest_filename(destdir)
2578                     show_popup(dialog, utf8(_("Sorry, destination directory can't contain non simple alphanumeric characters.")))
2579                     dest.grab_focus
2580                 elsif File.directory?(destdir) && Dir.entries(destdir).size > 2
2581                     keepon = !show_popup(dialog, utf8(_("The destination directory already exists. Are you sure you want to continue?")), { :okcancel => true })
2582                     dest.grab_focus
2583                 elsif File.exists?(destdir) && !File.directory?(destdir)
2584                     show_popup(dialog, utf8(_("There is already a file by the name of the destination directory. Please choose another one.")))
2585                     dest.grab_focus
2586                 elsif !theme_sizes.detect { |e| e[:value] != 'original' && e[:widget].active? }
2587                     show_popup(dialog, utf8(_("You need to select at least one size (not counting original).")))
2588                 else
2589                     system("mkdir '#{destdir}'")
2590                     if !File.directory?(destdir)
2591                         show_popup(dialog, utf8(_("Could not create destination directory. Permission denied?")))
2592                         dest.grab_focus
2593                     else
2594                         keepon = false
2595                     end
2596                 end
2597             else
2598                 keepon = ok = false
2599             end
2600         }
2601     end
2602     srcdir = from_utf8(src.text)
2603     destdir = from_utf8(dest.text)
2604     configskel = File.expand_path(from_utf8(conf.text))
2605     theme = theme_button.label
2606     sizes = theme_sizes.find_all { |e| e[:widget].active? }.collect { |e| e[:value] }.join(',')
2607     nperrow = nperrows.find { |e| e[:widget].active? }[:value]
2608     opt432 = optimize432.active?
2609     if src_nb_thread
2610         Thread.kill(src_nb_thread)
2611         gtk_thread_abandon  #- needed because we're about to destroy widgets in dialog, for which they may be some pending gtk calls
2612     end
2613     dialog.destroy
2614     Gtk.timeout_remove(timeout_src_nb)
2615
2616     if ok
2617         call_backend("booh-backend --source '#{srcdir}' --destination '#{destdir}' --config-skel '#{configskel}' --for-gui " +
2618                      "--verbose-level #{$verbose_level} --theme #{theme} --sizes #{sizes} --thumbnails-per-row #{nperrow} " +
2619                      "#{opt432 ? '--optimize-for-32' : ''} #{additional_booh_options}",
2620                      utf8(_("Please wait while scanning source directory...")),
2621                      'full scan',
2622                      { :closure_after => proc { open_file_user(configskel) } })
2623     end
2624 end
2625
2626 def properties
2627     dialog = Gtk::Dialog.new(utf8(_("Properties of your album")),
2628                              $main_window,
2629                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
2630                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
2631                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2632     
2633     source = $xmldoc.root.attributes['source']
2634     dest = $xmldoc.root.attributes['destination']
2635     theme = $xmldoc.root.attributes['theme']
2636     opt432 = !$xmldoc.root.attributes['optimize-for-32'].nil?
2637     nperrow = $xmldoc.root.attributes['thumbnails-per-row']
2638     limit_sizes = $xmldoc.root.attributes['limit-sizes']
2639     if limit_sizes
2640         limit_sizes = limit_sizes.split(/,/)
2641     end
2642
2643     tooltips = Gtk::Tooltips.new
2644     frame1 = Gtk::Frame.new(utf8(_("Locations"))).add(tbl = Gtk::Table.new(0, 0, false))
2645     tbl.attach(Gtk::Label.new(utf8(_("Directory of source images/videos: "))),
2646                0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2647     tbl.attach(Gtk::Alignment.new(0, 0.5, 0, 0).add(Gtk::Label.new.set_markup('<i>' + source + '</i>')),
2648                1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
2649     tbl.attach(Gtk::Label.new(utf8(_("Directory where the web-album is created: "))),
2650                0, 1, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2651     tbl.attach(Gtk::Alignment.new(0, 0.5, 0, 0).add(Gtk::Label.new.set_markup('<i>' + dest + '</i>')),
2652                1, 2, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2)
2653     tbl.attach(Gtk::Label.new(utf8(_("Filename where this album's properties are stored: "))),
2654                0, 1, 3, 4, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2655     tbl.attach(Gtk::Alignment.new(0, 0.5, 0, 0).add(Gtk::Label.new.set_markup('<i>' + $orig_filename + '</i>')),
2656                1, 2, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 2)
2657
2658     frame2 = Gtk::Frame.new(utf8(_("Configuration"))).add(vb = Gtk::VBox.new)
2659     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Theme: "))), false, false, 0).
2660                          pack_start(theme_button = Gtk::Button.new(theme), false, false, 0))
2661     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Sizes of images to generate: "))), false, false, 0).
2662                                    pack_start(sizes = Gtk::HBox.new, false, false, 0))
2663     vb.add(optimize432 = Gtk::CheckButton.new(utf8(_("Optimize for 3/2 aspect ratio"))).set_active(opt432))
2664     tooltips.set_tip(optimize432, utf8(_("Resize images with optimized sizes for 3/2 aspect ratio rather than 4/3 (typical aspect ratio of pictures from non digital cameras are 3/2 when pictures from digital cameras are 4/3)")), nil)
2665     vb.add(Gtk::HBox.new(false, 3).pack_start(Gtk::Label.new(utf8(_("Number of thumbnails per row: "))), false, false, 0).
2666                                    pack_start(nperrowradios = Gtk::HBox.new, false, false, 0))
2667
2668     theme_sizes = []
2669     nperrows = []
2670     recreate_theme_config = proc {
2671         theme_sizes.each { |e| sizes.remove(e[:widget]) }
2672         theme_sizes = []
2673         select_theme(theme_button.label, 'all', optimize432.active?, nperrow)
2674
2675         $images_size.each { |s|
2676             sizes.add(cb = Gtk::CheckButton.new(sizename(s['name'])))
2677             if limit_sizes
2678                 if limit_sizes.include?(s['name'])
2679                     cb.active = true
2680                 end
2681             else
2682                 if !s['optional']
2683                     cb.active = true
2684                 end
2685             end
2686             tooltips.set_tip(cb, utf8(s['description']), nil)
2687             theme_sizes << { :widget => cb, :value => s['name'] }
2688         }
2689         sizes.add(cb = Gtk::CheckButton.new(utf8(_('original'))))
2690         tooltips = Gtk::Tooltips.new
2691         tooltips.set_tip(cb, utf8(_("Include original image in web-album")), nil)
2692         if limit_sizes && limit_sizes.include?('original')
2693             cb.active = true
2694         end
2695         theme_sizes << { :widget => cb, :value => 'original' }
2696         sizes.show_all
2697
2698         nperrows.each { |e| nperrowradios.remove(e[:widget]) }
2699         nperrow_group = nil
2700         nperrows = []
2701         $allowed_N_values.each { |n|
2702             if nperrow_group
2703                 nperrowradios.add(rb = Gtk::RadioButton.new(nperrow_group, n.to_s, false))
2704             else
2705                 nperrowradios.add(nperrow_group = rb = Gtk::RadioButton.new(n.to_s))
2706             end
2707             nperrowradios.add(Gtk::Label.new('  '))
2708             if nperrow && n.to_s == nperrow || !nperrow && $default_N == n
2709                 rb.active = true
2710             end
2711             nperrows << { :widget => rb, :value => n.to_s }
2712         }
2713         nperrowradios.show_all
2714     }
2715     recreate_theme_config.call
2716
2717     theme_button.signal_connect('clicked') {
2718         if newtheme = theme_choose(theme_button.label)
2719             limit_sizes = nil
2720             nperrow = nil
2721             theme_button.label = newtheme
2722             recreate_theme_config.call
2723         end
2724     }
2725
2726     dialog.vbox.add(frame1)
2727     dialog.vbox.add(frame2)
2728     dialog.window_position = Gtk::Window::POS_MOUSE
2729     dialog.show_all
2730
2731     keepon = true
2732     ok = true
2733     while keepon
2734         dialog.run { |response|
2735             if response == Gtk::Dialog::RESPONSE_OK
2736                 if !theme_sizes.detect { |e| e[:value] != 'original' && e[:widget].active? }
2737                     show_popup(dialog, utf8(_("You need to select at least one size (not counting original).")))
2738                 else
2739                     keepon = false
2740                 end
2741             else
2742                 keepon = ok = false
2743             end
2744         }
2745     end
2746     save_theme = theme_button.label
2747     save_limit_sizes = theme_sizes.find_all { |e| e[:widget].active? }.collect { |e| e[:value] }
2748     save_opt432 = optimize432.active?
2749     save_nperrow = nperrows.find { |e| e[:widget].active? }[:value]
2750     dialog.destroy
2751
2752     if ok && (save_theme != theme || save_limit_sizes != limit_sizes || save_opt432 != opt432 || save_nperrow != nperrow)
2753         mark_document_as_dirty
2754         save_current_file
2755         call_backend("booh-backend --use-config '#{$filename}' --for-gui --verbose-level #{$verbose_level} " +
2756                      "--thumbnails-per-row #{save_nperrow} --theme #{save_theme} --sizes #{save_limit_sizes.join(',')} " +
2757                      "#{save_opt432 ? '--optimize-for-32' : ''} #{additional_booh_options}",
2758                      utf8(_("Please wait while scanning source directory...")),
2759                      'full scan',
2760                      { :closure_after => proc {
2761                              open_file($filename)
2762                              $modified = true
2763                          } })
2764     end
2765 end
2766
2767 def merge_current
2768     save_current_file
2769
2770     sel = $albums_tv.selection.selected_rows
2771
2772     call_backend("booh-backend --merge-config-onedir '#{$filename}' --dir '#{from_utf8($current_path)}' --for-gui " +
2773                  "--verbose-level #{$verbose_level} #{additional_booh_options}",
2774                  utf8(_("Please wait while scanning source directory...")),
2775                  'one dir scan',
2776                  { :closure_after => proc {
2777                          open_file($filename)
2778                          $albums_tv.selection.select_path(sel[0])
2779                          $modified = true
2780                      } })
2781 end
2782
2783 def merge_newsubs
2784     save_current_file
2785
2786     sel = $albums_tv.selection.selected_rows
2787
2788     call_backend("booh-backend --merge-config-subdirs '#{$filename}' --dir '#{from_utf8($current_path)}' --for-gui " +
2789                  "--verbose-level #{$verbose_level} #{additional_booh_options}",
2790                  utf8(_("Please wait while scanning source directory...")),
2791                  'one dir scan',
2792                  { :closure_after => proc {
2793                          open_file($filename)
2794                          $albums_tv.selection.select_path(sel[0])
2795                          $modified = true
2796                      } })
2797 end
2798
2799 def merge
2800     save_current_file
2801
2802     theme = $xmldoc.root.attributes['theme']
2803     limit_sizes = $xmldoc.root.attributes['limit-sizes']
2804     if limit_sizes
2805         limit_sizes = "--sizes #{limit_sizes}"
2806     end
2807     call_backend("booh-backend --merge-config '#{$filename}' --for-gui " +
2808                  "--verbose-level #{$verbose_level} --theme #{theme} #{limit_sizes} #{additional_booh_options}",
2809                  utf8(_("Please wait while scanning source directory...")),
2810                  'full scan',
2811                  { :closure_after => proc {
2812                          open_file($filename)
2813                          $modified = true
2814                      } })
2815 end
2816
2817 def save_as_do
2818     fc = Gtk::FileChooserDialog.new(utf8(_("Select a new filename to store this album's properties")),
2819                                     nil,
2820                                     Gtk::FileChooser::ACTION_SAVE,
2821                                     nil,
2822                                     [Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2823     fc.transient_for = $main_window
2824     fc.add_shortcut_folder(File.expand_path("~/.booh"))
2825     fc.set_current_folder(File.expand_path("~/.booh"))
2826     fc.filename = $orig_filename
2827     if fc.run == Gtk::Dialog::RESPONSE_ACCEPT
2828         $orig_filename = fc.filename
2829         save_current_file_user
2830     end
2831     fc.destroy
2832 end
2833
2834 def preferences
2835     dialog = Gtk::Dialog.new(utf8(_("Edit preferences")),
2836                              $main_window,
2837                              Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
2838                              [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
2839                              [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
2840
2841     dialog.vbox.add(notebook = Gtk::Notebook.new)
2842     notebook.append_page(tbl = Gtk::Table.new(0, 0, false), Gtk::Label.new(utf8(_("Options"))))
2843     tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Command for watching videos: ")))),
2844                0, 1, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
2845     tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(video_viewer_entry = Gtk::Entry.new.set_text($config['video-viewer'])),
2846                1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
2847     tooltips = Gtk::Tooltips.new
2848     tooltips.set_tip(video_viewer_entry, utf8(_("Use %f to specify the filename;
2849 for example: /usr/bin/mplayer %f")), nil)
2850     tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Browser's command: ")))),
2851                0, 1, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 2)
2852     tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(browser_entry = Gtk::Entry.new.set_text($config['browser'])),
2853                1, 2, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 2)
2854     tooltips.set_tip(browser_entry, utf8(_("Use %f to specify the filename;
2855 for example: /usr/bin/mozilla-firefox -remote 'openURL(%f,new-window)' || /usr/bin/mozilla-firefox %f")), nil)
2856     tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(smp_check = Gtk::CheckButton.new(utf8(_("Use symmetric multi-processing")))),
2857                0, 1, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2)
2858     tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(smp_hbox = Gtk::HBox.new.add(smp_spin = Gtk::SpinButton.new(2, 16, 1)).add(Gtk::Label.new(utf8(_("processors")))).set_sensitive(false)),
2859                1, 2, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2)
2860     tooltips.set_tip(smp_check, utf8(_("When activated, this option allows the thumbnails creation to run faster. However, if you don't have a multi-processor machine, this will only slow down processing!")), nil)
2861     tbl.attach(nogestures_check = Gtk::CheckButton.new(utf8(_("Disable mouse gestures"))),
2862                0, 2, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 2)
2863     tooltips.set_tip(nogestures_check, utf8(_("Mouse gestures are 'unusual' mouse movements triggering special actions, and are great for speeding up your editions. Get details on available mouse gestures from the Help menu.")), nil)
2864     tbl.attach(emptycomments_check = Gtk::CheckButton.new(utf8(_("Use empty comments for new albums"))),
2865                0, 2, 4, 5, Gtk::FILL, Gtk::SHRINK, 2, 2)
2866     tooltips.set_tip(emptycomments_check, utf8(_("Normally, filenames are used as comments for new albums. Check this if you prefer empty comments.")), nil)
2867     tbl.attach(deleteondisk_check = Gtk::CheckButton.new(utf8(_("Delete original images/videos as well"))),
2868                0, 2, 5, 6, Gtk::FILL, Gtk::SHRINK, 2, 2)
2869     tooltips.set_tip(deleteondisk_check, utf8(_("Normally, deleting an image or video in booh only removes it from the web-album. If you check this option, the original file in source directory will be removed as well. Undo is possible, since actual deletion is performed only when web-album is saved.")), nil)
2870     smp_check.signal_connect('toggled') {
2871         if smp_check.active?
2872             smp_hbox.sensitive = true
2873         else
2874             smp_hbox.sensitive = false
2875         end
2876     }
2877     if $config['mproc']
2878         smp_check.active = true
2879         smp_spin.value = $config['mproc'].to_i
2880     end
2881     nogestures_check.active = $config['nogestures']
2882     emptycomments_check.active = $config['emptycomments']
2883     deleteondisk_check.active = $config['deleteondisk']
2884
2885     notebook.append_page(tbl = Gtk::Table.new(0, 0, false), Gtk::Label.new(utf8(_("Advanced"))))
2886     tbl.attach(Gtk::Label.new.set_markup(utf8(_("Options to pass to <i>convert</i> when\nperforming 'enhance contrast': "))),
2887                0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 2, 2)
2888     tbl.attach(enhance_entry = Gtk::Entry.new.set_text($config['convert-enhance'] || $convert_enhance).set_size_request(250, -1),
2889                1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
2890
2891     dialog.vbox.show_all
2892     dialog.run { |response|
2893         if response == Gtk::Dialog::RESPONSE_OK
2894             $config['video-viewer'] = video_viewer_entry.text
2895             $config['browser'] = browser_entry.text
2896             if smp_check.active?
2897                 $config['mproc'] = smp_spin.value.to_i
2898             else
2899                 $config.delete('mproc')
2900             end
2901             $config['nogestures'] = nogestures_check.active?
2902             $config['emptycomments'] = emptycomments_check.active?
2903             $config['deleteondisk'] = deleteondisk_check.active?
2904
2905             $config['convert-enhance'] = enhance_entry.text
2906         end
2907     }
2908     dialog.destroy
2909 end
2910
2911 def perform_undo
2912     if $undo_tb.sensitive?
2913         $redo_tb.sensitive = $redo_mb.sensitive = true
2914         if not more_undoes = UndoHandler.undo($statusbar)
2915             $undo_tb.sensitive = $undo_mb.sensitive = false
2916         end
2917     end
2918 end
2919
2920 def perform_redo
2921     if $redo_tb.sensitive?
2922         $undo_tb.sensitive = $undo_mb.sensitive = true
2923         if not more_redoes = UndoHandler.redo($statusbar)
2924             $redo_tb.sensitive = $redo_mb.sensitive = false
2925         end
2926     end
2927 end
2928
2929 def show_one_click_explanation(intro)
2930     show_popup($main_window, utf8(_("<b>One-Click tools.</b>
2931
2932 %s When such a tool is activated
2933 (<span foreground='darkblue'>Rotate clockwise</span>, <span foreground='darkblue'>Rotate counter-clockwise</span>, <span foreground='darkblue'>Enhance</span> or <span foreground='darkblue'>Delete</span>), clicking
2934 on a thumbnail will immediately apply the desired action.
2935
2936 Click the <span foreground='darkblue'>None</span> icon when you're finished with One-Click tools.
2937 ") % intro))
2938 end
2939
2940 def get_license
2941     return <<"EOF"
2942                     GNU GENERAL PUBLIC LICENSE
2943                        Version 2, June 1991
2944
2945  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
2946                           675 Mass Ave, Cambridge, MA 02139, USA
2947  Everyone is permitted to copy and distribute verbatim copies
2948  of this license document, but changing it is not allowed.
2949
2950                             Preamble
2951
2952   The licenses for most software are designed to take away your
2953 freedom to share and change it.  By contrast, the GNU General Public
2954 License is intended to guarantee your freedom to share and change free
2955 software--to make sure the software is free for all its users.  This
2956 General Public License applies to most of the Free Software
2957 Foundation's software and to any other program whose authors commit to
2958 using it.  (Some other Free Software Foundation software is covered by
2959 the GNU Library General Public License instead.)  You can apply it to
2960 your programs, too.
2961
2962   When we speak of free software, we are referring to freedom, not
2963 price.  Our General Public Licenses are designed to make sure that you
2964 have the freedom to distribute copies of free software (and charge for
2965 this service if you wish), that you receive source code or can get it
2966 if you want it, that you can change the software or use pieces of it
2967 in new free programs; and that you know you can do these things.
2968
2969   To protect your rights, we need to make restrictions that forbid
2970 anyone to deny you these rights or to ask you to surrender the rights.
2971 These restrictions translate to certain responsibilities for you if you
2972 distribute copies of the software, or if you modify it.
2973
2974   For example, if you distribute copies of such a program, whether
2975 gratis or for a fee, you must give the recipients all the rights that
2976 you have.  You must make sure that they, too, receive or can get the
2977 source code.  And you must show them these terms so they know their
2978 rights.
2979
2980   We protect your rights with two steps: (1) copyright the software, and
2981 (2) offer you this license which gives you legal permission to copy,
2982 distribute and/or modify the software.
2983
2984   Also, for each author's protection and ours, we want to make certain
2985 that everyone understands that there is no warranty for this free
2986 software.  If the software is modified by someone else and passed on, we
2987 want its recipients to know that what they have is not the original, so
2988 that any problems introduced by others will not reflect on the original
2989 authors' reputations.
2990
2991   Finally, any free program is threatened constantly by software
2992 patents.  We wish to avoid the danger that redistributors of a free
2993 program will individually obtain patent licenses, in effect making the
2994 program proprietary.  To prevent this, we have made it clear that any
2995 patent must be licensed for everyone's free use or not licensed at all.
2996
2997   The precise terms and conditions for copying, distribution and
2998 modification follow.
2999
3000
3001                     GNU GENERAL PUBLIC LICENSE
3002    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
3003
3004   0. This License applies to any program or other work which contains
3005 a notice placed by the copyright holder saying it may be distributed
3006 under the terms of this General Public License.  The "Program", below,
3007 refers to any such program or work, and a "work based on the Program"
3008 means either the Program or any derivative work under copyright law:
3009 that is to say, a work containing the Program or a portion of it,
3010 either verbatim or with modifications and/or translated into another
3011 language.  (Hereinafter, translation is included without limitation in
3012 the term "modification".)  Each licensee is addressed as "you".
3013
3014 Activities other than copying, distribution and modification are not
3015 covered by this License; they are outside its scope.  The act of
3016 running the Program is not restricted, and the output from the Program
3017 is covered only if its contents constitute a work based on the
3018 Program (independent of having been made by running the Program).
3019 Whether that is true depends on what the Program does.
3020
3021   1. You may copy and distribute verbatim copies of the Program's
3022 source code as you receive it, in any medium, provided that you
3023 conspicuously and appropriately publish on each copy an appropriate
3024 copyright notice and disclaimer of warranty; keep intact all the
3025 notices that refer to this License and to the absence of any warranty;
3026 and give any other recipients of the Program a copy of this License
3027 along with the Program.
3028
3029 You may charge a fee for the physical act of transferring a copy, and
3030 you may at your option offer warranty protection in exchange for a fee.
3031
3032   2. You may modify your copy or copies of the Program or any portion
3033 of it, thus forming a work based on the Program, and copy and
3034 distribute such modifications or work under the terms of Section 1
3035 above, provided that you also meet all of these conditions:
3036
3037     a) You must cause the modified files to carry prominent notices
3038     stating that you changed the files and the date of any change.
3039
3040     b) You must cause any work that you distribute or publish, that in
3041     whole or in part contains or is derived from the Program or any
3042     part thereof, to be licensed as a whole at no charge to all third
3043     parties under the terms of this License.
3044
3045     c) If the modified program normally reads commands interactively
3046     when run, you must cause it, when started running for such
3047     interactive use in the most ordinary way, to print or display an
3048     announcement including an appropriate copyright notice and a
3049     notice that there is no warranty (or else, saying that you provide
3050     a warranty) and that users may redistribute the program under
3051     these conditions, and telling the user how to view a copy of this
3052     License.  (Exception: if the Program itself is interactive but
3053     does not normally print such an announcement, your work based on
3054     the Program is not required to print an announcement.)
3055
3056
3057 These requirements apply to the modified work as a whole.  If
3058 identifiable sections of that work are not derived from the Program,
3059 and can be reasonably considered independent and separate works in
3060 themselves, then this License, and its terms, do not apply to those
3061 sections when you distribute them as separate works.  But when you
3062 distribute the same sections as part of a whole which is a work based
3063 on the Program, the distribution of the whole must be on the terms of
3064 this License, whose permissions for other licensees extend to the
3065 entire whole, and thus to each and every part regardless of who wrote it.
3066
3067 Thus, it is not the intent of this section to claim rights or contest
3068 your rights to work written entirely by you; rather, the intent is to
3069 exercise the right to control the distribution of derivative or
3070 collective works based on the Program.
3071
3072 In addition, mere aggregation of another work not based on the Program
3073 with the Program (or with a work based on the Program) on a volume of
3074 a storage or distribution medium does not bring the other work under
3075 the scope of this License.
3076
3077   3. You may copy and distribute the Program (or a work based on it,
3078 under Section 2) in object code or executable form under the terms of
3079 Sections 1 and 2 above provided that you also do one of the following:
3080
3081     a) Accompany it with the complete corresponding machine-readable
3082     source code, which must be distributed under the terms of Sections
3083     1 and 2 above on a medium customarily used for software interchange; or,
3084
3085     b) Accompany it with a written offer, valid for at least three
3086     years, to give any third party, for a charge no more than your
3087     cost of physically performing source distribution, a complete
3088     machine-readable copy of the corresponding source code, to be
3089     distributed under the terms of Sections 1 and 2 above on a medium
3090     customarily used for software interchange; or,
3091
3092     c) Accompany it with the information you received as to the offer
3093     to distribute corresponding source code.  (This alternative is
3094     allowed only for noncommercial distribution and only if you
3095     received the program in object code or executable form with such
3096     an offer, in accord with Subsection b above.)
3097
3098 The source code for a work means the preferred form of the work for
3099 making modifications to it.  For an executable work, complete source
3100 code means all the source code for all modules it contains, plus any
3101 associated interface definition files, plus the scripts used to
3102 control compilation and installation of the executable.  However, as a
3103 special exception, the source code distributed need not include
3104 anything that is normally distributed (in either source or binary
3105 form) with the major components (compiler, kernel, and so on) of the
3106 operating system on which the executable runs, unless that component
3107 itself accompanies the executable.
3108
3109 If distribution of executable or object code is made by offering
3110 access to copy from a designated place, then offering equivalent
3111 access to copy the source code from the same place counts as
3112 distribution of the source code, even though third parties are not
3113 compelled to copy the source along with the object code.
3114
3115
3116   4. You may not copy, modify, sublicense, or distribute the Program
3117 except as expressly provided under this License.  Any attempt
3118 otherwise to copy, modify, sublicense or distribute the Program is
3119 void, and will automatically terminate your rights under this License.
3120 However, parties who have received copies, or rights, from you under
3121 this License will not have their licenses terminated so long as such
3122 parties remain in full compliance.
3123
3124   5. You are not required to accept this License, since you have not
3125 signed it.  However, nothing else grants you permission to modify or
3126 distribute the Program or its derivative works.  These actions are
3127 prohibited by law if you do not accept this License.  Therefore, by
3128 modifying or distributing the Program (or any work based on the
3129 Program), you indicate your acceptance of this License to do so, and
3130 all its terms and conditions for copying, distributing or modifying
3131 the Program or works based on it.
3132
3133   6. Each time you redistribute the Program (or any work based on the
3134 Program), the recipient automatically receives a license from the
3135 original licensor to copy, distribute or modify the Program subject to
3136 these terms and conditions.  You may not impose any further
3137 restrictions on the recipients' exercise of the rights granted herein.
3138 You are not responsible for enforcing compliance by third parties to
3139 this License.
3140
3141   7. If, as a consequence of a court judgment or allegation of patent
3142 infringement or for any other reason (not limited to patent issues),
3143 conditions are imposed on you (whether by court order, agreement or
3144 otherwise) that contradict the conditions of this License, they do not
3145 excuse you from the conditions of this License.  If you cannot
3146 distribute so as to satisfy simultaneously your obligations under this
3147 License and any other pertinent obligations, then as a consequence you
3148 may not distribute the Program at all.  For example, if a patent
3149 license would not permit royalty-free redistribution of the Program by
3150 all those who receive copies directly or indirectly through you, then
3151 the only way you could satisfy both it and this License would be to
3152 refrain entirely from distribution of the Program.
3153
3154 If any portion of this section is held invalid or unenforceable under
3155 any particular circumstance, the balance of the section is intended to
3156 apply and the section as a whole is intended to apply in other
3157 circumstances.
3158
3159 It is not the purpose of this section to induce you to infringe any
3160 patents or other property right claims or to contest validity of any
3161 such claims; this section has the sole purpose of protecting the
3162 integrity of the free software distribution system, which is
3163 implemented by public license practices.  Many people have made
3164 generous contributions to the wide range of software distributed
3165 through that system in reliance on consistent application of that
3166 system; it is up to the author/donor to decide if he or she is willing
3167 to distribute software through any other system and a licensee cannot
3168 impose that choice.
3169
3170 This section is intended to make thoroughly clear what is believed to
3171 be a consequence of the rest of this License.
3172
3173
3174   8. If the distribution and/or use of the Program is restricted in
3175 certain countries either by patents or by copyrighted interfaces, the
3176 original copyright holder who places the Program under this License
3177 may add an explicit geographical distribution limitation excluding
3178 those countries, so that distribution is permitted only in or among
3179 countries not thus excluded.  In such case, this License incorporates
3180 the limitation as if written in the body of this License.
3181
3182   9. The Free Software Foundation may publish revised and/or new versions
3183 of the General Public License from time to time.  Such new versions will
3184 be similar in spirit to the present version, but may differ in detail to
3185 address new problems or concerns.
3186
3187 Each version is given a distinguishing version number.  If the Program
3188 specifies a version number of this License which applies to it and "any
3189 later version", you have the option of following the terms and conditions
3190 either of that version or of any later version published by the Free
3191 Software Foundation.  If the Program does not specify a version number of
3192 this License, you may choose any version ever published by the Free Software
3193 Foundation.
3194
3195   10. If you wish to incorporate parts of the Program into other free
3196 programs whose distribution conditions are different, write to the author
3197 to ask for permission.  For software which is copyrighted by the Free
3198 Software Foundation, write to the Free Software Foundation; we sometimes
3199 make exceptions for this.  Our decision will be guided by the two goals
3200 of preserving the free status of all derivatives of our free software and
3201 of promoting the sharing and reuse of software generally.
3202
3203                             NO WARRANTY
3204
3205   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
3206 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
3207 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
3208 PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
3209 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
3210 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
3211 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
3212 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
3213 REPAIR OR CORRECTION.
3214
3215   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
3216 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
3217 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
3218 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
3219 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
3220 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
3221 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
3222 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
3223 POSSIBILITY OF SUCH DAMAGES.
3224 EOF
3225 end
3226
3227 def create_menu_and_toolbar
3228     
3229     #- menu
3230     mb = Gtk::MenuBar.new
3231
3232     filemenu = Gtk::MenuItem.new(utf8(_("_File")))
3233     filesubmenu = Gtk::Menu.new
3234     filesubmenu.append(new       = Gtk::ImageMenuItem.new(Gtk::Stock::NEW))
3235     filesubmenu.append(open      = Gtk::ImageMenuItem.new(Gtk::Stock::OPEN))
3236     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
3237     filesubmenu.append($save     = Gtk::ImageMenuItem.new(Gtk::Stock::SAVE).set_sensitive(false))
3238     filesubmenu.append($save_as  = Gtk::ImageMenuItem.new(Gtk::Stock::SAVE_AS).set_sensitive(false))
3239     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
3240     tooltips = Gtk::Tooltips.new
3241     filesubmenu.append($merge_current = Gtk::ImageMenuItem.new(utf8(_("Merge new/removed images/videos in current subalbum"))).set_sensitive(false))
3242     $merge_current.image = Gtk::Image.new("#{$FPATH}/images/stock-reset-16.png")
3243     tooltips.set_tip($merge_current, utf8(_("Take into account new/removed images/videos in currently viewed subalbum")), nil)
3244     filesubmenu.append($merge_newsubs = Gtk::ImageMenuItem.new(utf8(_("Merge new subalbums (subdirectories) in current subalbum"))).set_sensitive(false))
3245     $merge_newsubs.image = Gtk::Image.new("#{$FPATH}/images/stock-reset-16.png")
3246     tooltips.set_tip($merge_newsubs, utf8(_("Take into account new subalbums in currently viewed subalbum (and only here)")), nil)
3247     filesubmenu.append($merge    = Gtk::ImageMenuItem.new(utf8(_("Scan source directory to merge new subalbums and new/removed images/videos"))).set_sensitive(false))
3248     $merge.image = Gtk::Image.new("#{$FPATH}/images/stock-reset-16.png")
3249     tooltips.set_tip($merge, utf8(_("Take into account new subalbums (subdirectories) and new/removed images/videos in existing subalbums (anywhere)")), nil)
3250     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
3251     filesubmenu.append($generate = Gtk::ImageMenuItem.new(utf8(_("Generate web-album"))).set_sensitive(false))
3252     $generate.image = Gtk::Image.new("#{$FPATH}/images/stock-web-16.png")
3253     tooltips.set_tip($generate, utf8(_("(Re)generate web-album from latest changes into the destination directory")), nil)
3254     filesubmenu.append($view_wa = Gtk::ImageMenuItem.new(utf8(_("View web-album with browser"))).set_sensitive(false))
3255     $view_wa.image = Gtk::Image.new("#{$FPATH}/images/stock-view-webalbum-16.png")
3256     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
3257     filesubmenu.append($properties = Gtk::ImageMenuItem.new(Gtk::Stock::PROPERTIES).set_sensitive(false))
3258     tooltips.set_tip($properties, utf8(_("View and modify properties of the web-album")), nil)
3259     filesubmenu.append(            Gtk::SeparatorMenuItem.new)
3260     filesubmenu.append(quit      = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT))
3261     filemenu.set_submenu(filesubmenu)
3262     mb.append(filemenu)
3263
3264     new.signal_connect('activate') { new_album }
3265     open.signal_connect('activate') { open_file_popup }
3266     $save.signal_connect('activate') { save_current_file_user }
3267     $save_as.signal_connect('activate') { save_as_do }
3268     $merge_current.signal_connect('activate') { merge_current }
3269     $merge_newsubs.signal_connect('activate') { merge_newsubs }
3270     $merge.signal_connect('activate') { merge }
3271     $generate.signal_connect('activate') {
3272         save_current_file
3273         call_backend("booh-backend --config '#{$filename}' --verbose-level #{$verbose_level} #{additional_booh_options}",
3274                      utf8(_("Please wait while generating web-album...\nThis may take a while, please be patient.")),
3275                      'web-album',
3276                      { :successmsg => utf8(_("Your web-album is now ready in directory `%s'.
3277 Click to view it in your browser:") % $xmldoc.root.attributes['destination']),
3278                        :successmsg_linkurl => $xmldoc.root.attributes['destination'],
3279                        :failuremsg => utf8(_("There was something wrong when generating the web-album, sorry.")),
3280                        :closure_after => proc {
3281                              $xmldoc.elements.each('//dir') { |elem|
3282                                  elem.add_attribute('already-generated', 'true')
3283                              }
3284                              UndoHandler.cleanup   #- prevent save_changes to mark current dir as not already generated
3285                              $undo_tb.sensitive = $undo_mb.sensitive = false
3286                              $redo_tb.sensitive = $redo_mb.sensitive = false
3287                              save_current_file
3288                              $generated_outofline = true
3289                          }})
3290     }
3291     $view_wa.signal_connect('activate') {
3292         indexhtml = $xmldoc.root.attributes['destination'] + '/index.html'
3293         if File.exists?(indexhtml)
3294             open_url(indexhtml)
3295         else
3296             show_popup($main_window, utf8(_("Seems like you should generate the web-album first.")))
3297         end
3298     }
3299     $properties.signal_connect('activate') { properties }
3300
3301     quit.signal_connect('activate') { try_quit }
3302
3303     editmenu = Gtk::MenuItem.new(utf8(_("_Edit")))
3304     editsubmenu = Gtk::Menu.new
3305     editsubmenu.append($undo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::UNDO).set_sensitive(false))
3306     editsubmenu.append($redo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::REDO).set_sensitive(false))
3307     editsubmenu.append(           Gtk::SeparatorMenuItem.new)
3308     editsubmenu.append($remove_all_captions = Gtk::ImageMenuItem.new(utf8(_("Remove all captions in this sub-album"))).set_sensitive(false))
3309     $remove_all_captions.image = Gtk::Image.new("#{$FPATH}/images/stock-tool-eraser-16.png")
3310     tooltips.set_tip($remove_all_captions, utf8(_("Mainly useful when you don't want to type any caption, that will remove default captions made of filenames")), nil)
3311     editsubmenu.append(           Gtk::SeparatorMenuItem.new)
3312     editsubmenu.append(prefs    = Gtk::ImageMenuItem.new(Gtk::Stock::PREFERENCES))
3313     editmenu.set_submenu(editsubmenu)
3314     mb.append(editmenu)
3315
3316     $remove_all_captions.signal_connect('activate') { remove_all_captions }
3317
3318     prefs.signal_connect('activate') { preferences }
3319     
3320     helpmenu = Gtk::MenuItem.new(utf8(_("_Help")))
3321     helpsubmenu = Gtk::Menu.new
3322     helpsubmenu.append(one_click = Gtk::ImageMenuItem.new(utf8(_("One-click tools"))))
3323     one_click.image = Gtk::Image.new("#{$FPATH}/images/stock-tools-16.png")
3324     helpsubmenu.append(speed = Gtk::ImageMenuItem.new(utf8(_("Speedup: key shortcuts and mouse gestures"))))
3325     speed.image = Gtk::Image.new("#{$FPATH}/images/stock-info-16.png")
3326     helpsubmenu.append(        Gtk::SeparatorMenuItem.new)
3327     helpsubmenu.append(about = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT))
3328     helpmenu.set_submenu(helpsubmenu)
3329     mb.append(helpmenu)
3330
3331     one_click.signal_connect('activate') {
3332         show_one_click_explanation(_("One-Click tools are available in the toolbar."))
3333     }
3334     
3335     speed.signal_connect('activate') {
3336         show_popup($main_window, utf8(_("<span size='large' weight='bold'>Key shortcuts:</span>
3337
3338 <span foreground='darkblue'>Tab</span>: go to next image caption and select text (begin typing to erase current text!)
3339 <span foreground='darkblue'>Shift-Tab</span>: go to previous image caption
3340 <span foreground='darkblue'>Control-Left/Right/Up/Down</span>: go to specified direction's image caption
3341 <span foreground='darkblue'>Control-Enter</span>: for an image, open larger view; for a video, launch player
3342 <span foreground='darkblue'>Control-Delete</span>: delete image
3343 <span foreground='darkblue'>Shift-Left/Right/Up/Down</span>: move image left/right/up/down
3344 <span foreground='darkblue'>Alt-Left/Right</span>: rotate image clockwise/counter-clockwise
3345 <span foreground='darkblue'>Control-z</span>: undo
3346 <span foreground='darkblue'>Control-r</span>: redo
3347
3348 <span size='large' weight='bold'>Mouse gestures:</span>
3349
3350 Mouse gestures are 'unusual' mouse movements triggering special actions, and are great
3351 for speeding up your editions. If bothered, you can disable them from Edit/Preferences.
3352
3353 <span foreground='darkblue'>Left click, drag to the right, release</span>: rotate image clockwise
3354 <span foreground='darkblue'>Left click, drag to the left, release</span>: rotate image counter-clockwise
3355 <span foreground='darkblue'>Left click, drag to the bottom, release</span>: remove image
3356 <span foreground='darkblue'>Left click, hold left button, right click</span>: undo
3357 <span foreground='darkblue'>Right click, hold right button, left click</span>: redo
3358 ")), { :pos_centered => true, :not_transient => true })
3359     }
3360
3361
3362     about.signal_connect('activate') {
3363         Gtk::AboutDialog.set_url_hook { |dialog, url| open_url(url) }
3364         Gtk::AboutDialog.show($main_window, { :name => 'booh',
3365                                               :version => $VERSION,
3366                                               :copyright => 'Copyright (c) 2005 Guillaume Cottenceau',
3367                                               :license => get_license,
3368                                               :website => 'http://zarb.org/~gc/html/booh.html',
3369                                               :authors => [ 'Guillaume Cottenceau' ],
3370                                               :artists => [ 'Ayo73' ],
3371                                               :comments => utf8(_("''The Web-Album of choice for discriminating Linux users''")),
3372                                               :translator_credits => utf8(_('Japanese: Masao Mutoh
3373 German: Roland Eckert
3374 French: Guillaume Cottenceau')),
3375                                               :logo => Gdk::Pixbuf.new("#{$FPATH}/images/logo.png") })
3376     }
3377
3378
3379     #- toolbar
3380     tb = Gtk::Toolbar.new
3381
3382     tb.insert(-1, open = Gtk::MenuToolButton.new(Gtk::Stock::OPEN))
3383     open.label = utf8(_("Open"))  #- to avoid missing gtk2 l10n catalogs
3384     open.menu = Gtk::Menu.new
3385     open.signal_connect('clicked') { open_file_popup }
3386     open.signal_connect('show-menu') {
3387         lastopens = Gtk::Menu.new
3388         j = 0
3389         if $config['last-opens']
3390             $config['last-opens'].reverse.each { |e|
3391                 lastopens.attach(item = Gtk::ImageMenuItem.new(e, false), 0, 1, j, j + 1)
3392                 item.signal_connect('activate') {
3393                     if ask_save_modifications(utf8(_("Save this album?")),
3394                                               utf8(_("Do you want to save the changes to this album?")),
3395                                               { :yes => Gtk::Stock::YES, :no => Gtk::Stock::NO })
3396                         push_mousecursor_wait
3397                         msg = open_file_user(from_utf8(e))
3398                         pop_mousecursor
3399                         if msg
3400                             show_popup($main_window, msg)
3401                         end
3402                     end
3403                 }
3404                 j += 1
3405             }
3406             lastopens.show_all
3407         end
3408         open.menu = lastopens
3409     }
3410
3411     tb.insert(-1, Gtk::SeparatorToolItem.new)
3412
3413     tb.insert(-1, $r90 = Gtk::ToggleToolButton.new)
3414     $r90.icon_widget = Gtk::Image.new("#{$FPATH}/images/stock-rotate-90-16.png")
3415     $r90.label = utf8(_("Rotate"))
3416     tb.insert(-1, $r270 = Gtk::ToggleToolButton.new)
3417     $r270.icon_widget = Gtk::Image.new("#{$FPATH}/images/stock-rotate-270-16.png")
3418     $r270.label = utf8(_("Rotate"))
3419     tb.insert(-1, $enhance = Gtk::ToggleToolButton.new)
3420     $enhance.icon_widget = Gtk::Image.new("#{$FPATH}/images/stock-channels-16.png")
3421     $enhance.label = utf8(_("Enhance"))
3422     tb.insert(-1, $delete = Gtk::ToggleToolButton.new(Gtk::Stock::DELETE))
3423     $delete.label = utf8(_("Delete"))  #- to avoid missing gtk2 l10n catalogs
3424     tb.insert(-1, nothing = Gtk::ToolButton.new('').set_sensitive(false))
3425     nothing.icon_widget = Gtk::Image.new("#{$FPATH}/images/stock-none-16.png")
3426     nothing.label = utf8(_("None"))
3427
3428     tb.insert(-1, Gtk::SeparatorToolItem.new)
3429
3430     tb.insert(-1, $undo_tb = Gtk::ToolButton.new(Gtk::Stock::UNDO).set_sensitive(false))
3431     tb.insert(-1, $redo_tb = Gtk::ToolButton.new(Gtk::Stock::REDO).set_sensitive(false))
3432
3433
3434     $undo_tb.signal_connect('clicked')  { perform_undo }
3435     $undo_mb.signal_connect('activate') { perform_undo }
3436     $redo_tb.signal_connect('clicked')  { perform_redo }
3437     $redo_mb.signal_connect('activate') { perform_redo }
3438
3439     one_click_explain_try = Proc.new {
3440         if !$config['one-click-explained']
3441             show_one_click_explanation(_("You have just clicked on a One-Click tool."))
3442             $config['one-click-explained'] = true
3443         end
3444     }
3445
3446     $r90.signal_connect('toggled') {
3447         if $r90.active?
3448             set_mousecursor(Gdk::Cursor::SB_RIGHT_ARROW)
3449             one_click_explain_try.call
3450             $r270.active = false
3451             $enhance.active = false
3452             $delete.active = false
3453             nothing.sensitive = true
3454         else
3455             if !$r270.active? && !$enhance.active? && !$delete.active?
3456                 set_mousecursor_normal
3457                 nothing.sensitive = false
3458             else
3459                 nothing.sensitive = true
3460             end
3461         end
3462     }
3463     $r270.signal_connect('toggled') {
3464         if $r270.active?
3465             set_mousecursor(Gdk::Cursor::SB_LEFT_ARROW)
3466             one_click_explain_try.call
3467             $r90.active = false
3468             $enhance.active = false
3469             $delete.active = false
3470             nothing.sensitive = true
3471         else
3472             if !$r90.active? && !$enhance.active? && !$delete.active?
3473                 set_mousecursor_normal
3474                 nothing.sensitive = false
3475             else
3476                 nothing.sensitive = true
3477             end
3478         end
3479     }
3480     $enhance.signal_connect('toggled') {
3481         if $enhance.active?
3482             set_mousecursor(Gdk::Cursor::SPRAYCAN)
3483             one_click_explain_try.call
3484             $r90.active = false
3485             $r270.active = false
3486             $delete.active = false
3487             nothing.sensitive = true
3488         else
3489             if !$r90.active? && !$r270.active? && !$delete.active?
3490                 set_mousecursor_normal
3491                 nothing.sensitive = false
3492             else
3493                 nothing.sensitive = true
3494             end
3495         end
3496     }
3497     $delete.signal_connect('toggled') {
3498         if $delete.active?
3499             set_mousecursor(Gdk::Cursor::PIRATE)
3500             one_click_explain_try.call
3501             $r90.active = false
3502             $r270.active = false
3503             $enhance.active = false
3504             nothing.sensitive = true
3505         else
3506             if !$r90.active? && !$r270.active? && !$enhance.active?
3507                 set_mousecursor_normal
3508                 nothing.sensitive = false
3509             else
3510                 nothing.sensitive = true
3511             end
3512         end
3513     }
3514     nothing.signal_connect('clicked') {
3515         $r90.active = $r270.active = $enhance.active = $delete.active = false
3516