Class: Sh::View

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

Constant Summary collapse

TAB_NAMES =
{
  TAB_QUEUE => 'Queue',
  TAB_BROWSE => 'Browse',
  TAB_LYRICS => 'Lyrics'
}

Instance Method Summary collapse

Constructor Details

#initializeView

Returns a new instance of View.



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
135
136
137
138
139
140
141
142
143
144
145
146
147
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
173
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
215
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/sh_view.rb', line 20

def initialize
  # Initialize GTK
  Gtk.init

  # Attempt to load RNotify
  @rnotify = try_require 'RNotify'

  # Prepare notifications if RNotify is set up
  Notify.init 'Shroom' if @rnotify

  # Search for songs in library directory if it exists
  if File.exists? Global.prefs[:library_dir]
    cancelled = false
    # Prepare progress dialog
    dialog = Kelp::ProgressDialog.new("Updating database...")
    dialog.signal_connect('response') { cancelled = true}
    # Scan for new songs which are not in the database
    new_songs = []
    Dir[Global.prefs[:library_dir]+'/**/*'].each do |path|
      ext = File.extname path
      if Sh::Global::SUPPORTED_EXTENSIONS.include? ext
        new_songs << Song.new(path) unless $db.contains? path
      end
    end
    # Show progress dialog if there are new songs to process
    if new_songs.length > 0
      dialog.show_all
      Kelp.process_events
    end
    # inc = the fraction to increment the progress bar by for each song
    inc = 1 / new_songs.length.to_f
    # Enter new songs in database
    new_songs.each do |song|
      # Show path to song in the dialog
      dialog.message = song.path
      puts "Adding: #{song.path}"
      begin
        # Read metadata from song tags
        song.read_tags!
        # Save song to the database
        $db.save_song song
        # Increment progress bar
      rescue
        puts "Couldn't add song. Reason: #{$!}"
      end
      dialog.fraction += inc
      # Make sure that the GUI is updated
      Kelp.process_events
      # Stop adding songs if process is cancelled by the user
      break if cancelled
    end
    
    # Remove old entries from database
    dialog.message = "Cleaning out old entries..."
    dialog.pulse
    $db.clean

    # Destroy progress dialog
    dialog.destroy

    # Clean up
    new_songs = dialog = nil
  end

  # Load small 16x16 pixel Shroom logo
  icon = Gdk::Pixbuf.new Global.locate('icon_16x16.png')

  # Create main window
  @window = Window.new "Shroom"
  @window.icon = icon
  @window.resize 800, 600
  # Call 'quit' method when window is destroyed
  @window.signal_connect('destroy') do
    quit
  end

  # Create the status icon which appears in the system tray
  @status_icon = StatusIcon.new
  @status_icon.pixbuf = icon
  @status_icon.visible = true
  # Callback for when status icon is clicked
  @status_icon.signal_connect('activate') do |widget|
    # Toggle visibility of window
    @window.visible = !@window.visible?
  end

  content_pane = VBox.new(false, 0)
  @window.add content_pane

  # Create and add menu bar to the window
  content_pane.pack_start(create_menu, false, true, 0)

  vpaned = VBox.new
  content_pane.add vpaned

  # === Playback Controls ===
  vbox = VBox.new false, 0
  vpaned.pack_start vbox, false, true, 8
  hbox = HBox.new
  lbl_song = Label.new
  lbl_song.ellipsize = Pango::Layout::ELLIPSIZE_END
  hbox.add lbl_song
  lbl_time = Label.new(format_seconds(0) + "/" + format_seconds(0))
  hbox.pack_start lbl_time, false, false, 8
  vbox.pack_start hbox, false, false, 8
  box_controls = HBox.new
  vbox.add box_controls
  @btn_play = ToggleButton.new
  @btn_play.add Image.new Stock::MEDIA_PLAY, IconSize::SMALL_TOOLBAR
  box_controls.pack_start @btn_play, false, true, 0
  btn_prev = Button.new
  btn_prev.add Image.new Stock::MEDIA_PREVIOUS, IconSize::SMALL_TOOLBAR
  box_controls.pack_start btn_prev, false, true, 0
  seeker = HScale.new 0, 1, 0.01
  box_controls.add seeker
  seeker.draw_value = false
  btn_next = Button.new
  btn_next.add Image.new Stock::MEDIA_NEXT, IconSize::SMALL_TOOLBAR
  box_controls.pack_start btn_next, false, true, 0

  seeker.signal_connect('change_value') do |range, scroll, value|
    if @player
      seeker.value = value if value < 0.999
      pos = seeker.value * @player.duration
      @player.seek pos
    else
      seeker.value = 0
    end
  end
  GLib::Timeout.add(100) do
    if @player
      pos = @player.position.to_f
      seeker.value = pos / @player.duration
      lbl_time.text = format_seconds(pos) + "/" + format_seconds(@player.duration)
      lbl_song.set_markup @player.song.to_html
    else
      seeker.value = 0
      lbl_time.text = format_seconds(0) + "/" + format_seconds(0)
      lbl_song.set_markup "<b>Not playing</b>"
    end
    true
  end
  @btn_play.signal_connect('clicked') do |toggle|
    if toggle.active?
      @player.play
    else
      @player.pause
    end if @player
  end
  btn_next.signal_connect('clicked') do |btn|
    next_song
  end
  btn_prev.signal_connect('clicked') do |btn|
    prev_song
  end

  # Horizontally split area
  hpaned = HPaned.new
  vpaned.add hpaned

  # Content
  frm_content = Frame.new
  hpaned.add2 frm_content

  # === Sidebar ===
  sidebar = VBox.new(false, 0)
  hpaned.add1 sidebar
  sw = ScrolledWindow.new(nil, nil)
  sw.set_policy(POLICY_AUTOMATIC, POLICY_NEVER)
  sw.width_request = 150
  sidebar.add sw
  sto_tabs = ListStore.new(String)
  NUM_TABS.times do |tab|
    iter = sto_tabs.append
    iter[0] = TAB_NAMES[tab]
  end
  lst_tabs = TreeView.new sto_tabs
  lst_tabs.headers_visible = false
  renderer = CellRendererText.new
  column = TreeViewColumn.new('Invisible header',
    renderer,
    'text' => 0)
  lst_tabs.append_column column
  sw.add lst_tabs
  # Cover image widget
  @img_cover = Image.new
  event_box = EventBox.new
  event_box.add @img_cover
  event_box.signal_connect('button-press-event') do |w, event|
    Kelp::ImageDialog.new(@pixbuf, @player.song.album.title).show if @pixbuf and @player
  end
  @img_cover.height_request = 128
  @img_cover.width_request = 128
  bx = HBox.new false, 0
  bx.pack_start event_box, false, false, 16
  sidebar.pack_start bx, false, false, 16
  # Lyrics text view
  @html_lyrics = HtmlView.new
  scr_lyrics = ScrolledWindow.new(nil, nil)
  scr_lyrics.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
  scr_lyrics.add @html_lyrics
  # Browse area
  browse = Sh::Browse.new self
  queue = Sh::Queue.new self
  lst_tabs.selection.signal_connect('changed') do |selection|
    frm_content.remove frm_content.child if frm_content.child
    case selection.selected.path.indices[0]
    when TAB_QUEUE
      frm_content.child = queue.widget
      queue.update
    when TAB_BROWSE
      frm_content.child = browse.widget
    when TAB_LYRICS
      frm_content.child = scr_lyrics
    end
    frm_content.show_all
  end
  # Select 'Browse' tab
  i = 0
  lst_tabs.model.each do |row|
    lst_tabs.selection.select_path row[1] if i == TAB_BROWSE
    i += 1
  end

  # === Multimedia Keys ===
  MMKeys.new do |key|
    case key
    when 'Stop'
      stop
    when 'Next'
      next_song
    when 'Previous'
      prev_song
    when 'Play'
      if playing?
        pause
      else
        play
      end
    when 'PlayPause'
      if playing?
        pause
      else
        play
      end
    when 'Pause'
      pause
    end
  end

  # Show everything
  @window.show_all
