Class: GrooveDl::Widgets::Download
- Defined in:
- lib/groove-dl/widgets/download.rb
Overview
Download bar section
Constant Summary collapse
- RIGHT_CLICK =
3
Instance Attribute Summary collapse
-
#downloader ⇒ Object
Returns the value of attribute downloader.
-
#failed ⇒ Object
Returns the value of attribute failed.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#search_list ⇒ Object
Returns the value of attribute search_list.
-
#songs ⇒ Object
Returns the value of attribute songs.
-
#success ⇒ Object
Returns the value of attribute success.
Attributes inherited from Events
Instance Method Summary collapse
-
#create_failed_item(i, reason) ⇒ Object
Append row in failed list store.
-
#create_queue_item(data) ⇒ Object
Append row in queue list store.
-
#create_success_item(i) ⇒ Object
Append row in the success list store.
-
#download_file(song) ⇒ Object
Download song.
-
#download_songs ⇒ Object
Download songs in queue.
-
#initialize(client, app, search_list) ⇒ Download
constructor
Initialize download list and download button.
-
#notify_error(song, e) ⇒ Object
Notify erro.
-
#notify_success(song) ⇒ Object
Notify success.
-
#on_btn_add_to_queue_clicked ⇒ Object
Event when button add to queue is clicked.
-
#on_btn_clear_queue_clicked ⇒ Object
Event when button clear queue is clicked.
-
#on_btn_download_clicked ⇒ Object
Event when button download is clicked.
-
#on_download_success_button_press_event(widget, event) ⇒ Object
Open menu on right click.
-
#on_menu_open_activate ⇒ Object
Open downloaded song.
Constructor Details
#initialize(client, app, search_list) ⇒ Download
Initialize download list and download button
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
#downloader ⇒ Object
Returns the value of attribute downloader.
7 8 9 |
# File 'lib/groove-dl/widgets/download.rb', line 7 def downloader @downloader end |
#failed ⇒ Object
Returns the value of attribute failed.
8 9 10 |
# File 'lib/groove-dl/widgets/download.rb', line 8 def failed @failed end |
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/groove-dl/widgets/download.rb', line 8 def queue @queue end |
#search_list ⇒ Object
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 |
#songs ⇒ Object
Returns the value of attribute songs.
7 8 9 |
# File 'lib/groove-dl/widgets/download.rb', line 7 def songs @songs end |
#success ⇒ Object
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
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
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
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
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.) 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 ⇒ Object
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
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.) @app.get_object('download_label_failed') .set_text(format('Failed (%d)', @failed += 1)) end |
#notify_success(song) ⇒ Object
Notify success
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_clicked ⇒ Object
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_clicked ⇒ Object
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_clicked ⇒ Object
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 (, event) return unless event.is_a?(Gdk::EventButton) && event. == RIGHT_CLICK path, _model = .get_path_at_pos(event.x, event.y) .selection.select_path(path) @app.get_object('success_menu') .popup(nil, nil, event., event.time) end |
#on_menu_open_activate ⇒ Object
Open downloaded song
208 209 210 211 212 213 214 215 |
# File 'lib/groove-dl/widgets/download.rb', line 208 def 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 |