#! /usr/bin/ruby # # * BOOH * # # A.k.a 'Best web-album Of the world, Or your money back, Humerus'. # # The acronyn sucks, however this is a tribute to Dragon Ball by # Akira Toriyama, where the last enemy beaten by heroes of Dragon # Ball is named "Boo". But there was already a free software project # called Boo, so this one will be it "Booh". Or whatever. # # # Copyright (c) 2004-2006 Guillaume Cottenceau # # This software may be freely redistributed under the terms of the GNU # public license version 2. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require 'getoptlong' require 'tempfile' require 'thread' require 'gtk2' require 'gettext' include GetText bindtextdomain("booh") require 'rexml/document' include REXML require 'booh/booh-lib' include Booh require 'booh/UndoHandler' #- options $options = [ [ '--help', '-h', GetoptLong::NO_ARGUMENT, _("Get help message") ], [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, _("Set max verbosity level (0: errors, 1: warnings, 2: important messages, 3: other messages)") ], ] def usage puts _("Usage: %s [OPTION]...") % File.basename($0) $options.each { |ary| printf " %3s, %-15s %s\n", ary[1], ary[0], ary[3] } end def handle_options parser = GetoptLong.new parser.set_options(*$options.collect { |ary| ary[0..2] }) begin parser.each_option do |name, arg| case name when '--help' usage exit(0) when '--verbose-level' $verbose_level = arg.to_i end end puts "args: #{ARGV}" rescue puts $! usage exit(1) end end def memfree meminfo = IO.readlines('/proc/meminfo').join meminfo =~ /MemFree:.*?(\d+)/ or return -1 memfree = $1 meminfo =~ /Buffers:.*?(\d+)/ and buffers = $1 meminfo =~ /Cached:.*?(\d+)/ and cached = $1 return memfree.to_i + buffers.to_i + cached.to_i end def set_cache_memory_use_figure if $config['cache-memory-use'] =~ /memfree_(\d+)/ $config['cache-memory-use-figure'] = memfree*$1.to_f/100 else $config['cache-memory-use-figure'] = $config['cache-memory-use'].to_i end msg 2, "Set cache memory use figure: #{$config['cache-memory-use-figure']} kB" end def read_config $config = {} $config_file = File.expand_path('~/.booh-classifier-rc') if File.readable?($config_file) $xmldoc = REXML::Document.new(File.new($config_file)) $xmldoc.root.elements.each { |element| txt = element.get_text if txt if txt.value =~ /~~~/ $config[element.name] = txt.value.split(/~~~/) else $config[element.name] = txt.value end elsif element.elements.size == 0 $config[element.name] = '' else $config[element.name] = {} element.each { |chld| txt = chld.get_text $config[element.name][chld.name] = txt ? txt.value : nil } end } end $config['video-viewer'] ||= '/usr/bin/mplayer %f' $config['preload-distance'] ||= '5' $config['cache-memory-use'] ||= 'memfree_50%' $config['rotate-set-exif'] ||= 'true' set_cache_memory_use_figure end def check_config if !system("which identify >/dev/null 2>/dev/null") show_popup($main_window, utf8(_("The program 'identify' is needed to get images sizes and EXIF data. Please install it. It is generally available with the 'ImageMagick' software package.")), { :pos_centered => true }) end missing = %w(transcode mencoder).delete_if { |prg| system("which #{prg} >/dev/null 2>/dev/null") } if missing != [] show_popup($main_window, utf8(_("The following program(s) are needed to handle videos: '%s'. Videos will be ignored.") % missing.join(', ')), { :pos_centered => true }) end viewer_binary = $config['video-viewer'].split.first if viewer_binary && !File.executable?(viewer_binary) show_popup($main_window, utf8(_("The configured video viewer seems to be unavailable. You should fix this in Edit/Preferences so that you can view videos. Problem was: '%s' is not an executable file. Hint: don't forget to specify the full path to the executable, e.g. '/usr/bin/mplayer' is correct but 'mplayer' only is not.") % viewer_binary), { :pos_centered => true, :not_transient => true }) end end def write_config ios = File.open($config_file, "w") $xmldoc = Document.new "" $xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, $CURRENT_CHARSET) $config.each_pair { |key, value| elem = $xmldoc.root.add_element key if value.is_a? Hash $config[key].each_pair { |subkey, subvalue| subelem = elem.add_element subkey subelem.add_text subvalue.to_s } elsif value.is_a? Array elem.add_text value.join('~~~') else if !value elem.remove else elem.add_text value.to_s end end } $xmldoc.write(ios, 0) ios.close end def save_undo(name, closure, *params) UndoHandler.save_undo(name, closure, [ *params ]) $undo_mb.sensitive = true $redo_mb.sensitive = false end def get_mem IO.readlines('/proc/self/status').join =~ /VmRSS.*?(\d+)\s*kB/ puts $1 return $1.to_i end def show_mem(*txt) txt.length > 0 and print txt[0] puts get_mem end def min(v1, v2) return v1 < v2 ? v1 : v2 end def max(v1, v2) return v1 > v2 ? v1 : v2 end class Gdk::Color def darker color = dup color.red = max(color.red - 10000, 0) color.green = max(color.green - 10000, 0) color.blue = max(color.blue - 10000, 0) return color end def lighter color = dup color.red = min(color.red + 10000, 65535) color.green = min(color.green + 10000, 65535) color.blue = min(color.blue + 10000, 65535) return color end end $colors = [ Gdk::Color.new(0, 65535, 0), Gdk::Color.new(0, 0, 65535), Gdk::Color.new(65535, 65535, 0), Gdk::Color.new(0, 65535, 65535), Gdk::Color.new(65535, 0, 65535) ] class Tag attr_accessor :color, :name def initialize(name) @name = name end end $tags = {} class Entry @@thumbnails_height = 64 @@max_height = nil def Entry.thumbnails_height return @@thumbnails_height end attr_accessor :path, :type, :angle, :button, :removed, :tagged def initialize(path, type) @path = path @type = type if @@max_height.nil? @@max_height = $main_window.root_window.size[1] end @protect_cleanup = Mutex.new end def pixbuf_full @protect_cleanup.synchronize { if @pixbuf_full.nil? puts ">>> pixbuf_full #{path}" load_into_pixbuf_full end return @pixbuf_full } end def free_pixbuf_full @protect_cleanup.synchronize { if @pixbuf_full.nil? return false else puts ">>> free_pixbuf_full #{path}" @pixbuf_full = nil return true end } end def pixbuf_main(width, height) @protect_cleanup.synchronize { if @pixbuf_main.nil? || width != @width || height != @height puts ">>> pixbuf_main #{path}" @width = width @height = height load_into_pixbuf_full #- make sure it is loaded if @pixbuf_full.width.to_f / @pixbuf_full.height > width.to_f / height resized_height = @pixbuf_full.height * (width.to_f/@pixbuf_full.width) if @pixbuf_full.width > width || @pixbuf_full.height > resized_height @pixbuf_main = @pixbuf_full.scale(width, resized_height, Gdk::Pixbuf::INTERP_BILINEAR) else @pixbuf_main = @pixbuf_full end else resized_width = @pixbuf_full.width * (height.to_f/@pixbuf_full.height) if @pixbuf_full.width > resized_width || @pixbuf_full.height > height @pixbuf_main = @pixbuf_full.scale(resized_width, height, Gdk::Pixbuf::INTERP_BILINEAR) else @pixbuf_main = @pixbuf_full end end end return @pixbuf_main } end def free_pixbuf_main @protect_cleanup.synchronize { if @pixbuf_main.nil? return false else puts ">>> free_pixbuf_main #{path}" @pixbuf_main = nil return true end } end def pixbuf_thumbnail return @protect_cleanup.synchronize { if @pixbuf_thumbnail.nil? puts ">>> pixbuf_thumbnail #{path}" load_into_pixbuf_full #- make sure it is loaded @pixbuf_thumbnail = @pixbuf_full.scale(@pixbuf_full.width * (@@thumbnails_height.to_f/@pixbuf_full.height), @@thumbnails_height, Gdk::Pixbuf::INTERP_BILINEAR) end return @pixbuf_thumbnail } end def free_pixbuf_thumbnail @protect_cleanup.synchronize { if @pixbuf_thumbnail.nil? return false else puts ">>> free_pixbuf_thumbnail #{path}" @pixbuf_thumbnail = nil return true end } end def show_bg if removed red = Gdk::Color.new(65535, 0, 0) button.modify_bg(Gtk::StateType::NORMAL, red) button.modify_bg(Gtk::StateType::PRELIGHT, red.lighter) button.modify_bg(Gtk::StateType::ACTIVE, red) elsif tagged button.modify_bg(Gtk::StateType::NORMAL, tagged.color) button.modify_bg(Gtk::StateType::PRELIGHT, tagged.color.lighter) button.modify_bg(Gtk::StateType::ACTIVE, tagged.color) else # TODO need to add proper undo support :/ white = Gdk::Color.new(55535, 55535, 55535) button.modify_bg(Gtk::StateType::NORMAL, white) button.modify_bg(Gtk::StateType::PRELIGHT, white.lighter) button.modify_bg(Gtk::StateType::ACTIVE, white) end end private def load_into_pixbuf_full if @pixbuf_full.nil? puts ">>> load_into_pixbuf_full #{path}" begin @pixbuf_full = Gdk::Pixbuf.new(@path) rescue Gdk::PixbufError puts "Cannot load #{@path}: #{$!}" return end if @pixbuf_full if @angle.nil? @angle = guess_rotate(path) end if @angle != 0 puts ">>> load_into_pixbuf_full #{path} => rotate #{@angle}" @pixbuf_full = rotate_pixbuf(@pixbuf_full, @angle) end if @pixbuf_full.height > @@max_height #- save a lot of memory, don't store in actual full size @pixbuf_full = @pixbuf_full.scale(@pixbuf_full.width * (@@max_height.to_f/@pixbuf_full.height), @@max_height, Gdk::Pixbuf::INTERP_BILINEAR) end end end end def to_s @path end end class MainView < Gtk::DrawingArea def initialize super() signal_connect('expose-event') { draw } signal_connect('configure-event') { update_shown; GC.start } signal_connect('realize') { @preloader = Thread.new { #- background preloading while true puts "background main preloading triggered..." if ! @index.nil? w, h = window.size for j in 1 .. $config['preload-distance'].to_i i = @index + j if i < $allentries.size $allentries[i].pixbuf_main(w, h) end i = @index - j if i >= 0 $allentries[i].pixbuf_main(w, h) end GC.start mem = get_mem if mem > $config['cache-memory-use-figure'] puts "too much RSS, stopping main preloading" break end end check_memory_free_cache_if_needed end Thread.stop end } @preloader.priority = -2 } end def set_shown_entry(index) if index == @index || index < 0 || index >= $allentries.size return end old_index = @index @index = index if index.nil? @entry = nil else @entry = $allentries[index] end if ! @entry.button #- not loaded yet @index = old_index return end redraw @preloader.run @entry.button.grab_focus end def get_shown_entry return @index end def redraw update_shown w, h = window.size window.begin_paint(Gdk::Rectangle.new(0, 0, w, h)) window.clear draw window.end_paint end def update_shown if @entry width, height = window.size @pixbuf = @entry.pixbuf_main(width, height) if @pixbuf.width == width @xpos = 0 @ypos = (height-@pixbuf.height)/2 else @xpos = (width-@pixbuf.width)/2 @ypos = 0 end @preloader.run else @pixbuf = nil end end def draw if @pixbuf window.draw_pixbuf(nil, @pixbuf, 0, 0, @xpos, @ypos, -1, -1, Gdk::RGB::DITHER_NONE, -1, -1) end end end def check_memory_free_cache_if_needed GC.start mem = get_mem i = $mainview.get_shown_entry puts "mem: #{mem} index: #{i}" return if i.nil? ($allentries.size - 1).downto(1) { |j| if mem < $config['cache-memory-use-figure'] * 2 / 3 break end index = i + j puts "too much RSS, freeing full size of #{i+j} and #{i-j}..." freedsomething = false if i + j < $allentries.size freedsomething |= $allentries[i+j].free_pixbuf_full end if i - j > 0 freedsomething |= $allentries[i-j].free_pixbuf_full end if freedsomething GC.start mem = get_mem puts "\tmem now: #{mem}" end } end def autoscroll_if_needed(button) xpos_left = button.allocation.x xpos_right = button.allocation.x + button.allocation.width hadj = $imagesline_sw.hadjustment current_minx_visible = hadj.value current_maxx_visible = hadj.value + hadj.page_size if xpos_left < current_minx_visible #- autoscroll left newval = hadj.value - (current_minx_visible - xpos_left) hadj.value = newval button.queue_draw #- TOREMOVE: the visual focus is displayed incorrectly elsif xpos_right > current_maxx_visible #- autoscroll right newval = hadj.value + (xpos_right - current_maxx_visible) if newval > hadj.upper - hadj.page_size newval = hadj.upper - hadj.page_size end hadj.value = newval button.queue_draw #- TOREMOVE: the visual focus is displayed incorrectly end end def show_popup(parent, msg, *options) dialog = Gtk::Dialog.new if options[0] options = options[0] else options = {} end if options[:title] dialog.title = options[:title] else dialog.title = utf8(_("Booh message")) end lbl = Gtk::Label.new if options[:nomarkup] lbl.text = msg else lbl.markup = msg end if options[:centered] lbl.set_justify(Gtk::Justification::CENTER) end if options[:selectable] lbl.selectable = true end if options[:topwidget] dialog.vbox.add(options[0][:topwidget]) end if options[:scrolled] sw = Gtk::ScrolledWindow.new(nil, nil) sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) sw.add_with_viewport(lbl) dialog.vbox.add(sw) dialog.set_default_size(500, 600) else dialog.vbox.add(lbl) dialog.set_default_size(200, 120) end if options[:bottomwidget] dialog.vbox.add(options[:bottomwidget]) end if options[:okcancel] dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL) end dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK) if options[:pos_centered] dialog.window_position = Gtk::Window::POS_CENTER else dialog.window_position = Gtk::Window::POS_MOUSE end if options[:linkurl] linkbut = Gtk::Button.new('') linkbut.child.markup = "#{options[0][:linkurl]}" linkbut.signal_connect('clicked') { open_url(options[0][:linkurl] + '/index.html') dialog.response(Gtk::Dialog::RESPONSE_OK) set_mousecursor_normal } linkbut.relief = Gtk::RELIEF_NONE linkbut.signal_connect('enter-notify-event') { set_mousecursor(Gdk::Cursor::HAND2, linkbut); false } linkbut.signal_connect('leave-notify-event') { set_mousecursor(nil, linkbut); false } dialog.vbox.add(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(linkbut)) end dialog.show_all if options[:stuff_connector] options[:stuff_connector].call({ :dialog => dialog }) end if !options[:not_transient] dialog.transient_for = parent dialog.run { |response| if options[:data_getter] options[:data_getter].call end dialog.destroy if options[:okcancel] return response == Gtk::Dialog::RESPONSE_OK end } else dialog.signal_connect('response') { dialog.destroy } end end def thumbnail_keypressed(entry, i, event) if event.state & Gdk::Window::MOD1_MASK != 0 #- ALT pressed: Alt-Left and Alft-Right rotate if event.keyval == Gdk::Keyval::GDK_Left || event.keyval == Gdk::Keyval::GDK_Right if event.keyval == Gdk::Keyval::GDK_Left entry.angle = (entry.angle - 90) % 360 else entry.angle = (entry.angle + 90) % 360 end entry.free_pixbuf_full entry.free_pixbuf_main entry.free_pixbuf_thumbnail $mainview.redraw entry.button.set_image(img = Gtk::Image.new(entry.pixbuf_thumbnail)) end elsif event.state & Gdk::Window::CONTROL_MASK != 0 #- CONTROL pressed: Ctrl-z and Ctrl-r for undo/redo if event.keyval == Gdk::Keyval::GDK_z perform_undo end if event.keyval == Gdk::Keyval::GDK_r perform_redo end else removed_before = entry.removed tag_before = entry.tagged if event.keyval == Gdk::Keyval::GDK_Delete entry.removed = true entry.tagged = nil entry.show_bg $mainview.set_shown_entry(i + 1) save_undo(_("set for removal"), proc { entry.removed = removed_before entry.tagged = tag_before entry.show_bg $mainview.set_shown_entry(i) proc { entry.removed = true entry.tagged = nil entry.show_bg $mainview.set_shown_entry(i) } }) elsif event.keyval == Gdk::Keyval::GDK_space entry.removed = false entry.tagged = nil entry.show_bg $mainview.set_shown_entry(i + 1) save_undo(_("remove tag"), proc { entry.removed = removed_before entry.tagged = tag_before entry.show_bg $mainview.set_shown_entry(i) proc { entry.removed = false entry.tagged = nil entry.show_bg $mainview.set_shown_entry(i) } }) else char = [ Gdk::Keyval.to_unicode(event.keyval) ].pack("C*") if char =~ /^[a-zA-z0-9]$/ tag = $tags[char] if tag.nil? vb = Gtk::VBox.new(false, 0) vb.pack_start(entry = Gtk::Entry.new.set_text(char), false, false) vb.pack_start(Gtk::Alignment.new(0.5, 0.5, 0, 0).add(bt = Gtk::Button.new(utf8(_(" Change color "))))) text = nil color = nil bt.signal_connect('clicked') { color = $colors.shift if color.nil? color = Gdk::Color.new(16384 + rand(49151), 16384 + rand(49151), 16384 + rand(49151)) end bt.modify_bg(Gtk::StateType::NORMAL, color) bt.modify_bg(Gtk::StateType::PRELIGHT, color) bt.modify_bg(Gtk::StateType::ACTIVE, color.darker) } bt.clicked entry.signal_connect('changed') { #- cannot add a new tag with first letter of an existing tag while $tags.has_key?(entry.text[0,1]) entry.text = entry.text.sub(/./, '') end } if show_popup($main_window, utf8(_("You typed the text character '%s', which is not associated with a tag.\nType in the full name of the tag below to create a new one.")) % char, { :okcancel => true, :bottomwidget => vb, :data_getter => proc { text = entry.text }, :stuff_connector => proc { |stuff| entry.select_region(0, 0) entry.position = -1 entry.signal_connect('activate') { stuff[:dialog].response(Gtk::Dialog::RESPONSE_OK) } } } ) if text.length > 0 char = text[0,1] #- in case it changed tag = Tag.new(text) tag.color = color $tags[char] = tag label = Gtk::Label.new.set_markup('(' + char + ')' + text[1..-1]).set_justify(Gtk::Justification::CENTER) $tags_vbox.pack_start(evt = Gtk::EventBox.new.add(label).modify_bg(Gtk::StateType::NORMAL, tag.color).show_all) end end else entry.removed = false entry.tagged = tag entry.show_bg $mainview.set_shown_entry(i + 1) save_undo(_("set tag"), proc { entry.removed = removed_before entry.tagged = tag_before entry.show_bg $mainview.set_shown_entry(i) proc { entry.removed = false entry.tagged = tag entry.show_bg $mainview.set_shown_entry(i) } }) end end end end end def sb_msg(msg) $statusbar.pop(0) if msg $statusbar.push(0, utf8(msg)) end end def show_entries e = Thread.new { t1 = Time.now show_mem sb_msg(_("Loading images...")) tooltips = Gtk::Tooltips.new counter = 0 $allentries.each_with_index { |entry, i| if entry.pixbuf_full entry.pixbuf_thumbnail gtk_thread_protect(proc { |i| entry = $allentries[i] entry.button = Gtk::Button.new.set_image(img = Gtk::Image.new(entry.pixbuf_thumbnail)) tooltips.set_tip(entry.button, File.basename(entry.path).gsub(/\.[^.]+$/, ''), nil) $imagesline.pack_start(entry.button.show_all, false, false) entry.button.signal_connect('clicked') { $mainview.set_shown_entry(i) } entry.button.signal_connect('focus-in-event') { entry.button.clicked; autoscroll_if_needed(entry.button) } entry.button.signal_connect('key-press-event') { |w, e| thumbnail_keypressed(entry, i, e) } if i == 0 entry.button.grab_focus end }, i) if i % 4 == 0 check_memory_free_cache_if_needed end counter += 1 end } check_memory_free_cache_if_needed sb_msg(_("%d images loaded.") % counter) puts "time: #{Time.now - t1}" } e.priority = -1 end def open_dir(path) #- remove visual stuff, so that user will see something is happening reset_tags reset_thumbnails $mainview.set_shown_entry(nil) sb_msg(_("Scanning source directory...")) path = File.expand_path(path.sub(%r|/$|, '')) examined_dirs = `find '#{path}' -type d -follow`.sort.collect { |v| v.chomp } #- validate first examined_dirs.each { |dir| if dir =~ /'/ return utf8(_("Source directory or sub-directories can't contain a single-quote character, sorry: %s") % dir) end Dir.entries(dir).each { |file| if file =~ /['"\[\]]/ return utf8(_("Files can't contain any of the characters ', \", [ or ], sorry: %s") % "#{dir}/#{file}") end } } #- scan for populate second examined_dirs.each { |dir| if File.basename(dir) =~ /^\./ msg 1, _("Ignoring directory %s, begins with a dot (indicating a hidden directory)") % dir next end Dir.entries(dir).each { |file| type = entry2type(file) if type && $allentries.size < 10 $allentries << Entry.new(File.join(dir, file), type) end } } $workingdir = path show_entries $execute.sensitive = true return nil end def open_dir_popup fc = Gtk::FileChooserDialog.new(utf8(_("Specify the directory to work with")), nil, Gtk::FileChooser::ACTION_SELECT_FOLDER, nil, [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]) fc.transient_for = $main_window if $workingdir fc.current_folder = $workingdir end ok = false while !ok if fc.run == Gtk::Dialog::RESPONSE_ACCEPT msg = open_dir(fc.filename) if msg show_popup(fc, msg) ok = false else ok = true end else ok = true end end fc.destroy end def gtk_thread_protect(proc, *params) if Thread.current == Thread.main proc.call(*params) else $protect_gtk_pending_calls.synchronize { $gtk_pending_calls << [ proc, params ] } end end def gtk_thread_flush $protect_gtk_pending_calls.synchronize { if $gtk_pending_calls.size > 0 elem = $gtk_pending_calls.shift elem[0].call(*elem[1]) end } end def try_quit(*options) Gtk.main_quit end def execute dialog = Gtk::Dialog.new dialog.title = utf8(_("Booh message")) vb1 = Gtk::VBox.new(false, 5) label = Gtk::Label.new.set_markup(utf8(_("You're about to execute actions on the marked images.\nPlease confirm below the actions. This operation is not undoable!"))) vb1.pack_start(label, false, false) table = Gtk::Table.new(0, 0, false) table.set_row_spacings(5) table.set_column_spacings(5) table.attach(Gtk::Label.new.set_markup(utf8(_("Tag name:"))).set_justify(Gtk::Justification::CENTER), 0, 1, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0) table.attach(Gtk::Label.new.set_markup(utf8(_("Amount of pictures:"))).set_justify(Gtk::Justification::CENTER), 1, 2, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0) table.attach(Gtk::Label.new.set_markup(utf8(_("Pictures examples:"))).set_justify(Gtk::Justification::CENTER), 2, 3, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0) table.attach(Gtk::Label.new.set_markup(utf8(_("Action to perform:"))).set_justify(Gtk::Justification::CENTER), 3, 4, 0, 1, Gtk::FILL, Gtk::FILL, 5, 0) add_row = proc { |row, name, color, truthproc, normal| 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)), 0, 1, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5) counter = 0 examples = Gtk::HBox.new(false, 5) $allentries.each { |entry| if truthproc.call(entry) counter += 1 if counter < 4 examples.pack_start(Gtk::Image.new(entry.pixbuf_thumbnail), false, false) elsif counter == 4 examples.pack_start(Gtk::Label.new.set_markup("..."), false, false) end end } table.attach(Gtk::Label.new(counter.to_s).set_justify(Gtk::Justification::CENTER), 1, 2, row, row + 1, 0, 0, 5, 5) table.attach(examples, 2, 3, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5) combostore = Gtk::ListStore.new(Gdk::Pixbuf, String) iter = combostore.append if normal iter[0] = $main_window.render_icon(Gtk::Stock::GO_FORWARD, Gtk::IconSize::MENU) iter[1] = utf8(_("Move to:")) else iter[0] = $main_window.render_icon(Gtk::Stock::DELETE, Gtk::IconSize::MENU) iter[1] = utf8(_("Permanently remove")) end iter = combostore.append iter[0] = $main_window.render_icon(Gtk::Stock::MEDIA_STOP, Gtk::IconSize::MENU) iter[1] = utf8(_("Do nothing")) combo = Gtk::ComboBox.new(combostore) combo.active = 0 renderer = Gtk::CellRendererPixbuf.new combo.pack_start(renderer, false) combo.set_attributes(renderer, :pixbuf => 0) renderer = Gtk::CellRendererText.new combo.pack_start(renderer, true) combo.set_attributes(renderer, :text => 1) if normal pathbutton = Gtk::Button.new.add(pathlabel = Gtk::Label.new.set_markup(utf8(_("(unset)")))) lastpath = $workingdir pathbutton.signal_connect('clicked') { fc = Gtk::FileChooserDialog.new(utf8(_("Specify the directory where to move the pictures to")), nil, Gtk::FileChooser::ACTION_SELECT_FOLDER, nil, [Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]) fc.transient_for = dialog fc.current_folder = lastpath if fc.run == Gtk::Dialog::RESPONSE_ACCEPT pathlabel.text = fc.filename end lastpath = fc.filename fc.destroy } combo.signal_connect('changed') { pathbutton.sensitive = combo.active == 0 } vb = Gtk::VBox.new(false, 5) vb.pack_start(combo, false, false) vb.pack_start(pathbutton, false, false) table.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(vb), 3, 4, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5) { :combo => combo, :pathlabel => pathlabel } else table.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(combo), 3, 4, row, row + 1, Gtk::FILL, Gtk::FILL, 5, 5) { :combo => combo } end } stuff = {} stuff['toremove'] = add_row.call(1, utf8(_("to remove")), Gdk::Color.new(65535, 0, 0), proc { |entry| entry.removed }, false) $tags.values.each_with_index { |tag, row| stuff[tag] = add_row.call(row + 2, tag.name, tag.color, proc { |entry| entry.tagged == tag }, true) } vb1.pack_start(sw = Gtk::ScrolledWindow.new(nil, nil).add_with_viewport(table).set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC), true, true) toremove_amount = $allentries.find_all { |entry| entry.removed }.size check_removal = Gtk::CheckButton.new(utf8(_("I have noticed I am about to permanently remove the %d above mentioned pictures.") % toremove_amount)) if toremove_amount > 0 vb1.pack_start(check_removal, false, false) stuff['toremove'][:combo].signal_connect('changed') { |widget| check_removal.sensitive = widget.active == 0 } end dialog.vbox.add(vb1) dialog.set_default_size(800, 600) dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL) dialog.add_button(Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK) dialog.window_position = Gtk::Window::POS_MOUSE dialog.transient_for = $main_window dialog.show_all while true dialog.run { |response| if response == Gtk::Dialog::RESPONSE_OK if toremove_amount > 0 && ! check_removal.active? && check_removal.sensitive? show_popup(dialog, utf8(_("You have not confirmed that you noticed the permanent removal of the pictures marked for deletion."))) break end problem = false tag2entries = {} $tags.values.each { |tag| tag2entries[tag] = [] } $allentries.each { |entry| entry.tagged and tag2entries[entry.tagged] << entry } stuff.keys.each { |key| if key.is_a?(Tag) && stuff[key][:combo].active == 0 path = stuff[key][:pathlabel].text if path[0] != ?/ show_popup(dialog, utf8(_("You have not selected a directory where to move %s.") % key.name)) problem = true break end st = File.stat(path) if ! st.directory? || ! st.writable? show_popup(dialog, utf8(_("Directory where to move %s is not valid or not writable.") % key.name)) problem = true break end tag2entries[key].each { |entry| begin File.stat(File.join(path, File.basename(entry.path))) show_popup(dialog, utf8(_("Sorry, a file '%s' already exists in '%s'.") % [ File.basename(entry.path), path ])) problem = true break rescue end } if problem break end end } if ! problem puts "execute!" dialog.destroy return end else dialog.destroy return end } end end def preferences dialog = Gtk::Dialog.new(utf8(_("Edit preferences")), $main_window, Gtk::Dialog::MODAL | Gtk::Dialog::DESTROY_WITH_PARENT, [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK], [Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]) # $config['cache-memory-use'] ||= 'memfree_50%' # if $config['cache-memory-use'] =~ /memfree_(\d+)/ # $config['cache-memory-use-figure'] = memfree*$1.to_f/100 # else # $config['cache-memory-use-figure'] = $config['cache-memory-use'].to_i # end tooltips = Gtk::Tooltips.new dialog.vbox.add(tbl = Gtk::Table.new(0, 0, false)) tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Command for watching videos: ")))), 0, 1, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2) 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)), 1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 2) tooltips.set_tip(video_viewer_entry, utf8(_("Use %f to specify the filename;\nfor example: /usr/bin/mplayer %f")), nil) tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Preloading distance: ")))), 0, 1, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 2) 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)), 1, 2, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 2) tooltips.set_tip(preload_distance, utf8(_("Amount of pictures preloaded left and right to the currently shown")), nil) tbl.attach(Gtk::Alignment.new(1, 0.5, 0, 0).add(Gtk::Label.new.set_markup(utf8(_("Cache memory use: ")))), 0, 1, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2) tbl.attach(Gtk::Alignment.new(0, 0.5, 1, 0).add(cache_vbox = Gtk::VBox.new(false, 0)), 1, 2, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 2) cache_vbox.pack_start(Gtk::HBox.new(false, 0).pack_start(cache_memfree_radio = Gtk::RadioButton.new(''), false, false). pack_start(cache_memfree_spin = Gtk::SpinButton.new(0, 100, 10), false, false). pack_start(cache_memfree_label = Gtk::Label.new(utf8(_("% of free memory"))), false, false), false, false) cache_memfree_spin.signal_connect('value-changed') { cache_memfree_radio.active = true } tooltips.set_tip(cache_memfree_spin, utf8(_("Percentage of free memory (+ buffers/cache) measured at startup")), nil) cache_vbox.pack_start(Gtk::HBox.new(false, 0).pack_start(cache_specify_radio = Gtk::RadioButton.new(cache_memfree_radio, ''), false, false). pack_start(cache_specify_spin = Gtk::SpinButton.new(0, 4000, 50), false, false). pack_start(cache_specify_label = Gtk::Label.new(utf8(_("MB"))).set_sensitive(false), false, false), false, false) cache_specify_spin.signal_connect('value-changed') { cache_specify_radio.active = true } cache_memfree_radio.signal_connect('toggled') { if cache_memfree_radio.active? cache_memfree_label.sensitive = true cache_specify_label.sensitive = false else cache_specify_label.sensitive = true cache_memfree_label.sensitive = false end } tooltips.set_tip(cache_specify_spin, utf8(_("Amount of memory in megabytes")), nil) if $config['cache-memory-use'] =~ /memfree_(\d+)/ cache_memfree_spin.value = $1.to_i else cache_specify_spin.value = $config['cache-memory-use'].to_i end tbl.attach(update_exif_orientation_check = Gtk::CheckButton.new(utf8(_("Update file's EXIF orientation when rotating a picture"))), 0, 2, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 2) tooltips.set_tip(update_exif_orientation_check, utf8(_("")), nil) update_exif_orientation_check.active = $config['rotate-set-exif'] dialog.vbox.show_all dialog.run { |response| if response == Gtk::Dialog::RESPONSE_OK $config['video-viewer'] = from_utf8(video_viewer_entry.text) $config['preload-distance'] = preload_distance.value $config['rotate-set-exif'] = update_exif_orientation_check.active? if cache_memfree_radio.active? $config['cache-memory-use'] = "memfree_#{cache_memfree_spin.value}%" else $config['cache-memory-use'] = cache_specify_spin.value end set_cache_memory_use_figure p $config end } dialog.destroy end def perform_undo if $undo_mb.sensitive? $redo_mb.sensitive = true if not more_undoes = UndoHandler.undo($statusbar) $undo_mb.sensitive = false end end end def perform_redo if $redo_mb.sensitive? $undo_mb.sensitive = true if not more_redoes = UndoHandler.redo($statusbar) $redo_mb.sensitive = false end end end def create_menubar #- menu mb = Gtk::MenuBar.new filemenu = Gtk::MenuItem.new(utf8(_("_File"))) filesubmenu = Gtk::Menu.new filesubmenu.append(open = Gtk::ImageMenuItem.new(Gtk::Stock::OPEN)) filesubmenu.append( Gtk::SeparatorMenuItem.new) filesubmenu.append($execute = Gtk::ImageMenuItem.new(Gtk::Stock::EXECUTE).set_sensitive(false)) filesubmenu.append( Gtk::SeparatorMenuItem.new) filesubmenu.append(quit = Gtk::ImageMenuItem.new(Gtk::Stock::QUIT)) filemenu.set_submenu(filesubmenu) mb.append(filemenu) open.signal_connect('activate') { open_dir_popup } $execute.signal_connect('activate') { execute } quit.signal_connect('activate') { try_quit } editmenu = Gtk::MenuItem.new(utf8(_("_Edit"))) editsubmenu = Gtk::Menu.new editsubmenu.append($undo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::UNDO).set_sensitive(false)) editsubmenu.append($redo_mb = Gtk::ImageMenuItem.new(Gtk::Stock::REDO).set_sensitive(false)) editsubmenu.append( Gtk::SeparatorMenuItem.new) editsubmenu.append(prefs = Gtk::ImageMenuItem.new(Gtk::Stock::PREFERENCES)) editmenu.set_submenu(editsubmenu) mb.append(editmenu) $undo_mb.signal_connect('activate') { perform_undo } $redo_mb.signal_connect('activate') { perform_redo } prefs.signal_connect('activate') { preferences } helpmenu = Gtk::MenuItem.new(utf8(_("_Help"))) helpsubmenu = Gtk::Menu.new helpsubmenu.append(tutos = Gtk::ImageMenuItem.new(utf8(_("Online tutorials (opens a web-browser)")))) tutos.image = Gtk::Image.new("#{$FPATH}/images/stock-web-16.png") helpsubmenu.append(Gtk::SeparatorMenuItem.new) helpsubmenu.append(about = Gtk::ImageMenuItem.new(Gtk::Stock::ABOUT)) helpmenu.set_submenu(helpsubmenu) mb.append(helpmenu) tutos.signal_connect('activate') { open_url('http://booh.org/tutorial.html') } about.signal_connect('activate') { share } #- no toolbar, to save height return mb end def reset_tags for child in $tags_vbox.children $tags_vbox.remove(child) end $tags_vbox.pack_start(Gtk::Label.new(utf8(_("Tags list:"))).set_justify(Gtk::Justification::CENTER), false, false).show_all end def reset_thumbnails $allentries = [] for child in $imagesline.children $imagesline.remove(child) end end def create_main_window mb = create_menubar main_vbox = Gtk::VBox.new(false, 0) main_vbox.pack_start(mb, false, false) mainview_hbox = Gtk::HBox.new mainview_hbox.pack_start(Gtk::Alignment.new(0.5, 0, 0, 0).add($tags_vbox = Gtk::VBox.new(false, 5)), false, true) reset_tags mainview_hbox.pack_start($mainview = MainView.new, true, true) main_vbox.pack_start(mainview_hbox, true, true) $imagesline_sw = Gtk::ScrolledWindow.new(nil, nil) $imagesline_sw.set_policy(Gtk::POLICY_ALWAYS, Gtk::POLICY_NEVER) $imagesline_sw.add_with_viewport($imagesline = Gtk::HBox.new(false, 0).show) main_vbox.pack_start($imagesline_sw, false, false) main_vbox.pack_end($statusbar = Gtk::Statusbar.new, false, false) $imagesline.set_size_request(-1, Entry.thumbnails_height + $imagesline_sw.hscrollbar.size_request[1]) $main_window = Gtk::Window.new $main_window.add(main_vbox) $main_window.signal_connect('delete-event') { try_quit({ :disallow_cancel => true }) } #- read/save size and position of window if $config['pos-x'] && $config['pos-y'] $main_window.move($config['pos-x'].to_i, $config['pos-y'].to_i) else $main_window.window_position = Gtk::Window::POS_CENTER end msg 3, "size: #{$config['width']}x#{$config['height']}" $main_window.set_default_size(($config['width'] || 700).to_i, ($config['height'] || 600).to_i) $main_window.signal_connect('configure-event') { msg 3, "configure: pos: #{$main_window.window.root_origin.inspect} size: #{$main_window.window.size.inspect}" x, y = $main_window.window.root_origin width, height = $main_window.window.size $config['pos-x'] = x $config['pos-y'] = y $config['width'] = width $config['height'] = height false } $protect_gtk_pending_calls = Mutex.new $gtk_pending_calls = [] Gtk.timeout_add(50) { gtk_thread_flush true } Gtk.timeout_add(10000) { show_mem true } $main_window.show_all end Thread.abort_on_exception = true read_config Gtk.init #- Gdk::Pixbuf#rotate memory leak check (in ruby-gnome2 <= 0.16.0) #pb = Gdk::Pixbuf.new("#{$FPATH}/images/logo.png") #1.upto(5) { pb = pb.rotate(Gdk::Pixbuf::ROTATE_CLOCKWISE) } #GC.start #mem = get_mem #1.upto(5) { pb = pb.rotate(Gdk::Pixbuf::ROTATE_CLOCKWISE) } #GC.start #mem2 = get_mem #if mem2 != mem # puts _("Gdk::Pixbuf#scale memory leak detected (this is normal with unpatched ruby-gnome2 <= 0.16.0). Application would slow down to a crawl, won't proceed.") # exit 1 #end create_main_window check_config if ARGV[0] open_dir(ARGV[0]) end Gtk.main write_config