1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34
35//	Icon cache is used for drawing node icons; it caches icons
36//	and reuses them for successive draws
37
38#include <Debug.h>
39
40#include "PoseList.h"
41
42
43BPose*
44PoseList::FindPose(const node_ref* node, int32* resultingIndex) const
45{
46	int32 count = CountItems();
47	for (int32 index = 0; index < count; index++) {
48		BPose* pose = ItemAt(index);
49		ASSERT(pose->TargetModel());
50		if (*pose->TargetModel()->NodeRef() == *node) {
51			if (resultingIndex != NULL)
52				*resultingIndex = index;
53
54			return pose;
55		}
56	}
57
58	return NULL;
59}
60
61
62BPose*
63PoseList::FindPose(const entry_ref* entry, int32* resultingIndex) const
64{
65	int32 count = CountItems();
66	for (int32 index = 0; index < count; index++) {
67		BPose* pose = ItemAt(index);
68		ASSERT(pose->TargetModel());
69		if (*pose->TargetModel()->EntryRef() == *entry) {
70			if (resultingIndex != NULL)
71				*resultingIndex = index;
72
73			return pose;
74		}
75	}
76	return NULL;
77}
78
79
80BPose*
81PoseList::FindPose(const Model* model, int32* resultingIndex) const
82{
83	return FindPose(model->NodeRef(), resultingIndex);
84}
85
86
87BPose*
88PoseList::DeepFindPose(const node_ref* node, int32* resultingIndex) const
89{
90	int32 count = CountItems();
91	for (int32 index = 0; index < count; index++) {
92		BPose* pose = ItemAt(index);
93		Model* model = pose->TargetModel();
94		if (*model->NodeRef() == *node) {
95			if (resultingIndex != NULL)
96				*resultingIndex = index;
97
98			return pose;
99		}
100		// if model is a symlink, try matching node with the target
101		// of the link
102		if (model->IsSymLink()) {
103			model = model->LinkTo();
104			if (model != NULL && *model->NodeRef() == *node) {
105				if (resultingIndex != NULL)
106					*resultingIndex = index;
107
108				return pose;
109			}
110		}
111	}
112
113	return NULL;
114}
115
116
117PoseList*
118PoseList::FindAllPoses(const node_ref* node) const
119{
120	int32 count = CountItems();
121	PoseList *result = new PoseList(5, false);
122	for (int32 index = 0; index < count; index++) {
123		BPose *pose = ItemAt(index);
124		Model *model = pose->TargetModel();
125		if (*model->NodeRef() == *node) {
126			result->AddItem(pose, 0);
127			continue;
128		}
129
130		if (!model->IsSymLink())
131			continue;
132
133		model = model->LinkTo();
134		if (model != NULL && *model->NodeRef() == *node) {
135			result->AddItem(pose);
136			continue;
137		}
138
139		if (model == NULL) {
140			Model model(pose->TargetModel()->EntryRef(), true);
141			if (*model.NodeRef() == *node)
142				result->AddItem(pose);
143		}
144	}
145
146	return result;
147}
148
149
150BPose*
151PoseList::FindPoseByFileName(const char* name, int32* _index) const
152{
153	int32 count = CountItems();
154	for (int32 index = 0; index < count; index++) {
155		BPose* pose = ItemAt(index);
156		ASSERT(pose->TargetModel());
157		if (strcmp(pose->TargetModel()->EntryRef()->name, name) == 0) {
158			if (_index != NULL)
159				*_index = index;
160
161			return pose;
162		}
163	}
164
165	return NULL;
166}
167