From 4a4e8e24805b53c37d0b0d609c16d5c8bb6ea890 Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Fri, 17 Jan 2014 18:34:22 +0100 Subject: [PATCH] Merged nicos notifications, add cookie support --- htdocs/index.html | 7 ++++++ htdocs/js/mpd.js | 56 ++++++++++++++++++++++++++++++++++++++++++----- src/http_server.c | 1 + 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/htdocs/index.html b/htdocs/index.html index bd4778b..57e5835 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -138,6 +138,12 @@ +
+ +
+ @@ -185,6 +191,7 @@ ================================================== --> + diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index a742e46..4a701eb 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -1,7 +1,9 @@ var socket; var last_state; var current_app; +var lastSongTitle = ""; var current_song = new Object(); +var desktopNotification; var app = $.sammy(function() { this.before('/', function(e, data) { @@ -60,6 +62,12 @@ $(document).ready(function(){ socket.send("MPD_API_SET_SEEK,"+current_song.currentSongId+","+seekVal); } }); + + if(!notificationsSupported()) + $('#btnnotify').addClass("disabled"); + else + if ($.cookie("notification") === "true") + $('#btnnotify').addClass("active") }); @@ -125,7 +133,6 @@ function webSocketConnect() { $(this).children().last().find("a").stop().remove(); } }); - break; case "browse": if(current_app !== 'browse') @@ -269,10 +276,14 @@ function webSocketConnect() { notification += obj.data.artist + "
"; } - $('.top-right').notify({ - message:{html: notification}, - type: "info", - }).show(); + if ($.cookie("notification") === "true") + songNotify(obj.data.title, obj.data.artist + " " + obj.data.album ); + else + $('.top-right').notify({ + message:{html: notification}, + type: "info", + }).show(); + break; case "error": $('.top-right').notify({ @@ -394,10 +405,43 @@ $('#btnrepeat').on('click', function (e) { socket.send("MPD_API_TOGGLE_REPEAT," + ($(this).hasClass('active') ? 0 : 1)); }); +$('#btnnotify').on('click', function (e) { + console.log("setting this"); + if($.cookie("notification") === "true") + $.cookie("notification", false); + else { + window.webkitNotifications.requestPermission(); + if (window.webkitNotifications.checkPermission() == 0) + { + $.cookie("notification", true); + $('btnnotify').addClass("active"); + } + } +}); + function getVersion() { $.get( "/api/get_version", function(response) { $('#ympd_version').text(response.data.ympd_version); $('#mpd_version').text(response.data.mpd_version); }); -} \ No newline at end of file +} + +function notificationsSupported() { + return "webkitNotifications" in window; +} + +function songNotify(artist, title) { + if (!notificationsSupported()) + return; + + if (window.webkitNotifications.checkPermission() == 0) { + var notification = window.webkitNotifications.createNotification("assets/favicon.ico", artist, title); + notification.show(); + setTimeout(function(notification) { + notification.cancel(); + }, 2000, notification); + } else { + window.webkitNotifications.requestPermission(); + } +} diff --git a/src/http_server.c b/src/http_server.c index 15e8bbf..1756745 100644 --- a/src/http_server.c +++ b/src/http_server.c @@ -25,6 +25,7 @@ static const struct serveable whitelist[] = { { "/js/bootstrap.min.js", "text/javascript" }, { "/js/mpd.js", "text/javascript" }, { "/js/jquery-1.10.2.min.js", "text/javascript" }, + { "/js/jquery.cookie.js", "text/javascript" }, { "/js/bootstrap-slider.js", "text/javascript" }, { "/js/bootstrap-notify.js", "text/javascript" }, { "/js/sammy.js", "text/javascript" },