xref: /haiku/src/servers/print/Transport.cpp (revision 9abbe87f8cf1d2b2e5b00c77240c94db52a3c3b7)
162cb58a8SIthamar R. Adema /*
262cb58a8SIthamar R. Adema  * Copyright 2008, Haiku. All rights reserved.
362cb58a8SIthamar R. Adema  * Distributed under the terms of the MIT License.
462cb58a8SIthamar R. Adema  *
562cb58a8SIthamar R. Adema  * Authors:
662cb58a8SIthamar R. Adema  *		Ithamar R. Adema
762cb58a8SIthamar R. Adema  */
862cb58a8SIthamar R. Adema 
962cb58a8SIthamar R. Adema #include "Transport.h"
1062cb58a8SIthamar R. Adema 
1162cb58a8SIthamar R. Adema // BeOS API
1262cb58a8SIthamar R. Adema #include <PrintTransportAddOn.h>
1362cb58a8SIthamar R. Adema #include <Application.h>
1462cb58a8SIthamar R. Adema #include <image.h>
1562cb58a8SIthamar R. Adema #include <Entry.h>
1662cb58a8SIthamar R. Adema 
17*9abbe87fSMichael Pfeiffer 
1862cb58a8SIthamar R. Adema BObjectList<Transport> Transport::sTransports;
1962cb58a8SIthamar R. Adema 
20*9abbe87fSMichael Pfeiffer 
2162cb58a8SIthamar R. Adema // ---------------------------------------------------------------
2262cb58a8SIthamar R. Adema // Find [static]
2362cb58a8SIthamar R. Adema //
2462cb58a8SIthamar R. Adema // Searches the static object list for a transport object with the
2562cb58a8SIthamar R. Adema // specified name.
2662cb58a8SIthamar R. Adema //
2762cb58a8SIthamar R. Adema // Parameters:
2862cb58a8SIthamar R. Adema //    name - Printer definition name we're looking for.
2962cb58a8SIthamar R. Adema //
3062cb58a8SIthamar R. Adema // Returns:
3162cb58a8SIthamar R. Adema //    Pointer to Transport object, or NULL if not found.
3262cb58a8SIthamar R. Adema // ---------------------------------------------------------------
33*9abbe87fSMichael Pfeiffer Transport*
34*9abbe87fSMichael Pfeiffer Transport::Find(const BString& name)
3562cb58a8SIthamar R. Adema {
3662cb58a8SIthamar R. Adema 		// Look in list to find printer definition
3762cb58a8SIthamar R. Adema 	for (int32 idx=0; idx < sTransports.CountItems(); idx++) {
3862cb58a8SIthamar R. Adema 		if (name == sTransports.ItemAt(idx)->Name()) {
3962cb58a8SIthamar R. Adema 			return sTransports.ItemAt(idx);
4062cb58a8SIthamar R. Adema 		}
4162cb58a8SIthamar R. Adema 	}
4262cb58a8SIthamar R. Adema 
4362cb58a8SIthamar R. Adema 		// None found, so return NULL
4462cb58a8SIthamar R. Adema 	return NULL;
4562cb58a8SIthamar R. Adema }
4662cb58a8SIthamar R. Adema 
47*9abbe87fSMichael Pfeiffer 
48*9abbe87fSMichael Pfeiffer Transport*
49*9abbe87fSMichael Pfeiffer Transport::At(int32 idx)
5062cb58a8SIthamar R. Adema {
5162cb58a8SIthamar R. Adema 	return sTransports.ItemAt(idx);
5262cb58a8SIthamar R. Adema }
5362cb58a8SIthamar R. Adema 
54*9abbe87fSMichael Pfeiffer 
55*9abbe87fSMichael Pfeiffer void
56*9abbe87fSMichael Pfeiffer Transport::Remove(Transport* transport)
5762cb58a8SIthamar R. Adema {
5862cb58a8SIthamar R. Adema 	sTransports.RemoveItem(transport);
5962cb58a8SIthamar R. Adema }
6062cb58a8SIthamar R. Adema 
61*9abbe87fSMichael Pfeiffer 
62*9abbe87fSMichael Pfeiffer int32
63*9abbe87fSMichael Pfeiffer Transport::CountTransports()
6462cb58a8SIthamar R. Adema {
6562cb58a8SIthamar R. Adema 	return sTransports.CountItems();
6662cb58a8SIthamar R. Adema }
6762cb58a8SIthamar R. Adema 
68*9abbe87fSMichael Pfeiffer 
69*9abbe87fSMichael Pfeiffer status_t
70*9abbe87fSMichael Pfeiffer Transport::Scan(directory_which which)
7162cb58a8SIthamar R. Adema {
7262cb58a8SIthamar R. Adema 	BDirectory dir;
7362cb58a8SIthamar R. Adema 	status_t rc;
7462cb58a8SIthamar R. Adema 	BPath path;
7562cb58a8SIthamar R. Adema 
7662cb58a8SIthamar R. Adema 	// Try to find specified transport addon directory
7762cb58a8SIthamar R. Adema 	if ((rc=find_directory(which,&path)) != B_OK)
7862cb58a8SIthamar R. Adema 		return rc;
7962cb58a8SIthamar R. Adema 
8062cb58a8SIthamar R. Adema 	if ((rc=path.Append("Print/transport")) != B_OK)
8162cb58a8SIthamar R. Adema 		return rc;
8262cb58a8SIthamar R. Adema 
8362cb58a8SIthamar R. Adema 	if ((rc=dir.SetTo(path.Path())) != B_OK)
8462cb58a8SIthamar R. Adema 		return rc;
8562cb58a8SIthamar R. Adema 
8662cb58a8SIthamar R. Adema 	// Walk over all entries in directory
8762cb58a8SIthamar R. Adema 	BEntry entry;
8862cb58a8SIthamar R. Adema 	while(dir.GetNextEntry(&entry) == B_OK) {
8962cb58a8SIthamar R. Adema 		if (!entry.IsFile())
9062cb58a8SIthamar R. Adema 			continue;
9162cb58a8SIthamar R. Adema 
9262cb58a8SIthamar R. Adema 		if (entry.GetPath(&path) != B_OK)
9362cb58a8SIthamar R. Adema 			continue;
9462cb58a8SIthamar R. Adema 
9562cb58a8SIthamar R. Adema 		// If we have loaded the transport from a previous scanned directory,
9662cb58a8SIthamar R. Adema 		// ignore this one.
9762cb58a8SIthamar R. Adema 		if (Transport::Find(path.Leaf()) != NULL)
9862cb58a8SIthamar R. Adema 			continue;
9962cb58a8SIthamar R. Adema 
10062cb58a8SIthamar R. Adema 		be_app->AddHandler(new Transport(path));
10162cb58a8SIthamar R. Adema 	}
10262cb58a8SIthamar R. Adema 
10362cb58a8SIthamar R. Adema 	return B_OK;
10462cb58a8SIthamar R. Adema 
10562cb58a8SIthamar R. Adema }
10662cb58a8SIthamar R. Adema 
107*9abbe87fSMichael Pfeiffer 
10862cb58a8SIthamar R. Adema // ---------------------------------------------------------------
10962cb58a8SIthamar R. Adema // Transport [constructor]
11062cb58a8SIthamar R. Adema //
11162cb58a8SIthamar R. Adema // Initializes the transport object with data read from the
11262cb58a8SIthamar R. Adema // attributes attached to the printer definition node.
11362cb58a8SIthamar R. Adema //
11462cb58a8SIthamar R. Adema // Parameters:
11562cb58a8SIthamar R. Adema //    node - Printer definition node for this printer.
11662cb58a8SIthamar R. Adema //
11762cb58a8SIthamar R. Adema // Returns:
11862cb58a8SIthamar R. Adema //    none.
11962cb58a8SIthamar R. Adema // ---------------------------------------------------------------
12062cb58a8SIthamar R. Adema Transport::Transport(const BPath& path)
12162cb58a8SIthamar R. Adema 	: BHandler(B_EMPTY_STRING),
122eb5a25caSIthamar R. Adema 	fPath(path),
12362cb58a8SIthamar R. Adema 	fImageID(-1),
12462cb58a8SIthamar R. Adema 	fFeatures(0)
12562cb58a8SIthamar R. Adema {
12662cb58a8SIthamar R. Adema 	// Load transport addon
12762cb58a8SIthamar R. Adema 	image_id id = ::load_add_on(path.Path());
12862cb58a8SIthamar R. Adema 	if (id < B_OK)
12962cb58a8SIthamar R. Adema 		return;
13062cb58a8SIthamar R. Adema 
13162cb58a8SIthamar R. Adema 	// Find transport_features symbol, to determine if we need to keep
13262cb58a8SIthamar R. Adema 	// this transport loaded
13362cb58a8SIthamar R. Adema 	int* transport_features_ptr;
13462cb58a8SIthamar R. Adema 	if (get_image_symbol(id, B_TRANSPORT_FEATURES_SYMBOL,
13562cb58a8SIthamar R. Adema 			B_SYMBOL_TYPE_DATA, (void**)&transport_features_ptr) != B_OK) {
13662cb58a8SIthamar R. Adema 		unload_add_on(id);
13762cb58a8SIthamar R. Adema 	} else {
13862cb58a8SIthamar R. Adema 		fFeatures = *transport_features_ptr;
13962cb58a8SIthamar R. Adema 
140eb5a25caSIthamar R. Adema 		if (*transport_features_ptr & B_TRANSPORT_IS_HOTPLUG) {
141eb5a25caSIthamar R. Adema 			// We are hotpluggable; so keep us loaded!
14262cb58a8SIthamar R. Adema 			fImageID = id;
14362cb58a8SIthamar R. Adema 		}
14462cb58a8SIthamar R. Adema 		else // No extended Transport support; so no need to keep loaded
14562cb58a8SIthamar R. Adema 			::unload_add_on(id);
14662cb58a8SIthamar R. Adema 	}
14762cb58a8SIthamar R. Adema 
14862cb58a8SIthamar R. Adema 	sTransports.AddItem(this);
14962cb58a8SIthamar R. Adema }
15062cb58a8SIthamar R. Adema 
151*9abbe87fSMichael Pfeiffer 
15262cb58a8SIthamar R. Adema Transport::~Transport()
15362cb58a8SIthamar R. Adema {
15462cb58a8SIthamar R. Adema 	sTransports.RemoveItem(this);
15562cb58a8SIthamar R. Adema }
15662cb58a8SIthamar R. Adema 
157*9abbe87fSMichael Pfeiffer 
158*9abbe87fSMichael Pfeiffer status_t
159*9abbe87fSMichael Pfeiffer Transport::ListAvailablePorts(BMessage* msg)
160eb5a25caSIthamar R. Adema {
161eb5a25caSIthamar R. Adema 	status_t (*list_ports)(BMessage*);
162eb5a25caSIthamar R. Adema 	image_id id = fImageID;
163eb5a25caSIthamar R. Adema 	status_t rc = B_OK;
164eb5a25caSIthamar R. Adema 
165eb5a25caSIthamar R. Adema 	// Load image if not loaded yet
166eb5a25caSIthamar R. Adema 	if (id == -1 && (id=load_add_on(fPath.Path())) < 0)
167eb5a25caSIthamar R. Adema 		return id;
168eb5a25caSIthamar R. Adema 
169eb5a25caSIthamar R. Adema 	// Get pointer to addon function
170eb5a25caSIthamar R. Adema 	if ((rc=get_image_symbol(id, B_TRANSPORT_LIST_PORTS_SYMBOL,
171eb5a25caSIthamar R. Adema 			B_SYMBOL_TYPE_TEXT, (void**)&list_ports)) != B_OK)
172eb5a25caSIthamar R. Adema 		goto done;
173eb5a25caSIthamar R. Adema 
174eb5a25caSIthamar R. Adema 	// run addon...
175eb5a25caSIthamar R. Adema 	rc = (*list_ports)(msg);
176eb5a25caSIthamar R. Adema 
177eb5a25caSIthamar R. Adema done:
178eb5a25caSIthamar R. Adema 	// clean up if needed
179eb5a25caSIthamar R. Adema 	if (fImageID != id)
180eb5a25caSIthamar R. Adema 		unload_add_on(id);
181eb5a25caSIthamar R. Adema 
182eb5a25caSIthamar R. Adema 	return rc;
183eb5a25caSIthamar R. Adema }
184eb5a25caSIthamar R. Adema 
185*9abbe87fSMichael Pfeiffer 
186eb5a25caSIthamar R. Adema // ---------------------------------------------------------------
18762cb58a8SIthamar R. Adema // MessageReceived
18862cb58a8SIthamar R. Adema //
18962cb58a8SIthamar R. Adema // Handle scripting messages.
19062cb58a8SIthamar R. Adema //
19162cb58a8SIthamar R. Adema // Parameters:
19262cb58a8SIthamar R. Adema //    msg - message.
19362cb58a8SIthamar R. Adema // ---------------------------------------------------------------
194*9abbe87fSMichael Pfeiffer void
195*9abbe87fSMichael Pfeiffer Transport::MessageReceived(BMessage* msg)
19662cb58a8SIthamar R. Adema {
19762cb58a8SIthamar R. Adema 	switch(msg->what) {
19862cb58a8SIthamar R. Adema 		case B_GET_PROPERTY:
19962cb58a8SIthamar R. Adema 		case B_SET_PROPERTY:
20062cb58a8SIthamar R. Adema 		case B_CREATE_PROPERTY:
20162cb58a8SIthamar R. Adema 		case B_DELETE_PROPERTY:
20262cb58a8SIthamar R. Adema 		case B_COUNT_PROPERTIES:
20362cb58a8SIthamar R. Adema 		case B_EXECUTE_PROPERTY:
20462cb58a8SIthamar R. Adema 			HandleScriptingCommand(msg);
20562cb58a8SIthamar R. Adema 			break;
20662cb58a8SIthamar R. Adema 
20762cb58a8SIthamar R. Adema 		default:
20862cb58a8SIthamar R. Adema 			Inherited::MessageReceived(msg);
20962cb58a8SIthamar R. Adema 	}
21062cb58a8SIthamar R. Adema }
211