xref: /haiku/src/servers/print/Transport.cpp (revision 62cb58a8ef1cf63329b2ab6277655c9a866697cd)
1*62cb58a8SIthamar R. Adema /*
2*62cb58a8SIthamar R. Adema  * Copyright 2008, Haiku. All rights reserved.
3*62cb58a8SIthamar R. Adema  * Distributed under the terms of the MIT License.
4*62cb58a8SIthamar R. Adema  *
5*62cb58a8SIthamar R. Adema  * Authors:
6*62cb58a8SIthamar R. Adema  *		Ithamar R. Adema
7*62cb58a8SIthamar R. Adema  */
8*62cb58a8SIthamar R. Adema 
9*62cb58a8SIthamar R. Adema #include "Transport.h"
10*62cb58a8SIthamar R. Adema 
11*62cb58a8SIthamar R. Adema // BeOS API
12*62cb58a8SIthamar R. Adema #include <PrintTransportAddOn.h>
13*62cb58a8SIthamar R. Adema #include <Application.h>
14*62cb58a8SIthamar R. Adema #include <image.h>
15*62cb58a8SIthamar R. Adema #include <Entry.h>
16*62cb58a8SIthamar R. Adema 
17*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
18*62cb58a8SIthamar R. Adema BObjectList<Transport> Transport::sTransports;
19*62cb58a8SIthamar R. Adema 
20*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
21*62cb58a8SIthamar R. Adema // Find [static]
22*62cb58a8SIthamar R. Adema //
23*62cb58a8SIthamar R. Adema // Searches the static object list for a transport object with the
24*62cb58a8SIthamar R. Adema // specified name.
25*62cb58a8SIthamar R. Adema //
26*62cb58a8SIthamar R. Adema // Parameters:
27*62cb58a8SIthamar R. Adema //    name - Printer definition name we're looking for.
28*62cb58a8SIthamar R. Adema //
29*62cb58a8SIthamar R. Adema // Returns:
30*62cb58a8SIthamar R. Adema //    Pointer to Transport object, or NULL if not found.
31*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
32*62cb58a8SIthamar R. Adema Transport* Transport::Find(const BString& name)
33*62cb58a8SIthamar R. Adema {
34*62cb58a8SIthamar R. Adema 		// Look in list to find printer definition
35*62cb58a8SIthamar R. Adema 	for (int32 idx=0; idx < sTransports.CountItems(); idx++) {
36*62cb58a8SIthamar R. Adema 		if (name == sTransports.ItemAt(idx)->Name()) {
37*62cb58a8SIthamar R. Adema 			return sTransports.ItemAt(idx);
38*62cb58a8SIthamar R. Adema 		}
39*62cb58a8SIthamar R. Adema 	}
40*62cb58a8SIthamar R. Adema 
41*62cb58a8SIthamar R. Adema 		// None found, so return NULL
42*62cb58a8SIthamar R. Adema 	return NULL;
43*62cb58a8SIthamar R. Adema }
44*62cb58a8SIthamar R. Adema 
45*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
46*62cb58a8SIthamar R. Adema Transport* Transport::At(int32 idx)
47*62cb58a8SIthamar R. Adema {
48*62cb58a8SIthamar R. Adema 	return sTransports.ItemAt(idx);
49*62cb58a8SIthamar R. Adema }
50*62cb58a8SIthamar R. Adema 
51*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
52*62cb58a8SIthamar R. Adema void Transport::Remove(Transport* transport)
53*62cb58a8SIthamar R. Adema {
54*62cb58a8SIthamar R. Adema 	sTransports.RemoveItem(transport);
55*62cb58a8SIthamar R. Adema }
56*62cb58a8SIthamar R. Adema 
57*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
58*62cb58a8SIthamar R. Adema int32 Transport::CountTransports()
59*62cb58a8SIthamar R. Adema {
60*62cb58a8SIthamar R. Adema 	return sTransports.CountItems();
61*62cb58a8SIthamar R. Adema }
62*62cb58a8SIthamar R. Adema 
63*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
64*62cb58a8SIthamar R. Adema status_t Transport::Scan(directory_which which)
65*62cb58a8SIthamar R. Adema {
66*62cb58a8SIthamar R. Adema 	BDirectory dir;
67*62cb58a8SIthamar R. Adema 	status_t rc;
68*62cb58a8SIthamar R. Adema 	BPath path;
69*62cb58a8SIthamar R. Adema 
70*62cb58a8SIthamar R. Adema 	// Try to find specified transport addon directory
71*62cb58a8SIthamar R. Adema 	if ((rc=find_directory(which,&path)) != B_OK)
72*62cb58a8SIthamar R. Adema 		return rc;
73*62cb58a8SIthamar R. Adema 
74*62cb58a8SIthamar R. Adema 	if ((rc=path.Append("Print/transport")) != B_OK)
75*62cb58a8SIthamar R. Adema 		return rc;
76*62cb58a8SIthamar R. Adema 
77*62cb58a8SIthamar R. Adema 	if ((rc=dir.SetTo(path.Path())) != B_OK)
78*62cb58a8SIthamar R. Adema 		return rc;
79*62cb58a8SIthamar R. Adema 
80*62cb58a8SIthamar R. Adema 	// Walk over all entries in directory
81*62cb58a8SIthamar R. Adema 	BEntry entry;
82*62cb58a8SIthamar R. Adema 	while(dir.GetNextEntry(&entry) == B_OK) {
83*62cb58a8SIthamar R. Adema 		if (!entry.IsFile())
84*62cb58a8SIthamar R. Adema 			continue;
85*62cb58a8SIthamar R. Adema 
86*62cb58a8SIthamar R. Adema 		if (entry.GetPath(&path) != B_OK)
87*62cb58a8SIthamar R. Adema 			continue;
88*62cb58a8SIthamar R. Adema 
89*62cb58a8SIthamar R. Adema 		// If we have loaded the transport from a previous scanned directory,
90*62cb58a8SIthamar R. Adema 		// ignore this one.
91*62cb58a8SIthamar R. Adema 		if (Transport::Find(path.Leaf()) != NULL)
92*62cb58a8SIthamar R. Adema 			continue;
93*62cb58a8SIthamar R. Adema 
94*62cb58a8SIthamar R. Adema 		be_app->AddHandler(new Transport(path));
95*62cb58a8SIthamar R. Adema 	}
96*62cb58a8SIthamar R. Adema 
97*62cb58a8SIthamar R. Adema 	return B_OK;
98*62cb58a8SIthamar R. Adema 
99*62cb58a8SIthamar R. Adema }
100*62cb58a8SIthamar R. Adema 
101*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
102*62cb58a8SIthamar R. Adema // Transport [constructor]
103*62cb58a8SIthamar R. Adema //
104*62cb58a8SIthamar R. Adema // Initializes the transport object with data read from the
105*62cb58a8SIthamar R. Adema // attributes attached to the printer definition node.
106*62cb58a8SIthamar R. Adema //
107*62cb58a8SIthamar R. Adema // Parameters:
108*62cb58a8SIthamar R. Adema //    node - Printer definition node for this printer.
109*62cb58a8SIthamar R. Adema //
110*62cb58a8SIthamar R. Adema // Returns:
111*62cb58a8SIthamar R. Adema //    none.
112*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
113*62cb58a8SIthamar R. Adema Transport::Transport(const BPath& path)
114*62cb58a8SIthamar R. Adema 	: BHandler(B_EMPTY_STRING),
115*62cb58a8SIthamar R. Adema 	fName(path.Leaf()),
116*62cb58a8SIthamar R. Adema 	fImageID(-1),
117*62cb58a8SIthamar R. Adema 	fFeatures(0)
118*62cb58a8SIthamar R. Adema {
119*62cb58a8SIthamar R. Adema 	// Load transport addon
120*62cb58a8SIthamar R. Adema 	image_id id = ::load_add_on(path.Path());
121*62cb58a8SIthamar R. Adema 	if (id < B_OK)
122*62cb58a8SIthamar R. Adema 		return;
123*62cb58a8SIthamar R. Adema 
124*62cb58a8SIthamar R. Adema 	// Find transport_features symbol, to determine if we need to keep
125*62cb58a8SIthamar R. Adema 	// this transport loaded
126*62cb58a8SIthamar R. Adema 	int* transport_features_ptr;
127*62cb58a8SIthamar R. Adema 	if (get_image_symbol(id, B_TRANSPORT_FEATURES_SYMBOL,
128*62cb58a8SIthamar R. Adema 			B_SYMBOL_TYPE_DATA, (void**)&transport_features_ptr) != B_OK) {
129*62cb58a8SIthamar R. Adema 		unload_add_on(id);
130*62cb58a8SIthamar R. Adema 	} else {
131*62cb58a8SIthamar R. Adema 		fFeatures = *transport_features_ptr;
132*62cb58a8SIthamar R. Adema 
133*62cb58a8SIthamar R. Adema 		if (*transport_features_ptr & B_TRANSPORT_SUPPORTS_PROBE) {
134*62cb58a8SIthamar R. Adema 			// Transport supports probing, so it needs to stay loaded...
135*62cb58a8SIthamar R. Adema 			printf("IRA: Transport %s supports probing!\n", path.Path());
136*62cb58a8SIthamar R. Adema 			fImageID = id;
137*62cb58a8SIthamar R. Adema 		}
138*62cb58a8SIthamar R. Adema 		else // No extended Transport support; so no need to keep loaded
139*62cb58a8SIthamar R. Adema 			::unload_add_on(id);
140*62cb58a8SIthamar R. Adema 	}
141*62cb58a8SIthamar R. Adema 
142*62cb58a8SIthamar R. Adema 	sTransports.AddItem(this);
143*62cb58a8SIthamar R. Adema }
144*62cb58a8SIthamar R. Adema 
145*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
146*62cb58a8SIthamar R. Adema Transport::~Transport()
147*62cb58a8SIthamar R. Adema {
148*62cb58a8SIthamar R. Adema 	sTransports.RemoveItem(this);
149*62cb58a8SIthamar R. Adema }
150*62cb58a8SIthamar R. Adema 
151*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
152*62cb58a8SIthamar R. Adema // MessageReceived
153*62cb58a8SIthamar R. Adema //
154*62cb58a8SIthamar R. Adema // Handle scripting messages.
155*62cb58a8SIthamar R. Adema //
156*62cb58a8SIthamar R. Adema // Parameters:
157*62cb58a8SIthamar R. Adema //    msg - message.
158*62cb58a8SIthamar R. Adema // ---------------------------------------------------------------
159*62cb58a8SIthamar R. Adema void Transport::MessageReceived(BMessage* msg)
160*62cb58a8SIthamar R. Adema {
161*62cb58a8SIthamar R. Adema 	switch(msg->what) {
162*62cb58a8SIthamar R. Adema 		case B_GET_PROPERTY:
163*62cb58a8SIthamar R. Adema 		case B_SET_PROPERTY:
164*62cb58a8SIthamar R. Adema 		case B_CREATE_PROPERTY:
165*62cb58a8SIthamar R. Adema 		case B_DELETE_PROPERTY:
166*62cb58a8SIthamar R. Adema 		case B_COUNT_PROPERTIES:
167*62cb58a8SIthamar R. Adema 		case B_EXECUTE_PROPERTY:
168*62cb58a8SIthamar R. Adema 			HandleScriptingCommand(msg);
169*62cb58a8SIthamar R. Adema 			break;
170*62cb58a8SIthamar R. Adema 
171*62cb58a8SIthamar R. Adema 		default:
172*62cb58a8SIthamar R. Adema 			Inherited::MessageReceived(msg);
173*62cb58a8SIthamar R. Adema 	}
174*62cb58a8SIthamar R. Adema }
175