xref: /haiku/src/apps/cortex/RouteApp/LiveNodeIO.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // LiveNodeIO.cpp
33 
34 #include "LiveNodeIO.h"
35 #include "ImportContext.h"
36 #include "ExportContext.h"
37 #include "NodeSetIOContext.h"
38 #include "StringContent.h"
39 
40 #include "NodeManager.h"
41 
42 #include <Debug.h>
43 #include <MediaAddOn.h>
44 #include <MediaDefs.h>
45 #include <MediaRoster.h>
46 #include <Path.h>
47 
48 #include "route_app_io.h"
49 
50 __USE_CORTEX_NAMESPACE
51 
52 // -------------------------------------------------------- //
53 // *** ctor/dtor
54 // -------------------------------------------------------- //
55 
56 LiveNodeIO::~LiveNodeIO() {}
57 
58 LiveNodeIO::LiveNodeIO() :
59 	m_kind(0LL),
60 	m_exportValid(false) {}
61 
62 LiveNodeIO::LiveNodeIO(
63 	const NodeManager*			manager,
64 	const NodeSetIOContext*	context,
65 	media_node_id						node) :
66 	m_exportValid(false) {
67 
68 	status_t err;
69 	ASSERT(manager);
70 	ASSERT(context);
71 
72 	err = _get_node_signature(
73 		manager,
74 		context,
75 		node,
76 		m_key,
77 		m_name,
78 		m_kind);
79 	if(err < B_OK)
80 		return;
81 
82 	m_exportValid = true;
83 }
84 
85 // -------------------------------------------------------- //
86 // *** import operations
87 // -------------------------------------------------------- //
88 
89 // locate the referenced live node
90 status_t LiveNodeIO::getNode(
91 	const NodeManager*			manager,
92 	const NodeSetIOContext*	context,
93 	media_node_id*					outNode) const {
94 
95 	ASSERT(manager);
96 	ASSERT(context);
97 	status_t err;
98 
99 	if(hasKey()) {
100 		// match key against previously imported nodes
101 		err = context->getNodeFor(key(), outNode);
102 
103 		if(err < B_OK) {
104 			// match key against system nodes
105 			err = _match_system_node_key(key(), manager, outNode);
106 
107 			if(err < B_OK) {
108 				PRINT((
109 					"!!! No node found for key '%s'\n",
110 					key()));
111 				return B_NAME_NOT_FOUND;
112 			}
113 		}
114 	}
115 	else {
116 		err = _match_node_signature(
117 			name(),
118 			kind(),
119 			outNode);
120 		if(err < B_OK) {
121 			PRINT((
122 				"!!! No node found named '%s' with kinds %" B_PRId64 "\n",
123 				name(),
124 				kind()));
125 			return B_NAME_NOT_FOUND;
126 		}
127 	}
128 
129 	return B_OK;
130 }
131 
132 // -------------------------------------------------------- //
133 // *** document-type setup
134 // -------------------------------------------------------- //
135 
136 /*static*/
137 void LiveNodeIO::AddTo(
138 	XML::DocumentType*			docType) {
139 
140 	// map self
141 	docType->addMapping(new Mapping<LiveNodeIO>(_LIVE_NODE_ELEMENT));
142 }
143 
144 // -------------------------------------------------------- //
145 // *** IPersistent
146 // -------------------------------------------------------- //
147 
148 // -------------------------------------------------------- //
149 // EXPORT:
150 // -------------------------------------------------------- //
151 
152 void LiveNodeIO::xmlExportBegin(
153 	ExportContext&					context) const {
154 
155 	if(!m_exportValid) {
156 		context.reportError(
157 			"LiveNodeIO::xmlExportBegin():\n"
158 			"*** invalid ***\n");
159 		return;
160 	}
161 
162 	context.beginElement(_LIVE_NODE_ELEMENT);
163 }
164 
165 void LiveNodeIO::xmlExportAttributes(
166 	ExportContext&					context) const {
167 
168 	if(m_key.Length() > 0)
169 		context.writeAttr("key", m_key.String());
170 }
171 
172 void LiveNodeIO::xmlExportContent(
173 	ExportContext&					context) const {
174 
175 	if(m_name.Length() > 0) {
176 		context.beginContent();
177 
178 		_write_simple(_NAME_ELEMENT, m_name.String(), context);
179 		_write_node_kinds(m_kind, context);
180 	}
181 }
182 
183 void LiveNodeIO::xmlExportEnd(
184 	ExportContext&					context) const {
185 
186 	context.endElement();
187 }
188 
189 // -------------------------------------------------------- //
190 // IMPORT:
191 // -------------------------------------------------------- //
192 
193 void LiveNodeIO::xmlImportBegin(
194 	ImportContext&					context) {}
195 
196 void LiveNodeIO::xmlImportAttribute(
197 	const char*							key,
198 	const char*							value,
199 	ImportContext&					context) {
200 
201 	if(!strcmp(key, "key")) {
202 		m_key = value;
203 	}
204 	else {
205 		BString err;
206 		err << "LiveNodeIO: unknown attribute '" << key << "'\n";
207 		context.reportError(err.String());
208 	}
209 }
210 
211 void LiveNodeIO::xmlImportContent(
212 	const char*							data,
213 	uint32									length,
214 	ImportContext&					context) {}
215 
216 void LiveNodeIO::xmlImportChild(
217 	IPersistent*						child,
218 	ImportContext&					context) {
219 
220 	StringContent* obj = dynamic_cast<StringContent*>(child);
221 	if(!obj) {
222 		BString err;
223 		err << "LiveNodeIO: unexpected element '" <<
224 			context.element() << "'\n";
225 		context.reportError(err.String());
226 		return;
227 	}
228 
229 	if(!strcmp(context.element(), _NAME_ELEMENT))
230 		m_name = obj->content;
231 	else if(!strcmp(context.element(), _KIND_ELEMENT))
232 		_read_node_kind(m_kind, obj->content.String(), context);
233 	else {
234 		BString err;
235 		err << "LiveNodeIO: unexpected element '" <<
236 			context.element() << "'\n";
237 		context.reportError(err.String());
238 	}
239 
240 	delete child;
241 }
242 
243 void LiveNodeIO::xmlImportComplete(
244 	ImportContext&					context) {}
245 
246 // END -- LiveNodeIO.cpp --
247 
248