xref: /haiku/src/servers/registrar/AppInfoList.cpp (revision 5d9e40fe9252c8f9c5e5e41594545bfa4419fcc7)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
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 "AppInfoList.h"
28 #include "RosterAppInfo.h"
29 
30 /*!
31 	\class AppInfoList
32 	\brief A list of RosterAppInfos.
33 
34 	Features adding/removing of RosterAppInfos and method for finding
35 	infos by signature, team ID, entry_ref or token.
36 	The method It() returns an iterator, an instance of the basic
37 	AppInfoList::Iterator class.
38 */
39 
40 
41 // constructor
42 /*!	\brief Creates an empty list.
43 */
44 AppInfoList::AppInfoList()
45 		   : fInfos()
46 {
47 }
48 
49 // destructor
50 /*!	\brief Frees all resources associated with this object.
51 
52 	The RosterAppInfos the list contains are deleted.
53 */
54 AppInfoList::~AppInfoList()
55 {
56 	// delete all infos
57 	for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++)
58 		delete info;
59 }
60 
61 // AddInfo
62 /*!	\brief Adds a RosterAppInfos to the list.
63 	\param info The RosterAppInfo to be added
64 	\return \c true on success, false if \a info is \c NULL or there's not
65 			enough memory for this operation.
66 */
67 bool
68 AppInfoList::AddInfo(RosterAppInfo *info)
69 {
70 	bool result = false;
71 	if (info)
72 		result = fInfos.AddItem(info);
73 	return result;
74 }
75 
76 // RemoveInfo
77 /*!	\brief Removes a RosterAppInfos from the list.
78 	\param info The RosterAppInfo to be removed
79 	\return \c true on success, false if \a info was not in the list.
80 */
81 bool
82 AppInfoList::RemoveInfo(RosterAppInfo *info)
83 {
84 	return fInfos.RemoveItem(info);
85 }
86 
87 // MakeEmpty
88 /*!	\brief Removes all RosterAppInfos from the list.
89 */
90 void
91 AppInfoList::MakeEmpty()
92 {
93 	fInfos.MakeEmpty();
94 }
95 
96 // InfoFor
97 /*!	\brief Returns the RosterAppInfo with the supplied signature.
98 
99 	If the list contains more than one RosterAppInfo with the given signature,
100 	it is undefined, which one is returned.
101 
102 	\param signature The signature
103 	\return A RosterAppInfo with the supplied signature, or \c NULL, if
104 			\a signature is \c NULL or the list doesn't contain an info with
105 			this signature.
106 */
107 RosterAppInfo *
108 AppInfoList::InfoFor(const char *signature) const
109 {
110 	return InfoAt(IndexOf(signature));
111 }
112 
113 // InfoFor
114 /*!	\brief Returns the RosterAppInfo with the supplied team ID.
115 	\param team The team ID
116 	\return A RosterAppInfo with the supplied team ID, or \c NULL, if the list
117 			doesn't contain an info with this team ID.
118 */
119 RosterAppInfo *
120 AppInfoList::InfoFor(team_id team) const
121 {
122 	return InfoAt(IndexOf(team));
123 }
124 
125 // InfoFor
126 /*!	\brief Returns the RosterAppInfo with the supplied entry_ref.
127 
128 	If the list contains more than one RosterAppInfo with the given entry_ref,
129 	it is undefined, which one is returned.
130 
131 	\param ref The entry_ref
132 	\return A RosterAppInfo with the supplied entry_ref, or \c NULL, if
133 			\a ref is \c NULL or the list doesn't contain an info with
134 			this entry_ref.
135 */
136 RosterAppInfo *
137 AppInfoList::InfoFor(const entry_ref *ref) const
138 {
139 	return InfoAt(IndexOf(ref));
140 }
141 
142 // InfoForToken
143 /*!	\brief Returns the RosterAppInfo with the supplied token.
144 
145 	If the list contains more than one RosterAppInfo with the given token,
146 	it is undefined, which one is returned.
147 
148 	\param token The token
149 	\return A RosterAppInfo with the supplied token, or \c NULL, if the list
150 			doesn't contain an info with the token.
151 */
152 RosterAppInfo *
153 AppInfoList::InfoForToken(uint32 token) const
154 {
155 	return InfoAt(IndexOfToken(token));
156 }
157 
158 // CountInfos
159 /*!	\brief Returns the number of RosterAppInfos this list contains.
160 	\return The number of RosterAppInfos this list contains.
161 */
162 int32
163 AppInfoList::CountInfos() const
164 {
165 	return fInfos.CountItems();
166 }
167 
168 // It
169 /*!	\brief Returns a list iterator.
170 	\return A list iterator.
171 */
172 AppInfoList::Iterator
173 AppInfoList::It()
174 {
175 	return Iterator(this, 0);
176 }
177 
178 // RemoveInfo
179 /*!	\brief Removes a RosterAppInfo at a given index.
180 	\param index The index of the info to be removed
181 	\return A pointer to the removed RosterAppInfo, or \c NULL, if the index
182 			is out of range.
183 */
184 RosterAppInfo *
185 AppInfoList::RemoveInfo(int32 index)
186 {
187 	return (RosterAppInfo*)fInfos.RemoveItem(index);
188 }
189 
190 // InfoAt
191 /*!	\brief Returns a RosterAppInfo at a given index.
192 	\param index The index of the info to be returned
193 	\return A pointer to the RosterAppInfo, or \c NULL, if the index
194 			is out of range.
195 */
196 RosterAppInfo *
197 AppInfoList::InfoAt(int32 index) const
198 {
199 	return (RosterAppInfo*)fInfos.ItemAt(index);
200 }
201 
202 // IndexOf
203 /*!	\brief Returns the list index of the supplied RosterAppInfo.
204 	\param info The RosterAppInfo in question
205 	\return The index of the supplied info, or -1, if \a info is \c NULL or not
206 			contained in the list.
207 */
208 int32
209 AppInfoList::IndexOf(RosterAppInfo *info) const
210 {
211 	return fInfos.IndexOf(info);
212 }
213 
214 // IndexOf
215 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
216 		   signature.
217 
218 	If the list contains more than one RosterAppInfo with the given signature,
219 	it is undefined, which one is returned.
220 
221 	\param signature The signature
222 	\return The index of the found RosterAppInfo, or -1, if \a signature is
223 			\c NULL or the list doesn't contain an info with this signature.
224 */
225 int32
226 AppInfoList::IndexOf(const char *signature) const
227 {
228 	if (signature) {
229 		for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
230 			if (!strcmp(info->signature, signature))
231 				return i;
232 		}
233 	}
234 	return -1;
235 }
236 
237 // IndexOf
238 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
239 		   team ID.
240 
241 	\param team The team ID
242 	\return The index of the found RosterAppInfo, or -1, if the list doesn't
243 			contain an info with this team ID.
244 */
245 int32
246 AppInfoList::IndexOf(team_id team) const
247 {
248 	for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
249 		if (info->team == team)
250 			return i;
251 	}
252 	return -1;
253 }
254 
255 // IndexOf
256 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
257 		   entry_ref.
258 
259 	If the list contains more than one RosterAppInfo with the given entry_ref,
260 	it is undefined, which one is returned.
261 
262 	\param ref The entry_ref
263 	\return The index of the found RosterAppInfo, or -1, if \a ref is
264 			\c NULL or the list doesn't contain an info with this entry_ref.
265 */
266 int32
267 AppInfoList::IndexOf(const entry_ref *ref) const
268 {
269 	if (ref) {
270 		for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
271 			if (info->ref == *ref)
272 				return i;
273 		}
274 	}
275 	return -1;
276 }
277 
278 // IndexOfToken
279 /*!	\brief Returns the list index of a RosterAppInfo with the supplied
280 		   token.
281 
282 	\param token The token
283 	\return The index of the found RosterAppInfo, or -1, if the list doesn't
284 			contain an info with this token.
285 */
286 int32
287 AppInfoList::IndexOfToken(uint32 token) const
288 {
289 	for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
290 		if (info->token == token)
291 			return i;
292 	}
293 	return -1;
294 }
295 
296