xref: /haiku/src/apps/icon-o-matic/generic/command/CompoundCommand.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
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
14 CompoundCommand::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
27 CompoundCommand::~CompoundCommand()
28 {
29 	for (int32 i = 0; i < fCount; i++)
30 		delete fCommands[i];
31 	delete[] fCommands;
32 }
33 
34 // InitCheck
35 status_t
36 CompoundCommand::InitCheck()
37 {
38 	status_t status = fCommands && fCount > 0 ? B_OK : B_BAD_VALUE;
39 	return status;
40 }
41 
42 // Perform
43 status_t
44 CompoundCommand::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
68 status_t
69 CompoundCommand::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
85 status_t
86 CompoundCommand::Redo()
87 {
88 	return Perform();
89 }
90 
91 // GetName
92 void
93 CompoundCommand::GetName(BString& name)
94 {
95 	name << fName.String();
96 }
97