Esempio n. 1
0
File: mpd.cpp Progetto: kpence/kpmpc
std::string Mpd::getTag(const char *_album, mpd_tag_type type) {
    std::string tagRet = "";
    mpd_song *song;
    if (!mpd_search_db_songs(conn, false)) { err(); return NULL; }

    if (!mpd_search_add_tag_constraint(conn, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, _album)) { err(); return NULL; }

    if (!mpd_search_commit(conn)) { err(); return NULL; }

    if (type == MPD_TAG_UNKNOWN) {
        if ((song = mpd_recv_song(conn)) != NULL) {
            if (mpd_song_get_uri(song) != NULL)
                tagRet = mpd_song_get_uri(song);
            else
                tagRet = "";
        }
    } else if ((song = mpd_recv_song(conn)) != NULL) {
        if (mpd_song_get_tag(song, type, 0) != NULL)
            tagRet = mpd_song_get_tag(song, type, 0);
        else
            tagRet = "";
    }

    mpd_song_free(song);

    if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { err(); return NULL; }

    if (!mpd_response_finish(conn)) { err(); return NULL; }

    return tagRet;
}
Esempio n. 2
0
int cmd_tab ( int argc, char ** argv, struct mpd_connection *conn )
{
	struct mpd_song *song;
	char empty[] = "";
	char *dir = empty;
	char *tmp = NULL;

	if (argc == 1) {
		if (strrchr(argv[0], '/')) {
			dir = strdup(argv[0]);
			if (!dir) return 0;
			tmp = strrchr(dir, '/');
			if (tmp) *tmp = '\0'; // XXX: It's unpossible for tmp to be NULL.
		}
	}

	if (!mpd_send_list_all(conn, dir))
		printErrorAndExit(conn);

	if (*dir) free(dir);

	while ((song = mpd_recv_song(conn)) != NULL) {
		if (argc != 1 ||
		    strncmp(mpd_song_get_uri(song), argv[0],
			    strlen(argv[0])) == 0)
			printf("%s\n",
			       charset_from_utf8(mpd_song_get_uri(song)));

		mpd_song_free(song);
	}

	my_finishCommand(conn);
	return 0;
}
Esempio n. 3
0
/**
 * Checks if a mpd_song might be an FM4 stream
 *
 * @return true if it looks like an FM4 stream, false otherwise
 */
