Class: Gtk2Mp3::GUI

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk2mp3/gui.rb

Constant Summary collapse

MUTEX =
Mutex.new
ID =
lambda{|_|File.basename(_.strip.split(/\n/).first.strip,'.*')}

Instance Method Summary collapse

Constructor Details

#initialize(program) ⇒ GUI

Returns a new instance of GUI.



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
# File 'lib/gtk2mp3/gui.rb', line 77

def initialize(program)
  window,minime,menu = program.window,program.mini_menu,program.app_menu
  @db = JSON.parse File.read(CONFIG[:DBM])
  window.signal_connect('delete-event'){File.write(CONFIG[:DBM], JSON.pretty_generate(@db))}

  # Build
  vbox = Such::Box.new(window, :VBOX)
  hbox = Such::Box.new(vbox, :HBOX)
  Such::Button.new(hbox, :next_button!){next_song!}
  Such::Button.new(hbox, :stop_button!){stop_song!}
  @label = Such::Label.new(vbox)
  menu.each{|_|_.destroy if _.label=='Full Screen' or _.label=='Help'}
  minime.each{|_|_.destroy}
  minime.append_menu_item(:stop_item!){stop_song!}
  minime.append_menu_item(:next_item!){next_song!}

  # Inits
  @list = `mpc listall`.lines.map{|_|ID[_]}.uniq
  @skipped=@playing=nil
  next_song!
  @time,@played = Time.now,@playing
  Thread.new do
    sleep(1) # mpd needs a little time to settle
    mpc_idle_player
  end

  window.show_all
end

Instance Method Details

#db_updateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gtk2mp3/gui.rb', line 41

def db_update
  if @skipped
    up(@skipped) if Time.now-@time<CONFIG[:PLAYED]
  elsif @played
    down(@played)
    @playing = ID[`mpc current`]
    if play?(@playing)
      @label.text = @playing
    else
      next_song
    end
  end
  @time,@played,@skipped = Time.now,@playing,nil
end

#down(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/gtk2mp3/gui.rb', line 26

def down(id)
  if count=@db[id]
    count -= 1
    if count>0
      @db[id] = count
    else
      @db.delete(id)
    end
  end
end

#mpc_idle_playerObject



56
57
58
59
60
61
# File 'lib/gtk2mp3/gui.rb', line 56

def mpc_idle_player
  loop do
    system 'mpc idle player'
    MUTEX.synchronize{db_update}
  end
end

#next_songObject



22
23
24
# File 'lib/gtk2mp3/gui.rb', line 22

def next_song
  @label.text = @playing = ID[`mpc searchplay filename '#{random_song}'`]
end

#next_song!Object



63
64
65
66
67
68
# File 'lib/gtk2mp3/gui.rb', line 63

def next_song!
  MUTEX.synchronize do
    @skipped = @playing
    next_song
  end
end

#play?(id) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/gtk2mp3/gui.rb', line 10

def play?(id)
  rand(@db[id].to_i+1)==0
end

#random_songObject



14
15
16
17
18
19
20
# File 'lib/gtk2mp3/gui.rb', line 14

def random_song
  n = @list.length
  loop do
    id = @list[rand(n)]
    return id if play?(id)
  end
end

#stop_song!Object



70
71
72
73
74
75
# File 'lib/gtk2mp3/gui.rb', line 70

def stop_song!
  MUTEX.synchronize do
    @skipped = @playing = @played = nil
    system 'mpc stop'
  end
end

#up(id) ⇒ Object



37
38
39
# File 'lib/gtk2mp3/gui.rb', line 37

def up(id)
  @db[id] = @db[id].to_i+1
end