-local connection
-local function error_handler(err)
- mpd_widget:set_text("Error: " .. tostring(err))
- timer.start_new(10, function()
- connection:send("ping")
+local function sec_to_min(sec)
+ m, s = math.modf((sec or 0.0) / 60)
+ return string.format('%d:%02d', m, math.floor(s * 60))
+end
+
+local function song_duration(elapsed, duration)
+ return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
+end
+
+function widget:get_info(callback)
+ local status = nil
+ self._connection:send("status", function(_, s) status = s end)
+
+ local song = nil
+
+ self._connection:send("currentsong", function(_, s)
+ song = s
+ self._connection:albumart(song.file, function(_, art)
+ local info = {}
+ if art and art.binary then
+ info.icon = '/tmp/test.jpg'
+ local f = io.open(info.icon, 'w')
+ f:write(art.binary)
+ f:close()
+ end
+
+ info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
+ if not song.artist then
+ info.text = string.format("%s", song.title or song.file)
+ else
+ info.text = string.format("%s - %s", song.artist, song.title)
+ end
+ if song.album then
+ info.text = info.text .. "\n" .. tostring(song.album or "")
+ end
+ callback(info)
+ end)
+ end)
+end
+
+function widget:popup_show()
+ self:get_info(function(table)
+ if self._notification then return end
+ self._notification = naughty.notify(
+ { title = table.title
+ , icon = table.icon
+ , icon_size = 64
+ , text = table.text
+ , timeout = 0
+ , destroy = function() self._timer:stop(); self._notification = nil end
+ })
+ self._timer:start()