csv: Clean `CsvDocument` a little bit.
[nit.git] / lib / mpd.nit
index ced7e17..9a56b95 100644 (file)
@@ -34,12 +34,11 @@ class MPDConnection
        do
                var p: nullable Socket = null
 
-               p = new Socket.stream_with_host(host, port)
-               p.connect
+               p = new Socket.client(host, port)
 
                sys.nanosleep(0,5000)
 
-               var rep = p.read
+               var rep = p.read(1024)
                assert not rep.is_empty
                if not rep.has_prefix("OK") then
                        print "MPD responded {rep}"
@@ -61,7 +60,7 @@ class MPDConnection
 
                socket.write(msg)
                sys.nanosleep(0,5000)
-               var rep = socket.read
+               var rep = socket.read(1024)
                if not rep.has_prefix("OK") then
                        print "Error: MPD responded {rep}"
                        socket.close
@@ -82,12 +81,12 @@ class MPDConnection
 
                # get current status
                socket.write("status\n")
-               var rep = socket.read
+               var rep = socket.read(1024)
                for l in rep.split_with("\n") do
                        var words = l.split_with(" ")
                        if words.length > 1 then
                                var key = words[0].to_lower
-                               var first_space = l.index_of(' ')
+                               var first_space = l.chars.index_of(' ')
                                var rest = l.substring_from(first_space+1)
                                if  key == "volume:" then
                                        volume = rest.to_i
@@ -150,14 +149,15 @@ class MPDConnection
                var album: nullable String = null
                var artist: nullable String = null
                var title: nullable String = null
+               var time: nullable Int = null
 
                socket.write("currentsong\n")
-               var rep = socket.read
+               var rep = socket.read(1024)
                for l in rep.split_with("\n") do
                        var words = l.split_with(" ")
                        if words.length > 1 then
                                var key = words[0].to_lower
-                               var first_space = l.index_of(' ')
+                               var first_space = l.chars.index_of(' ')
                                var rest = l.substring_from(first_space+1)
                                if key == "album:" then
                                        album = rest
@@ -165,16 +165,24 @@ class MPDConnection
                                        artist = rest
                                else if key == "title:" then
                                        title = rest
+                               else if key == "time:" then
+                                       time = rest.to_i
                                end
                        end
                end
 
-               if album != null and artist != null and title != null then
-                       return new SongInfo(album, artist, title)
+               if album != null and artist != null and
+                       title != null and time != null then
+                       return new SongInfo(album, artist, title, time)
                else
                        return null
                end
        end
+
+       fun load_playlist(name: String)
+       do
+               write_and_check "load \"{name}\""
+       end
 end
 
 # MPD song info
@@ -182,6 +190,7 @@ class SongInfo
        var album: String
        var artist: String
        var title: String
+       var time: Int
 end
 
 # MPD server status