File: mpdclient.cxx

package info (click to toggle)
ncmpc 0.33-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,976 kB
  • sloc: cpp: 10,895; python: 133; makefile: 39; ruby: 28; sh: 11
file content (861 lines) | stat: -rw-r--r-- 19,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2018 The Music Player Daemon Project
 * Project homepage: http://musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "mpdclient.hxx"
#include "callbacks.hxx"
#include "filelist.hxx"
#include "config.h"
#include "gidle.hxx"
#include "charset.hxx"

#include <mpd/client.h>

#include <assert.h>

void
mpdclient::OnEnterIdleTimer(const boost::system::error_code &error) noexcept
{
	if (error)
		return;

	assert(source != nullptr);
	assert(!idle);

	idle = source->Enter();
}

void
mpdclient::ScheduleEnterIdle() noexcept
{
	assert(source != nullptr);

	/* automatically re-enter MPD "idle" mode */
	boost::system::error_code error;
	enter_idle_timer.expires_from_now(std::chrono::seconds(0), error);
	enter_idle_timer.async_wait(std::bind(&mpdclient::OnEnterIdleTimer,
					      this, std::placeholders::_1));
}

static void
mpdclient_invoke_error_callback(enum mpd_error error,
				const char *message)
{
	if (error == MPD_ERROR_SERVER)
		/* server errors are UTF-8, the others are locale */
		mpdclient_error_callback(Utf8ToLocale(message).c_str());
	else
		mpdclient_error_callback(message);
}

void
mpdclient::InvokeErrorCallback() noexcept
{
	assert(connection != nullptr);

	enum mpd_error error = mpd_connection_get_error(connection);
	assert(error != MPD_ERROR_SUCCESS);

	mpdclient_invoke_error_callback(error,
					mpd_connection_get_error_message(connection));
}

void
mpdclient::OnIdle(unsigned _events) noexcept
{
	assert(IsConnected());

	idle = false;

	events |= _events;
	Update();

	mpdclient_idle_callback(events);
	events = 0;

	if (source != nullptr)
		ScheduleEnterIdle();
}

void
mpdclient::OnIdleError(enum mpd_error error,
		       gcc_unused enum mpd_server_error server_error,
		       const char *message) noexcept
{
	assert(IsConnected());

	idle = false;

	mpdclient_invoke_error_callback(error, message);
	Disconnect();
	mpdclient_lost_callback();
}

/****************************************************************************/
/*** mpdclient functions ****************************************************/
/****************************************************************************/

bool
mpdclient::HandleError()
{
	enum mpd_error error = mpd_connection_get_error(connection);

	assert(error != MPD_ERROR_SUCCESS);

	if (error == MPD_ERROR_SERVER &&
	    mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_PERMISSION &&
	    mpdclient_auth_callback(this))
		return true;

	mpdclient_invoke_error_callback(error,
					mpd_connection_get_error_message(connection));

	if (!mpd_connection_clear_error(connection)) {
		Disconnect();
		mpdclient_lost_callback();
	}

	return false;
}

#ifdef ENABLE_ASYNC_CONNECT
#ifndef _WIN32

static bool
is_local_socket(const char *host)
{
	return *host == '/' || *host == '@';
}

static bool
settings_is_local_socket(const struct mpd_settings *settings)
{
	const char *host = mpd_settings_get_host(settings);
	return host != nullptr && is_local_socket(host);
}

#endif
#endif

mpdclient::mpdclient(boost::asio::io_service &io_service,
		     const char *_host, unsigned _port,
		     unsigned _timeout_ms, const char *_password)
	:timeout_ms(_timeout_ms), password(_password),
	 enter_idle_timer(io_service)
{
#ifdef ENABLE_ASYNC_CONNECT
	settings = mpd_settings_new(_host, _port, _timeout_ms,
				    nullptr, nullptr);
	if (settings == nullptr)
		fprintf(stderr, "Out of memory\n");

#ifndef _WIN32
	settings2 = _host == nullptr && _port == 0 &&
		settings_is_local_socket(settings)
		? mpd_settings_new(_host, 6600, _timeout_ms, nullptr, nullptr)
		: nullptr;
#endif

#else
	host = _host;
	port = _port;
#endif
}

