Esempio n. 1
0
bool Connection::AddRandomSongs(size_t number)
{
	prechecksNoCommandsList();
	std::vector<std::string> files;
	mpd_send_list_all(m_connection.get(), "/");
	while (mpd_pair *item = mpd_recv_pair_named(m_connection.get(), "file"))
	{
		files.push_back(item->value);
		mpd_return_pair(m_connection.get(), item);
	}
	mpd_response_finish(m_connection.get());
	checkErrors();
	
	if (number > files.size())
	{
		//if (itsErrorHandler)
		//	itsErrorHandler(this, 0, "Requested number of random songs is bigger than size of your library", itsErrorHandlerUserdata);
		return false;
	}
	else
	{
		std::random_shuffle(files.begin(), files.end());
		StartCommandsList();
		auto it = files.begin()+rand()%(std::max(size_t(1), files.size()-number));
		for (size_t i = 0; i < number && it != files.end(); ++i, ++it)
			AddSong(*it);
		CommitCommandsList();
	}
	return true;
}
Esempio n. 2
0
File: command.c Progetto: daaang/mpc
int
cmd_listall(int argc, char **argv, struct mpd_connection *conn)
{
	const char * listall = "";
	int i = 0;

	if (argc > 0)
		listall = charset_to_utf8(argv[i]);

	do {
		char *tmp = strdup(listall);
		strip_trailing_slash(tmp);

		if (options.custom_format) {
			if (!mpd_send_list_all_meta(conn, tmp))
				printErrorAndExit(conn);

			print_entity_list(conn, MPD_ENTITY_TYPE_UNKNOWN);
		} else {
			if (!mpd_send_list_all(conn, tmp))
				printErrorAndExit(conn);

			print_filenames(conn);
		}

		my_finishCommand(conn);
		free(tmp);
	} while (++i < argc && (listall = charset_to_utf8(argv[i])) != NULL);

	return 0;
}
Esempio n. 3
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. 4
0
int cmd_lstab ( int argc, char ** argv, struct mpd_connection *conn )
{
	struct mpd_directory *dir;

	if (argc != 1)
		return 0;

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

	while ((dir = mpd_recv_directory(conn)) != NULL) {
		if (strncmp(mpd_directory_get_path(dir), argv[0],
			    strlen(argv[0])) == 0)
			printf("%s\n",
			       charset_from_utf8(mpd_directory_get_path(dir)));

		mpd_directory_free(dir);
	}

	my_finishCommand(conn);

	return 0;
}
Esempio n. 5
0
/*
 * pgmpc_ls
 * List all songs of remote server.
 */
Datum
pgmpc_ls(PG_FUNCTION_ARGS)
{
	TupleDesc   tupdesc;
	Tuplestorestate *tupstore;
	char *path = NULL;

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

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

	/*
	 * Run the command to get all the songs.
	 */
	pgmpc_init();
	if (!mpd_send_list_all(mpd_conn, path))
		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. 6
0
/*
 * returns: 0 - error; 1 - ok 
 */
int mqr_update_playlist() {
	struct mpd_entity *entity;
	const struct mpd_directory *dir;
	// const struct mpd_song *song;
	const char *path;

	char *toplevel_dirs[256];
	int array_sz = 0;
	int i;

	// Clear current playlist (QUEUE)
	if (!mpd_run_clear(conn)) {
		fprintf(stderr, "ERROR: MPD: Unable to clear the current playlist\n");
	} else
		if (DEBUG)
			fprintf(stderr, "DEBUG: [mgr-thread] mpd: playlist cleared\n");

	// list items & add to the playlist
	mpd_send_list_all(conn, NULL);
	while ((entity = mpd_recv_entity(conn)) != NULL) {
		path = NULL;

		switch (mpd_entity_get_type(entity)) {
			case MPD_ENTITY_TYPE_UNKNOWN:
				break;
			case MPD_ENTITY_TYPE_SONG:
				// ignore top level items
				// song = mpd_entity_get_song(entity);
				// path = mpd_song_get_uri(song);
				// if (strchr(path, '/') == NULL) {
				//	printf("FOUND SON %s\n", path);
				// }
				break;
			case MPD_ENTITY_TYPE_DIRECTORY:
				// check if has subfolders
				dir = mpd_entity_get_directory(entity);
				path = mpd_directory_get_path(dir);
				if (strchr(path, '/') != NULL)
					path = NULL;
				break;
			case MPD_ENTITY_TYPE_PLAYLIST:
				break;
				
		}

		if (path != NULL && array_sz < 255) {
			toplevel_dirs[ array_sz++ ] = strdup( path );
		}

		mpd_entity_free(entity);
	}
	mpd_response_finish(conn);

	for (i = 0; i < array_sz; i++) {
		if (!mpd_run_add(conn, toplevel_dirs[i])) {
			fprintf(stderr, "ERROR: MPD: Unable to add item to the file playlist: %s\n", toplevel_dirs[i]);
			continue;
		}

		if (DEBUG)
			fprintf(stderr, "DEBUG: [mgr-thread]: mpd: entry added to the playlist: %s\n", toplevel_dirs[i]);

		free( toplevel_dirs[i] );
	}

	return 1;
}