1 /* 2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ingo Weinhold <ingo_weinhold@gmx.de> 7 */ 8 9 10 #include "VolumeState.h" 11 12 #include <AutoDeleter.h> 13 #include <AutoLocker.h> 14 15 16 VolumeState::VolumeState() 17 : 18 fPackagesByFileName(), 19 fPackagesByNodeRef() 20 { 21 } 22 23 24 VolumeState::~VolumeState() 25 { 26 fPackagesByFileName.Clear(); 27 28 Package* package = fPackagesByNodeRef.Clear(true); 29 while (package != NULL) { 30 Package* next = package->NodeRefHashTableNext(); 31 delete package; 32 package = next; 33 } 34 } 35 36 37 bool 38 VolumeState::Init() 39 { 40 return fPackagesByFileName.Init() == B_OK 41 && fPackagesByNodeRef.Init() == B_OK; 42 } 43 44 45 void 46 VolumeState::AddPackage(Package* package) 47 { 48 fPackagesByFileName.Insert(package); 49 fPackagesByNodeRef.Insert(package); 50 } 51 52 53 void 54 VolumeState::RemovePackage(Package* package) 55 { 56 fPackagesByFileName.Remove(package); 57 fPackagesByNodeRef.Remove(package); 58 } 59 60 61 void 62 VolumeState::SetPackageActive(Package* package, bool active) 63 { 64 package->SetActive(active); 65 } 66 67 68 void 69 VolumeState::ActivationChanged(const PackageSet& activatedPackage, 70 const PackageSet& deactivatePackages) 71 { 72 for (PackageSet::iterator it = activatedPackage.begin(); 73 it != activatedPackage.end(); ++it) { 74 (*it)->SetActive(true); 75 } 76 77 for (PackageSet::iterator it = deactivatePackages.begin(); 78 it != deactivatePackages.end(); ++it) { 79 Package* package = *it; 80 RemovePackage(package); 81 delete package; 82 } 83 } 84 85 86 VolumeState* 87 VolumeState::Clone() const 88 { 89 VolumeState* clone = new(std::nothrow) VolumeState; 90 if (clone == NULL) 91 return NULL; 92 ObjectDeleter<VolumeState> cloneDeleter(clone); 93 94 for (PackageFileNameHashTable::Iterator it 95 = fPackagesByFileName.GetIterator(); 96 Package* package = it.Next();) { 97 Package* clonedPackage = package->Clone(); 98 if (clonedPackage == NULL) 99 return NULL; 100 clone->AddPackage(clonedPackage); 101 } 102 103 return cloneDeleter.Detach(); 104 } 105