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 "AddPathsCommand.h" 10 11 #include <new> 12 #include <stdio.h> 13 #include <string.h> 14 15 #include "PathContainer.h" 16 #include "VectorPath.h" 17 18 using std::nothrow; 19 20 // constructor 21 AddPathsCommand::AddPathsCommand(PathContainer* container, 22 VectorPath** const paths, 23 int32 count, 24 bool ownsPaths, 25 int32 index) 26 : Command(), 27 fContainer(container), 28 fPaths(paths && count > 0 ? new (nothrow) VectorPath*[count] : NULL), 29 fCount(count), 30 fOwnsPaths(ownsPaths), 31 fIndex(index), 32 fPathsAdded(false) 33 { 34 if (!fContainer || !fPaths) 35 return; 36 37 memcpy(fPaths, paths, sizeof(VectorPath*) * fCount); 38 } 39 40 // destructor 41 AddPathsCommand::~AddPathsCommand() 42 { 43 if (fOwnsPaths && !fPathsAdded && fPaths) { 44 for (int32 i = 0; i < fCount; i++) 45 fPaths[i]->Release(); 46 } 47 delete[] fPaths; 48 } 49 50 // InitCheck 51 status_t 52 AddPathsCommand::InitCheck() 53 { 54 return fContainer && fPaths ? B_OK : B_NO_INIT; 55 } 56 57 // Perform 58 status_t 59 AddPathsCommand::Perform() 60 { 61 status_t ret = B_OK; 62 63 // add paths to container 64 int32 index = fIndex; 65 for (int32 i = 0; i < fCount; i++) { 66 if (fPaths[i] && !fContainer->AddPath(fPaths[i], index)) { 67 ret = B_ERROR; 68 // roll back 69 for (int32 j = i - 1; j >= 0; j--) 70 fContainer->RemovePath(fPaths[j]); 71 break; 72 } 73 index++; 74 } 75 fPathsAdded = true; 76 77 return ret; 78 } 79 80 // Undo 81 status_t 82 AddPathsCommand::Undo() 83 { 84 // remove paths from container 85 for (int32 i = 0; i < fCount; i++) { 86 fContainer->RemovePath(fPaths[i]); 87 } 88 fPathsAdded = false; 89 90 return B_OK; 91 } 92 93 // GetName 94 void 95 AddPathsCommand::GetName(BString& name) 96 { 97 if (fOwnsPaths) { 98 if (fCount > 1) 99 name << "Add Paths"; 100 else 101 name << "Add Path"; 102 } else { 103 if (fCount > 1) 104 name << "Assign Paths"; 105 else 106 name << "Assign Path"; 107 } 108 } 109