xref: /haiku/src/servers/registrar/AppInfoList.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2005, Haiku
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		AppInfoList.cpp
23 //	Author:			Ingo Weinhold (bonefish@users.sf.net)
24 //	Description:	A helper class for TRoster. A list of RosterAppInfos.
25 //------------------------------------------------------------------------------
26 
27 #include <algorithm>
28 
29 #include <string.h>
30 
31 #include "AppInfoList.h"
32 #include "RosterAppInfo.h"
33 
34 /*!
35 	\class AppInfoList
36 	\brief A list of RosterAppInfos.
37 
38 	Features adding/removing of RosterAppInfos and method for finding
39 	infos by signature, team ID, entry_ref or token.
40 	The method It() returns an iterator, an instance of the basic
41 	AppInfoList::Iterator class.
42 */
43 
44 
45 // constructor
46 /*!	\brief Creates an empty list.
47 */
48 AppInfoList::AppInfoList()
49 		   : fInfos()
50 {
51 }
52 
53 // destructor
54 /*!	\brief Frees all resources associated with this object.
55 
56 	The RosterAppInfos the list contains are deleted.
57 */
58 AppInfoList::~AppInfoList()
59 {
60 	// delete all infos
61 	MakeEmpty(true);
62 }
63 
64 // AddInfo
65 /*!	\brief Adds a RosterAppInfos to the list.
66 	\param info The RosterAppInfo to be added
67 	\return \c true on success, false if \a info is \c NULL or there's not
68 			enough memory for this operation.
69 */
70 bool
71 AppInfoList::AddInfo(RosterAppInfo *info)
72 {
73 	bool result = false;
74 	if (info)
75 		result = fInfos.AddItem(info);
76 	return result;
77 }
78 
79 // RemoveInfo
80 /*!	\brief Removes a RosterAppInfos from the list.
81 	\param info The RosterAppInfo to be removed
82 	\return \c true on success, false if \a info was not in the list.
83 */
84 bool
85 AppInfoList::RemoveInfo(RosterAppInfo *info)
86 {
87 	return fInfos.RemoveItem(info);
88 }
89 
90 // MakeEmpty
91 /*!	\brief Removes all RosterAppInfos from the list.
92 */
93 void
94 AppInfoList::MakeEmpty(bool deleteInfos)
95 {
96 	if (deleteInfos) {
97 		for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++)
98 			delete info;
99 	}
100 
101 	fInfos.MakeEmpty();
102 }
103 
104 // InfoFor
105 /*!	\brief Returns the RosterAppInfo with the supplied signature.
106 
107 	If the list contains more than one RosterAppInfo with the given signature,
108 	it is undefined, which one is returned.
109 
110 	\param signature The signature
111 	\return A RosterAppInfo with the supplied signature, or \c NULL, if
112 			\a signature is \c NULL or the list doesn't contain an info with
113 			this signature.
114 */
115 RosterAppInfo *
116 AppInfoList::InfoFor(const char *signature) const
117 {
118 	return InfoAt(IndexOf(signature));
119 }
120 
121 // InfoFor
122 /*!	\brief Returns the RosterAppInfo with the supplied team ID.
123 	\param team The team ID
124 	\return A RosterAppInfo with the supplied team ID, or \c NULL, if the list
125 			doesn't contain an info with this team ID.
126 */
127 RosterAppInfo *
128 AppInfoList::InfoFor(team_id team) const
129 {
130 	return InfoAt(IndexOf(team));
131 }
132 
133 // InfoFor
134 /*!	\brief Returns the RosterAppInfo with the supplied entry_ref.
135 
136 	If the list contains more than one RosterAppInfo with the given entry_ref,
137 	it is undefined, which one is returned.
138 
139 	\param ref The entry_ref
140 	\return A RosterAppInfo with the supplied entry_ref, or \c NULL, if
141 			\a ref is \c NULL or the list doesn't contain an info with
142 			this entry_ref.
143 */
144 RosterAppInfo *
145 AppInfoList::InfoFor(const entry_ref *ref) const
146 {
147 	return InfoAt(IndexOf(ref));
148 }
149 
150 // InfoForToken
151 /*!	\brief Returns the RosterAppInfo with the supplied token.
152 
153 	If the list contains more than one RosterAppInfo with the given token,
154 	it is undefined, which one is returned.
155 
156 	\param token The token
157 	\return A RosterAppInfo with the supplied token, or \c NULL, if the list
158 			doesn't contain an info with the token.
159 */
160 RosterAppInfo *
161 AppInfoList::InfoForToken(uint32 token) const
162 {
163 	return InfoAt(IndexOfToken(token));
164 }
165 
166 // CountInfos
167 /*!	\brief Returns the number of RosterAppInfos this list contains.
168 	\return The number of RosterAppInfos this list contains.
169 */
170 int32
171 AppInfoList::CountInfos() const
172 {
173 	return fInfos.CountItems();
174 }
175 
176 // It
177 /*!	\brief Returns a list iterator.
178 	\return A list iterator.
179 */
180 AppInfoList::Iterator
181 AppInfoList::It()
182 {
183 	return Iterator(this, 0);
184 }
185 
186 // Sort
187 /*!	\brief Sorts the infos in ascending order according to the given compare
188 		   function.
189 	\param cmpFunc The compare function to be used.
190 */
191 void
192 AppInfoList::Sort(int (*cmpFunc)(const RosterAppInfo *, const RosterAppInfo *))
193 {
194 	int32 count = CountInfos();
195 	if (count > 1) {
196 		RosterAppInfo **infos = (RosterAppInfo **)fInfos.Items();
197 		std::sort(infos, infos + count, cmpFunc);
198 	}
199 }
200 
201 // RemoveInfo
202 /*!	\brief Removes a RosterAppInfo at a given index.
203 	\param index The index of the info to be removed
204 	\return A pointer to the removed RosterAppInfo, or \c NULL, if the index
205 			is out of range.
206 */
207 RosterAppInfo *
208 AppInfoList::RemoveInfo(int32 index)
209 {
210 	return (RosterAppInfo*)fInfos.RemoveItem(index);
211 }
212 
213 // InfoAt
214 /*!	\brief Returns a RosterAppInfo at a given index.
215 	\param index The index of the info to be returned
216 	\return A pointer to the RosterAppInfo, or \c NULL, if the index
217 			is out of range.
218 */
219 RosterAppInfo *
220 AppInfoList::InfoAt(int32 index) const
221 {
222 	return (RosterAppInfo*)fInfos.ItemAt(index);
223 }
224 
225 // IndexOf
226 /*!	\brief Returns the list index of the supplied RosterAppInfo.
227 	\param info The RosterAppInfo in question
228 	\return The index of the supplied info, or -1, if \a info is \c NULL or not
229 			contained in the list.
230 */
231 int32
232 AppInfoList::IndexOf(RosterAppInfo *info) const
233 {
234 	return fInfos.IndexOf(info);
235 }
236 
237 // IndexOf
238 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
239 		   signature.
240 
241 	If the list contains more than one RosterAppInfo with the given signature,
242 	it is undefined, which one is returned.
243 
244 	\param signature The signature
245 	\return The index of the found RosterAppInfo, or -1, if \a signature is
246 			\c NULL or the list doesn't contain an info with this signature.
247 */
248 int32
249 AppInfoList::IndexOf(const char *signature) const
250 {
251 	if (signature) {
252 		for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
253 			if (!strcasecmp(info->signature, signature))
254 				return i;
255 		}
256 	}
257 	return -1;
258 }
259 
260 // IndexOf
261 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
262 		   team ID.
263 
264 	\param team The team ID
265 	\return The index of the found RosterAppInfo, or -1, if the list doesn't
266 			contain an info with this team ID.
267 */
268 int32
269 AppInfoList::IndexOf(team_id team) const
270 {
271 	for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
272 		if (info->team == team)
273 			return i;
274 	}
275 	return -1;
276 }
277 
278 // IndexOf
279 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
280 		   entry_ref.
281 
282 	If the list contains more than one RosterAppInfo with the given entry_ref,
283 	it is undefined, which one is returned.
284 
285 	\param ref The entry_ref
286 	\return The index of the found RosterAppInfo, or -1, if \a ref is
287 			\c NULL or the list doesn't contain an info with this entry_ref.
288 */
289 int32
290 AppInfoList::IndexOf(const entry_ref *ref) const
291 {
292 	if (ref) {
293 		for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
294 			if (info->ref == *ref)
295 				return i;
296 		}
297 	}
298 	return -1;
299 }
300 
301 // IndexOfToken
302 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
303 		   token.
304 
305 	\param token The token
306 	\return The index of the found RosterAppInfo, or -1, if the list doesn't
307 			contain an info with this token.
308 */
309 int32
310 AppInfoList::IndexOfToken(uint32 token) const
311 {
312 	for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
313 		if (info->token == token)
314 			return i;
315 	}
316 	return -1;
317 }
318 
319