static std::string
settings_name(const struct mpd_settings *settings)
{
	assert(settings != nullptr);

	const char *host = mpd_settings_get_host(settings);
	if (host == nullptr)
		host = "unknown";

	if (host[0] == '/')
		return host;

	unsigned port = mpd_settings_get_port(settings);
	if (port == 0 || port == 6600)
		return host;

	char buffer[256];
	snprintf(buffer, sizeof(buffer), "%s:%u", host, port);
	return buffer;
}

std::string
mpdclient::GetSettingsName() const
{
#ifdef ENABLE_ASYNC_CONNECT
	return settings_name(settings);
#else
	struct mpd_settings *settings =
		mpd_settings_new(host, port, 0, nullptr, nullptr);
	if (settings == nullptr)
		return "unknown";

	auto name = settings_name(settings);
	mpd_settings_free(settings);
	return std::move(name);
#endif
}

void
mpdclient::ClearStatus() noexcept
{
	if (status == nullptr)
		return;

	mpd_status_free(status);
	status = nullptr;

	volume = -1;
	playing = false;
	playing_or_paused = false;
	state = MPD_STATE_UNKNOWN;
}

void
mpdclient::Disconnect()
{
#ifdef ENABLE_ASYNC_CONNECT
	if (async_connect != nullptr) {
		aconnect_cancel(async_connect);
		async_connect = nullptr;
	}
#endif

	CancelEnterIdle();

	delete source;
	source = nullptr;
	idle = false;

	if (connection) {
		mpd_connection_free(connection);
		++connection_id;
	}
	connection = nullptr;

	ClearStatus();

	playlist.clear();

	current_song = nullptr;

	/* everything has changed after a disconnect */
	events |= MPD_IDLE_ALL;
}

bool
mpdclient::OnConnected(struct mpd_connection *_connection) noexcept
{
	connection = _connection;

	if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
		InvokeErrorCallback();
		Disconnect();
		mpdclient_failed_callback();
		return false;
	}

#ifdef ENABLE_ASYNC_CONNECT
	if (timeout_ms > 0)
		mpd_connection_set_timeout(connection, timeout_ms);
#endif

	/* send password */
	if (password != nullptr &&
	    !mpd_run_password(connection, password)) {
		InvokeErrorCallback();
		Disconnect();
		mpdclient_failed_callback();
		return false;
	}

	source = new MpdIdleSource(get_io_service(), *connection, *this);
	ScheduleEnterIdle();

	++connection_id;

	/* everything has changed after a connection has been
	   established */
	events = (enum mpd_idle)MPD_IDLE_ALL;

	mpdclient_connected_callback();
	return true;
}

#ifdef ENABLE_ASYNC_CONNECT

void
mpdclient::OnAsyncMpdConnect(struct mpd_connection *_connection) noexcept
{
	assert(async_connect != nullptr);
	async_connect = nullptr;

	const char *password2 =
		mpd_settings_get_password(&GetSettings());
	if (password2 != nullptr && !mpd_run_password(_connection, password2)) {
		mpdclient_error_callback(mpd_connection_get_error_message(_connection));
		mpd_connection_free(_connection);
		mpdclient_failed_callback();
		return;
	}

	OnConnected(_connection);
}

void
mpdclient::OnAsyncMpdConnectError(const char *message) noexcept
{
	assert(async_connect != nullptr);
	async_connect = nullptr;

#ifndef _WIN32
	if (!connecting2 && settings2 != nullptr) {
		connecting2 = true;
		StartConnect(*settings2);
		return;
	}
#endif

	mpdclient_error_callback(message);
	mpdclient_failed_callback();
}

void
mpdclient::StartConnect(const struct mpd_settings &s) noexcept
{
	aconnect_start(get_io_service(), &async_connect,
		       mpd_settings_get_host(&s),
		       mpd_settings_get_port(&s),
		       *this);
}

#endif

