BPicture: Fix archive constructor.
[haiku.git] / src / kits / package / ActivationTransaction.cpp
blob2a73b02fe8a1f9e3648be6c62ad91b8455a5d6c4
1 /*
2 * Copyright 2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include <package/ActivationTransaction.h>
12 #include <new>
14 #include <Message.h>
17 namespace BPackageKit {
18 namespace BPrivate {
21 BActivationTransaction::BActivationTransaction()
23 fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
24 fChangeCount(0),
25 fTransactionDirectoryName(),
26 fPackagesToActivate(),
27 fPackagesToDeactivate()
32 BActivationTransaction::BActivationTransaction(BMessage* archive,
33 status_t* _error)
35 fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
36 fChangeCount(0),
37 fTransactionDirectoryName(),
38 fPackagesToActivate(),
39 fPackagesToDeactivate()
41 status_t error;
42 int32 location;
43 if ((error = archive->FindInt32("location", &location)) == B_OK
44 && (error = archive->FindInt64("change count", &fChangeCount)) == B_OK
45 && (error = archive->FindString("transaction",
46 &fTransactionDirectoryName)) == B_OK
47 && (error = _ExtractStringList(archive, "activate",
48 fPackagesToActivate)) == B_OK
49 && (error = _ExtractStringList(archive, "deactivate",
50 fPackagesToDeactivate)) == B_OK) {
51 if (location >= 0
52 && location <= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT) {
53 fLocation = (BPackageInstallationLocation)location;
54 } else
55 error = B_BAD_VALUE;
58 if (_error != NULL)
59 *_error = error;
63 BActivationTransaction::~BActivationTransaction()
68 status_t
69 BActivationTransaction::InitCheck() const
71 if (fLocation < 0 || fLocation >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
72 || fTransactionDirectoryName.IsEmpty()
73 || (fPackagesToActivate.IsEmpty() && fPackagesToDeactivate.IsEmpty())) {
74 return B_BAD_VALUE;
76 return B_OK;
80 status_t
81 BActivationTransaction::SetTo(BPackageInstallationLocation location,
82 int64 changeCount, const BString& directoryName)
84 if (location < 0 || location >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
85 || directoryName.IsEmpty()) {
86 return B_BAD_VALUE;
89 fLocation = location;
90 fChangeCount = changeCount;
91 fTransactionDirectoryName = directoryName;
92 fPackagesToActivate.MakeEmpty();
93 fPackagesToDeactivate.MakeEmpty();
95 return B_OK;
99 BPackageInstallationLocation
100 BActivationTransaction::Location() const
102 return fLocation;
106 void
107 BActivationTransaction::SetLocation(BPackageInstallationLocation location)
109 fLocation = location;
113 int64
114 BActivationTransaction::ChangeCount() const
116 return fChangeCount;
120 void
121 BActivationTransaction::SetChangeCount(int64 changeCount)
123 fChangeCount = changeCount;
127 const BString&
128 BActivationTransaction::TransactionDirectoryName() const
130 return fTransactionDirectoryName;
134 void
135 BActivationTransaction::SetTransactionDirectoryName(
136 const BString& directoryName)
138 fTransactionDirectoryName = directoryName;
142 const BStringList&
143 BActivationTransaction::PackagesToActivate() const
145 return fPackagesToActivate;
149 bool
150 BActivationTransaction::SetPackagesToActivate(const BStringList& packages)
152 fPackagesToActivate = packages;
153 return fPackagesToActivate.CountStrings() == packages.CountStrings();
157 bool
158 BActivationTransaction::AddPackageToActivate(const BString& package)
160 return fPackagesToActivate.Add(package);
164 const BStringList&
165 BActivationTransaction::PackagesToDeactivate() const
167 return fPackagesToDeactivate;
171 bool
172 BActivationTransaction::SetPackagesToDeactivate(const BStringList& packages)
174 fPackagesToDeactivate = packages;
175 return fPackagesToDeactivate.CountStrings() == packages.CountStrings();
179 bool
180 BActivationTransaction::AddPackageToDeactivate(const BString& package)
182 return fPackagesToDeactivate.Add(package);
186 status_t
187 BActivationTransaction::Archive(BMessage* archive, bool deep) const
189 status_t error = BArchivable::Archive(archive, deep);
190 if (error != B_OK)
191 return error;
193 if ((error = archive->AddInt32("location", fLocation)) != B_OK
194 || (error = archive->AddInt64("change count", fChangeCount)) != B_OK
195 || (error = archive->AddString("transaction",
196 fTransactionDirectoryName)) != B_OK
197 || (error = archive->AddStrings("activate", fPackagesToActivate))
198 != B_OK
199 || (error = archive->AddStrings("deactivate", fPackagesToDeactivate))
200 != B_OK) {
201 return error;
204 return B_OK;
208 /*static*/ BArchivable*
209 BActivationTransaction::Instantiate(BMessage* archive)
211 if (validate_instantiation(archive, "BActivationTransaction"))
212 return new(std::nothrow) BActivationTransaction(archive);
213 return NULL;
217 /*static*/ status_t
218 BActivationTransaction::_ExtractStringList(BMessage* archive, const char* field,
219 BStringList& _list)
221 status_t error = archive->FindStrings(field, &_list);
222 return error == B_NAME_NOT_FOUND ? B_OK : error;
223 // If the field doesn't exist, that's OK.
227 } // namespace BPrivate
228 } // namespace BPackageKit