void MPDConnection::connectToMPD(const ServerInfo &server) {
	if (d->connected) {
		if (d->server == server) // Trying to reconncet to same server, ignore
			return;
		// Trying to connect to another server. disconnect, then connect to it.
		disconnectFromMPD();
	}

	setCaller("MPDConnection::connectToMPD", "mpd_newConnection");
	d->connection = mpd_newConnection(server.address().toUtf8(), server.port(), Config::instance()->timeoutTime());
	if (!finishCommand()) {
		disconnectFromMPD(tr("Could not connect to server %1:%2").arg(server.address()).arg(server.port()));
		return;
	}

	if (!server.password().isEmpty()) {
		mpd_call(MPDConnection::connectToMPD, Password, server.password().toUtf8());
		if (!finishCommand()) {
			disconnectFromMPD(tr("Authentication failed"));
			return;
		}
	}

	d->connected = true;
	d->server = server;
	emit connected(server);
}
bool MPDConnection::finishCommand() {
	if (d->connection->error)
	 {
		QString errormsg = QString::fromUtf8(d->connection->errorStr).replace("\n", "");
		switch (d->connection->error) {
			case MPD_ERROR_TIMEOUT:
			case MPD_ERROR_SYSTEM:
			case MPD_ERROR_UNKHOST:
			case MPD_ERROR_CONNPORT:
			case MPD_ERROR_NOTMPD:
			case MPD_ERROR_NORESPONSE:
			case MPD_ERROR_SENDING:
			case MPD_ERROR_CONNCLOSED:
				// These are the serious errors. Set errormsg.
				qWarning("Error: `%s (error code %d). Disconnecting.", qPrintable(errormsg), d->connection->error);
				disconnectFromMPD(errormsg);
				return false;
			default:
				// Just warnings. We ignore them.
				qWarning("Warning: `%s (error code %d). Ignoring.", qPrintable(errormsg), d->connection->error);
		}
		qWarning("Problem occured while executing command: %s, called from: %s", qPrintable(d->command), qPrintable(d->caller));
		d->caller = d->command = QString();
		mpd_clearError(d->connection);
		return false;
	} else
		mpd_finishCommand(d->connection);
	// Clear error code, just in case.
	mpd_clearError(d->connection);
	return true;
}
void MPDPlaylistsConnection::listPlaylist()
{
	QByteArray *data;

	sendCommand("listplaylist");
	data = readFromSocket();

	delete data;
	disconnectFromMPD();
}
void MPDPlaylistsConnection::rm(QString name)
{
	QByteArray data("rm ");
	data += "\"" + name.toUtf8().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";

	sendCommand(data);

	if(!commandOk()) {
		qDebug("Couldn't remove playlist");
	}
	disconnectFromMPD();
}
void MPDPlaylistsConnection::listPlaylists()
{
	QByteArray *data;

	sendCommand("listplaylists");
	data = readFromSocket();

	emit playlistsRetrieved(MPDParseUtils::parsePlaylists(data));

	delete data;
	disconnectFromMPD();
}
void MPDPlaylistsConnection::rename(const QString oldName, const QString newName)
{
	QByteArray data("rename ");
	data += "\"" + oldName.toUtf8().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
	data += " ";
	data += "\"" + newName.toUtf8().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";

	sendCommand(data);

	if(!commandOk()) {
		qDebug("Couldn't rename playlist");
	}
	disconnectFromMPD();
}
Beispiel #7
0
/******************** Connection handling ***********************************/
static int  connectToMPD(unsigned timeout) {
	disconnectFromMPD();

	D.conn = mpd_newConnection(D.host, D.port, timeout);
	pdie_on(!D.conn, "connect");

	if (D.password && !error()) {
		mpd_sendPasswordCommand(D.conn, D.password);
		if (!error()) {
			mpd_finishCommand(D.conn);
		}
	}

	return !error();
}
void MPDPlaylistsConnection::save(QString name)
{
	QByteArray data("save ");
	data += "\"" + name.toUtf8().replace("\\", "\\\\").replace("\"", "\\\"") + "\"";

	sendCommand(data);

	MPDError *mpdError = commandResult();
	if(mpdError->isError()) {
		if(mpdError->errorString().endsWith("Playlist already exists\n")) {
			QMessageBox::warning(NULL, "Warning", "Playlist couldn't be saved since there already exists a playlist with the same name.");
		}
	}

	delete mpdError;
	disconnectFromMPD();
}