void
mpdclient::Connect()
{
	/* close any open connection */
	Disconnect();

#ifdef ENABLE_ASYNC_CONNECT
#ifndef _WIN32
	connecting2 = false;
#endif
	StartConnect(*settings);
#else
	/* connect to MPD */
	struct mpd_connection *new_connection =
		mpd_connection_new(host, port, timeout_ms);
	if (new_connection == nullptr)
		fprintf(stderr, "Out of memory\n");

	OnConnected(new_connection);
#endif
}

bool
mpdclient::Update()
{
	auto *c = GetConnection();

	if (c == nullptr)
		return false;

	/* free the old status */
	ClearStatus();

	/* retrieve new status */
	status = mpd_run_status(c);
	if (status == nullptr)
		return HandleError();

	volume = mpd_status_get_volume(status);
	state = mpd_status_get_state(status);
	playing = state == MPD_STATE_PLAY;
	playing_or_paused = state == MPD_STATE_PLAY
		|| state == MPD_STATE_PAUSE;

	/* check if the playlist needs an update */
	if (playlist.version != mpd_status_get_queue_version(status)) {
		bool retval;

		if (!playlist.empty())
			retval = UpdateQueueChanges();
		else
			retval = UpdateQueue();
		if (!retval)
			return false;
	}

	/* update the current song */
	if (current_song == nullptr || mpd_status_get_song_id(status) >= 0)
		current_song = playlist.GetChecked(mpd_status_get_song_pos(status));

	return true;
}

struct mpd_connection *
mpdclient::GetConnection()
{
	if (source != nullptr && idle) {
		idle = false;
		source->Leave();

		if (source != nullptr)
			ScheduleEnterIdle();
	}

	return connection;
}

const struct mpd_status *
mpdclient::ReceiveStatus() noexcept
{
	assert(connection != nullptr);

	struct mpd_status *new_status = mpd_recv_status(connection);
	if (new_status == nullptr) {
		HandleError();
		return nullptr;
	}

	if (status != nullptr)
		mpd_status_free(status);
	return status = new_status;
}

/****************************************************************************/
/*** MPD Commands  **********************************************************/
/****************************************************************************/

bool
mpdclient_cmd_crop(struct mpdclient *c)
{
	if (!c->playing_or_paused)
		return false;

	int length = mpd_status_get_queue_length(c->status);
	int current = mpd_status_get_song_pos(c->status);
	if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
		return true;

	struct mpd_connection *connection = c->GetConnection();
	if (connection == nullptr)
		return false;

	mpd_command_list_begin(connection, false);

	if (current < length - 1)
		mpd_send_delete_range(connection, current + 1, length);
	if (current > 0)
		mpd_send_delete_range(connection, 0, current);

	mpd_command_list_end(connection);

	return c->FinishCommand();
}

bool
mpdclient::RunClearQueue() noexcept
{
	auto *c = GetConnection();
	if (c == nullptr)
		return false;

	/* send "clear" and "status" */
	if (!mpd_command_list_begin(c, false) ||
	    !mpd_send_clear(c) ||
	    !mpd_send_status(c) ||
	    !mpd_command_list_end(c))
		return HandleError();

	/* receive the new status, store it in the mpdclient struct */

	const struct mpd_status *new_status = ReceiveStatus();
	if (new_status == nullptr)
		return false;

	if (!mpd_response_finish(c))
		return HandleError();

	/* update mpdclient.playlist */

	if (mpd_status_get_queue_length(new_status) == 0) {
		/* after the "clear" command, the queue is really
		   empty - this means we can clear it locally,
		   reducing the UI latency */
		playlist.clear();
		playlist.version = mpd_status_get_queue_version(new_status);
		current_song = nullptr;
	}

	events |= MPD_IDLE_QUEUE;
	return true;
}

bool
mpdclient::RunVolume(unsigned value) noexcept
{
	struct mpd_connection *c = GetConnection();
	if (c == nullptr)
		return false;

	mpd_send_set_volume(c, value);
	return FinishCommand();
}

bool
mpdclient::RunVolumeUp() noexcept
{
	if (volume < 0 || volume >= 100)
		return true;

	return RunVolume(++volume);
}

bool
mpdclient::RunVolumeDown() noexcept
{
	if (volume <= 0)
		return true;

	return RunVolume(--volume);
}

