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.



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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/sh_view.rb', line 27

def initialize
  Log.warning 'Instance of View already created!' if @@instance
	@@instance = self

  GLib.application_name = "Shroom"
  
  # 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]
    # Prepare progress dialog
    dialog = Kelp::ProgressDialog.new("Updating database...")
    cancelled = false
    dialog.signal_connect('response') { cancelled = true }

    # Scan for new songs which are not in the database
    new_songs = []
    Find.find(Global.prefs[:library_dir]) do |path|
      ext = File.extname path
      if Global::SUPPORTED_EXTENSIONS.include? ext
        new_songs << path if Song.first(:path => path).nil?
      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 |path|
      # Show path to song in the dialog
      dialog.message = path
      
      begin
        # Save song to the database
        Database.add_song path
        Log.debug "Added #{path} to database"
      rescue
        Log.info "Failed to add ${path} to database", $!
      end
      # Increment progress bar
      frac = dialog.fraction + inc
      dialog.fraction = frac > 1 ? 1 : frac
      # 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

    Song.clean_missing_songs
    Album.clean_empty_albums
    Artist.clean_empty_artists

    # Destroy progress dialog
    dialog.destroy

    # Clean up
    new_songs = dialog = nil
  end
  
  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

  # Allow user to seek using the slider
  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
      # Update slider and time label according to position in song
      pos = @player.position.to_f
      seeker.value = pos / @player.duration
      lbl_time.text = format_seconds(pos) + "/" + format_seconds(@player.duration)
      # Ensure label shows title of currently playing song
      lbl_song.set_markup @player.song.to_html
    else
      # No song is playing or paused, reflect this on display
      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
  #FIXME
  Drag.dest_set(event_box, 0, [], 0)
  event_box.signal_connect('drag-data-received') do |w, dc, x, y, data, info, time|
    p data
    dc.drop_finish(true, time)
    true
  end
  event_box.signal_connect('drag-motion') do |w, dc, x, y, time|
    dc.drag_status(Gdk::DragContext::ACTION_COPY, time)
    true
  end
  event_box.signal_connect('drag-drop') do |w, dc, x, y, time|
    Drag.get_data(w, dc, dc.targets.last, time)
    true
  end
  # END FIXME
  @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
  @txt_lyrics = TextView.new
  @txt_lyrics.editable = false
  @txt_lyrics.cursor_visible = false
  @txt_lyrics.left_margin = @txt_lyrics.right_margin = 8
  scr_lyrics = ScrolledWindow.new(nil, nil)
  scr_lyrics.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
  scr_lyrics.add @txt_lyrics
  @buf_lyrics = @txt_lyrics.buffer
  @buf_lyrics.create_tag 'title', 'font' => 'Sans 18'
  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



327
328
329
# File 'lib/sh_view.rb', line 327

def self.instance
	return @@instance
end

Instance Method Details

#next_songObject



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/sh_view.rb', line 712

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



692
693
694
695
696
697
# File 'lib/sh_view.rb', line 692

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

#paused?Boolean

Returns:

  • (Boolean)


703
704
705
# File 'lib/sh_view.rb', line 703

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

#playObject



685
686
687
688
689
690
# File 'lib/sh_view.rb', line 685

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

#playing?Boolean

Returns:

  • (Boolean)


699
700
701
# File 'lib/sh_view.rb', line 699

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

#prev_songObject



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/sh_view.rb', line 728

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



670
671
672
# File 'lib/sh_view.rb', line 670

def queue
  return @queue
end

#quitObject



749
750
751
# File 'lib/sh_view.rb', line 749

def quit
  Gtk.main_quit
end

#set_queue(queue, pos = 0) ⇒ Object



674
675
676
677
678
# File 'lib/sh_view.rb', line 674

def set_queue queue, pos=0
  @queue_pos = pos
  @queue = queue
  prepare_song
end

#set_queue_pos(pos) ⇒ Object



680
681
682
683
# File 'lib/sh_view.rb', line 680

def set_queue_pos pos
  @queue_pos = pos
  prepare_song
end

#showObject



744
745
746
747
# File 'lib/sh_view.rb', line 744

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

#stopObject



707
708
709
710
# File 'lib/sh_view.rb', line 707

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

#update_song(song) ⇒ Object



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

def update_song song
  @browse.update_song(song) do |s|
    new_song = yield s
    song = new_song if new_song.is_a? Song
  end
end