class Smartdict::Gui::Controller

Constants

HISTORY_SIZE_ON_START

Attributes

from_lang_combo_box[R]
interchange_button[R]
main_window[R]
menu_bar[R]
status_bar[R]
status_icon[R]
text_view[R]
to_lang_combo_box[R]
translate_button[R]
word_entry[R]
word_list[R]

Public Class Methods

new() click to toggle source
# File lib/smartdict/gui/controller.rb, line 15
def initialize
  @translator = Smartdict::Translator.new(
    :from_lang => config.from_lang,
    :to_lang   => config.to_lang,
    :log       => true
  )

  @main_window      = MainWindow.new(self)
  @word_entry       = WordEntry.new(self)
  @translate_button = TranslateButton.new(self)
  @menu_bar         = MenuBar.new(self)
  @text_view        = TextView.new(self)
  @status_icon      = StatusIcon.new(self)
  @status_bar       = StatusBar.new(self)
  @from_lang_combo_box = LangComboBox.new(self, config.from_lang) do |lang|
    @translator.default_opts[:from_lang] = lang
  end
  @to_lang_combo_box = LangComboBox.new(self, config.to_lang) do |lang|
    @translator.default_opts[:to_lang] = lang
  end
  @interchange_button = InterchangeButton.new(self)
  @word_list          = WordList.new(self)

  @db_mutex = Mutex.new

  load_history
end

Public Instance Methods

export_translations(format_name, file_path, options = {}) click to toggle source
# File lib/smartdict/gui/controller.rb, line 91
def export_translations(format_name, file_path, options = {})
  format = Smartdict::Core::FormatManager.find(format_name)
  translations = Smartdict::Log.fetch(options)
  content = format.format_list(translations)
  File.open(file_path, 'w') { |file| file.write content }
end
focus_word_entry() click to toggle source
# File lib/smartdict/gui/controller.rb, line 102
def focus_word_entry
  @word_entry.grab_focus
end
interchange_langs() click to toggle source
# File lib/smartdict/gui/controller.rb, line 98
def interchange_langs
  @to_lang_combo_box.active, @from_lang_combo_box.active = @from_lang_combo_box.active, @to_lang_combo_box.active
end
open_about_window() click to toggle source
# File lib/smartdict/gui/controller.rb, line 83
def open_about_window
  AboutWindow.new
end
open_export_dialog() click to toggle source
# File lib/smartdict/gui/controller.rb, line 87
def open_export_dialog
  ExportDialog.new(self)
end
quit() click to toggle source
# File lib/smartdict/gui/controller.rb, line 49
def quit
  ::Gtk.main_quit
end
run() click to toggle source
# File lib/smartdict/gui/controller.rb, line 43
def run
  @main_window.draw_window
  @main_window.show_all
  Gtk.main
end
translate() click to toggle source
# File lib/smartdict/gui/controller.rb, line 53
def translate
  word      = @word_entry.text.strip.downcase
  from_lang = @translator.default_opts[:from_lang]
  to_lang   = @translator.default_opts[:to_lang]
  status_message = "Looking for \"#{word}\" ..."

  safe_concurrent_run do
    do_with_status(status_message) do
      with_reported_exception do
        if add_to_history?(word, from_lang, to_lang)
          translation = @translator.translate(word)
          add_to_history(translation)
        else
          translation = @translator.translate(word, :log => false)
        end
        @text_view.show_translation(translation)
        @word_list.scroll_up
      end
    end
  end
end
translate_selected_word(word, from_lang, to_lang) click to toggle source

@param [String] word @param [String] from_lang language code @param [String] to_lang language code

# File lib/smartdict/gui/controller.rb, line 78
def translate_selected_word(word, from_lang, to_lang)
  translation = @translator.translate(word, :log => false)
  @text_view.show_translation(translation)
end

Private Instance Methods

add_to_history(translation) click to toggle source
# File lib/smartdict/gui/controller.rb, line 117
def add_to_history(translation)
  @word_list.prepend_word(translation)
end
add_to_history?(word, from_lang, to_lang) click to toggle source
# File lib/smartdict/gui/controller.rb, line 113
def add_to_history?(word, from_lang, to_lang)
  !@word_list.first_item_matches?(word, from_lang, to_lang)
end
config() click to toggle source
# File lib/smartdict/gui/controller.rb, line 109
def config
  @config ||= Config.new
end
do_with_status(status_message) { || ... } click to toggle source
# File lib/smartdict/gui/controller.rb, line 136
def do_with_status(status_message)
  context_id = @status_bar.get_context_id(status_message)
  @status_bar.push(context_id, status_message)
  yield
ensure
  @status_bar.pop(context_id)
end
load_history() click to toggle source
# File lib/smartdict/gui/controller.rb, line 121
def load_history
  translations = Smartdict::Log.fetch(:order_desc => true, :limit => HISTORY_SIZE_ON_START)
  translations.reverse.each do |tr|
    @word_list.prepend_word(tr)
  end
end
safe_concurrent_run() { || ... } click to toggle source
# File lib/smartdict/gui/controller.rb, line 128
def safe_concurrent_run
  Thread.new do
    @db_mutex.synchronize do
      yield
    end
  end
end
with_reported_exception() { || ... } click to toggle source
# File lib/smartdict/gui/controller.rb, line 144
def with_reported_exception
  yield
rescue Smartdict::TranslationNotFound => error
  @text_view.buffer.text = error.message
rescue SocketError => error
  @text_view.buffer.text = "Socket error occurred. Please verify your network connection."
rescue Exception => error
  @text_view.show_error(error)
end