bool
mpdclient_cmd_add_path(struct mpdclient *c, const char *path_utf8)
{
	struct mpd_connection *connection = c->GetConnection();
	if (connection == nullptr)
		return false;

	return mpd_send_add(connection, path_utf8)?
		c->FinishCommand() : false;
}

bool
mpdclient::RunAdd(const struct mpd_song &song) noexcept
{
	auto *c = GetConnection();
	if (c == nullptr || status == nullptr)
		return false;

	/* send the add command to mpd; at the same time, get the new
	   status (to verify the new playlist id) and the last song
	   (we hope that's the song we just added) */

	if (!mpd_command_list_begin(c, true) ||
	    !mpd_send_add(c, mpd_song_get_uri(&song)) ||
	    !mpd_send_status(c) ||
	    !mpd_send_get_queue_song_pos(c, playlist.size()) ||
	    !mpd_command_list_end(c) ||
	    !mpd_response_next(c))
		return HandleError();

	events |= MPD_IDLE_QUEUE;

	const struct mpd_status *new_status = ReceiveStatus();
	if (new_status == nullptr)
		return false;

	if (!mpd_response_next(c))
		return HandleError();

	struct mpd_song *new_song = mpd_recv_song(c);
	if (!mpd_response_finish(c) || new_song == nullptr) {
		if (new_song != nullptr)
			mpd_song_free(new_song);

		return mpd_connection_clear_error(c) ||
			HandleError();
	}

	if (mpd_status_get_queue_length(new_status) == playlist.size() + 1 &&
	    mpd_status_get_queue_version(new_status) == playlist.version + 1) {
		/* the cheap route: match on the new playlist length
		   and its version, we can keep our local playlist
		   copy in sync */
		playlist.version = mpd_status_get_queue_version(new_status);

		/* the song we just received has the correct id;
		   append it to the local playlist */
		playlist.push_back(*new_song);
	}

	mpd_song_free(new_song);

	return true;
}

bool
mpdclient::RunDelete(unsigned pos) noexcept
{
	auto *c = GetConnection();
	if (c == nullptr || status == nullptr)
		return false;

	if (pos >= playlist.size())
		return false;

	const auto &song = playlist[pos];

	/* send the delete command to mpd; at the same time, get the
	   new status (to verify the playlist id) */

	if (!mpd_command_list_begin(c, false) ||
	    !mpd_send_delete_id(c, mpd_song_get_id(&song)) ||
	    !mpd_send_status(c) ||
	    !mpd_command_list_end(c))
		return HandleError();

	events |= MPD_IDLE_QUEUE;

	const struct mpd_status *new_status = ReceiveStatus();
	if (new_status == nullptr)
		return false;

	if (!mpd_response_finish(c))
		return HandleError();

	if (mpd_status_get_queue_length(new_status) == playlist.size() - 1 &&
	    mpd_status_get_queue_version(new_status) == playlist.version + 1) {
		/* the cheap route: match on the new playlist length
		   and its version, we can keep our local playlist
		   copy in sync */
		playlist.version = mpd_status_get_queue_version(new_status);

		/* remove the song from the local playlist */
		playlist.RemoveIndex(pos);

		/* remove references to the song */
		if (current_song == &song)
			current_song = nullptr;
	}

	return true;
}

bool
mpdclient::RunDeleteRange(unsigned start, unsigned end) noexcept
{
	if (end == start + 1)
		/* if that's not really a range, we choose to use the
		   safer "deleteid" version */
		return RunDelete(start);

	auto *c = GetConnection();
	if (c == nullptr)
		return false;

	/* send the delete command to mpd; at the same time, get the
	   new status (to verify the playlist id) */

	if (!mpd_command_list_begin(c, false) ||
	    !mpd_send_delete_range(c, start, end) ||
	    !mpd_send_status(c) ||
	    !mpd_command_list_end(c))
		return HandleError();

	events |= MPD_IDLE_QUEUE;

	const struct mpd_status *new_status = ReceiveStatus();
	if (new_status == nullptr)
		return false;

	if (!mpd_response_finish(c))
		return HandleError();

	if (mpd_status_get_queue_length(new_status) == playlist.size() - (end - start) &&
	    mpd_status_get_queue_version(new_status) == playlist.version + 1) {
		/* the cheap route: match on the new playlist length
		   and its version, we can keep our local playlist
		   copy in sync */
		playlist.version = mpd_status_get_queue_version(new_status);

		/* remove the song from the local playlist */
		while (end > start) {
			--end;

			/* remove references to the song */
			if (current_song == &playlist[end])
				current_song = nullptr;

			playlist.RemoveIndex(end);
		}
	}

	return true;
}

