* Copyright © 2007-2009 Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "CopyPLItemsCommand.h"
#include <new>
#include <stdio.h>
#include <Autolock.h>
#include <Catalog.h>
#include <Locale.h>
#include "Playlist.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MediaPlayer-CopyPLItemsCmd"
using std::nothrow;
CopyPLItemsCommand::CopyPLItemsCommand(Playlist* playlist,
BList indices, int32 toIndex)
:
PLItemsCommand(),
fPlaylist(playlist),
fCount(indices.CountItems()),
fItems(fCount > 0 ? new (nothrow) PlaylistItem*[fCount] : NULL),
fToIndex(toIndex),
fItemsCopied(false)
{
if (indices.IsEmpty() || !fPlaylist || !fItems) {
delete[] fItems;
fItems = NULL;
return;
}
memset(fItems, 0, sizeof(PlaylistItem*) * fCount);
for (int32 i = 0; i < fCount; i++) {
PlaylistItem* item =
fPlaylist->ItemAt((int32)(addr_t)indices.ItemAt(i));
if (item != NULL)
fItems[i] = item->Clone();
if (fItems[i] == NULL) {
_CleanUp(fItems, fCount, true);
return;
}
}
}
CopyPLItemsCommand::~CopyPLItemsCommand()
{
_CleanUp(fItems, fCount, !fItemsCopied);
}
status_t
CopyPLItemsCommand::InitCheck()
{
if (!fPlaylist || !fItems)
return B_NO_INIT;
return B_OK;
}
status_t
CopyPLItemsCommand::Perform()
{
BAutolock _(fPlaylist);
status_t ret = B_OK;
fItemsCopied = true;
int32 index = fToIndex;
for (int32 i = 0; i < fCount; i++) {
if (!fPlaylist->AddItem(fItems[i], index++)) {
ret = B_NO_MEMORY;
break;
}
}
return ret;
}
status_t
CopyPLItemsCommand::Undo()
{
BAutolock _(fPlaylist);
fItemsCopied = false;
PlaylistItem* current = fPlaylist->ItemAt(fPlaylist->CurrentItemIndex());
int32 index = fToIndex;
for (int32 i = 0; i < fCount; i++) {
fPlaylist->RemoveItem(index++, false);
}
if (current != NULL)
fPlaylist->SetCurrentItemIndex(fPlaylist->IndexOf(current), false);
return B_OK;
}
void
CopyPLItemsCommand::GetName(BString& name)
{
if (fCount > 1)
name << B_TRANSLATE("Copy Entries");
else
name << B_TRANSLATE("Copy Entry");
}