Class: Sh::Browse

Inherits:
Object show all
Defined in:
lib/sh_browse.rb

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Browse

Returns a new instance of Browse.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sh_browse.rb', line 3

def initialize view
  @tree = TreeView.new
  @tree.selection.mode = Gtk::SELECTION_MULTIPLE

  ren_artist = CellRendererText.new
  col_artist = TreeViewColumn.new('Songs', ren_artist)
  col_artist.set_cell_data_func(ren_artist) do |tvc, cell, model, iter|
    cell.text = ''
    data = iter[0]
    if data.is_a? Song
      if data.title
        cell.text = "%.2d\t%s" % [data.track_num, data.title]
      else
        cell.text = File.basename(data.path)
      end
    else
      cell.text = data.to_s
    end
  end
  @tree.append_column col_artist

  @model = TreeStore.new(Object, Array)
  @model.set_default_sort_func do |oa, ob|
    a1, b1 = oa[0].to_s, ob[0].to_s
    a2, b2 = a1.gsub(/[^\w ]/, ""), b1.gsub(/[^\w ]/, "")
    cmp = 0

    unless a2.empty? or b2.empty?
      cmp = a2.casecmp b2 if cmp == 0
      cmp = a2 <=> b2 if cmp == 0
    end

    cmp = a1.casecmp b1 if cmp == 0
    cmp = a1 <=> b1 if cmp == 0
    cmp
  end
  @model.set_sort_column_id(TreeSortable::DEFAULT_SORT_COLUMN_ID,
    Gtk::SORT_ASCENDING)
  @tree.model = @model

  GLib::Timeout.add(1000) do
    fill_model
    false
  end

  @tree.signal_connect('button-press-event') do |widget, event|
    # Right-click
    if event.button == 3
      path = @tree.get_path_at_pos(event.x, event.y).first
      data = @model.get_iter(path)[0]
      selection = @tree.selection
      selected_rows = selection.selected_rows
      clicked_in_selection = false
      selected_rows.each do |sel|
        clicked_in_selection = sel.indices == path.indices
        break if clicked_in_selection
      end

      unless clicked_in_selection
        # Change the selection
        selection.unselect_all
        selection.select_path(path)
        selected_rows = selection.selected_rows
      end

      #puts selected_rows

      if data.is_a? Sh::Song
        song = data
        menu = Menu.new
        
        Actions.get(:song).each do |action|
       	pbtn = nil
       	if action[:stock_image]
       		pbtn = ImageMenuItem.new(action.name)
       		pbtn.image = Image.new(action[:stock_image], IconSize::MENU)
       	else
         	pbtn = MenuItem.new(action.name)
         end
       	menu.append pbtn
			if action.tags.include? :playlist
				pmnu = Gtk::Menu.new
				Sh::Playlists.load_playlists.each do |playlist|
        	pbtn_playlist = Gtk::MenuItem.new playlist.name
        	pbtn_playlist.signal_connect('activate') do |widget|
        		action.execute song, playlist
        	end
        	pmnu.append pbtn_playlist
        end
        pbtn.submenu = pmnu
			else
       	pbtn.signal_connect('activate') do |widget|
       		action.execute song
       	end
       end
        end
        
        menu.show_all
        menu.popup(nil, nil, event.button, event.time)
      end
    end
  end
  @tree.signal_connect('row_activated') do |widget, path, col|
    iter = @tree.model.get_iter(path)
    parent = iter.parent
    if iter[0].is_a? Song
      view.stop
      queue = []
      while iter.parent == parent
        queue << iter[0]
        iter.next!
      end
      prev = []
      iter = parent.first_child
      until iter.path.indices == path.indices
        prev << iter[0]
        iter.next!
      end
      queue.insert 0, *prev
      view.set_queue queue, prev.size
      view.play
    elsif not iter[1].empty?
      view.stop
      view.set_queue iter[1].dup
      view.play
    end
  end

  @scroll = ScrolledWindow.new(nil, nil)
  @scroll.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
  @scroll.add @tree
end

Instance Method Details

#fill_modelObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/sh_browse.rb', line 216

def fill_model
  @artist_nodes = {}
  @model.clear

  Artist.each do |artist|
    artist_node = create_artist_node artist

    unknown_album_songs = []
    non_album_songs = []
    artist.songs.each do |song|
      if song.album.id == Album::UNKNOWN
        unknown_album_songs << song
      elsif song.album.id == Album::NON_ALBUM
        non_album_songs << song
      end
    end

    unless unknown_album_songs.empty?
      create_album_node artist_node, Album[Album::UNKNOWN], unknown_album_songs
    end
    unless non_album_songs.empty?
      create_album_node artist_node, Album[Album::NON_ALBUM], non_album_songs
    end

    artist.albums.each do |album|
      create_album_node artist_node, album, album.songs
    end

    artist_node[1] = artist_node[1].sort_by {|s| s.to_s}

    Kelp.process_events
  end
end

#insert_song(song) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sh_browse.rb', line 174

def insert_song song
  artist_node = album_node = nil

  if song.album.special_case?
    artist_node = @artist_nodes[song.artist.id]
  else
    artist_node = @artist_nodes[song.album.artist.id]
  end

  if artist_node and artist_node.has_child?
    iter = artist_node.first_child
    loop do
      if iter[0].id == song.album.id
        album_node = iter
        break
      else
        break unless iter.next!
      end
    end
  end

  # Artist node
  unless artist_node
    artist_node = create_artist_node song.artist
  end

  # Album node
  unless album_node
    album_node = create_album_node artist_node, song.album, []
  end

  artist_node[1] << song
  artist_node[1] = artist_node[1].sort_by{|s| s.to_s}
  album_node[1] << song
  album_node[1] = album_node[1].sort_by{|s| s.to_s}

  # Song node
  song_node = @model.append album_node
  song_node[0] = song
  song_node[1] = [song]
end

#remove_song(song) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sh_browse.rb', line 148

def remove_song song
  # TODO: Improve this
  song_node = nil
  if song.is_a? TreeIter
    song_node = song
  else
    @model.each do |model, path, iter|
      if iter[0] == song
        song_node = iter
      end
    end
  end
  
  if song_node
    album_node = song_node.parent
    artist_node = album_node.parent
    @model.remove song_node
    album_node[1].delete song
    # Removal of these nodes are commented out because they can result
    # in parentless song nodes
    #@model.remove album_node unless album_node.has_child?
    artist_node[1].delete song
    #@model.remove artist_node unless artist_node.has_child?
  end
end

#update_song(song) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/sh_browse.rb', line 140

def update_song song
  # TODO: Improve this
  remove_song song
  new_song = yield song
  song = new_song if new_song.is_a? Song
  insert_song song
end

#widgetObject



136
137
138
# File 'lib/sh_browse.rb', line 136

def widget
  return @scroll
end