bool
mpdclient::RunMove(unsigned dest_pos, unsigned src_pos) noexcept
{
	if (dest_pos == src_pos)
		return true;

	auto *c = GetConnection();
	if (c == nullptr)
		return false;

	/* send the "move" command to MPD; at the same time, get the
	   new status (to verify the playlist id) */

	if (!mpd_command_list_begin(c, false) ||
	    !mpd_send_move(c, src_pos, dest_pos) ||
	    !mpd_send_status(c) ||
	    !mpd_command_list_end(c))
		return HandleError();

	events |= MPD_IDLE_QUEUE;

	const struct mpd_status *new_status = ReceiveStatus();
	if (status == nullptr)
		return false;

	if (!mpd_response_finish(c))
		return HandleError();

	if (mpd_status_get_queue_length(new_status) == playlist.size() &&
	    mpd_status_get_queue_version(new_status) == playlist.version + 1) {
		/* the cheap route: match on the new playlist length
		   and its version, we can keep our local playlist
		   copy in sync */
		playlist.version = mpd_status_get_queue_version(new_status);

		/* swap songs in the local playlist */
		playlist.Move(dest_pos, src_pos);
	}

	return true;
}

/* The client-to-client protocol (MPD 0.17.0) */

bool
mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
{
	struct mpd_connection *connection = c->GetConnection();

	if (connection == nullptr)
		return false;

	if (!mpd_send_subscribe(connection, channel))
		return c->HandleError();

	return c->FinishCommand();
}

bool
mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
{
	struct mpd_connection *connection = c->GetConnection();
	if (connection == nullptr)
		return false;

	if (!mpd_send_unsubscribe(connection, channel))
		return c->HandleError();

	return c->FinishCommand();
}

bool
mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
			   const char *text)
{
	struct mpd_connection *connection = c->GetConnection();
	if (connection == nullptr)
		return false;

	if (!mpd_send_send_message(connection, channel, text))
		return c->HandleError();

	return c->FinishCommand();
}

/****************************************************************************/
/*** Playlist management functions ******************************************/
/****************************************************************************/

/* update playlist */
bool
mpdclient::UpdateQueue()
{
	auto *c = GetConnection();
	if (c == nullptr)
		return false;

	playlist.clear();

	mpd_send_list_queue_meta(c);

	struct mpd_entity *entity;
	while ((entity = mpd_recv_entity(c))) {
		if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
			playlist.push_back(*mpd_entity_get_song(entity));

		mpd_entity_free(entity);
	}

	playlist.version = mpd_status_get_queue_version(status);
	current_song = nullptr;

	return FinishCommand();
}

/* update playlist (plchanges) */
bool
mpdclient::UpdateQueueChanges()
{
	auto *c = GetConnection();
	if (c == nullptr)
		return false;

	mpd_send_queue_changes_meta(c, playlist.version);

	struct mpd_song *s;
	while ((s = mpd_recv_song(c)) != nullptr) {
		int pos = mpd_song_get_pos(s);

		if (pos >= 0 && (unsigned)pos < playlist.size()) {
			/* update song */
			playlist.Replace(pos, *s);
		} else {
			/* add a new song */
			playlist.push_back(*s);
		}

		mpd_song_free(s);
	}

	/* remove trailing songs */

	unsigned length = mpd_status_get_queue_length(status);
	while (length < playlist.size()) {
		/* Remove the last playlist entry */
		playlist.RemoveIndex(playlist.size() - 1);
	}

	current_song = nullptr;
	playlist.version = mpd_status_get_queue_version(status);

	return FinishCommand();
}