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',
  TAB_COVER_BROWSE => 'Cover Browse',
  TAB_PLAYLISTS => 'Playlists'
}
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeView

Returns a new instance of View.



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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/sh_view.rb', line 26

def initialize
  Sh::Log.warning 'Instance of Browse already created!' if @@instance
	@@instance = self
  
  # 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
      
      begin
        # Read metadata from song tags
        song.read_tags!
        # Save song to the database
        $db.save_song song
        Sh::Log.debug "Added #{song.path} to database"
      rescue
        Sh::Log.info "Failed to add ${song.path} to database", $!
      end
      # Increment progress bar
      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
  
  Sh::Log.debug 'Synchronized library dir with database'

  # 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
  box_lyrics = VBox.new false, 0
  box_lyrics.add scr_lyrics
  @btn_refresh_lyrics = Button.new Stock::REFRESH
  box_lyrics.pack_start @btn_refresh_lyrics, false, false
  queue = Sh::Queue.new self
  browse = Sh::Browse.new self
  cover_browse = Sh::CoverBrowse.new self
  playlists = Sh::Playlists.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 = box_lyrics
    when TAB_COVER_BROWSE
      frm_content.child = cover_browse.widget
    when TAB_PLAYLISTS
    	frm_content.child = playlists.widget
    end
    frm_content.show_all
  end
  # Select 'Browse' tab on startup
  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', 'PlayPause'
      if playing?
        pause
      else
        play
      end
    when 'Pause'
      pause
    end
  end
  
  ShroomActions.init

  # Show everything
  @window.show_all
end

Class Method Details

.instanceObject



291
292
293
# File 'lib/sh_view.rb', line 291

def self.instance
	return @@instance
end

Instance Method Details

#next_songObject



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/sh_view.rb', line 631

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



611
612
613
614
615
616
# File 'lib/sh_view.rb', line 611

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

#paused?Boolean

Returns:

  • (Boolean)


622
623
624
# File 'lib/sh_view.rb', line 622

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

#playObject



604
605
606
607
608
609
# File 'lib/sh_view.rb', line 604

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

#playing?Boolean

Returns:

  • (Boolean)


618
619
620
# File 'lib/sh_view.rb', line 618

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

#prev_songObject



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/sh_view.rb', line 647

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



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

def queue
  return @queue
end

#queue=(queue) ⇒ Object



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

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

#queue_pos=(pos) ⇒ Object



599
600
601
602
# File 'lib/sh_view.rb', line 599

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

#quitObject



668
669
670
# File 'lib/sh_view.rb', line 668

def quit
  Gtk.main_quit
end

#showObject



663
664
665
666
# File 'lib/sh_view.rb', line 663

def show
	Sh::Log.debug 'Starting GTK main loop'
  Gtk.main
end

#stopObject



626
627
628
629
# File 'lib/sh_view.rb', line 626

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