5 # A.k.a 'Best web-album Of the world, Or your money back, Humerus'.
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.
13 # Copyright (c) 2004-2011 Guillaume Cottenceau <http://zarb.org/~gc/resource/gc_mail.png>
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 require 'booh/libadds'
35 bindtextdomain("booh")
37 require 'booh/rexml/document'
40 require 'booh/booh-lib'
42 require 'booh/UndoHandler'
47 [ '--help', '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ],
48 [ '--sort-by-exif-date', '-s', GetoptLong::NO_ARGUMENT, _("Sort entries by EXIF date") ],
49 [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ],
50 [ '--version', '-V', GetoptLong::NO_ARGUMENT, _("Print version and exit") ],
53 $preloader_allowed = false
56 puts _("Usage: %s [OPTION]...") % File.basename($0)
58 printf " %3s, %-15s %s\n", ary[1], ary[0], ary[3]
63 parser = GetoptLong.new
64 parser.set_options(*$options.collect { |ary| ary[0..2] })
66 parser.each_option do |name, arg|
72 when '--sort-by-exif-date'
73 $sort_by_exif_date = true
75 when '--verbose-level'
76 $verbose_level = arg.to_i
79 puts _("Booh version %s
81 Copyright (c) 2005-2011 Guillaume Cottenceau.
82 This is free software; see the source for copying conditions. There is NO
83 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.") % $VERSION
97 meminfo = IO.readlines('/proc/meminfo').join
98 meminfo =~ /MemTotal:.*?(\d+)/ or return -1
100 meminfo =~ /SwapTotal:.*?(\d+)/ or return -1
101 return memory + $1.to_i
105 if $startup_memfree.nil?
106 meminfo = IO.readlines('/proc/meminfo').join
107 meminfo =~ /MemFree:.*?(\d+)/ or return -1
109 meminfo =~ /Buffers:.*?(\d+)/ and buffers = $1
110 meminfo =~ /Cached:.*?(\d+)/ and cached = $1
111 $startup_memfree = memfree.to_i + buffers.to_i + cached.to_i
113 return $startup_memfree
116 def set_cache_memory_use_figure
118 if $config['cache-memory-use'] =~ /memfree_(\d+)/
119 $config['cache-memory-use-figure'] = startup_memfree*$1.to_f/100
121 $config['cache-memory-use-figure'] = $config['cache-memory-use'].to_i
123 #- cannot fork if process is > 0.5 total memory
124 if $config['cache-memory-use-figure'] > total_memory * 0.4
125 $config['cache-memory-use-figure'] = total_memory * 0.4
126 msg 2, _("Cache memory used: %s kB (reduced because cannot exceed 50%% of total memory)") % $config['cache-memory-use-figure']
128 msg 2, _("Cache memory used: %s kB") % $config['cache-memory-use-figure']
134 $config_file = File.expand_path('~/.booh-classifier-rc')
135 if File.readable?($config_file)
136 $xmldoc = REXML::Document.new(File.new($config_file))
137 $xmldoc.root.elements.each { |element|
138 txt = element.get_text
140 if txt.value =~ /~~~/
141 $config[element.name] = txt.value.split(/~~~/)
143 $config[element.name] = txt.value
145 elsif element.elements.size == 0
146 $config[element.name] = ''
148 $config[element.name] = {}
149 element.each { |chld|
151 $config[element.name][chld.name] = txt ? txt.value : nil
156 $config['video-viewer'] ||= '/usr/bin/mplayer %f || /usr/bin/vlc %f'
157 $config['browser'] ||= "/usr/bin/mozilla-firefox -remote 'openURL(%f,new-window)' || /usr/bin/mozilla-firefox %f || /usr/bin/firefox -remote 'openURL(%f,new-window)' || /usr/bin/firefox %f"
158 $config['preload-distance'] ||= '5'
159 $config['cache-memory-use'] ||= 'memfree_80%'
160 $config['rotate-set-exif'] ||= 'true'
161 $config['thumbnails-height'] ||= '64'
162 set_cache_memory_use_figure
166 missing = %w(mplayer).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") }
168 show_popup($main_window, utf8(_("The following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')), { :pos_centered => true })
171 if !system("which exif >/dev/null 2>/dev/null")
172 show_popup($main_window, utf8(_("The program 'exif' is needed to view EXIF data. Please install it.")), { :pos_centered => true })
174 viewer_binary = $config['video-viewer'].split.first
175 if viewer_binary && ! File.executable?(viewer_binary)
176 show_popup($main_window, utf8(_("The configured video viewer seems to be unavailable.
177 You should fix this in Edit/Preferences so that you can view videos.
179 Problem was: '%s' is not an executable file.
180 Hint: don't forget to specify the full path to the executable,
181 e.g. '/usr/bin/mplayer' is correct but 'mplayer' only is not.") % viewer_binary), { :pos_centered => true, :not_transient => true })
187 ios = File.open($config_file, "w")
188 $xmldoc = Document.new "<booh-classifier-rc version='#{$VERSION}'/>"
189 $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET)
190 $config.each_pair { |key, value|
191 elem = $xmldoc.root.add_element key
193 $config[key].each_pair { |subkey, subvalue|
194 subelem = elem.add_element subkey
195 subelem.add_text subvalue.to_s
197 elsif value.is_a? Array
198 elem.add_text value.join('~~~')
203 elem.add_text value.to_s
211 def save_undo(name, closure, *params)
212 UndoHandler.save_undo(name, closure, [ *params ])
213 $undo_mb.sensitive = true
214 $redo_mb.sensitive = false
218 IO.readlines('/proc/self/status').join =~ /VmSize.*?(\d+)\s*kB/
219 msg 3, "VmSize: #{$1}"
224 txt.length > 0 and print txt[0]
225 msg 2, "RSS: #{get_mem}"
231 color.red = [ color.red - 10000, 0 ].max
232 color.green = [ color.green - 10000, 0 ].max
233 color.blue = [ color.blue - 10000, 0 ].max
238 color.red = [ color.red + 10000, 65535 ].min
239 color.green = [ color.green + 10000, 65535 ].min
240 color.blue = [ color.blue + 10000, 65535 ].min
245 $color_red = Gdk::Color.new(65535, 0, 0)
246 $colors = [ Gdk::Color.new(0, 65535, 0),
247 Gdk::Color.new(0, 0, 65535),
248 Gdk::Color.new(65535, 65535, 0),
249 Gdk::Color.new(0, 65535, 65535),
250 Gdk::Color.new(65535, 0, 65535) ]
253 attr_accessor :color, :name, :button, :counter
259 class InterruptedLoading < Exception
260 #- not a StandardError, not catched by a simple rescue
263 def show_pixbufs_present
264 if 3 <= $verbose_level
265 out = 'Full pixbufs ['
266 for entry in $allentries
267 out += entry.pixbuf_full_present? ? 'F' : '.'
275 def Entry.thumbnails_height
276 return $config['thumbnails-height'].to_i
279 attr_accessor :path, :guipath, :type, :angle, :button, :image, :alignment, :removed, :labeled, :loader
281 def initialize(path, type, guipath)
286 @@max_width = $main_window.root_window.size[0] - $labels_vbox.allocation.width - ( $videoborder_pixbuf.width + MainView.borders_thickness) * 2
290 def pixbuf_full_present?
291 return ! @pixbuf_full.nil?
297 msg 3, ">>> free_pixbuf_full #{path}"
304 Gtk.main_iteration while Gtk.events_pending?
305 width, height = $mainview.window.size
306 width = MainView.get_usable_width(width)
307 height = MainView.get_usable_height(height)
308 if @pixbuf_main.nil? || width != @width || height != @height
311 load_into_pixbuf_full #- make sure it is loaded
315 if @pixbuf_full.width.to_f / @pixbuf_full.height > width.to_f / height
316 resized_height = @pixbuf_full.height * (width.to_f/@pixbuf_full.width)
317 if @pixbuf_full.width > width || @pixbuf_full.height > resized_height
318 @pixbuf_main = @pixbuf_full.scale(width, resized_height, Gdk::Pixbuf::INTERP_BILINEAR)
320 @pixbuf_main = @pixbuf_full
323 resized_width = @pixbuf_full.width * (height.to_f/@pixbuf_full.height)
324 if @pixbuf_full.width > resized_width || @pixbuf_full.height > height
325 @pixbuf_main = @pixbuf_full.scale(resized_width, height, Gdk::Pixbuf::INTERP_BILINEAR)
327 @pixbuf_main = @pixbuf_full
333 def pixbuf_main_present?
334 return ! @pixbuf_main.nil?
340 msg 3, ">>> free_pixbuf_main #{path}"
347 Gtk.main_iteration while Gtk.events_pending?
348 if @pixbuf_thumbnail.nil?
350 msg 3, ">>> pixbuf_thumbnail from main #{path}"
351 @pixbuf_thumbnail = @pixbuf_main.scale(@pixbuf_main.width * (Entry.thumbnails_height.to_f/@pixbuf_main.height), Entry.thumbnails_height, Gdk::Pixbuf::INTERP_BILINEAR)
353 msg 3, ">>> pixbuf_thumbnail from file #{path}"
354 @pixbuf_thumbnail = load_into_pixbuf_at_size { |w, h|
356 if h > Entry.thumbnails_height
357 [ w * Entry.thumbnails_height.to_f/h, Entry.thumbnails_height ]
362 if w > Entry.thumbnails_height
363 [ Entry.thumbnails_height, h * Entry.thumbnails_height.to_f/w ]
371 return @pixbuf_thumbnail
373 def free_pixbuf_thumbnail
374 if @pixbuf_thumbnail.nil?
377 msg 3, ">>> free_pixbuf_thumbnail #{path}"
378 @pixbuf_thumbnail = nil
394 if outline_color.nil?
395 button.modify_bg(Gtk::StateType::NORMAL, nil)
396 button.modify_bg(Gtk::StateType::PRELIGHT, nil)
397 button.modify_bg(Gtk::StateType::ACTIVE, nil)
399 button.modify_bg(Gtk::StateType::NORMAL, outline_color)
400 button.modify_bg(Gtk::StateType::PRELIGHT, outline_color.lighter)
401 button.modify_bg(Gtk::StateType::ACTIVE, outline_color)
405 def get_beautified_name
407 size = get_image_size(path)
408 return _("%s (%sx%s, %s KB)") % [@guipath.gsub(/\.[^.]+$/, ''),
411 commify(file_size(path)/1024)]
413 return _("%s (video - %s KB)") % [@guipath.gsub(/\.[^.]+$/, ''),
414 commify(file_size(path)/1024)]
420 #- avoid unneeded memory allocation
421 @loader.signal_handler_disconnect(@area_prepared_cb)
425 #- ignore loader errors, at that point they are fairly normal, we're canceling a partial load
432 def load_into_pixbuf_full
434 msg 3, ">>> load_into_pixbuf_full #{path}"
435 @pixbuf_full = load_into_pixbuf_at_size { |w, h|
438 #- save memory and speedup (+35%) loading
439 [ w * (factor = @@max_width.to_f/w), h * factor ]
445 [ w * (factor = @@max_width.to_f/h), h * factor ]
455 def load_into_pixbuf_at_size(&specify_size)
457 if @video_image_path.nil?
458 orig_base = File.basename(path)
459 tmpdir = gen_video_thumbnail(path, false, 0)
463 @video_image_path = "#{tmpdir}/00000001.jpg"
465 image_path = @video_image_path
471 @angle = guess_rotate(image_path)
477 #- use a pixbuf loader and check Gtk.events_pending? on each chunk, to keep the UI responsive even
478 #- if loaded pictures are several MBs large
480 @loader = Gdk::PixbufLoader.new
481 @loader.signal_connect('size-prepared') { |l, w, h|
482 @loader.set_size(*specify_size.call(w, h))
484 @area_prepared_cb = @loader.signal_connect('area-prepared') { @loaded_pixbuf = @loader.pixbuf }
487 msg 3, "calling load_not_freezing_ui on #{image_path}, offset #{@loader_offset}"
488 @loader_offset = @loader.load_not_freezing_ui(image_path, @loader_offset)
489 if @loader_offset > 0
491 raise InterruptedLoading
494 if @loaded_pixbuf.nil?
495 raise "Loaded pixbuf nil - #{path} #{image_path}"
498 msg 0, "Cannot load #{image_path}: #{$!}"
501 if @video_image_path && @loader.nil?
502 File.delete(@video_image_path)
503 Dir.rmdir(File.dirname(@video_image_path))
504 @video_image_path = nil
509 msg 3, ">>> load_into_pixbuf_full #{image_path} => rotate #{@angle}"
510 @loaded_pixbuf = rotate_pixbuf(@loaded_pixbuf, @angle)
513 retval = @loaded_pixbuf
528 msg 3, "GC in #{Time.now - start} s"
531 def free_cache_if_needed
532 i = $allentries.index($mainview.get_shown_entry)
534 if get_mem > $config['cache-memory-use-figure']
535 msg 3, "too much RSS, triggering GC"
538 if get_mem < $config['cache-memory-use-figure']
541 msg 3, "too much RSS, freeing some cache"
544 ($allentries.size - 1).downto($config['preload-distance'].to_i + 1) { |j|
546 if i + j < $allentries.size
547 $allentries[i + j].free_pixbuf_full
548 if $allentries[i + j].free_pixbuf_main
553 $allentries[i - j].free_pixbuf_full
554 if $allentries[i - j].free_pixbuf_main
560 if get_mem < $config['cache-memory-use-figure'] * 3 / 4
561 msg 3, "RSS down enough - freeing done in #{Time.now - start} s"
568 msg 3, "freeing done in #{Time.now - start} s"
572 def run_preloader_real
573 msg 3, "*** >> main preloading triggered..."
574 if $mainview.get_shown_entry
576 if $config['preload-distance'].to_i == 0
579 index = $allentries.index($mainview.get_shown_entry)
587 while ! right_done || ! left_done
590 while index_right < $allentries.size && ! visible($allentries[index_right])
593 if index_right == $allentries.size
596 if ! $allentries[index_right].pixbuf_main_present?
597 msg 3, "preloading #{$allentries[index_right].path}"
599 $allentries[index_right].pixbuf_main
600 rescue InterruptedLoading
601 msg 3, "*** >>>> interrupted, rerun"
605 loaded << index_right
607 if loaded_right == $config['preload-distance'].to_i
615 while index_left >= 0 && ! visible($allentries[index_left])
621 if ! $allentries[index_left].pixbuf_main_present?
622 msg 3, "preloading #{$allentries[index_left].path}"
624 $allentries[index_left].pixbuf_main
625 rescue InterruptedLoading
626 msg 3, "*** >>>> interrupted, rerun"
632 if loaded_left == $config['preload-distance'].to_i
638 #- in case just loaded another directory
639 if $preloader_force_exit
640 $preloader_force_exit = false
643 #- in case moved fast
644 if index != $allentries.index($mainview.get_shown_entry)
645 msg 3, "*** >>>> moved already, rerun"
650 msg 3, "*** << main preloading finished"
655 if ! $preloader_allowed
656 msg 3, "*** preloader not yet allowed"
660 if $preloader_running
661 msg 3, "preloader already running"
664 msg 3, "run preloader"
665 $preloader_running = true
667 msg 3, "begin preloader from timeout "
668 if run_preloader_real
669 $preloader_running = false
677 class MainView < Gtk::DrawingArea
679 @@borders_thickness = 5
680 @@borders_length = 25
681 @@redraw_pending = nil
683 def MainView.borders_thickness
684 return @@borders_thickness
687 def MainView.get_usable_width(available_width)
688 return available_width - ($videoborder_pixbuf.width + @@borders_thickness) * 2
691 def MainView.get_usable_height(available_height)
692 return available_height - @@borders_thickness * 2
697 signal_connect('expose-event') { draw }
698 signal_connect('configure-event') { update_shown }
701 def try_show_entry(entry)
702 if entry && entry.button
703 if entry.button.has_focus?
706 entry.button.grab_focus
711 def set_shown_entry(entry)
713 if entry && entry == @entry
716 if entry && ! entry.button
721 @entry and msg 3, "*** set entry to #{@entry.path}"
723 msg 3, "entry shown in: #{Time.now - t1} s"
730 def show_next_entry(entry)
731 index = $allentries.index(entry)
732 if index < $allentries.size - 1
735 while index < $allentries.size - 1 && $allentries[index] && $allentries[index].button && ! $allentries[index].button.visible?
738 while $allentries[index] && $allentries[index].button && ! $allentries[index].button.visible? && index > 0
741 if index < $allentries.size && $allentries[index] && $allentries[index].button && $allentries[index].button.visible?
742 try_show_entry($allentries[index])
745 #- find a fallback before
746 while index < $allentries.size && index > 0 && $allentries[index] && (! $allentries[index].button || ! $allentries[index].button.visible?)
749 if index < $allentries.size && index > 0 && $allentries[index] && $allentries[index].button && $allentries[index].button.visible?
750 try_show_entry($allentries[index])
756 msg 3, "redraw already pending"
760 @@redraw_pending = Gtk.idle_add {
761 msg 3, "begin redraw from timeout "
763 msg 3, "try redraw from timeout"
765 @@redraw_pending = nil
768 rescue InterruptedLoading
769 msg 3, "interrupted, will retry"
776 @entry and sb_msg(_("Selected %s") % @entry.get_beautified_name)
781 window.begin_paint(Gdk::Rectangle.new(0, 0, w, h))
789 msg 3, "################################################ trying to show #{@entry.path}"
790 pixbuf = @entry.pixbuf_main
793 width, height = window.size
794 @xpos = (width - @pixbuf.width)/2
795 @ypos = (height - @pixbuf.height)/2
808 window.draw_pixbuf(nil, @pixbuf, 0, 0, @xpos, @ypos, -1, -1, Gdk::RGB::DITHER_NONE, -1, -1)
809 if @entry && @entry.type == 'video'
810 window.draw_borders($videoborder_pixbuf, @xpos - $videoborder_pixbuf.width, @xpos + @pixbuf.width, @ypos, @ypos + @pixbuf.height)
812 if @entry && ! @entry.outline_color.nil?
813 gc = Gdk::GC.new(window)
814 colormap.alloc_color(@entry.outline_color, false, true)
815 gc.set_foreground(@entry.outline_color)
816 if @entry.type == 'video'
817 xleft = @xpos - $videoborder_pixbuf.width
818 xright = @xpos + @pixbuf.width + $videoborder_pixbuf.width
821 xright = @xpos + @pixbuf.width
823 window.draw_polygon(gc, true, [[xleft - @@borders_thickness, @ypos - @@borders_thickness],
824 [xright + @@borders_thickness, @ypos - @@borders_thickness],
825 [xright + @@borders_thickness, @ypos + @pixbuf.height + @@borders_thickness],
826 [xleft - @@borders_thickness, @ypos + @pixbuf.height + @@borders_thickness],
827 [xleft - @@borders_thickness, @ypos - 1],
828 [xleft - 1, @ypos - 1],
829 [xleft - 1, @ypos + @pixbuf.height + 1],
830 [xright + 1, @ypos + @pixbuf.height + 1],
831 [xright + 1, @ypos - 1],
832 [xleft - @@borders_thickness, @ypos - 1]])
838 def autoscroll_if_needed(button, center)
839 xpos_left = button.allocation.x
840 xpos_right = button.allocation.x + button.allocation.width
841 hadj = $imagesline_sw.hadjustment
842 current_minx_visible = hadj.value
843 current_maxx_visible = hadj.value + hadj.page_size
845 if xpos_left < current_minx_visible
847 newval = hadj.value - (current_minx_visible - xpos_left)
849 elsif xpos_right > current_maxx_visible
851 newval = hadj.value + (xpos_right - current_maxx_visible)
852 if newval > hadj.upper - hadj.page_size
853 newval = hadj.upper - hadj.page_size
858 hadj.value = clamp((xpos_left + xpos_right) / 2 - hadj.page_size / 2, 0, hadj.upper - hadj.page_size)
862 def show_popup(parent, msg, *options)
863 dialog = Gtk::Dialog.new
870 dialog.title = options[:title]
872 dialog.title = utf8(_("Booh message"))
875 if options[:nomarkup]
880 if options[:centered]
881 lbl.set_justify(Gtk::Justification::CENTER)
883 if options[:selectable]
884 lbl.selectable = true
886 if options[:scrolled]
887 sw = Gtk::ScrolledWindow.new(nil, nil)
888 sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
889 sw.add_with_viewport(lbl)
891 dialog.set_default_size(500, 600)
894 dialog.set_default_size(200, 120)
896 if options[:bottomwidget]
897 dialog.vbox.add(options[:bottomwidget])
899 if options[:okcancel]
900 cancel = dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL)
901 dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK)
902 elsif options[:yestoall]
903 cancel = dialog.add_button(Gtk::Stock::NO, Gtk::Dialog::RESPONSE_NO)
904 if ! options[:bottomwidget]
907 dialog.add_button(Gtk::Stock::YES, Gtk::Dialog::RESPONSE_YES)
908 dialog.add_button(utf8(_("Yes to all")), Gtk::Dialog::RESPONSE_ACCEPT)
910 ok = dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK)
911 if ! options[:bottomwidget]
916 if options[:pos_centered]
917 dialog.window_position = Gtk::Window::POS_CENTER
919 dialog.window_position = Gtk::Window::POS_MOUSE
923 linkbut = Gtk::Button.new('')
924 linkbut.child.markup = "<span foreground=\"#00000000FFFF\" underline=\"single\">#{options[0][:linkurl]}</span>"
925 linkbut.signal_connect('clicked') {
926 open_url(options[0][:linkurl] + '/index.html')
927 dialog.response(Gtk::Dialog::RESPONSE_OK)
928 set_mousecursor_normal
930 linkbut.relief = Gtk::RELIEF_NONE
931 linkbut.signal_connect('enter-notify-event') { set_mousecursor(Gdk::Cursor::HAND2, linkbut); false }
932 linkbut.signal_connect('leave-notify-event') { set_mousecursor(nil, linkbut); false }
933 dialog.vbox.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(linkbut))
938 if options[:stuff_connector]
939 options[:stuff_connector].call({ :dialog => dialog })
942 if !options[:not_transient]
943 dialog.transient_for = parent
944 dialog.run { |response|
945 if options[:data_getter]
946 options[:data_getter].call
949 if options[:okcancel]
950 return response == Gtk::Dialog::RESPONSE_OK
951 elsif options[:yestoall]
952 return response == Gtk::Dialog::RESPONSE_YES ? 'yes' : response == Gtk::Dialog::RESPONSE_ACCEPT ? 'yestoall' : 'no'
956 dialog.signal_connect('response') { dialog.destroy }
960 def view_entry(entry)
961 if entry.type == 'image'
962 show_popup($main_window,
963 utf8(`exif -m '#{entry.path}'`),
964 { :title => utf8(_("EXIF data of %s") % File.basename(entry.path)), :nomarkup => true, :scrolled => true, :not_transient => true })
966 cmd = from_utf8($config['video-viewer']).gsub('%f', "'#{entry.path}'") + ' &'
974 $allentries.each { |entry|
975 if ! entry.removed && entry.labeled.nil?
979 $unlabelled_counter.set_markup('<tt>' + value.to_s + '</tt>')
981 $allentries.each { |entry|
986 $toremove_counter.set_markup('<tt>' + value.to_s + '</tt>')
987 $labels.values.each { |label|
989 $allentries.each { |entry|
990 if entry.labeled == label
994 label.counter.set_markup('<tt>' + value.to_s + '</tt>')
998 def thumbnail_keypressed(entry, event)
999 if event.state & Gdk::Window::MOD1_MASK != 0
1000 #- ALT pressed: Alt-Left and Alft-Right rotate
1001 if event.keyval == Gdk::Keyval::GDK_Left || event.keyval == Gdk::Keyval::GDK_Right
1002 if event.keyval == Gdk::Keyval::GDK_Left
1003 entry.angle = (entry.angle - 90) % 360
1005 entry.angle = (entry.angle + 90) % 360
1007 entry.free_pixbuf_full
1008 entry.free_pixbuf_main
1009 entry.free_pixbuf_thumbnail
1010 show_pixbufs_present
1012 entry.image.pixbuf = entry.pixbuf_thumbnail
1013 if $config['rotate-set-exif'] == 'true' && entry.type == 'image'
1014 Exif.set_orientation(entry.path, angle_to_exif_orientation(entry.angle))
1018 elsif event.state & Gdk::Window::CONTROL_MASK != 0
1019 #- CONTROL pressed: Ctrl-z and Ctrl-r for undo/redo, Ctrl-space for recentre
1020 if event.keyval == Gdk::Keyval::GDK_z
1023 if event.keyval == Gdk::Keyval::GDK_r
1026 if event.keyval == Gdk::Keyval::GDK_space
1027 shown = $mainview.get_shown_entry
1028 shown and autoscroll_if_needed(shown.button, true)
1032 removed_before = entry.removed
1033 label_before = entry.labeled
1035 if event.keyval == Gdk::Keyval::GDK_Delete
1036 if ! FileTest.writable?(entry.path)
1037 show_popup($main_window, utf8(_("Notice: no write access to '%s', permission will be denied at execute step.") % entry.path))
1039 entry.removed = true
1042 update_visibility(entry)
1044 $mainview.show_next_entry(entry)
1046 save_undo(_("set for removal"),
1048 entry.removed = removed_before
1049 entry.labeled = label_before
1051 update_visibility(entry)
1053 if entry.button.visible?
1054 $mainview.try_show_entry(entry)
1057 entry.removed = true
1060 update_visibility(entry)
1062 if entry.button.visible?
1063 $mainview.try_show_entry(entry)
1068 elsif event.keyval == Gdk::Keyval::GDK_space
1070 msg = _("Cleared label")
1072 msg = _("Cleared set for removal")
1074 entry.removed = false
1078 $mainview.show_next_entry(entry)
1082 entry.removed = removed_before
1083 entry.labeled = label_before
1086 $mainview.try_show_entry(entry)
1088 entry.removed = false
1092 $mainview.try_show_entry(entry)
1096 elsif event.keyval == Gdk::Keyval::GDK_Return
1099 elsif event.keyval == Gdk::Keyval::GDK_Home
1101 while $allentries[index] && $allentries[index].button && !visible($allentries[index])
1104 if $allentries[index] && $allentries[index].button
1105 $allentries[index].button.grab_focus
1108 elsif event.keyval == Gdk::Keyval::GDK_End
1109 index = $allentries.size - 1
1110 while $allentries[index] && ! $allentries[index].button
1114 while $allentries[index] && $allentries[index].button && !visible($allentries[index])
1117 if $allentries[index] && $allentries[index].button
1118 $allentries[index].button.grab_focus
1122 char = [ Gdk::Keyval.to_unicode(event.keyval) ].pack("C*")
1123 if char =~ /^[a-zA-z0-9]$/
1124 label = $labels[char]
1127 vb = Gtk::VBox.new(false, 0)
1128 vb.pack_start(labelentry = Gtk::Entry.new.set_text(char), false, false)
1129 vb.pack_start(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(bt = Gtk::ColorButton.new))
1130 color = bt.color = Gdk::Color.new(16384 + rand(49151), 16384 + rand(49151), 16384 + rand(49151))
1131 bt.signal_connect('color-set') { color = bt.color }
1133 labelentry.signal_connect('changed') { #- cannot add a new label with first letter of an existing label
1134 while $labels.has_key?(labelentry.text[0,1])
1135 labelentry.text = labelentry.text.sub(/./, '')
1138 if show_popup($main_window,
1139 utf8(_("You typed the text character '%s', which is not associated with a label.\nType in the full name of the label below to create a new one.")) % char,
1140 { :okcancel => true, :bottomwidget => vb, :data_getter => proc { text = labelentry.text },
1141 :stuff_connector => proc { |stuff| labelentry.select_region(0, 0)
1142 labelentry.position = -1
1143 labelentry.signal_connect('activate') { stuff[:dialog].response(Gtk::Dialog::RESPONSE_OK) } } } )
1145 char = text[0,1] #- in case it changed
1146 label = Label.new(text)
1148 $labels[char] = label
1149 $ordered_labels << label
1150 lbl = Gtk::Label.new.set_markup('<b>(' + char + ')</b>' + text[1..-1]).set_justify(Gtk::Justification::CENTER)
1151 $labels_vbox.pack_start(Gtk::HBox.new(false, 5).pack_start(label.button = Gtk::CheckButton.new.add(evt = Gtk::EventBox.new.add(lbl))).
1152 pack_start(Gtk::Label.new, true, true).
1153 pack_start(label.counter = Gtk::Label.new.set_markup('<tt>0</tt>'), false, false).show_all)
1154 label.button.active = true
1155 label.button.signal_connect('toggled') { update_all_visibilities }
1156 evt.modify_bg(Gtk::StateType::NORMAL, label.color)
1157 evt.modify_bg(Gtk::StateType::PRELIGHT, label.color.lighter.lighter)
1158 evt.modify_bg(Gtk::StateType::ACTIVE, label.color.lighter)
1164 entry.removed = false
1165 entry.labeled = label
1167 update_visibility(entry)
1169 $mainview.show_next_entry(entry)
1171 save_undo(_("set label"),
1173 entry.removed = removed_before
1174 entry.labeled = label_before
1176 update_visibility(entry)
1178 if entry.button.visible?
1179 $mainview.try_show_entry(entry)
1182 entry.removed = false
1183 entry.labeled = label
1185 update_visibility(entry)
1187 if entry.button.visible?
1188 $mainview.try_show_entry(entry)
1201 $statusbar.push(0, utf8(msg))
1205 def show_entry(entry, i, tips)
1207 #msg 3, "showing entry #{entry}"
1208 entry.image = Gtk::Image.new(entry.pixbuf_thumbnail)
1209 if entry.type == 'video'
1210 entry.button = Gtk::Button.new.add(Gtk::HBox.new.pack_start(da1 = Gtk::DrawingArea.new.set_size_request($videoborder_pixbuf.width, -1), false, false).
1211 pack_start(entry.image).
1212 pack_start(da2 = Gtk::DrawingArea.new.set_size_request($videoborder_pixbuf.width, -1), false, false))
1213 da1.signal_connect('realize') { da1.window.set_back_pixmap($videoborder_pixmap, false) }
1214 da2.signal_connect('realize') { da2.window.set_back_pixmap($videoborder_pixmap, false) }
1216 entry.button = Gtk::Button.new.add(entry.image)
1218 tips.set_tip(entry.button, entry.get_beautified_name, nil)
1219 $imagesline.pack_start(entry.alignment = Gtk::Alignment.new(0.5, 1, 0, 0).add(entry.button).show_all, false, false)
1220 entry.button.signal_connect('clicked') {
1221 shown = $mainview.get_shown_entry
1223 shown and shown.alignment.set(0.5, 1, 0, 0)
1224 entry.alignment.set(0.5, 0, 0, 0)
1225 autoscroll_if_needed(entry.button, false)
1226 $mainview.set_shown_entry(entry)
1229 entry.button.signal_connect('button-press-event') { |w, event|
1230 if entry.type == 'video' && event.event_type == Gdk::Event::BUTTON2_PRESS
1234 entry.button.signal_connect('focus-in-event') { entry.button.clicked }
1235 entry.button.signal_connect('key-press-event') { |w, e| thumbnail_keypressed(entry, e) }
1237 entry.button.grab_focus
1239 update_visibility(entry)
1242 def show_entries(allentries)
1244 sb_msg(_("Loading images..."))
1245 $loading_progressbar.fraction = 0
1246 $loading_progressbar.text = utf8(_("Loading... %d%") % 0)
1247 $loading_progressbar.show
1249 total_loaded_files = 0
1250 total_loaded_size = 0
1252 tips = Gtk::Tooltips.new
1253 while i < allentries.size
1255 entry = allentries[i]
1257 loaded_pixbuf = entry.pixbuf_main
1259 loaded_pixbuf = entry.pixbuf_thumbnail
1261 rescue InterruptedLoading
1265 if $allentries != allentries
1266 #- loaded another directory while this one was not yet finished
1267 msg 3, "allentries differ, stopping this deprecated load"
1272 show_entry(entry, i, tips)
1273 if $allentries != allentries
1274 #- loaded another directory while this one was not yet finished
1275 msg 3, "allentries differ, stopping this deprecated load"
1279 total_loaded_size += file_size(entry.path)
1280 total_loaded_files += 1
1282 if i > $config['preload-distance'].to_i && i <= $config['preload-distance'].to_i * 2
1283 #- when we're at preload distance, begin preloading to preload distance
1285 allentries[i - $config['preload-distance'].to_i].pixbuf_main
1286 rescue InterruptedLoading
1289 if i == $config['preload-distance'].to_i * 2 + 1
1290 #- when we're after double preload distance, activate normal preloading
1291 $preloader_allowed = true
1295 allentries.delete_at(i)
1297 $loading_progressbar.fraction = i.to_f / allentries.size
1298 $loading_progressbar.text = utf8(_("Loading... %d%") % (100 * $loading_progressbar.fraction))
1306 $preloader_allowed = true
1307 if i <= $config['preload-distance'].to_i * 2
1308 #- not yet preloaded correctly
1311 sb_msg(_("%d images of total %s kB loaded in %3.2f seconds.") % [ total_loaded_files, commify(total_loaded_size / 1024), Time.now - t1 ])
1312 $loading_progressbar.hide
1313 $execute.sensitive = true
1319 $mainview.set_shown_entry(nil)
1321 $preloader_allowed = false
1322 $execute.sensitive = false
1325 def open_dir(*paths)
1326 #- remove visual stuff, so that user will see something is happening
1328 sb_msg(_("Scanning source directory... %s") % "")
1329 Gtk.main_iteration while Gtk.events_pending?
1332 path = File.expand_path(path.sub(%r|/$|, ''))
1335 if File.directory?(path)
1336 examined_dirs = `find '#{path}' -type d -follow`.split("\n").sort.collect { |v| v.chomp }
1338 examined_dirs.each { |dir|
1340 return utf8(_("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir)
1343 Dir.entries(dir).each { |file|
1344 if file =~ /'/ && type = entry2type(file) && type == 'video'
1345 return utf8(_("Videos can't contain a single quote character ('), sorry: %s") % "#{dir}/#{file}")
1349 puts "Failed to open directory #{dir}: #{$!}"
1353 #- scan for populate second
1354 examined_dirs.each { |dir|
1355 if File.basename(dir) =~ /^\./
1356 msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir
1360 entries += Dir.entries(dir).collect { |file| File.join(dir, file) }
1362 #- already puts'ed 10 lines upper
1364 sb_msg(_("Scanning source directory... %s") % (_("%d entries found") % entries.size))
1365 Gtk.main_iteration while Gtk.events_pending?
1372 if $sort_by_exif_date
1374 entries.each { |file|
1375 date_time = Exif.datetimeoriginal(file)
1377 dates[file] = date_time
1380 entries = smartsort(entries, dates)
1384 entries.each { |file|
1385 type = entry2type(file)
1387 if File.directory?(path)
1388 $allentries << Entry.new(file, type, file[path.length + 1 .. -1])
1390 $allentries << Entry.new(file, type, file)
1399 fc = Gtk::FileChooserDialog.new(utf8(_("Specify the directory to work with")),
1401 Gtk::FileChooser::ACTION_SELECT_FOLDER,
1403 [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
1404 fc.transient_for = $main_window
1406 fc.current_folder = $workingdir
1411 if fc.run == Gtk::Dialog::RESPONSE_ACCEPT && fc.filename
1412 msg = open_dir(fc.filename)
1426 show_entries($allentries)
1430 def try_quit(*options)
1431 if ! $allentries.detect { |e| e.removed || e.labeled } || show_popup($main_window,
1432 utf8(_("You have not executed the classification. Are you sure you want to quit?")),
1433 { :okcancel => true })
1443 dialog = Gtk::Dialog.new
1444 dialog.title = utf8(_("Booh message"))
1446 vb1 = Gtk::VBox.new(false, 5)
1447 label = Gtk::Label.new.set_markup(utf8(_("You're about to <b>execute</b> actions on the marked images.\nPlease confirm below the actions. You cannot undo this operation!")))
1448 vb1.pack_start(label, false, false)
1450 lastpath = $workingdir
1452 table = Gtk::Table.new(0, 0, false)
1453 table.set_row_spacings(5)
1454 table.set_column_spacings(5)
1455 table.attach(Gtk::Label.new.set_markup(utf8(_("<b>Label name:</b>"))).set_justify(Gtk::Justification::CENTER), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0)
1456 table.attach(Gtk::Label.new.set_markup(utf8(_("<b>Amount of pictures:</b>"))).set_justify(Gtk::Justification::CENTER), 1, 2, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0)
1457 table.attach(Gtk::Label.new.set_markup(utf8(_("<b>Pictures examples:</b>"))).set_justify(Gtk::Justification::CENTER), 2, 3, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0)
1458 table.attach(Gtk::Label.new.set_markup(utf8(_("<b>Action to perform:</b>"))).set_justify(Gtk::Justification::CENTER), 3, 4, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0)
1459 add_row = proc { |row, name, color, truthproc, normal|
1460 table.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(Gtk::EventBox.new.add(Gtk::Label.new.set_markup(name)).modify_bg(Gtk::StateType::NORMAL, color)),
1461 0, 1, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5)
1463 examples = Gtk::HBox.new(false, 5)
1464 $allentries.each { |entry|
1465 if truthproc.call(entry)
1468 thumbnail = Gtk::Image.new(entry.pixbuf_thumbnail)
1469 if entry.type == 'video'
1470 thumbnail = Gtk::HBox.new.pack_start(da1 = Gtk::DrawingArea.new.set_size_request($videoborder_pixbuf.width, -1), false, false).
1471 pack_start(thumbnail).
1472 pack_start(da2 = Gtk::DrawingArea.new.set_size_request($videoborder_pixbuf.width, -1), false, false)
1473 da1.signal_connect('realize') { da1.window.set_back_pixmap($videoborder_pixmap, false) }
1474 da2.signal_connect('realize') { da2.window.set_back_pixmap($videoborder_pixmap, false) }
1476 examples.pack_start(thumbnail, false, false)
1478 examples.pack_start(Gtk::Label.new.set_markup("<b>...</b>"), false, false)
1482 table.attach(Gtk::Label.new(counter.to_s).set_justify(Gtk::Justification::CENTER), 1, 2, row, row + 1, 0, 0, 5, 5)
1483 table.attach(examples, 2, 3, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5)
1489 combostore = Gtk::ListStore.new(Gdk::Pixbuf, String)
1490 iter = combostore.append
1492 iter[0] = $main_window.render_icon(Gtk::Stock::PASTE, Gtk::IconSize::MENU)
1493 iter[1] = utf8(_("Copy to:"))
1494 iter = combostore.append
1495 iter[0] = $main_window.render_icon(Gtk::Stock::GO_FORWARD, Gtk::IconSize::MENU)
1496 iter[1] = utf8(_("Move to:"))
1498 iter[0] = $main_window.render_icon(Gtk::Stock::DELETE, Gtk::IconSize::MENU)
1499 iter[1] = utf8(_("Permanently remove"))
1501 iter = combostore.append
1502 iter[0] = $main_window.render_icon(Gtk::Stock::MEDIA_STOP, Gtk::IconSize::MENU)
1503 iter[1] = utf8(_("Do nothing"))
1504 combo = Gtk::ComboBox.new(combostore)
1506 renderer = Gtk::CellRendererPixbuf.new
1507 combo.pack_start(renderer, false)
1508 combo.set_attributes(renderer, :pixbuf => 0)
1509 renderer = Gtk::CellRendererText.new
1510 combo.pack_start(renderer, true)
1511 combo.set_attributes(renderer, :text => 1)
1514 pathbutton = Gtk::Button.new.add(pathlabel = Gtk::Label.new.set_markup(utf8(_("<i>(unset)</i>"))))
1515 pathbutton.signal_connect('clicked') {
1516 fc = Gtk::FileChooserDialog.new(utf8(_("Specify the directory where to move the pictures to")),
1518 Gtk::FileChooser::ACTION_SELECT_FOLDER,
1520 [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
1521 fc.transient_for = dialog
1523 fc.current_folder = lastpath
1525 if fc.run == Gtk::Dialog::RESPONSE_ACCEPT && fc.filename
1526 pathlabel.text = fc.filename
1527 pathlabel.set_alignment(0, 0.5)
1529 lastpath = fc.filename
1532 combo.signal_connect('changed') {
1533 pathbutton.sensitive = combo.active <= 1
1535 vb = Gtk::VBox.new(false, 5)
1536 vb.pack_start(combo, false, false)
1537 vb.pack_start(pathbutton, false, false)
1538 table.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(vb), 3, 4, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5)
1539 { :combo => combo, :pathlabel => pathlabel }
1541 table.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(combo), 3, 4, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5)
1546 stuff['toremove'] = add_row.call(1, utf8(_("<i>to remove</i>")), $color_red, proc { |entry| entry.removed }, false)
1547 $ordered_labels.each_with_index { |label, row| stuff[label] = add_row.call(row + 2, label.name, label.color, proc { |entry| entry.labeled == label }, true) }
1548 vb1.pack_start(sw = Gtk::ScrolledWindow.new(nil, nil).add_with_viewport(table).set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC), true, true)
1550 toremove_amount = $allentries.find_all { |entry| entry.removed }.size
1551 toremove_size = commify($allentries.find_all { |entry| entry.removed }.collect { |entry| file_size(entry.path) }.sum / 1024)
1552 check_removal = Gtk::CheckButton.new(utf8(_("I have noticed I am about to permanently remove the %d above mentioned pictures (total %s kB).") % [ toremove_amount, toremove_size ]))
1553 if toremove_amount > 0
1554 vb1.pack_start(check_removal, false, false)
1555 stuff['toremove'][:combo].signal_connect('changed') { |widget|
1556 check_removal.sensitive = widget.active == 0
1560 dialog.vbox.add(vb1)
1562 dialog.set_default_size(800, 600)
1563 dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL)
1564 dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK)
1565 dialog.window_position = Gtk::Window::POS_MOUSE
1566 dialog.transient_for = $main_window
1571 dialog.run { |response|
1572 if response == Gtk::Dialog::RESPONSE_OK
1574 if toremove_amount > 0 && stuff['toremove'][:combo].active == 0
1575 if ! check_removal.active?
1576 show_popup(dialog, utf8(_("You have not confirmed that you noticed the permanent removal of the pictures marked for deletion.")))
1580 $allentries.each { |entry|
1582 if ! FileTest.writable?(entry.path)
1583 show_popup(dialog, utf8(_("Sorry, permission denied to remove '%s'.") % [ entry.path ]))
1591 $labels.values.each { |label| label2entries[label] = [] }
1592 $allentries.each { |entry| entry.labeled and label2entries[entry.labeled] << entry }
1593 stuff.keys.each { |key|
1594 if key.is_a?(Label) && stuff[key][:combo] && stuff[key][:combo].active <= 1
1595 destination = stuff[key][:pathlabel].text
1596 if destination[0] != ?/
1597 show_popup(dialog, utf8(_("You have not selected a directory where to move/copy %s.") % key.name))
1602 Dir.mkdir(destination)
1606 st = File.stat(destination)
1608 show_popup(dialog, utf8(_("Directory %s, where to move/copy %s, is not valid or not createable.") % [destination, key.name]))
1612 if ! st.directory? || ! writable(destination)
1613 show_popup(dialog, utf8(_("Directory %s, where to move/copy %s, is not valid or not writable.") % [destination, key.name]))
1617 label2entries[key].each { |entry|
1619 File.stat(File.join(destination, File.basename(entry.path)))
1620 show_popup(dialog, utf8(_("Sorry, a file '%s' already exists in directory '%s'.") % [ File.basename(entry.path), destination ]))
1626 if stuff[key][:combo].active == 1
1627 label2entries[key].each { |entry|
1628 if ! FileTest.writable?(entry.path)
1629 show_popup(dialog, utf8(_("Sorry, permission denied to move '%s'.") % [ entry.path ]))
1645 stuff.keys.each { |key|
1646 if key.is_a?(Label) && stuff[key][:combo] && stuff[key][:combo].active <= 1
1647 destination = stuff[key][:pathlabel].text
1648 label2entries[key].each { |entry|
1649 if stuff[key][:combo].active == 0
1650 result = `cp -dp '#{entry.path}' '#{destination}' 2>&1`
1651 elsif stuff[key][:combo].active == 1
1652 result = `mv '#{entry.path}' '#{destination}' 2>&1`
1654 if $?.exitstatus > 0
1655 simplified_error = result.sub(/#{Regexp.quote(destination + '/' + File.basename(entry.path))}/, ''). #'
1656 sub(/#{Regexp.quote(entry.path)}/, '').
1657 sub(/#{Regexp.quote(File.basename(entry.path))}/, '')
1658 if ! ignored_errors.include?(simplified_error)
1659 response = show_popup($main_window,
1660 utf8(_("Failure:\n\n%s\nDo you wish to continue?" % result)),
1661 { :yestoall => true })
1663 raise "failure on '#{entry.path}'"
1664 elsif response == 'yestoall'
1665 ignored_errors << simplified_error
1669 if stuff[key][:combo].active == 0
1679 if stuff['toremove'][:combo] && stuff['toremove'][:combo].active == 0
1680 $allentries.each { |entry|
1682 File.delete(entry.path)
1688 msg 1, "woops: #{$!}\n" + $@.join("\n")
1690 show_popup(dialog, utf8(_("Successfully moved %d files, copied %d file, and removed %d files.") % [ moved, copied, removed ]))
1714 if entry.labeled.button.active?
1720 if $toremove_button.active?
1726 if $unlabelled_button.active?
1734 def update_visibility(entry)
1746 def update_all_visibilities_aux
1747 $allentries.each { |entry|
1748 update_visibility(entry)
1750 shown = $mainview.get_shown_entry
1752 while shown.button && ! shown.button.visible? && shown != $allentries.last
1753 shown = $allentries[$allentries.index(shown) + 1]
1755 if shown.button && shown.button.visible?
1756 shown.button.grab_focus
1759 $allentries.reverse.each { |entry|
1760 if entry.button && entry.button.visible?
1761 entry.button.grab_focus
1767 def update_all_visibilities
1768 update_all_visibilities_aux
1769 Gtk.main_iteration while Gtk.events_pending?
1770 shown = $mainview.get_shown_entry
1771 shown and autoscroll_if_needed(shown.button, false)
1776 dialog = Gtk::Dialog.new(utf8(_("Edit preferences")),
1778 Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT,
1779 [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK],
1780 [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL])
1782 tooltips = Gtk::Tooltips.new
1786 dialog.vbox.add(tbl = Gtk::Table.new(0, 0, false))
1787 tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Command for watching videos: ")))),
1788 0, 1, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1789 tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(video_viewer_entry = Gtk::Entry.new.set_text($config['video-viewer']).set_size_request(250, -1)),
1790 1, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1791 tooltips.set_tip(video_viewer_entry, utf8(_("Use %f to specify the filename;\nfor example: /usr/bin/mplayer %f")), nil)
1794 tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Browser's command: ")))),
1795 0, 1, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1796 tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(browser_entry = Gtk::Entry.new.set_text($config['browser'])),
1797 1, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1798 tooltips.set_tip(browser_entry, utf8(_("Use %f to specify the filename;\nfor example: /usr/bin/mozilla-firefox -remote 'openURL(%f,new-window)' || /usr/bin/mozilla-firefox %f")), nil)
1801 tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Thumbnails height: ")))),
1802 0, 1, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1803 tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(thumbnails_height = Gtk::SpinButton.new(32, 256, 16).set_value($config['thumbnails-height'].to_i)),
1804 1, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1805 tooltips.set_tip(thumbnails_height, utf8(_("The desired height of the thumbnails in the thumbnails line of the bottom")), nil)
1808 tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Preloading distance: ")))),
1809 0, 1, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1810 tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(preload_distance = Gtk::SpinButton.new(0, 50, 1).set_value($config['preload-distance'].to_i)),
1811 1, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1812 tooltips.set_tip(preload_distance, utf8(_("Amount of pictures preloaded left and right to the currently shown")), nil)
1815 tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Cache memory use: ")))),
1816 0, 1, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1817 tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(cache_vbox = Gtk::VBox.new(false, 0)),
1818 1, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1819 cache_vbox.pack_start(Gtk::HBox.new(false, 0).pack_start(cache_memfree_radio = Gtk::RadioButton.new(''), false, false).
1820 pack_start(cache_memfree_spin = Gtk::SpinButton.new(0, 100, 10), false, false).
1821 pack_start(cache_memfree_label = Gtk::Label.new(utf8(_("% of free memory"))), false, false), false, false)
1822 cache_memfree_spin.signal_connect('value-changed') { cache_memfree_radio.active = true }
1823 tooltips.set_tip(cache_memfree_spin, utf8(_("Percentage of free memory (+ buffers/cache) measured at startup")), nil)
1824 cache_vbox.pack_start(Gtk::HBox.new(false, 0).pack_start(cache_specify_radio = Gtk::RadioButton.new(cache_memfree_radio, ''), false, false).
1825 pack_start(cache_specify_spin = Gtk::SpinButton.new(0, 4000, 50), false, false).
1826 pack_start(cache_specify_label = Gtk::Label.new(utf8(_("MB"))).set_sensitive(false), false, false), false, false)
1827 cache_specify_spin.signal_connect('value-changed') { cache_specify_radio.active = true }
1828 cache_memfree_radio.signal_connect('toggled') {
1829 if cache_memfree_radio.active?
1830 cache_memfree_label.sensitive = true
1831 cache_specify_label.sensitive = false
1833 cache_specify_label.sensitive = true
1834 cache_memfree_label.sensitive = false
1837 tooltips.set_tip(cache_specify_spin, utf8(_("Amount of memory in megabytes")), nil)
1838 if $config['cache-memory-use'] =~ /memfree_(\d+)/
1839 cache_memfree_spin.value = $1.to_i
1841 cache_specify_spin.value = $config['cache-memory-use'].to_i / 1024
1845 tbl.attach(update_exif_orientation_check = Gtk::CheckButton.new(utf8(_("Update file's EXIF orientation when rotating a picture"))),
1846 0, 2, table_y, table_y + 1, Gtk::FILL, Gtk::SHRINK, 2, 2)
1847 tooltips.set_tip(update_exif_orientation_check, utf8(_("When rotating a picture (Alt-Right/Left), also update EXIF orientation in the file itself")), nil)
1848 update_exif_orientation_check.active = $config['rotate-set-exif'] == 'true'
1850 dialog.vbox.show_all
1851 dialog.run { |response|
1852 if response == Gtk::Dialog::RESPONSE_OK
1853 $config['video-viewer'] = from_utf8(video_viewer_entry.text)
1854 $config['browser'] = from_utf8(browser_entry.text)
1855 $config['thumbnails-height'] = thumbnails_height.value
1856 $config['preload-distance'] = preload_distance.value
1857 $config['rotate-set-exif'] = update_exif_orientation_check.active?.to_s
1858 if cache_memfree_radio.active?
1859 $config['cache-memory-use'] = "memfree_#{cache_memfree_spin.value}%"
1861 $config['cache-memory-use'] = cache_specify_spin.value.to_i * 1024
1863 set_cache_memory_use_figure
1870 if $undo_mb.sensitive?
1871 $redo_mb.sensitive = true
1872 if not more_undoes = UndoHandler.undo($statusbar)
1873 $undo_mb.sensitive = false
1879 if $redo_mb.sensitive?
1880 $undo_mb.sensitive = true
1881 if not more_redoes = UndoHandler.redo($statusbar)
1882 $redo_mb.sensitive = false
1889 mb = Gtk::MenuBar.new
1891 filemenu = Gtk::MenuItem.new(utf8(_("_File")))
1892 filesubmenu = Gtk::Menu.new
1893 filesubmenu.append(open = Gtk::ImageMenuItem.new(Gtk::Stock::OPEN))
1894 filesubmenu.append( Gtk::SeparatorMenuItem.new)
1895 filesubmenu.append($execute = Gtk::ImageMenuItem.new(Gtk::Stock::EXECUTE).set_sensitive(false))
1896 filesubmenu.append( Gtk::SeparatorMenuItem.new)
1897 filesubmenu.append(quit = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT))
1898 filemenu.set_submenu(filesubmenu)
1901 open.signal_connect('activate') { open_dir_popup }
1902 $execute.signal_connect('activate') { execute }
1903 quit.signal_connect('activate') { try_quit }
1905 editmenu = Gtk::MenuItem.new(utf8(_("_Edit")))
1906 editsubmenu = Gtk::Menu.new
1907 editsubmenu.append($undo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::UNDO).set_sensitive(false))
1908 editsubmenu.append($redo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::REDO).set_sensitive(false))
1909 editsubmenu.append( Gtk::SeparatorMenuItem.new)
1910 editsubmenu.append(prefs = Gtk::ImageMenuItem.new(Gtk::Stock::PREFERENCES))
1911 editmenu.set_submenu(editsubmenu)
1914 $undo_mb.signal_connect('activate') { perform_undo }
1915 $redo_mb.signal_connect('activate') { perform_redo }
1916 prefs.signal_connect('activate') { preferences }
1918 helpmenu = Gtk::MenuItem.new(utf8(_("_Help")))
1919 helpsubmenu = Gtk::Menu.new
1920 helpsubmenu.append(howto = Gtk::ImageMenuItem.new(Gtk::Stock::HELP))
1921 helpsubmenu.append(speed = Gtk::ImageMenuItem.new(utf8(_("Speedup: key shortcuts"))))
1922 speed.image = Gtk::Image.new("#{$FPATH}/images/stock-info-16.png")
1923 helpsubmenu.append(tutos = Gtk::ImageMenuItem.new(utf8(_("Online tutorials (opens a web-browser)"))))
1924 tutos.image = Gtk::Image.new("#{$FPATH}/images/stock-web-16.png")
1925 helpsubmenu.append(Gtk::SeparatorMenuItem.new)
1926 helpsubmenu.append(about = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT))
1927 helpmenu.set_submenu(helpsubmenu)
1930 howto.signal_connect('activate') {
1931 show_popup($main_window, utf8(_("<span size='large' weight='bold'>Help</span>
1933 1. Open a directory with <span foreground='darkblue'>File/Open</span>; the classifier will scan it (including subdirectories) and
1934 show thumbnails for all photos and videos at the bottom.
1936 2. You can then navigate through images with the <span foreground='darkblue'>Left/Right</span> keyboard keys, or by <span foreground='darkblue'>clicking</span>
1939 3. You may associate a <span foreground='darkblue'>label</span> to each thumbnail. Either hit the <span foreground='darkblue'>Delete</span> key to associate
1940 the built-in <i>to remove</i> label, or hit any alphabetical key to associate a label you define.
1941 The first time you hit a key without any label associated, a popup will ask for the full
1942 name of this label, and what color you want. To clear the current label, hit the <span foreground='darkblue'>Space</span> key.
1944 4. To help you better view what thumbnails are associated to your labels, you may <span foreground='darkblue'>hide</span>
1945 some of them by unchecking the labels checkboxes on the left.
1947 5. Once you're finished reviewing all thumbnails, use <span foreground='darkblue'>File/Execute</span> to execute the desired
1948 actions according to associated labels. You can permanently remove (or not) images with
1949 the <i>to remove</i> label, and copy or move images with the labels you defined.
1950 ")), { :pos_centered => true, :not_transient => true })
1952 speed.signal_connect('activate') {
1953 show_popup($main_window, utf8(_("<span size='large' weight='bold'>Key shortcuts</span>
1955 <span foreground='darkblue'>Left/Right</span>: move left and right in images
1956 <span foreground='darkblue'>Enter</span>: 'view' current image: for images, display EXIF data; for videos, play it
1957 <span foreground='darkblue'>Alt-Left/Right</span>: rotate current image clockwise/counter-clockwise
1958 <span foreground='darkblue'>Delete</span>: assign the 'to remove' label on current image
1959 <span foreground='darkblue'>Space</span>: clear any label on current image
1960 <span foreground='darkblue'>Control-z</span>: undo
1961 <span foreground='darkblue'>Control-r</span>: redo
1962 <span foreground='darkblue'>Control-Space</span>: recenter thumbnails on current item
1964 Any alphabetical key will assign (or popup for) the associated label on current image.
1965 ")), { :pos_centered => true, :not_transient => true })
1967 tutos.signal_connect('activate') { open_url('http://booh.org/tutorial') }
1968 about.signal_connect('activate') { call_about }
1971 #- no toolbar, to save height
1977 for child in $labels_vbox.children
1978 $labels_vbox.remove(child)
1980 $labels_vbox.pack_start(Gtk::Label.new(utf8(_("Labels list:"))).set_justify(Gtk::Justification::CENTER), false, false).show_all
1982 $ordered_labels = []
1983 lbl = Gtk::Label.new.set_markup(utf8(_("<i>unlabelled</i>")))
1984 $labels_vbox.pack_start(Gtk::HBox.new(false, 5).pack_start($unlabelled_button = Gtk::CheckButton.new.add(Gtk::EventBox.new.add(lbl)), false, false).
1985 pack_start(Gtk::Label.new, true, true). #- I suck
1986 pack_start($unlabelled_counter = Gtk::Label.new.set_markup('<tt>0</tt>'), false, false).show_all)
1987 $unlabelled_button.active = true
1988 $unlabelled_button.signal_connect('toggled') { update_all_visibilities }
1989 lbl = Gtk::Label.new.set_markup(utf8(_("<i>to remove</i>")))
1990 $labels_vbox.pack_start(Gtk::HBox.new(false, 5).pack_start($toremove_button = Gtk::CheckButton.new.add(evt = Gtk::EventBox.new.add(lbl)), false, false).
1991 pack_start(Gtk::Label.new, true, true).
1992 pack_start($toremove_counter = Gtk::Label.new.set_markup('<tt>0</tt>'), false, false).show_all)
1993 $toremove_button.active = true
1994 $toremove_button.signal_connect('toggled') { update_all_visibilities }
1995 evt.modify_bg(Gtk::StateType::NORMAL, $color_red)
1996 evt.modify_bg(Gtk::StateType::PRELIGHT, $color_red.lighter.lighter)
1997 evt.modify_bg(Gtk::StateType::ACTIVE, $color_red.lighter)
2001 $allentries.each { |e|
2006 def reset_thumbnails
2009 if $preloader_running
2010 $preloader_force_exit = true
2012 for child in $imagesline.children
2013 $imagesline.remove(child)
2015 set_imagesline_size_request
2018 def set_imagesline_size_request
2019 $imagesline.set_size_request(-1, Gtk::Button.new.size_request[1] + Entry.thumbnails_height + Entry.thumbnails_height/4)
2022 def create_main_window
2024 $videoborder_pixbuf = Gdk::Pixbuf.new("#{$FPATH}/images/video_border.png")
2025 $videoborder_pixmap, = $videoborder_pixbuf.render_pixmap_and_mask(0)
2029 main_vbox = Gtk::VBox.new(false, 0)
2030 main_vbox.pack_start(mb, false, false)
2031 mainview_hbox = Gtk::HBox.new
2032 mainview_hbox.pack_start(Gtk::Alignment.new(0.5, 0, 1, 1).add(left_vbox = Gtk::VBox.new(false, 5)), false, true)
2033 left_vbox.pack_start(($labels_vbox = Gtk::VBox.new(false, 5)), false, true)
2034 left_vbox.pack_end($loading_progressbar = Gtk::ProgressBar.new.set_text(utf8(_("Loading... %d%") % 0)), false, true)
2035 mainview_hbox.pack_start($mainview = MainView.new, true, true)
2036 main_vbox.pack_start(mainview_hbox, true, true)
2037 $imagesline_sw = Gtk::ScrolledWindow.new(nil, nil)
2038 $imagesline_sw.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_NEVER)
2039 $imagesline_sw.add_with_viewport($imagesline = Gtk::HBox.new(false, 0).show)
2040 main_vbox.pack_start($imagesline_sw, false, false)
2041 main_vbox.pack_end($statusbar = Gtk::Statusbar.new, false, false)
2043 set_imagesline_size_request
2045 $main_window = create_window
2046 $main_window.add(main_vbox)
2047 $main_window.signal_connect('delete-event') {
2051 #- read/save size and position of window
2052 if $config['pos-x'] && $config['pos-y']
2053 $main_window.move($config['pos-x'].to_i, $config['pos-y'].to_i)
2055 $main_window.window_position = Gtk::Window::POS_CENTER
2057 msg 3, "size: #{$config['width']}x#{$config['height']}"
2058 $main_window.set_default_size(($config['width'] || 800).to_i, ($config['height'] || 600).to_i)
2059 $main_window.signal_connect('configure-event') {
2060 msg 3, "configure: pos: #{$main_window.window.root_origin.inspect} size: #{$main_window.window.size.inspect}"
2061 x, y = $main_window.window.root_origin
2062 width, height = $main_window.window.size
2063 $config['pos-x'] = x
2064 $config['pos-y'] = y
2065 $config['width'] = width
2066 $config['height'] = height
2070 $main_window.show_all
2071 $loading_progressbar.hide
2084 if msg = open_dir(*ARGV)
2088 show_entries($allentries)