Skip to content

Commit

Permalink
Merge branch 'queue_save' of https://github.com/jcorporation/libmympd…
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Dec 22, 2023
2 parents 27d5ade + a8fef3e commit f22f49f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
libmpdclient 2.22 (not yet released)
* drop the unmaintained Vala bindings
* fix "version.h" conflicts when used as a Meson subproject
* support MPD protocol 0.24.0
- command "save [create|replace|append]"

libmpdclient 2.21 (2023/12/20)
* meson.build: allow using as a Meson subproject
Expand Down
72 changes: 72 additions & 0 deletions include/mpd/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ struct mpd_connection;
*/
struct mpd_playlist;

/**
* MPDs queue save modes.
*
* @since libmpdclient 2.21, MPD 0.24.
*/
enum mpd_queue_save_mode {
/** Create new playlist */
MPD_QUEUE_SAVE_MODE_CREATE = 0,

/** Replace existing playlist */
MPD_QUEUE_SAVE_MODE_REPLACE,

/** Append to existing playlist */
MPD_QUEUE_SAVE_MODE_APPEND,

/** Unknown queue save mode */
MPD_QUEUE_SAVE_MODE_UNKNOWN
};

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -374,6 +393,59 @@ mpd_send_save(struct mpd_connection *connection, const char *name);
bool
mpd_run_save(struct mpd_connection *connection, const char *name);

/**
* Saves the current queue as a m3u file in the playlist directory
* (i.e. name.m3u).
* This function supports the mode argument that can be
* MPD_QUEUE_SAVE_MODE_CREATE: creates a new playlist, same as mpd_send_save
* MPD_QUEUE_SAVE_MODE_REPLACE: replaces an existing playlist
* MPD_QUEUE_SAVE_MODE_APPEND: appends to an existing playlist
*
* @since libmpdclient 2.21, MPD 0.24.
*
* @param connection the connection to MPD
* @param name the name of the playlist file
* @param mode the desired save mode
* @return true on success, false on error
*/
bool
mpd_send_save_queue(struct mpd_connection *connection, const char *name,
enum mpd_queue_save_mode mode);

/**
* Shortcut for mpd_send_save_queue() and mpd_response_finish().
*
* @since libmpdclient 2.21, MPD 0.24.
*
* @param connection the connection to MPD
* @param name the name of the playlist file
* @param mode the desired save mode
* @return true on success, false on error
*/
bool
mpd_run_save_queue(struct mpd_connection *connection, const char *name,
enum mpd_queue_save_mode mode);

/**
* Looks up the name of the specified queue save mode.
*
* @return the name, or NULL if the queue save mode is not valid
*
* @since libmpdclient 2.21.
*/
const char *
mpd_lookup_queue_save_mode(enum mpd_queue_save_mode mode);

/**
* Parse the string to check which queue save mode it contains.
*
* @return the queue save mode enum
*
* @since libmpdclient 2.21.
*/
enum mpd_queue_save_mode
mpd_parse_queue_save_mode(const char *mode);

/**
* Load a stored playlist into the queue.
*
Expand Down
4 changes: 4 additions & 0 deletions libmpdclient.ld
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ global:
mpd_run_playlist_delete_range;
mpd_send_playlist_add_to;
mpd_run_playlist_add_to;
mpd_send_save_queue;
mpd_run_save_queue;
mpd_parse_queue_save_mode;
mpd_lookup_queue_save_mode;

/* mpd/queue.h */
mpd_send_list_queue_meta;
Expand Down
48 changes: 48 additions & 0 deletions src/cplaylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include <limits.h>
#include <stdio.h>
#include <string.h>

/* (bits+1)/3 (plus the sign character) */
enum {
Expand Down Expand Up @@ -195,6 +196,53 @@ mpd_run_save(struct mpd_connection *connection, const char *name)
mpd_response_finish(connection);
}

enum mpd_queue_save_mode mpd_parse_queue_save_mode(const char *p)
{
if (strcmp(p, "create") == 0)
return MPD_QUEUE_SAVE_MODE_CREATE;
else if (strcmp(p, "replace") == 0)
return MPD_QUEUE_SAVE_MODE_REPLACE;
else if (strcmp(p, "append") == 0)
return MPD_QUEUE_SAVE_MODE_APPEND;
else
return MPD_QUEUE_SAVE_MODE_UNKNOWN;
}

const char *
mpd_lookup_queue_save_mode(enum mpd_queue_save_mode mode)
{
switch (mode) {
case MPD_QUEUE_SAVE_MODE_CREATE:
return "create";
case MPD_QUEUE_SAVE_MODE_REPLACE:
return "replace";
case MPD_QUEUE_SAVE_MODE_APPEND:
return "append";
case MPD_QUEUE_SAVE_MODE_UNKNOWN:
return NULL;
}
return NULL;
}

bool
mpd_send_save_queue(struct mpd_connection *connection, const char *name,
enum mpd_queue_save_mode mode)
{
const char *mode_str = mpd_lookup_queue_save_mode(mode);
if (mode_str == NULL)
return false;
return mpd_send_command(connection, "save", name, mode_str, NULL);
}

bool
mpd_run_save_queue(struct mpd_connection *connection, const char *name,
enum mpd_queue_save_mode mode)
{
return mpd_run_check(connection) &&
mpd_send_save_queue(connection, name, mode) &&
mpd_response_finish(connection);
}

bool
mpd_send_load(struct mpd_connection *connection, const char *name)
{
Expand Down

0 comments on commit f22f49f

Please sign in to comment.