if (playlist.isCollaborative() == null) {
playlist.setCollaborative(false);
}
ChannelCallback callback = new ChannelCallback();
/* Create channel and buffer. */
Channel channel = new Channel("Create-Playlist-Channel", Channel.Type.TYPE_PLAYLIST, callback);
if (user.getPlaylists() == null) {
log.warn("user playlists not loaded yet! should it be? loading..");
new LoadUserPlaylists(store, user).send(protocol);
}
PlaylistContainer playlists = user.getPlaylists();
playlists.getItems().add(playlist);
int position = playlists.getItems().size() - 1;
String xml = String.format
("<change><ops><add><i>%s</i><items>%s</items></add></ops><time>%s</time><user>%s</user></change>" +
"<version>%010d,%010d,%010d,%d</version>",
// change
position,
Hex.toHex(playlist.getUUID()) + "02",
new Date().getTime() / 1000,
user.getName(),
// version
playlists.getRevision() + 1, // new revision of user playlists
playlists.getItems().size(), // new size of user playlists
playlists.calculateChecksum(), // new checksum of user playlists
playlist.isCollaborative() ? 1 : 0
);
byte[] xmlBytes = xml.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(2 + 16 + 1 + 4 + 4 + 4 + 1 + 1 + xmlBytes.length);
buffer.putShort((short) channel.getId());
buffer.put(Hex.toBytes("00000000000000000000000000000000")); // UUID? not used
buffer.put((byte) 0x00); // type? not used
buffer.putInt((int) playlists.getRevision()); // previous revision of user playlists
buffer.putInt(position);
buffer.putInt((int) playlists.getChecksum()); // previous checksum of user playlists
buffer.put((byte) (playlist.isCollaborative() ? 0x01 : 0x00));
buffer.put((byte) 0x03); // unknown
buffer.put(xmlBytes);
buffer.flip();
/* Register channel. */
Channel.register(channel);
/* Send packet. */
protocol.sendPacket(PacketType.changePlaylist, buffer, "create playlist");
/* Get response. */
byte[] data = callback.getData("create playlist response");
xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<playlists>\n" +
new String(data, Charset.forName("UTF-8")) +
"\n</playlists>";