bool
fm4_is_fm4_stream(struct mpd_song *song)
{
	int rank = 0;
	const char *title = NULL;
	const char *uri = NULL;
	const char *title_prefix_fm4 = "FM4";

	if (song == NULL)
		return false;

	/* Information is contained in the title tag */
	title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
	uri = mpd_song_get_uri(song);

	if (title == NULL)
		return false;

	if (strcmp("http://mp3stream1.apasf.apa.at:8000/", uri) == 0)
		rank += 10;

	if (strncmp(title_prefix_fm4, title, sizeof(title_prefix_fm4)))
		rank += 5;

	if (rank >= FM4_THRESHOLD)
		return true;

	return false;
}
Esempio n. 4
0
static void
song_changed(const struct mpd_song *song)
{
	g_assert(song != NULL);

	if (mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) == NULL ||
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0) == NULL) {
		g_message("New song detected with tags missing (%s)",
				mpd_song_get_uri(song));
		g_timer_start(timer);
		return;
	}
	g_timer_start(timer);

	g_debug("New song detected (%s - %s), id: %u, pos: %u",
			mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
			mpd_song_get_id(song), mpd_song_get_pos(song));

	as_now_playing(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
			mpd_song_get_tag(song, MPD_TAG_ALBUM, 0),
			mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0),
			mpd_song_get_duration(song));
}
Esempio n. 5
0
static int
rmtag_song(struct mpdcron_connection *conn, const char *tag, const char *expr)
{
	int changes;

	if (expr != NULL) {
		if (!mpdcron_rmtag_expr(conn, expr, tag, &changes)) {
			eulog(LOG_ERR, "Failed to remove tag from song: %s",
					conn->error->message);
			return 1;
		}
	}
	else {
		char *esc_uri, *myexpr;
		struct mpd_song *song;

		if ((song = load_current_song()) == NULL)
			return 1;

		esc_uri = quote(mpd_song_get_uri(song));
		myexpr = g_strdup_printf("uri=%s", esc_uri);
		g_free(esc_uri);
		mpd_song_free(song);

		if (!mpdcron_rmtag_expr(conn, myexpr, tag, &changes)) {
			eulog(LOG_ERR, "Failed to remove tag from current playing song: %s",
					conn->error->message);
			g_free(myexpr);
			return 1;
		}
		g_free(myexpr);
	}
	printf("Modified %d entries\n", changes);
	return 0;
}
Esempio n. 6
0
std::string Song::getURI(unsigned idx) const
{
	assert(m_song);
	if (idx > 0)
		return "";
	else
		return mpd_song_get_uri(m_song.get());
}
Esempio n. 7
0
char *
getmpdstat() {
    struct mpd_song * song = NULL;
	const char * title = NULL;
	const char * artist = NULL;
	const char * uri = NULL;
	const char * titletag = NULL;
	const char * artisttag = NULL;
	char * retstr = NULL;
	int elapsed = 0, total = 0;
    struct mpd_connection * conn ;
    if (!(conn = mpd_connection_new("localhost", 0, 30000)) ||
        mpd_connection_get_error(conn)){
            return smprintf("");
    }

    mpd_command_list_begin(conn, true);
    mpd_send_status(conn);
    mpd_send_current_song(conn);
    mpd_command_list_end(conn);

    struct mpd_status* theStatus = mpd_recv_status(conn);
        if ((theStatus) && (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)) {
                mpd_response_next(conn);
                song = mpd_recv_song(conn);
		titletag = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
		artisttag = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
                title = smprintf("%s",titletag);
                artist = smprintf("%s",artisttag);
		uri = smprintf("%s",mpd_song_get_uri(song));

                elapsed = mpd_status_get_elapsed_time(theStatus);
                total = mpd_status_get_total_time(theStatus);
                mpd_song_free(song);

		/* If the song isn't tagged, then just use the filename */
		if(artisttag == NULL && titletag == NULL) {
			retstr = smprintf("%s[%s%s%s | %s%.2d:%.2d%s/%.2d:%.2d%s]",
					colcyan, colyellow, uri, colcyan,
					colbyellow, elapsed/60, elapsed%60,
					colyellow, total/60,   total%60, colcyan);
		} else {
			retstr = smprintf("%s[%s%s%s | %s%s%s | %s%.2d:%.2d%s/%.2d:%.2d%s]",
					colcyan, colyellow, artist, colcyan, colred, title, colcyan,
					colbyellow, elapsed/60, elapsed%60,
					colyellow, total/60,   total%60, colcyan);
		}
                free((char*)title);
                free((char*)artist);
		free((char*)uri);
        }
        else retstr = smprintf("");
		mpd_response_finish(conn);
		mpd_connection_free(conn);
		return retstr;
}
Esempio n. 8
0
char* mpd_get_album(struct mpd_song const *song)
{
    char *str;

    str = (char *)mpd_song_get_tag(song, MPD_TAG_ALBUM, 0);
    if(str == NULL){
        str = basename((char *)mpd_song_get_uri(song));
    }

    return str;
}
Esempio n. 9
0
char* mpd_get_title(struct mpd_song const *song)
{
    char *str;

    str = (char *)mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
    if(str == NULL){
        str = basename((char *)mpd_song_get_uri(song));
    }

    return str;
}
Esempio n. 10
0
std::string Song::getDirectory(unsigned idx) const
{
	assert(m_song);
	if (idx > 0 || isStream())
		return "";
	const char *uri = mpd_song_get_uri(m_song.get());
	const char *name = strrchr(uri, '/');
	if (name)
		return std::string(uri, name-uri);
	else
		return "/";
}
Esempio n. 11
0
char *
get_mpd(void) {
	struct mpd_connection *con;
	struct mpd_status *status;
	struct mpd_song *song;
	int status_type;
	char *res;
	const char *artist = NULL, *title = NULL;

	con = mpd_connection_new(NULL, 0, 30000);
	if(mpd_connection_get_error(con)) {
		mpd_connection_free(con);
		return NO_MPD;
	}

	mpd_command_list_begin(con, true);
	mpd_send_status(con);
	mpd_send_current_song(con);
	mpd_command_list_end(con);

	status = mpd_recv_status(con);
	if(!status) {
		mpd_connection_free(con);
		return NO_MPD;
	}
	mpd_response_next(con);
	song = mpd_recv_song(con);
	title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
	if(!title)
		title = mpd_song_get_uri(song);
	artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);

	status_type = mpd_status_get_state(status);
	switch(status_type) {
		case(MPD_STATE_PLAY):
			res = smprintf(MPD, "Playing", artist, title);
			break;
		case(MPD_STATE_PAUSE):
			res = smprintf(MPD, "Paused", artist, title);
			break;
		case(MPD_STATE_STOP):
			res = smprintf(MPD, "Stopped", artist, title);
			break;
		default:
			res = NO_MPD;
			break;
	}
	mpd_song_free(song);
	mpd_response_finish(con);
	mpd_status_free(status);
	mpd_connection_free(con);
	return res;
}
Esempio n. 12
0
int main() {
	char fullpath[100], text[100];
	char *buf = NULL, *sub = NULL, *state = NULL;
	
	conn = mpd_connection_new(NULL, 0, 10000);
	notify = notify_notification_new(NULL, NULL, NULL);
	notify_init("mpdnotify");

	while(mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) {
		mpd_command_list_begin(conn, true);
		mpd_send_status(conn);
		mpd_send_current_song(conn);
		mpd_command_list_end(conn);

		status = mpd_recv_status(conn);
		if(mpd_status_get_state(status) == MPD_STATE_PLAY)
			state = "Playing:";
		else if(mpd_status_get_state(status) == MPD_STATE_PAUSE)
			state = "Paused:";
		else
			state = "Stopped:";
		mpd_response_next(conn);
		song = mpd_recv_song(conn);

		if(song) {
			artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
			title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
			path = mpd_song_get_uri(song);

			sprintf(text, "%s - %s", artist, title);

			buf = fullpath;
			sub = strrchr(path, '/');
			buf = strcpy(buf, mpdroot);
			buf = strncat(buf, path, strlen(path) - strlen(sub) + 1);
			buf = strcat(buf, albumart);
			icon = gdk_pixbuf_new_from_file(fullpath, NULL);

			if(icon)
				notify_notification_update(notify, state, text, NULL);
			else
				notify_notification_update(notify, state, text, "sound");
			notify_notification_set_icon_from_pixbuf(notify, icon);
			notify_notification_show(notify, &gerror);

			mpd_song_free(song);
		}
		mpd_response_finish(conn);
	}
	mpd_connection_free(conn);
	notify_uninit();
	return 0;
}
Esempio n. 13
0
char* mpd_get_artist(struct mpd_song const *song)
{
    char *str;
    str = (char *)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
    if(str == NULL) {
        str = basename((char *)mpd_song_get_uri(song));
        if  (str == NULL)
            str = nullstr;
    }

    return str;
}
Esempio n. 14
0
static void getFilename(RESULT * result)
{
    const char *value = NULL;
    mpd_update();
    if (currentSong != NULL) {
	value = mpd_song_get_uri(currentSong);
    }
    if (value)
	SetResult(&result, R_STRING, charset_from_utf8(value));
    else
	SetResult(&result, R_STRING, "");
}
Esempio n. 15
0
static void
song_ended(const struct mpd_song *song)
{
	int elapsed;

	g_assert(song != NULL);

	elapsed = g_timer_elapsed(timer, NULL);

	if (mpd_song_get_tag(song, MPD_TAG_ARTIST, 0) == NULL ||
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0) == NULL) {
		g_message("Song (%s) has missing tags, skipping",
				mpd_song_get_uri(song));
		return;
	}
	else if (!played_long_enough(elapsed, mpd_song_get_duration(song))) {
		g_message("Song (%s - %s), id: %u, pos: %u not played long enough, skipping",
				mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
				mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
				mpd_song_get_id(song), mpd_song_get_pos(song));
		return;
	}

	g_debug("Submitting old song (%s - %s), id: %u, pos: %u",
			mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
			mpd_song_get_id(song), mpd_song_get_pos(song));

	/* FIXME: libmpdclient doesn't have any way to fetch the musicbrainz id.
	 */
	as_songchange(mpd_song_get_uri(song),
			mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
			mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
			mpd_song_get_tag(song, MPD_TAG_ALBUM, 0),
			mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0),
			mpd_song_get_duration(song) > 0
			? mpd_song_get_duration(song)
			: g_timer_elapsed(timer, NULL),
			NULL);
}
Esempio n. 16
0
void get_random_song(struct mpd_connection *conn, char *str, char *path)
{
    struct mpd_entity *entity;
    int listened0 = 65000,
        skipnum, numberofsongs = 0;

    struct mpd_stats *stats = mpd_run_stats(conn);
    if (stats == NULL)
        return;
    numberofsongs = mpd_stats_get_number_of_songs(stats);
    mpd_stats_free(stats);
    skipnum = rand() % numberofsongs;

    syslog(LOG_DEBUG, "%s: path %s; number of songs: %i skip: %i\n",
            __func__, path, numberofsongs, skipnum);
    if (!mpd_send_list_all_meta(conn, ""))//path))
    {
        syslog(LOG_ERR, "%s: error: mpd_send_list_meta %s\n", __func__, path);
        return;
    }

    while((entity = mpd_recv_entity(conn)) != NULL)
    {
        const struct mpd_song *song;
        if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
        {
            if (skipnum-- > 0)
                continue;

            int listened;
            song = mpd_entity_get_song(entity);
            listened = db_get_song_numplayed(mpd_get_title(song),
                    mpd_get_artist(song));
            if (listened < listened0) {
                listened0 = listened;
                syslog(LOG_DEBUG, "listened: %i ", listened);
                int probability = 50 +
                    db_get_song_rating(mpd_get_title(song),
                            mpd_get_artist(song));
                syslog(LOG_DEBUG, "probability: %i ", probability);
                bool Yes = (rand() % 100) < probability;
                if (Yes) {
                    sprintf(str, "%s", mpd_song_get_uri(song));
                    syslog(LOG_DEBUG, "uri: %s ", str);
                    syslog(LOG_DEBUG, "title: %s ", mpd_get_title(song));
                    syslog(LOG_DEBUG, "artist: %s", mpd_get_artist(song));
                }
            }
        }
        mpd_entity_free(entity);
    }
}
Esempio n. 17
0
static int fetch_items(MENU_SEARCH *menu,char *str,CLIENT_OBJECT *cli,DB_OBJECT *db)
{
    if (!(str && strlen(str)))
        return 1;

    /* send query */
    if (!mpd_search_db_songs(cli->conn,false))
        clear_or_exit_on_error(cli->conn);
    if (!mpd_search_add_tag_constraint(cli->conn,MPD_OPERATOR_DEFAULT,MPD_TAG_TITLE,str))
        clear_or_exit_on_error(cli->conn);
    if (!mpd_search_commit(cli->conn))
        clear_or_exit_on_error(cli->conn);

    /* process response */
    int block = 1024;
    menu->items = (MENU_SEARCH_ITEM*)calloc(block,sizeof(MENU_SEARCH_ITEM));

    struct mpd_song *song;
    int i = 0;
	while ((song = mpd_recv_song(cli->conn))) {

        /* realloc if memory not enough */
        if (i >= block) {
            block *= 2;
            menu->items = (MENU_SEARCH_ITEM*)realloc(menu->items,block*sizeof(MENU_SEARCH_ITEM));
        }

        unsigned id = mpd_song_get_id(song);
        const char *uri = mpd_song_get_uri(song);
        const char *title = mpd_song_get_tag(song,MPD_TAG_TITLE,0);

        menu->items[i].id = id;
        menu->items[i].title = (char *)malloc((strlen(title)+1)*sizeof(char));
        strcpy(menu->items[i].title,title);
        menu->items[i++].node = get_node_by_uri(db,uri);

		mpd_song_free(song);

	}

    if (mpd_connection_get_error(cli->conn) == MPD_ERROR_SUCCESS)
        mpd_response_finish(cli->conn);
    else
        clear_or_exit_on_error(cli->conn);

    menu->num = i;

    return 0;
}
Esempio n. 18
0
/* create a list suitable for GCompletion from path */
GList *
gcmp_list_from_path(struct mpdclient *c, const gchar *path,
		    GList *list, gint types)
{
	struct mpd_connection *connection;
	struct mpd_entity *entity;

	connection = mpdclient_get_connection(c);
	if (connection == NULL)
		return list;

	mpd_send_list_meta(connection, path);

	while ((entity = mpd_recv_entity(connection)) != NULL) {
		char *name;

		if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
		    types & GCMP_TYPE_DIR) {
			const struct mpd_directory *dir =
				mpd_entity_get_directory(entity);
			gchar *tmp = utf8_to_locale(mpd_directory_get_path(dir));
			gsize size = strlen(tmp)+2;

			name = g_malloc(size);
			g_strlcpy(name, tmp, size);
			g_strlcat(name, "/", size);
			g_free(tmp);
		} else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG &&
			   types & GCMP_TYPE_FILE) {
			const struct mpd_song *song =
				mpd_entity_get_song(entity);
			name = utf8_to_locale(mpd_song_get_uri(song));
		} else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST &&
			   types & GCMP_TYPE_PLAYLIST) {
			const struct mpd_playlist *playlist =
				mpd_entity_get_playlist(entity);
			name = utf8_to_locale(mpd_playlist_get_path(playlist));
		} else {
			mpd_entity_free(entity);
			continue;
		}

		list = g_list_append(list, name);
		mpd_entity_free(entity);
	}

	return list;
}
Esempio n. 19
0
std::string Song::getName(unsigned idx) const
{
	assert(m_song);
	mpd_song *s = m_song.get();
	const char *res = mpd_song_get_tag(s, MPD_TAG_NAME, idx);
	if (res)
		return res;
	else if (idx > 0)
		return "";
	const char *uri = mpd_song_get_uri(s);
	const char *name = strrchr(uri, '/');
	if (name)
		return name+1;
	else
		return uri;
}
Esempio n. 20
0
int mpd_search(char *buffer, char *searchstr)
{
    int i = 0;
    char *cur = buffer;
    const char *end = buffer + MAX_SIZE;
    struct mpd_song *song;

    if(mpd_search_db_songs(mpd.conn, false) == false)
        RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
    else if(mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false)
        RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint");
    else if(mpd_search_commit(mpd.conn) == false)
        RETURN_ERROR_AND_RECOVER("mpd_search_commit");
    else {
        cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"search\",\"data\":[ ");

        while((song = mpd_recv_song(mpd.conn)) != NULL) {
            cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"song\",\"uri\":");
            cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_uri(song));
            cur += json_emit_raw_str(cur, end - cur, ",\"album\":");
            cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song));
            cur += json_emit_raw_str(cur, end - cur, ",\"artist\":");
            cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song));
            cur += json_emit_raw_str(cur, end - cur, ",\"duration\":");
            cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song));
            cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
            cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));
            cur += json_emit_raw_str(cur, end - cur, "},");
            mpd_song_free(song);

            /* Maximum results */
            if(i++ >= 300)
            {
                cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"wrap\"},");
                break;
            }
        }

        /* remove last ',' */
        cur--;

        cur += json_emit_raw_str(cur, end - cur, "]}");
    }
    return cur - buffer;
}
Esempio n. 21
0
/* if no tag is availabe, use filename */
static void getArtist(RESULT * result)
{
    const char *value = NULL;
    mpd_update();
    if (currentSong != NULL) {
	value = mpd_song_get_tag(currentSong, MPD_TAG_ARTIST, 0);
	if (!value) {
	    value = mpd_song_get_tag(currentSong, MPD_TAG_ALBUM_ARTIST, 0);
	}
	if (!value) {
	    value = mpd_song_get_uri(currentSong);
	}
    }
    if (value)
	SetResult(&result, R_STRING, charset_from_utf8(value));
    else
	SetResult(&result, R_STRING, "");
}
Esempio n. 22
0
int mpd_delete_current_song(struct mpd_connection *conn)
{
    struct mpd_song *song;
    char *currentsonguri = NULL;

    song = mpd_run_current_song(conn);
    if(song == NULL)
        return 0;

    db_update_song_rating(mpd_get_title(song),
            mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -5);

    currentsonguri = mpd_song_get_uri(song);
    printf("%s: let's delete song: %s\n", __func__, currentsonguri);

    delete_file_forever(currentsonguri);

    mpd_song_free(song);

    return 1;
}
Esempio n. 23
0
void get_song_to_delete(char *str)
{
    struct mpd_entity *entity;
    int rating0 = 65000;
    if (!mpd_send_list_meta(mpd.conn, "/")) {
        syslog(LOG_DEBUG, "error: mpd_send_list_meta\n");
        return;
    }
    while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
        if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
            const struct mpd_song *song = mpd_entity_get_song(entity);
            int rating = db_get_song_rating(mpd_get_title(song),
                    mpd_get_artist(song));
            if (rating < rating0) {
                rating0 = rating;
                sprintf(str, "%s", mpd_song_get_uri(song));
            }
        }
        mpd_entity_free(entity);
    }
}
Esempio n. 24
0
void print_entity_list(struct mpd_connection *c, enum mpd_entity_type filter_type)
{
    struct mpd_entity *entity;
    while ((entity = mpd_recv_entity(c)) != NULL) {
        const struct mpd_directory *dir;
        const struct mpd_song *song;
        const struct mpd_playlist *playlist;

        enum mpd_entity_type type = mpd_entity_get_type(entity);
        if (filter_type != MPD_ENTITY_TYPE_UNKNOWN &&
            type != filter_type)
            type = MPD_ENTITY_TYPE_UNKNOWN;

        switch (type) {
        case MPD_ENTITY_TYPE_UNKNOWN:
            break;

        case MPD_ENTITY_TYPE_DIRECTORY:
            dir = mpd_entity_get_directory(entity);
            printf("%s\n", mpd_directory_get_path(dir)); //charset_from_utf8(mpd_directory_get_path(dir)));
            break;

        case MPD_ENTITY_TYPE_SONG:
            song = mpd_entity_get_song(entity);
            /*if (options.custom_format) {
                pretty_print_song(song);
                puts("");
            } else*/
                printf("%s\n", mpd_song_get_uri(song)); //charset_from_utf8(mpd_song_get_uri(song)));
            break;

        case MPD_ENTITY_TYPE_PLAYLIST:
            playlist = mpd_entity_get_playlist(entity);
            printf("%s\n", mpd_playlist_get_path(playlist)); //charset_from_utf8(mpd_playlist_get_path(playlist)));
            break;
        }

        mpd_entity_free(entity);
    }
}
Esempio n. 25
0
/**
 * MPD stopped playing this song.
 */
