xref: /haiku/src/kits/package/ActivationTransaction.cpp (revision 5ac9b506412b11afb993bb52d161efe7666958a5)
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 
21 BActivationTransaction::BActivationTransaction()
22 	:
23 	fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
24 	fChangeCount(0),
25 	fTransactionDirectoryName(),
26 	fPackagesToActivate(),
27 	fPackagesToDeactivate()
28 {
29 }
30 
31 
32 BActivationTransaction::BActivationTransaction(BMessage* archive,
33 	status_t* _error)
34 	:
35 	fLocation(B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT),
36 	fChangeCount(0),
37 	fTransactionDirectoryName(),
38 	fPackagesToActivate(),
39 	fPackagesToDeactivate()
40 {
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;
56 	}
57 
58 	if (_error != NULL)
59 		*_error = error;
60 }
61 
62 
63 BActivationTransaction::~BActivationTransaction()
64 {
65 }
66 
67 
68 status_t
69 BActivationTransaction::InitCheck() const
70 {
71 	if (fLocation < 0 || fLocation >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
72 		|| fTransactionDirectoryName.IsEmpty()
73 		|| (fPackagesToActivate.IsEmpty() && fPackagesToDeactivate.IsEmpty())) {
74 		return B_BAD_VALUE;
75 	}
76 	return B_OK;
77 }
78 
79 
80 status_t
81 BActivationTransaction::SetTo(BPackageInstallationLocation location,
82 	int64 changeCount, const BString& directoryName)
83 {
84 	if (location < 0 || location >= B_PACKAGE_INSTALLATION_LOCATION_ENUM_COUNT
85 		|| directoryName.IsEmpty()) {
86 		return B_BAD_VALUE;
87 	}
88 
89 	fLocation = location;
90 	fChangeCount = changeCount;
91 	fTransactionDirectoryName = directoryName;
92 	fPackagesToActivate.MakeEmpty();
93 	fPackagesToDeactivate.MakeEmpty();
94 
95 	return B_OK;
96 }
97 
98 
99 BPackageInstallationLocation
100 BActivationTransaction::Location() const
101 {
102 	return fLocation;
103 }
104 
105 
106 void
107 BActivationTransaction::SetLocation(BPackageInstallationLocation location)
108 {
109 	fLocation = location;
110 }
111 
112 
113 int64
114 BActivationTransaction::ChangeCount() const
115 {
116 	return fChangeCount;
117 }
118 
119 
120 void
121 BActivationTransaction::SetChangeCount(int64 changeCount)
122 {
123 	fChangeCount = changeCount;
124 }
125 
126 
127 const BString&
128 BActivationTransaction::TransactionDirectoryName() const
129 {
130 	return fTransactionDirectoryName;
131 }
132 
133 
134 void
135 BActivationTransaction::SetTransactionDirectoryName(
136 	const BString& directoryName)
137 {
138 	fTransactionDirectoryName = directoryName;
139 }
140 
141 
142 const BStringList&
143 BActivationTransaction::PackagesToActivate() const
144 {
145 	return fPackagesToActivate;
146 }
147 
148 
149 bool
150 BActivationTransaction::SetPackagesToActivate(const BStringList& packages)
151 {
152 	fPackagesToActivate = packages;
153 	return fPackagesToActivate.CountStrings() == packages.CountStrings();
154 }
155 
156 
157 bool
158 BActivationTransaction::AddPackageToActivate(const BString& package)
159 {
160 	return fPackagesToActivate.Add(package);
161 }
162 
163 
164 const BStringList&
165 BActivationTransaction::PackagesToDeactivate() const
166 {
167 	return fPackagesToDeactivate;
168 }
169 
170 
171 bool
172 BActivationTransaction::SetPackagesToDeactivate(const BStringList& packages)
173 {
174 	fPackagesToDeactivate = packages;
175 	return fPackagesToDeactivate.CountStrings() == packages.CountStrings();
176 }
177 
178 
179 bool
180 BActivationTransaction::AddPackageToDeactivate(const BString& package)
181 {
182 	return fPackagesToDeactivate.Add(package);
183 }
184 
185 
186 status_t
187 BActivationTransaction::Archive(BMessage* archive, bool deep) const
188 {
189 	status_t error = BArchivable::Archive(archive, deep);
190 	if (error != B_OK)
191 		return error;
192 
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;
202 	}
203 
204 	return B_OK;
205 }
206 
207 
208 /*static*/ BArchivable*
209 BActivationTransaction::Instantiate(BMessage* archive)
210 {
211 	if (validate_instantiation(archive, "BActivationTransaction"))
212 		return new(std::nothrow) BActivationTransaction(archive);
213 	return NULL;
214 }
215 
216 
217 /*static*/ status_t
218 BActivationTransaction::_ExtractStringList(BMessage* archive, const char* field,
219 	BStringList& _list)
220 {
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.
224 }
225 
226 
227 }	// namespace BPrivate
228 }	// namespace BPackageKit
229