class GrooveDl::Widgets::Download

Download bar section

Constants

FAILED_COLUMN_REASON
QUEUE_COLUMN_PGBAR_TEXT
RIGHT_CLICK
SUCCESS_COLUMN_SIZE

Attributes

downloader[RW]
failed[RW]
failed_store[R]
queue[RW]
queue_store[R]
search_list[RW]
songs[RW]
success[RW]
success_store[R]

Public Class Methods

new(client, app, search_list) click to toggle source

Initialize download list and download button

@param [Grooveshark::Client] client Grooveshark client @param [Gtk::Builder] app Application created by Gtk builder @param [Search] search_list Search class for getting list

@return [Download]

Calls superclass method
# File lib/groove-dl/widgets/download.rb, line 32
def initialize(client, app, search_list)
  super(client, app)
  @songs = {}
  @queue = 0
  @success = 0
  @failed = 0
  @search_list = search_list
  @downloader = GrooveDl::Downloader.new(@client)
  @downloader.type = 'gui'
  @app.get_object('directory_chooser').filename = Dir.tmpdir
end

Public Instance Methods

create_failed_item(i, reason) click to toggle source

Append row in failed list store

@param [Gtk::TreeIter] i Iterable element @param [String] reason Why this download have failed

# File lib/groove-dl/widgets/download.rb, line 123
def create_failed_item(i, reason)
  iter = @failed_store.append
  path = i[FAILED_COLUMN_PATH]
  iter[FAILED_COLUMN_PATH] = path
  iter[FAILED_COLUMN_REASON] = reason
end
create_queue_item(data) click to toggle source

Append row in queue list store

@param [Hash] data Data parsed

# File lib/groove-dl/widgets/download.rb, line 91
def create_queue_item(data)
  data.each do |id, element|
    if element.is_a?(Grooveshark::Song)
      iter = @queue_store.append
      iter[QUEUE_COLUMN_PATH] =
        @downloader.build_path(@app.get_object('directory_chooser')
                                 .filename,
                               element)
      iter[QUEUE_COLUMN_PGBAR_VALUE] = 0
      iter[QUEUE_COLUMN_PGBAR_TEXT] = nil
      @songs[element.id] = { iter: iter, song: element }
    else
      playlist = Grooveshark::Playlist.new(@client,
                                           'playlist_id' => id)
      result = {}
      playlist.load_songs.each do |song|
        result[song.id] = song
      end

      create_queue_item(result)
    end
  end

  return if @songs.empty?
end
create_success_item(i) click to toggle source

Append row in the success list store

@param [Gtk::TreeIter] i Iterable element

# File lib/groove-dl/widgets/download.rb, line 135
def create_success_item(i)
  iter = @success_store.append
  path = i[SUCCESS_COLUMN_PATH]
  iter[SUCCESS_COLUMN_PATH] = path
  size = (File.size?(path).to_f / (1024 * 1024)).round(2)
  iter[SUCCESS_COLUMN_SIZE] = "#{size} MB"
end
download_file(song) click to toggle source

Download song

@param [Grooveshark::Song] song Song to download

# File lib/groove-dl/widgets/download.rb, line 168
def download_file(song)
  begin
    @downloader.download(song[:song], song[:iter])
    notify_success(song)
  rescue Errors::AlreadyDownloaded => e
    GrooveDl.configuration.logger.info(e.message)
    notify_success(song)
  rescue Grooveshark::GeneralError => e
    GrooveDl.configuration.logger.error(e)
    notify_error(song, e)
  end
  @app.get_object('download_label_queue')
    .set_text(format('Queue (%d)', @queue -= 1))
  @queue_store.remove(song[:iter])
end
download_songs() click to toggle source

Download songs in queue

# File lib/groove-dl/widgets/download.rb, line 146
def download_songs
  concurrency = @app.get_object('concurrency_entry').text.to_i
  concurrency = 5 if concurrency.zero?
  Thread.abort_on_exception = true
  Thread.new do
    nb = 0
    @songs.each do |_id, s|
      nb += 1
      Thread.new do
        download_file(s)
        nb -= 1
      end
      sleep(0.5) until nb < concurrency
    end
  end
end
notify_error(song, e) click to toggle source

Notify erro

@param [Grooveshark::Song] song Song displayed in failed page @param [StandardError] e Exception to retrieve message

# File lib/groove-dl/widgets/download.rb, line 201
def notify_error(song, e)
  create_failed_item(song[:iter], e.message)
  @app.get_object('download_label_failed')
    .set_text(format('Failed (%d)', @failed += 1))
end
notify_success(song) click to toggle source

Notify success

@param [Grooveshark::Song] song Song displayed in success page

# File lib/groove-dl/widgets/download.rb, line 189
def notify_success(song)
  create_success_item(song[:iter])
  @app.get_object('download_label_success')
    .set_text(format('Success (%d)', @success += 1))
end
on_btn_add_to_queue_clicked() click to toggle source

Event when button add to queue is clicked

# File lib/groove-dl/widgets/download.rb, line 65
def on_btn_add_to_queue_clicked
  selected = {}
  column_id = GrooveDl::Widgets::Search::COLUMN_ID
  column_checkbox = GrooveDl::Widgets::Search::COLUMN_CHECKBOX
  search_list_store = @app.get_object('search_list_store')
  search_list_store.each do |_model, _path, iter|
    next unless iter[column_checkbox]
    selected[iter[column_id]] = @search_list.data[iter[column_id]]
    iter[column_checkbox] = false
  end

  @queue_store = @app.get_object('download_queue_list_store')
  @failed_store = @app.get_object('download_failed_list_store')
  @success_store = @app.get_object('download_success_list_store')
  create_queue_item(selected)

  @queue = @songs.count
  @app.get_object('download_label_queue')
    .set_text(format('Queue (%d)', @queue))
end
on_btn_clear_queue_clicked() click to toggle source

Event when button clear queue is clicked

# File lib/groove-dl/widgets/download.rb, line 58
def on_btn_clear_queue_clicked
  @app.get_object('download_queue_list_store').clear
end
on_btn_download_clicked() click to toggle source

Event when button download is clicked

# File lib/groove-dl/widgets/download.rb, line 47
def on_btn_download_clicked
  return if @queue.zero?
  @app.get_object('btn_clear_queue').sensitive = false
  @app.get_object('btn_add_to_queue').sensitive = false

  download_songs
end
on_download_success_button_press_event(widget, event) click to toggle source

Open menu on right click

# File lib/groove-dl/widgets/download.rb, line 222
def on_download_success_button_press_event(widget, event)
  return unless event.is_a?(Gdk::EventButton) &&
                event.button == RIGHT_CLICK

  path, _model = widget.get_path_at_pos(event.x, event.y)
  widget.selection.select_path(path)
  @app.get_object('success_menu')
    .popup(nil, nil, event.button, event.time)
end
on_menu_open_activate() click to toggle source

Open downloaded song

# File lib/groove-dl/widgets/download.rb, line 210
def on_menu_open_activate
  treeview = @app.get_object('download_success')
  iter = treeview.selection.selected
  Thread.new do
    path = iter[Download::QUEUE_COLUMN_PATH]
    system("xdg-open #{Shellwords.escape(path)}")
  end
end