end

Instance Method Details

#next_songObject



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/sh_view.rb', line 602

def next_song
  if @queue
    playing = @player.playing?
    @player.stop
    @queue_pos += 1
    if @queue_pos < @queue.size
      prepare_song
      play if playing
    else
      @queue_pos = 0
      prepare_song
      @btn_play.active = false
    end
  end
end

#pauseObject



582
583
584
585
586
587
# File 'lib/sh_view.rb', line 582

def pause
  if @player
    @btn_play.active = false
    @player.pause
  end
end

#paused?Boolean

Returns:

  • (Boolean)


593
594
595
# File 'lib/sh_view.rb', line 593

def paused?
  return @player && @player.paused?
end

#playObject



575
576
577
578
579
580
# File 'lib/sh_view.rb', line 575

def play
  if @player
    @btn_play.active = true
    @player.play
  end
end

#playing?Boolean

Returns:

  • (Boolean)


589
590
591
# File 'lib/sh_view.rb', line 589

def playing?
  return @player && @player.playing?
end

#prev_songObject



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/sh_view.rb', line 618

def prev_song
  if @queue
    playing = @player.playing?
    @player.stop
    @queue_pos -= 1
    if @queue_pos >= 0
      prepare_song
      play if playing
    else
      @queue_pos = 0
      prepare_song
      @btn_play.active = false
    end
  end
end

#queueObject



560
561
562
# File 'lib/sh_view.rb', line 560

def queue
  return @queue
end

#queue=(queue) ⇒ Object



564
565
566
567
568
# File 'lib/sh_view.rb', line 564

def queue=(queue)
  @queue_pos = 0
  @queue = queue
  prepare_song
end

#queue_pos=(pos) ⇒ Object



570
571
572
573
# File 'lib/sh_view.rb', line 570

def queue_pos=(pos)
  @queue_pos = pos
  prepare_song
end

#quitObject



638
639
640
# File 'lib/sh_view.rb', line 638

def quit
  Gtk.main_quit
end

#showObject



634
635
636
# File 'lib/sh_view.rb', line 634

def show
  Gtk.main
end

#stopObject



597
598
599
600
# File 'lib/sh_view.rb', line 597

def stop
  @btn_play.active = false
  @player.stop if @player
end