1 //------------------------------------------------------------------------------
2 // Copyright (c) 2001-2005, Haiku
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
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 //------------------------------------------------------------------------------
31 #include "AppInfoList.h"
32 #include "RosterAppInfo.h"
36 \brief A list of RosterAppInfos.
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.
46 /*! \brief Creates an empty list.
48 AppInfoList::AppInfoList()
54 /*! \brief Frees all resources associated with this object.
56 The RosterAppInfos the list contains are deleted.
58 AppInfoList::~AppInfoList()
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.
71 AppInfoList::AddInfo(RosterAppInfo
*info
)
75 result
= fInfos
.AddItem(info
);
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.
85 AppInfoList::RemoveInfo(RosterAppInfo
*info
)
87 return fInfos
.RemoveItem(info
);
91 /*! \brief Removes all RosterAppInfos from the list.
94 AppInfoList::MakeEmpty(bool deleteInfos
)
97 for (int32 i
= 0; RosterAppInfo
*info
= InfoAt(i
); i
++)
105 /*! \brief Returns the RosterAppInfo with the supplied signature.
107 If the list contains more than one RosterAppInfo with the given signature,
108 it is undefined, which one is returned.
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
116 AppInfoList::InfoFor(const char *signature
) const
118 return InfoAt(IndexOf(signature
));
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.
128 AppInfoList::InfoFor(team_id team
) const
130 return InfoAt(IndexOf(team
));
134 /*! \brief Returns the RosterAppInfo with the supplied entry_ref.
136 If the list contains more than one RosterAppInfo with the given entry_ref,
137 it is undefined, which one is returned.
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
145 AppInfoList::InfoFor(const entry_ref
*ref
) const
147 return InfoAt(IndexOf(ref
));
151 /*! \brief Returns the RosterAppInfo with the supplied token.
153 If the list contains more than one RosterAppInfo with the given token,
154 it is undefined, which one is returned.
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.
161 AppInfoList::InfoForToken(uint32 token
) const
163 return InfoAt(IndexOfToken(token
));
167 /*! \brief Returns the number of RosterAppInfos this list contains.
168 \return The number of RosterAppInfos this list contains.
171 AppInfoList::CountInfos() const
173 return fInfos
.CountItems();
177 /*! \brief Returns a list iterator.
178 \return A list iterator.
180 AppInfoList::Iterator
183 return Iterator(this, 0);
187 /*! \brief Sorts the infos in ascending order according to the given compare
189 \param lessFunc The compare function (less than) to be used.
193 bool (*lessFunc
)(const RosterAppInfo
*, const RosterAppInfo
*))
195 int32 count
= CountInfos();
197 RosterAppInfo
**infos
= (RosterAppInfo
**)fInfos
.Items();
198 std::sort(infos
, infos
+ count
, lessFunc
);
203 /*! \brief Removes a RosterAppInfo at a given index.
204 \param index The index of the info to be removed
205 \return A pointer to the removed RosterAppInfo, or \c NULL, if the index
209 AppInfoList::RemoveInfo(int32 index
)
211 return (RosterAppInfo
*)fInfos
.RemoveItem(index
);
215 /*! \brief Returns a RosterAppInfo at a given index.
216 \param index The index of the info to be returned
217 \return A pointer to the RosterAppInfo, or \c NULL, if the index
221 AppInfoList::InfoAt(int32 index
) const
223 return (RosterAppInfo
*)fInfos
.ItemAt(index
);
227 /*! \brief Returns the list index of the supplied RosterAppInfo.
228 \param info The RosterAppInfo in question
229 \return The index of the supplied info, or -1, if \a info is \c NULL or not
230 contained in the list.
233 AppInfoList::IndexOf(RosterAppInfo
*info
) const
235 return fInfos
.IndexOf(info
);
239 /*! \brief Returns the list index of a RosterAppInfo with the supplied
242 If the list contains more than one RosterAppInfo with the given signature,
243 it is undefined, which one is returned.
245 \param signature The signature
246 \return The index of the found RosterAppInfo, or -1, if \a signature is
247 \c NULL or the list doesn't contain an info with this signature.
250 AppInfoList::IndexOf(const char *signature
) const
253 for (int32 i
= 0; RosterAppInfo
*info
= InfoAt(i
); i
++) {
254 if (!strcasecmp(info
->signature
, signature
))
262 /*! \brief Returns the list index of a RosterAppInfo with the supplied
265 \param team The team ID
266 \return The index of the found RosterAppInfo, or -1, if the list doesn't
267 contain an info with this team ID.
270 AppInfoList::IndexOf(team_id team
) const
272 for (int32 i
= 0; RosterAppInfo
*info
= InfoAt(i
); i
++) {
273 if (info
->team
== team
)
280 /*! \brief Returns the list index of a RosterAppInfo with the supplied
283 If the list contains more than one RosterAppInfo with the given entry_ref,
284 it is undefined, which one is returned.
286 \param ref The entry_ref
287 \return The index of the found RosterAppInfo, or -1, if \a ref is
288 \c NULL or the list doesn't contain an info with this entry_ref.
291 AppInfoList::IndexOf(const entry_ref
*ref
) const
294 for (int32 i
= 0; RosterAppInfo
*info
= InfoAt(i
); i
++) {
295 if (info
->ref
== *ref
)
303 /*! \brief Returns the list index of a RosterAppInfo with the supplied
306 \param token The token
307 \return The index of the found RosterAppInfo, or -1, if the list doesn't
308 contain an info with this token.
311 AppInfoList::IndexOfToken(uint32 token
) const
313 for (int32 i
= 0; RosterAppInfo
*info
= InfoAt(i
); i
++) {
314 if (info
->token
== token
)