Class: GrooveDl::Widgets::Download

Inherits:
Events
  • Object
show all
Defined in:
lib/groove-dl/widgets/download.rb

Overview

Download bar section

Constant Summary collapse

RIGHT_CLICK =
3

Instance Attribute Summary collapse

Attributes inherited from Events

#app, #client

Instance Method Summary collapse

Constructor Details

#initialize(client, app, search_list) ⇒ Download

Initialize download list and download button

Parameters:

  • client (Grooveshark::Client)

    Grooveshark client

  • app (Gtk::Builder)

    Application created by Gtk builder

  • search_list (Search)

    Search class for getting list



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/groove-dl/widgets/download.rb', line 31

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

Instance Attribute Details

#downloaderObject

Returns the value of attribute downloader.



7
8
9
# File 'lib/groove-dl/widgets/download.rb', line 7

def downloader
  @downloader
end

#failedObject

Returns the value of attribute failed.



8
9
10
# File 'lib/groove-dl/widgets/download.rb', line 8

def failed
  @failed
end

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/groove-dl/widgets/download.rb', line 8

def queue
  @queue
end

#search_listObject

Returns the value of attribute search_list.



7
8
9
# File 'lib/groove-dl/widgets/download.rb', line 7

def search_list
  @search_list
end

#songsObject

Returns the value of attribute songs.



7
8
9
# File 'lib/groove-dl/widgets/download.rb', line 7

def songs
  @songs
end

#successObject

Returns the value of attribute success.



8
9
10
# File 'lib/groove-dl/widgets/download.rb', line 8

def success
  @success
end

Instance Method Details

#create_failed_item(i, reason) ⇒ Object

Append row in failed list store

Parameters:

  • i (Gtk::TreeIter)

    Iterable element

  • reason (String)

    Why this download have failed



121
122
123
124
125
126
# File 'lib/groove-dl/widgets/download.rb', line 121

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) ⇒ Object

Append row in queue list store

Parameters:

  • data (Hash)

    Data parsed



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/groove-dl/widgets/download.rb', line 89

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) ⇒ Object

Append row in the success list store

Parameters:

  • i (Gtk::TreeIter)

    Iterable element



133
134
135
136
137
138
139
# File 'lib/groove-dl/widgets/download.rb', line 133

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) ⇒ Object

Download song

Parameters:

  • song (Grooveshark::Song)

    Song to download



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/groove-dl/widgets/download.rb', line 166

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_songsObject

Download songs in queue



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/groove-dl/widgets/download.rb', line 144

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) ⇒ Object

Notify erro

Parameters:

  • song (Grooveshark::Song)

    Song displayed in failed page

  • e (StandardError)

    Exception to retrieve message



199
200
201
202
203
# File 'lib/groove-dl/widgets/download.rb', line 199

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) ⇒ Object

Notify success

Parameters:

  • song (Grooveshark::Song)

    Song displayed in success page



187
188
189
190
191
# File 'lib/groove-dl/widgets/download.rb', line 187

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_clickedObject

Event when button add to queue is clicked



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/groove-dl/widgets/download.rb', line 64

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]]
  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_clickedObject

Event when button clear queue is clicked



57
58
59
# File 'lib/groove-dl/widgets/download.rb', line 57

def on_btn_clear_queue_clicked
  @app.get_object('download_queue_list_store').clear
end

#on_btn_download_clickedObject

Event when button download is clicked



46
47
48
49
50
51
52
# File 'lib/groove-dl/widgets/download.rb', line 46

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) ⇒ Object

Open menu on right click



220
221
222
223
224
225
226
227
228
# File 'lib/groove-dl/widgets/download.rb', line 220

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_activateObject

Open downloaded song



208
209
210
211
212
213
214
215
# File 'lib/groove-dl/widgets/download.rb', line 208

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