void
song_ended(const struct mpd_song *song, bool love)
{
    int elapsed = g_timer_elapsed(timer, NULL);

    if (!played_long_enough(elapsed, mpd_song_get_duration(song)))
        return;

    /* FIXME:
       libmpdclient doesn't have any way to fetch the musicbrainz id. */
    as_songchange(mpd_song_get_uri(song),
                  mpd_song_get_tag(song, MPD_TAG_ARTIST, 0),
                  mpd_song_get_tag(song, MPD_TAG_TITLE, 0),
                  mpd_song_get_tag(song, MPD_TAG_ALBUM, 0),
                  mpd_song_get_tag(song, MPD_TAG_TRACK, 0),
                  mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID, 0),
                  mpd_song_get_duration(song) > 0
                  ? mpd_song_get_duration(song)
                  : g_timer_elapsed(timer, NULL),
                  love,
                  NULL);
}
Esempio n. 26
0
char* getFilename(Config* config, int status) {

	if (!config->curr_song) {
		return strdup("");
	}

	const char* uri = mpd_song_get_uri(config->curr_song);

	if (!uri || uri[0] == 0) {
		return strdup("");
	}

	const char* str = strrchr(uri, '/');

	// check if more then the / is in the string
	if (!str || strlen(str) < 2) {
		str = uri;
	}

	str++;

	return strdup(str);
}
Esempio n. 27
0
bool mpd_is_in_queue(const char *uri)
{
    bool res = false;
    struct mpd_entity *entity;
    struct mpd_connection *conn = mpd_connection_new(NULL, NULL, 3000);

    if (conn == NULL) {
        syslog(LOG_ERR, "%s - Out of memory.", __func__);
        goto DONE;
    }
    if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) {
        syslog(LOG_ERR, "%s - MPD connection: %s\n", __func__,
                mpd_connection_get_error_message(conn));
        goto DONE;
    }
    if (!mpd_send_list_queue_meta(conn)) {
        syslog(LOG_ERR, "%s: %s", __func__, mpd_connection_get_error_message(conn));
        mpd_connection_clear_error(conn);
        goto DONE;
    }
    while((entity = mpd_recv_entity(conn)) != NULL) {
        const struct mpd_song *song;
        if(mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG && !res) {
            song = mpd_entity_get_song(entity);
            if (strcmp(mpd_song_get_uri(song), uri) == 0) {
                syslog(LOG_INFO, "%s: %s is already in the queue", __func__, uri);
                res = true;
            }
        }
        mpd_entity_free(entity);
    }

