Move to Apache License
[amazing.git] / lib / amazing / widgets / moc.rb
blob97c8437fafe7d3a5646f461b3b071c4d04f052f6
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'amazing/widget'
16 require 'amazing/proc_file'
18 module Amazing
19   module Widgets
20     class Moc < Widget
21       description "Music On Console status"
22       option :mocp, "Path to mocp program", "mocp"
23       field :state, "Play state, :playing, :paused or :stopped"
24       field :file, "Playing file"
25       field :title, "Title as seen by MOC"
26       field :artist, "Artist name"
27       field :song_title, "Song title"
28       field :album, "Album of song"
29       field :total_time, "Total length of song"
30       field :time_left, "Time left of playing song"
31       field :total_sec, "Total length of song in seconds"
32       field :current_time, "Current position in playing song"
33       field :current_sec, "Current position in playing song in seconds"
34       field :bitrate, "Song bitrate"
35       field :rate, "Song sample rate"
37       default do
38         case @state
39         when :playing
40           "#@artist - #@song_title"
41         when :paused
42           "#@artist - #@song_title [paused]"
43         when :stopped
44           "[moc not playing]"
45         end
46       end
48       init do
49         moc = ProcFile.new(IO.popen("#@mocp --info"))[0]
50         @state = {:play => :playing, :pause => :paused, :stop => :stopped}[moc["State"].downcase.to_sym]
51         @file = moc["File"]
52         @title = moc["Title"]
53         @artist = moc["Artist"]
54         @song_title = moc["SongTitle"]
55         @album = moc["Album"]
56         @total_time = moc["TotalTime"]
57         @time_left = moc["TimeLeft"]
58         @total_sec = moc["TotalSec"].to_i
59         @current_time = moc["CurrentTime"]
60         @current_sec = moc["CurrentSec"].to_i
61         @bitrate = moc["Bitrate"]
62         @rate = moc["Rate"]
63       end
65       private
67       def playing?
68         @state == :playing
69       end
71       def paused?
72         @state == :paused
73       end
75       def stopped?
76         @state == :stopped
77       end
78     end
79   end
80 end