]> git.wh0rd.org - chrome-ext/music-player-client.git/blobdiff - main.js
handle connection errors
[chrome-ext/music-player-client.git] / main.js
diff --git a/main.js b/main.js
index 26d0bc7ae4bd7eb1ceb5ff5375500b3e4a051a18..3ff3ca46906251888f02af57737083d149c446e0 100644 (file)
--- a/main.js
+++ b/main.js
@@ -14,6 +14,10 @@ TcpClientSender.prototype.send = function(data, cb) {
 TcpClientSender.prototype.poll = function() {
        this.tcpclient.poll();
 }
+TcpClientSender.prototype.reconnect = function() {
+       this.tcpclient.disconnect();
+       this.tcpclient.connect();
+}
 
 function tramp_mpc_recv(data) {
        mpc.recv(data);
@@ -57,17 +61,23 @@ window.onload = function() {
 };
 
 window.onkeypress = function(e) {
+       if (e.target != document.body) {
+               /* Only allow the shortcuts when the focus is on the body.
+                  Otherwise you can't type these numbers into text fields. */
+               return;
+       }
+
        switch (e.keyCode) {
        case 49: // 1
                show_page('controls');
                break;
-       case 50: // 1
+       case 50: // 2
                show_page('metadata');
                break;
-       case 51: // 1
+       case 51: // 3
                show_page('playlist');
                break;
-       case 52: // 1
+       case 52: // 4
                show_page('options');
                break;
        }
@@ -93,7 +103,12 @@ function mpc_connect(host, port) {
 
        update_ui('init');
        tcpclient = new TcpClient(host, port);
-       tcpclient.connect(function() {
+       tcpclient.connect(function(resultCode) {
+               if (resultCode < 0) {
+                       update_ui('error', resultCode);
+                       return;
+               }
+
                var mpc_sender = new TcpClientSender(tcpclient);
                tcpclient.addResponseListener(tramp_mpc_recv);
                mpc = new Mpc(mpc_sender, update_ui);
@@ -153,12 +168,15 @@ function show_page(page) {
        if (typeof(page) != 'string')
                page = this.id.split('.')[1];
 
+       // We might not be connected in which case 'mpc' will be undefined.
        switch (page) {
        case 'playlist':
-               mpc.playlistinfo();
+               if (mpc)
+                       mpc.playlistinfo();
                // Fallthrough.
        case 'metadata':
-               mpc.currentsong();
+               if (mpc)
+                       mpc.currentsong();
                break;
        }
 
@@ -288,6 +306,7 @@ function update_ui(state, cmd) {
                ui_mpc_status.innerText = ({
                        'disconnect': 'Disconnecting...',
                        'init': 'Connecting...',
+                       'error': 'Connection error ' + cmd,
                })[state];
                return;
        }
@@ -305,67 +324,78 @@ function update_ui(state, cmd) {
                return;
        }
 
-       /* Update the metadata tab. */
-       var currentsong = {};
-       if ('Currentsong' in state)
+       /* Update the metadata tab only when things have changed. */
+       var currentsong;
+       if ('Currentsong' in state) {
                currentsong = state.Currentsong;
-       ui_mpc_metadata_album.innerText = currentsong.Album;
-       ui_mpc_metadata_artist.innerText = currentsong.Artist;
-       ui_mpc_metadata_title.innerText = currentsong.Title;
-       ui_mpc_metadata_date.innerText = currentsong.Date;
-       ui_mpc_metadata_file.innerText = currentsong.file;
-
-       /* Update the playlist tab. */
-       var playlist = [];
-       if ('Playlist' in state)
-               playlist = state.Playlist;
-       ui_mpc_playlist.innerHTML = '';
-       playlist.forEach(function(song) {
-               var cell, row = ui_mpc_playlist.insertRow(-1);
-               if (song.Pos == currentsong.Pos)
-                       row.style.fontWeight = 'bold';
-
-               cell = row.insertCell(-1);
-               cell.id = 'playlist_del';
-               cell.innerHTML = '&#164;';
-               cell.song_id = song.Id;
-               cell.title = 'delete';
-               cell.onclick = playlist_del;
-
-               cell = row.insertCell(-1);
-               cell.innerText = song.Pos;
-               cell.style.textAlign = 'right';
-               cell.song_id = song.Id;
-               cell.title = 'play';
-               cell.onclick = playlist_play;
-
-               if ('Artist' in song) {
-                       row.insertCell(-1).innerText = song.Artist;
-                       row.insertCell(-1).innerText = song.Album;
-                       row.insertCell(-1).innerText = song.Title;
-               } else {
-                       cell = row.insertCell(-1);
-                       cell.innerText = song.file;
-                       cell.colSpan = 3;
+               if (ui_mpc_metadata_file.lastUpdate != state.Currentsong.lastUpdate) {
+                       ui_mpc_metadata_album.innerText = currentsong.Album;
+                       ui_mpc_metadata_artist.innerText = currentsong.Artist;
+                       ui_mpc_metadata_title.innerText = currentsong.Title;
+                       ui_mpc_metadata_date.innerText = currentsong.Date;
+                       ui_mpc_metadata_file.innerText = currentsong.file;
                }
-               row.insertCell(-1).innerText = pretty_time(song.Time);
-       });
+       }
+
+       /* Update the playlist tab only when things have changed. */
+       if ('Playlist' in state && ui_mpc_playlist.lastUpdate != state.Playlist.lastUpdate) {
+               var playlist = state.Playlist;
+
+               ui_mpc_playlist.innerHTML = '';
+               playlist.forEach(function(song) {
+                       var cell, row = ui_mpc_playlist.insertRow(-1);
+                       if (currentsong && song.Pos == currentsong.Pos)
+                               row.style.fontWeight = 'bold';
+
+                       cell = row.insertCell(-1);
+                       cell.id = 'playlist_del';
+                       cell.innerHTML = '&#164;';
+                       cell.song_id = song.Id;
+                       cell.title = 'delete';
+                       cell.onclick = playlist_del;
+
+                       cell = row.insertCell(-1);
+                       cell.innerText = song.Pos;
+                       cell.style.textAlign = 'right';
+                       cell.song_id = song.Id;
+                       cell.title = 'play';
+                       cell.onclick = playlist_play;
+
+                       if ('Artist' in song) {
+                               row.insertCell(-1).innerText = song.Artist;
+                               row.insertCell(-1).innerText = song.Album;
+                               row.insertCell(-1).innerText = song.Title;
+                       } else {
+                               cell = row.insertCell(-1);
+                               cell.innerText = song.file;
+                               cell.colSpan = 3;
+                       }
+                       row.insertCell(-1).innerText = pretty_time(song.Time);
+               });
+
+               ui_mpc_playlist.lastUpdate = playlist.lastUpdate;
+       }
 
        /* Update the status tab. */
-       var time;
-       if ('time' in state)
+       var time, percent;
+       if ('time' in state) {
                // When stopped, there is no time field at all.
                time = state.time.split(':');
-       else
+               percent = Math.floor((0.0 + time[0]) * 100 / (0.0 + time[1]));
+       } else {
                time = [0, 0];
+               percent = 0;
+       }
        ui_mpc_seekcur.max = time[1];
        ui_mpc_seekcur.value = time[0];
-       percent = Math.floor((0.0 + time[0]) * 100 / (0.0 + time[1]));
        ui_mpc_seekcur.title = 'seekcur (' + percent + '%)';
        ui_mpc_currtime.innerText = [pretty_time(time[0]), pretty_time(time[1]), percent + '%'].join(' / ');
 
-       ui_mpc_setvol.value = state.volume;
-       ui_mpc_setvol.title = 'setvol (' + state.volume + '%)';
+       ui_mpc_setvol.title = 'setvol';
+       if ('volume' in state) {
+               ui_mpc_setvol.value = state.volume;
+               ui_mpc_setvol.title += ' (' + state.volume + '%)';
+       }
 
        [
                'consume', 'random', 'repeat', 'single',