1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan A��mus <superstippi@gmx.de>
7 */
8
9#include "CompoundCommand.h"
10
11#include <stdio.h>
12
13// constructor
14CompoundCommand::CompoundCommand(Command** commands,
15								 int32 count,
16								 const char* name,
17								 int32 nameIndex)
18	: Command(),
19	  fCommands(commands),
20	  fCount(count),
21	  fName(name),
22	  fNameIndex(nameIndex)
23{
24}
25
26// destructor
27CompoundCommand::~CompoundCommand()
28{
29	for (int32 i = 0; i < fCount; i++)
30		delete fCommands[i];
31	delete[] fCommands;
32}
33
34// InitCheck
35status_t
36CompoundCommand::InitCheck()
37{
38	status_t status = fCommands && fCount > 0 ? B_OK : B_BAD_VALUE;
39	return status;
40}
41
42// Perform
43status_t
44CompoundCommand::Perform()
45{
46	status_t status = InitCheck();
47	if (status >= B_OK) {
48		int32 i = 0;
49		for (; i < fCount; i++) {
50			if (fCommands[i])
51				status = fCommands[i]->Perform();
52			if (status < B_OK)
53				break;
54		}
55/*		if (status < B_OK) {
56			// roll back
57			i--;
58			for (; i >= 0; i--) {
59				if (fCommands[i])
60					fCommands[i]->Undo();
61			}
62		}*/
63	}
64	return status;
65}
66
67// Undo
68status_t
69CompoundCommand::Undo()
70{
71	status_t status = InitCheck();
72	if (status >= B_OK) {
73		int32 i = fCount - 1;
74		for (; i >= 0; i--) {
75			if (fCommands[i])
76				status = fCommands[i]->Undo();
77			if (status < B_OK)
78				break;
79		}
80	}
81	return status;
82}
83
84// Redo
85status_t
86CompoundCommand::Redo()
87{
88	return Perform();
89}
90
91// GetName
92void
93CompoundCommand::GetName(BString& name)
94{
95	name << fName.String();
96}
97