1// ServerInfo.cpp
2
3#include "RequestFlattener.h"
4#include "RequestUnflattener.h"
5#include "ServerInfo.h"
6
7// VisitString
8static
9void
10VisitString(RequestMember* member, RequestMemberVisitor* visitor,
11	HashString& string)
12{
13	StringData stringData;
14	stringData.SetTo(string.GetString());
15	visitor->Visit(member, stringData);
16}
17
18
19// #pragma mark -
20// #pragma mark ----- ShareInfo -----
21
22// constructor
23ShareInfo::ShareInfo()
24	: FlattenableRequestMember(),
25	  fShareName()
26{
27}
28
29// IsValid
30bool
31ShareInfo::IsValid() const
32{
33	return (fShareName.GetLength() > 0);
34}
35
36// ShowAround
37void
38ShareInfo::ShowAround(RequestMemberVisitor* visitor)
39{
40	VisitString(this, visitor, fShareName);
41}
42
43// Flatten
44status_t
45ShareInfo::Flatten(RequestFlattener* flattener)
46{
47	return flattener->WriteString(fShareName.GetString());
48}
49
50// Unflatten
51status_t
52ShareInfo::Unflatten(RequestUnflattener* unflattener)
53{
54	return unflattener->ReadString(fShareName);
55}
56
57// SetShareName
58status_t
59ShareInfo::SetShareName(const char* shareName)
60{
61	return (fShareName.SetTo(shareName) ? B_OK : B_NO_MEMORY);
62}
63
64// GetShareName
65const char*
66ShareInfo::GetShareName() const
67{
68	return fShareName.GetString();
69}
70
71
72// #pragma mark -
73// #pragma mark ----- ServerInfo -----
74
75// constructor
76ServerInfo::ServerInfo()
77	: FlattenableRequestMember(),
78	  fServerName(),
79	  fConnectionMethod(),
80	  fShareInfos()
81{
82}
83
84// constructor
85ServerInfo::ServerInfo(const ServerInfo& other)
86	: FlattenableRequestMember(),
87	  fServerName(),
88	  fConnectionMethod(),
89	  fShareInfos()
90{
91	(*this) = other;
92}
93
94// ShowAround
95void
96ServerInfo::ShowAround(RequestMemberVisitor* visitor)
97{
98	VisitString(this, visitor, fServerName);
99	VisitString(this, visitor, fConnectionMethod);
100	int32 count = fShareInfos.Count();
101	visitor->Visit(this, count);
102	for (int32 i = 0; i < count; i++)
103		visitor->Visit(this, fShareInfos.ElementAt(i));
104}
105
106// Flatten
107status_t
108ServerInfo::Flatten(RequestFlattener* flattener)
109{
110	flattener->WriteString(fServerName.GetString());
111	flattener->WriteString(fConnectionMethod.GetString());
112
113	int32 count = fShareInfos.Count();
114	flattener->WriteInt32(count);
115	for (int32 i = 0; i < count; i++)
116		flattener->Visit(this, fShareInfos.ElementAt(i));
117
118	return flattener->GetStatus();
119}
120
121// Unflatten
122status_t
123ServerInfo::Unflatten(RequestUnflattener* unflattener)
124{
125	unflattener->ReadString(fServerName);
126	unflattener->ReadString(fConnectionMethod);
127
128	int32 count;
129	if (unflattener->ReadInt32(count) != B_OK)
130		return unflattener->GetStatus();
131
132	for (int32 i = 0; i < count; i++) {
133		ShareInfo info;
134		unflattener->Visit(this, info);
135		if (info.IsValid())
136			AddShare(info.GetShareName());
137	}
138
139	return unflattener->GetStatus();
140}
141
142// SetServerName
143status_t
144ServerInfo::SetServerName(const char* serverName)
145{
146	return (fServerName.SetTo(serverName) ? B_OK : B_NO_MEMORY);
147}
148
149// GetServerName
150const char*
151ServerInfo::GetServerName() const
152{
153	return fServerName.GetString();
154}
155
156// SetConnectionMethod
157status_t
158ServerInfo::SetConnectionMethod(const char* connectionMethod)
159{
160	return (fConnectionMethod.SetTo(connectionMethod) ? B_OK : B_NO_MEMORY);
161}
162
163// GetConnectionMethod
164const char*
165ServerInfo::GetConnectionMethod() const
166{
167	return fConnectionMethod.GetString();
168}
169
170// AddShare
171status_t
172ServerInfo::AddShare(const char* shareName)
173{
174	ShareInfo shareInfo;
175	status_t error = shareInfo.SetShareName(shareName);
176	if (error == B_OK)
177		error = fShareInfos.PushBack(shareInfo);
178	return error;
179}
180
181// CountShares
182int32
183ServerInfo::CountShares() const
184{
185	return fShareInfos.Count();
186}
187
188// ShareInfoAt
189const ShareInfo&
190ServerInfo::ShareInfoAt(int32 index) const
191{
192	return fShareInfos.ElementAt(index);
193}
194
195// =
196ServerInfo&
197ServerInfo::operator=(const ServerInfo& other)
198{
199	fServerName = other.fServerName;
200	fConnectionMethod = other.fConnectionMethod;
201	fShareInfos.MakeEmpty();
202	int32 count = other.fShareInfos.Count();
203	for (int32 i = 0; i < count; i++)
204		fShareInfos.PushBack(other.fShareInfos.ElementAt(i));
205	return *this;
206}
207
208