DONE:
    if(conn != NULL)
        mpd_connection_free(conn);
    return res;
}
Esempio n. 28
0
static int
love_song(struct mpdcron_connection *conn, bool love, const char *expr)
{
	int changes;
	char *esc_uri, *myexpr;
	struct mpd_song *song;

	if (expr != NULL) {
		if (!mpdcron_love_expr(conn, love, expr, &changes)) {
			eulog(LOG_ERR, "Failed to %s song: %s",
					love ? "love" : "hate",
					conn->error->message);
			return 1;
		}
	}
	else {
		if ((song = load_current_song()) == NULL)
			return 1;

		esc_uri = quote(mpd_song_get_uri(song));
		myexpr = g_strdup_printf("uri=%s", esc_uri);
		g_free(esc_uri);
		mpd_song_free(song);

		if (!mpdcron_love_expr(conn, love, myexpr, &changes)) {
			eulog(LOG_ERR, "Failed to %s current playing song: %s",
					love ? "love" : "hate",
					conn->error->message);
			g_free(myexpr);
			return 1;
		}
		g_free(myexpr);
	}
	printf("Modified %d entries\n", changes);
	return 0;
}
Esempio n. 29
0
/*
 * pgmpc_playlist
 * List all songs in given playlist. If not playlist is specified list
 * songs of current playlist.
 */
