lib/alexandria/ui/dialogs/import_dialog.rb in alexandria-book-collection-manager-0.6.9 vs lib/alexandria/ui/dialogs/import_dialog.rb in alexandria-book-collection-manager-0.7.0

- old
+ new

@@ -1,7 +1,7 @@ # Copyright (C) 2004-2006 Laurent Sansonetti -# Copyright (C) 2011 Matijs van Zuijlen +# Copyright (C) 2011, 2016 Matijs van Zuijlen # # Alexandria is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. @@ -34,21 +34,21 @@ GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8') def initialize(parent, message) super(parent, _('Error while importing'), Gtk::Stock::DIALOG_QUESTION, - [[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL], - [_('_Continue'), Gtk::Dialog::RESPONSE_OK]], + [[Gtk::Stock::CANCEL, :cancel], + [_('_Continue'), :ok]], message) puts "Opened SkipEntryDialog #{inspect}" if $DEBUG - self.default_response = Gtk::Dialog::RESPONSE_CANCEL - show_all and @response = run + self.default_response = Gtk::ResponseType::CANCEL + show_all && (@response = run) destroy end def continue? - @response == Gtk::Dialog::RESPONSE_OK + @response == :ok end end class ImportDialog < Gtk::FileChooserDialog include GetText @@ -56,23 +56,23 @@ GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8') FILTERS = Alexandria::ImportFilter.all - def initialize(parent, &on_accept_cb) + def initialize(parent) super() puts 'ImportDialog opened.' if $DEBUG @destroyed = false self.title = _('Import a Library') - self.action = Gtk::FileChooser::ACTION_OPEN + self.action = :open self.transient_for = parent # self.deletable = false running = false - add_button(Gtk::Stock::HELP, Gtk::Dialog::RESPONSE_HELP) - add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL) + add_button(Gtk::Stock::HELP, :help) + add_button(Gtk::Stock::CANCEL, :cancel) import_button = add_button(_('_Import'), - Gtk::Dialog::RESPONSE_ACCEPT) + :accept) import_button.sensitive = false signal_connect('destroy') { if running @destroyed = true @@ -90,47 +90,44 @@ puts "Added ImportFilter #{filefilter} -- #{filefilter.name}" if $DEBUG filters[filefilter] = filter end signal_connect('selection_changed') do - import_button.sensitive = - filename and File.file?(filename) + import_button.sensitive = filename && File.file?(filename) end # before adding the (hidden) progress bar, we must re-set the # packing of the button box (currently packed at the end), # because the progressbar will be *after* the button box. - buttonbox = vbox.children.last - options = vbox.query_child_packing(buttonbox) - options[-1] = Gtk::PACK_START - vbox.set_child_packing(buttonbox, *options) - vbox.reorder_child(buttonbox, 1) + buttonbox = child.children.last + child.set_child_packing(buttonbox, pack_type: :start) + child.reorder_child(buttonbox, 1) pbar = Gtk::ProgressBar.new pbar.show_text = true - vbox.pack_start(pbar, false) + child.pack_start(pbar, expand: false) on_progress = proc do |fraction| begin pbar.show unless pbar.visible? pbar.fraction = fraction rescue - # TODO check if destroyed instead... + # TODO: check if destroyed instead... end end on_error = proc do |message| SkipEntryDialog.new(parent, message).continue? end exec_queue = ExecutionQueue.new - while !@destroyed and - (response = run) != Gtk::Dialog::RESPONSE_CANCEL and - response != Gtk::Dialog::RESPONSE_DELETE_EVENT + while !@destroyed && + ((response = run) != :cancel) && + (response != :delete_event) - if response == Gtk::Dialog::RESPONSE_HELP + if response == :help Alexandria::UI.display_help(self, 'import-library') next end file = File.basename(filename, '.*') base = GLib.locale_to_utf8(file) @@ -142,16 +139,12 @@ puts "Going forward with filter: #{filter.name}" if $DEBUG self.sensitive = false filter.on_iterate do |n, total| unless @destroyed - # convert to percents - coeff = total / 100.0 - percent = n / coeff - # fraction between 0 and 1 - fraction = percent / 100 - puts "#{inspect} Percentage: #{fraction}" if $DEBUG + fraction = n * 1.0 / total + puts "#{inspect} fraction: #{fraction}" if $DEBUG exec_queue.call(on_progress, fraction) end end not_cancelled = true @@ -171,20 +164,20 @@ trace = ex.backtrace.join("\n> ") log.error { "Import failed: #{ex.message} #{trace}" } end end - while thread.alive? and !@destroyed + while thread.alive? && !@destroyed # puts "Thread #{thread} still alive." running = true exec_queue.iterate Gtk.main_iteration_do(false) end unless @destroyed if library - on_accept_cb.call(library, @bad_isbns, @failed_isbns) + yield(library, @bad_isbns, @failed_isbns) break elsif not_cancelled puts "Raising ErrorDialog because not_cancelled is #{not_cancelled}" if $DEBUG ErrorDialog.new(parent, _("Couldn't import the library"), @@ -194,12 +187,10 @@ end pbar.hide self.sensitive = true end end - unless @destroyed - destroy - end + destroy unless @destroyed end end end end