xref: /haiku/src/kits/package/ActivationTransaction.cpp (revision 3376ed1a72dc862faea22f98e4610bcf83760749)
1 /*
2  * Copyright 2013, 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 <package/ActivationTransaction.h>
11 
12 #include <new>
13 
14 #include <Message.h>
15 
16 
17 namespace BPackageKit {
18 namespace BPrivate {
19 
20 
BActivationTransaction()21 BActivationTransaction::BActivationTransaction()
22 	:
23 	fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
24 	fChangeCount(0),
25 	fTransactionDirectoryName(),
26 	fPackagesToActivate(),
27 	fPackagesToDeactivate(),
28 	fFirstBootProcessing(false)
29 {
30 }
31 
32 
BActivationTransaction(BMessage * archive,status_t * _error)33 BActivationTransaction::BActivationTransaction(BMessage* archive,
34 	status_t* _error)
35 	:
36 	fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
37 	fChangeCount(0),
38 	fTransactionDirectoryName(),
39 	fPackagesToActivate(),
40 	fPackagesToDeactivate(),
41 	fFirstBootProcessing(false)
42 {
43 	status_t error;
44 	int32 location;
45 
46 	if (archive->FindBool("first boot processing", &fFirstBootProcessing)
47 			!= B_OK)
48 		fFirstBootProcessing = false; // Field is optional for compatibility.
49 
50 	if ((error = archive->FindInt32("location", &location)) == B_OK
51 		&& (error = archive->FindInt64("change count", &fChangeCount)) == B_OK
52 		&& (error = archive->FindString("transaction",
53 			&fTransactionDirectoryName)) == B_OK
54 		&& (error = _ExtractStringList(archive, "activate",
55 			fPackagesToActivate)) == B_OK
56 		&& (error = _ExtractStringList(archive, "deactivate",
57 			fPackagesToDeactivate)) == B_OK) {
58 		if (location >= 0
59 			&& location <= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT) {
60 			fLocation = (BPackageInstallationLocation)location;
61 		} else
62 			error = B_BAD_VALUE;
63 	}
64 
65 	if (_error != NULL)
66 		*_error = error;
67 }
68 
69 
~BActivationTransaction()70 BActivationTransaction::~BActivationTransaction()
71 {
72 }
73 
74 
75 status_t
InitCheck() const76 BActivationTransaction::InitCheck() const
77 {
78 	if (fLocation < 0 || fLocation >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
79 		|| fTransactionDirectoryName.IsEmpty()
80 		|| (fPackagesToActivate.IsEmpty() && fPackagesToDeactivate.IsEmpty())) {
81 		return B_BAD_VALUE;
82 	}
83 	return B_OK;
84 }
85 
86 
87 status_t
SetTo(BPackageInstallationLocation location,int64 changeCount,const BString & directoryName)88 BActivationTransaction::SetTo(BPackageInstallationLocation location,
89 	int64 changeCount, const BString& directoryName)
90 {
91 	if (location < 0 || location >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
92 		|| directoryName.IsEmpty()) {
93 		return B_BAD_VALUE;
94 	}
95 
96 	fLocation = location;
97 	fChangeCount = changeCount;
98 	fTransactionDirectoryName = directoryName;
99 	fPackagesToActivate.MakeEmpty();
100 	fPackagesToDeactivate.MakeEmpty();
101 	fFirstBootProcessing = false;
102 
103 	return B_OK;
104 }
105 
106 
107 BPackageInstallationLocation
Location() const108 BActivationTransaction::Location() const
109 {
110 	return fLocation;
111 }
112 
113 
114 void
SetLocation(BPackageInstallationLocation location)115 BActivationTransaction::SetLocation(BPackageInstallationLocation location)
116 {
117 	fLocation = location;
118 }
119 
120 
121 int64
ChangeCount() const122 BActivationTransaction::ChangeCount() const
123 {
124 	return fChangeCount;
125 }
126 
127 
128 void
SetChangeCount(int64 changeCount)129 BActivationTransaction::SetChangeCount(int64 changeCount)
130 {
131 	fChangeCount = changeCount;
132 }
133 
134 
135 const BString&
TransactionDirectoryName() const136 BActivationTransaction::TransactionDirectoryName() const
137 {
138 	return fTransactionDirectoryName;
139 }
140 
141 
142 void
SetTransactionDirectoryName(const BString & directoryName)143 BActivationTransaction::SetTransactionDirectoryName(
144 	const BString& directoryName)
145 {
146 	fTransactionDirectoryName = directoryName;
147 }
148 
149 
150 const BStringList&
PackagesToActivate() const151 BActivationTransaction::PackagesToActivate() const
152 {
153 	return fPackagesToActivate;
154 }
155 
156 
157 bool
SetPackagesToActivate(const BStringList & packages)158 BActivationTransaction::SetPackagesToActivate(const BStringList& packages)
159 {
160 	fPackagesToActivate = packages;
161 	return fPackagesToActivate.CountStrings() == packages.CountStrings();
162 }
163 
164 
165 bool
AddPackageToActivate(const BString & package)166 BActivationTransaction::AddPackageToActivate(const BString& package)
167 {
168 	return fPackagesToActivate.Add(package);
169 }
170 
171 
172 const BStringList&
PackagesToDeactivate() const173 BActivationTransaction::PackagesToDeactivate() const
174 {
175 	return fPackagesToDeactivate;
176 }
177 
178 
179 bool
SetPackagesToDeactivate(const BStringList & packages)180 BActivationTransaction::SetPackagesToDeactivate(const BStringList& packages)
181 {
182 	fPackagesToDeactivate = packages;
183 	return fPackagesToDeactivate.CountStrings() == packages.CountStrings();
184 }
185 
186 
187 bool
AddPackageToDeactivate(const BString & package)188 BActivationTransaction::AddPackageToDeactivate(const BString& package)
189 {
190 	return fPackagesToDeactivate.Add(package);
191 }
192 
193 
194 bool
FirstBootProcessing() const195 BActivationTransaction::FirstBootProcessing() const
196 {
197 	return fFirstBootProcessing;
198 }
199 
200 
201 void
SetFirstBootProcessing(bool processingIsOn)202 BActivationTransaction::SetFirstBootProcessing(bool processingIsOn)
203 {
204 	fFirstBootProcessing = processingIsOn;
205 }
206 
207 
208 status_t
Archive(BMessage * archive,bool deep) const209 BActivationTransaction::Archive(BMessage* archive, bool deep) const
210 {
211 	status_t error = BArchivable::Archive(archive, deep);
212 	if (error != B_OK)
213 		return error;
214 
215 	if ((error = archive->AddInt32("location", fLocation)) != B_OK
216 		|| (error = archive->AddInt64("change count", fChangeCount)) != B_OK
217 		|| (error = archive->AddString("transaction",
218 			fTransactionDirectoryName)) != B_OK
219 		|| (error = archive->AddStrings("activate", fPackagesToActivate))
220 			!= B_OK
221 		|| (error = archive->AddStrings("deactivate", fPackagesToDeactivate))
222 			!= B_OK
223 		|| (error = archive->AddBool("first boot processing",
224 			fFirstBootProcessing)) != B_OK) {
225 		return error;
226 	}
227 
228 	return B_OK;
229 }
230 
231 
232 /*static*/ BArchivable*
Instantiate(BMessage * archive)233 BActivationTransaction::Instantiate(BMessage* archive)
234 {
235 	if (validate_instantiation(archive, "BActivationTransaction"))
236 		return new(std::nothrow) BActivationTransaction(archive);
237 	return NULL;
238 }
239 
240 
241 /*static*/ status_t
_ExtractStringList(BMessage * archive,const char * field,BStringList & _list)242 BActivationTransaction::_ExtractStringList(BMessage* archive, const char* field,
243 	BStringList& _list)
244 {
245 	status_t error = archive->FindStrings(field, &_list);
246 	return error == B_NAME_NOT_FOUND ? B_OK : error;
247 		// If the field doesn't exist, that's OK.
248 }
249 
250 
251 }	// namespace BPrivate
252 }	// namespace BPackageKit
253