* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "AddStylesCommand.h"
#include <new>
#include <stdio.h>
#include <string.h>
#include <Catalog.h>
#include <Locale.h>
#include "StyleContainer.h"
#include "Style.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Icon-O-Matic-AddStylesCmd"
using std::nothrow;
AddStylesCommand::AddStylesCommand(StyleContainer* container,
Style** const styles,
int32 count,
int32 index)
: Command(),
fContainer(container),
fStyles(styles && count > 0 ? new (nothrow) Style*[count] : NULL),
fCount(count),
fIndex(index),
fStylesAdded(false)
{
if (!fContainer || !fStyles)
return;
memcpy(fStyles, styles, sizeof(Style*) * fCount);
}
AddStylesCommand::~AddStylesCommand()
{
if (!fStylesAdded && fStyles) {
for (int32 i = 0; i < fCount; i++)
fStyles[i]->ReleaseReference();
}
delete[] fStyles;
}
status_t
AddStylesCommand::InitCheck()
{
return fContainer && fStyles ? B_OK : B_NO_INIT;
}
status_t
AddStylesCommand::Perform()
{
status_t ret = B_OK;
int32 index = fIndex;
for (int32 i = 0; i < fCount; i++) {
if (fStyles[i] && !fContainer->AddStyle(fStyles[i], index)) {
ret = B_ERROR;
for (int32 j = i - 1; j >= 0; j--)
fContainer->RemoveStyle(fStyles[j]);
break;
}
index++;
}
fStylesAdded = true;
return ret;
}
status_t
AddStylesCommand::Undo()
{
for (int32 i = 0; i < fCount; i++) {
fContainer->RemoveStyle(fStyles[i]);
}
fStylesAdded = false;
return B_OK;
}
void
AddStylesCommand::GetName(BString& name)
{
if (fCount > 1)
name << B_TRANSLATE("Add Styles");
else
name << B_TRANSLATE("Add Style");
}