Datum
pgmpc_playlist(PG_FUNCTION_ARGS)
{
	TupleDesc   tupdesc;
	Tuplestorestate *tupstore;
	char *playlist = NULL;
	bool ret;

	if (PG_NARGS() == 1 && !PG_ARGISNULL(0))
		playlist = text_to_cstring(PG_GETARG_TEXT_PP(0));

	/* Initialize function context */
	pgmpc_init_setof_single(fcinfo, TEXTOID, "playlist", &tupdesc, &tupstore);

	/*
	 * Run the command to get all the songs.
	 */
	pgmpc_init();
	ret = playlist ?
		mpd_send_list_playlist_meta(mpd_conn, playlist) :
		mpd_send_list_queue_meta(mpd_conn);
	if (!ret)
		pgmpc_print_error();

	/* Now get all the songs and send them back to caller */
	while (true)
	{
		Datum       values[1];
		bool		nulls[1];
		struct mpd_song *song = mpd_recv_song(mpd_conn);

		/* Leave if done */
		if (song == NULL)
			break;

		/* Assign song name */
		nulls[0] = false;
		values[0] = CStringGetTextDatum(mpd_song_get_uri(song));

		/* Save values */
		tuplestore_putvalues(tupstore, tupdesc, values, nulls);

		/* Clean up for the next one */
		mpd_song_free(song);
	}

	/* We may be in error state, so check for it */
	if (mpd_connection_get_error(mpd_conn) != MPD_ERROR_SUCCESS)
	{
		const char *message = mpd_connection_get_error_message(mpd_conn);
		pgmpc_reset();
		ereport(ERROR,
				(errcode(ERRCODE_SYSTEM_ERROR),
				 errmsg("mpd command failed: %s",
						message)));
	}

	/* Clean up */
	pgmpc_reset();

	/* clean up and return the tuplestore */
	tuplestore_donestoring(tupstore);

	return (Datum) 0;
}
Esempio n. 30
0
int mpd_put_browse(char *buffer, char *path, unsigned int offset)
{
    char *cur = buffer;
    const char *end = buffer + MAX_SIZE;
    struct mpd_entity *entity;
    unsigned int entity_count = 0;

    if (!mpd_send_list_meta(mpd.conn, path))
        RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");

    cur += json_emit_raw_str(cur, end  - cur, "{\"type\":\"browse\",\"data\":[ ");

    while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
        const struct mpd_song *song;
        const struct mpd_directory *dir;
        const struct mpd_playlist *pl;

        if(offset > entity_count)
        {
            mpd_entity_free(entity);
            entity_count++;
            continue;
        }
        else if(offset + MAX_ELEMENTS_PER_PAGE - 1 < entity_count)
        {
            mpd_entity_free(entity);
            cur += json_emit_raw_str(cur, end  - cur, "{\"type\":\"wrap\",\"count\":");
            cur += json_emit_int(cur, end - cur, entity_count);
            cur += json_emit_raw_str(cur, end  - cur, "} ");
            break;
        }

        switch (mpd_entity_get_type(entity)) {
            case MPD_ENTITY_TYPE_UNKNOWN:
                break;

            case MPD_ENTITY_TYPE_SONG:
                song = mpd_entity_get_song(entity);
                cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"song\",\"uri\":");
                cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_uri(song));
                cur += json_emit_raw_str(cur, end - cur, ",\"duration\":");
                cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song));
                cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
                cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));
                cur += json_emit_raw_str(cur, end - cur, "},");
                break;

            case MPD_ENTITY_TYPE_DIRECTORY:
                dir = mpd_entity_get_directory(entity);

                cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"directory\",\"dir\":");
                cur += json_emit_quoted_str(cur, end - cur, mpd_directory_get_path(dir));
                cur += json_emit_raw_str(cur, end - cur, "},");
                break;

            case MPD_ENTITY_TYPE_PLAYLIST:
                pl = mpd_entity_get_playlist(entity);
                cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"playlist\",\"plist\":");
                cur += json_emit_quoted_str(cur, end - cur, mpd_playlist_get_path(pl));
                cur += json_emit_raw_str(cur, end - cur, "},");
                break;
        }
        mpd_entity_free(entity);
        entity_count++;
    }

    if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(mpd.conn)) {
        fprintf(stderr, "MPD mpd_send_list_meta: %s\n", mpd_connection_get_error_message(mpd.conn));
        mpd.conn_state = MPD_FAILURE;
        return 0;
    }

    /* remove last ',' */
    cur--;

    cur += json_emit_raw_str(cur, end - cur, "]}");
    return cur - buffer;
}