xref: /haiku/src/servers/print/Transport.cpp (revision eb5a25ca411a17399088806934ea4c4d01696a2f)
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 
1762cb58a8SIthamar R. Adema // ---------------------------------------------------------------
1862cb58a8SIthamar R. Adema BObjectList<Transport> Transport::sTransports;
1962cb58a8SIthamar R. Adema 
2062cb58a8SIthamar R. Adema // ---------------------------------------------------------------
2162cb58a8SIthamar R. Adema // Find [static]
2262cb58a8SIthamar R. Adema //
2362cb58a8SIthamar R. Adema // Searches the static object list for a transport object with the
2462cb58a8SIthamar R. Adema // specified name.
2562cb58a8SIthamar R. Adema //
2662cb58a8SIthamar R. Adema // Parameters:
2762cb58a8SIthamar R. Adema //    name - Printer definition name we're looking for.
2862cb58a8SIthamar R. Adema //
2962cb58a8SIthamar R. Adema // Returns:
3062cb58a8SIthamar R. Adema //    Pointer to Transport object, or NULL if not found.
3162cb58a8SIthamar R. Adema // ---------------------------------------------------------------
3262cb58a8SIthamar R. Adema Transport* Transport::Find(const BString& name)
3362cb58a8SIthamar R. Adema {
3462cb58a8SIthamar R. Adema 		// Look in list to find printer definition
3562cb58a8SIthamar R. Adema 	for (int32 idx=0; idx < sTransports.CountItems(); idx++) {
3662cb58a8SIthamar R. Adema 		if (name == sTransports.ItemAt(idx)->Name()) {
3762cb58a8SIthamar R. Adema 			return sTransports.ItemAt(idx);
3862cb58a8SIthamar R. Adema 		}
3962cb58a8SIthamar R. Adema 	}
4062cb58a8SIthamar R. Adema 
4162cb58a8SIthamar R. Adema 		// None found, so return NULL
4262cb58a8SIthamar R. Adema 	return NULL;
4362cb58a8SIthamar R. Adema }
4462cb58a8SIthamar R. Adema 
4562cb58a8SIthamar R. Adema // ---------------------------------------------------------------
4662cb58a8SIthamar R. Adema Transport* Transport::At(int32 idx)
4762cb58a8SIthamar R. Adema {
4862cb58a8SIthamar R. Adema 	return sTransports.ItemAt(idx);
4962cb58a8SIthamar R. Adema }
5062cb58a8SIthamar R. Adema 
5162cb58a8SIthamar R. Adema // ---------------------------------------------------------------
5262cb58a8SIthamar R. Adema void Transport::Remove(Transport* transport)
5362cb58a8SIthamar R. Adema {
5462cb58a8SIthamar R. Adema 	sTransports.RemoveItem(transport);
5562cb58a8SIthamar R. Adema }
5662cb58a8SIthamar R. Adema 
5762cb58a8SIthamar R. Adema // ---------------------------------------------------------------
5862cb58a8SIthamar R. Adema int32 Transport::CountTransports()
5962cb58a8SIthamar R. Adema {
6062cb58a8SIthamar R. Adema 	return sTransports.CountItems();
6162cb58a8SIthamar R. Adema }
6262cb58a8SIthamar R. Adema 
6362cb58a8SIthamar R. Adema // ---------------------------------------------------------------
6462cb58a8SIthamar R. Adema status_t Transport::Scan(directory_which which)
6562cb58a8SIthamar R. Adema {
6662cb58a8SIthamar R. Adema 	BDirectory dir;
6762cb58a8SIthamar R. Adema 	status_t rc;
6862cb58a8SIthamar R. Adema 	BPath path;
6962cb58a8SIthamar R. Adema 
7062cb58a8SIthamar R. Adema 	// Try to find specified transport addon directory
7162cb58a8SIthamar R. Adema 	if ((rc=find_directory(which,&path)) != B_OK)
7262cb58a8SIthamar R. Adema 		return rc;
7362cb58a8SIthamar R. Adema 
7462cb58a8SIthamar R. Adema 	if ((rc=path.Append("Print/transport")) != B_OK)
7562cb58a8SIthamar R. Adema 		return rc;
7662cb58a8SIthamar R. Adema 
7762cb58a8SIthamar R. Adema 	if ((rc=dir.SetTo(path.Path())) != B_OK)
7862cb58a8SIthamar R. Adema 		return rc;
7962cb58a8SIthamar R. Adema 
8062cb58a8SIthamar R. Adema 	// Walk over all entries in directory
8162cb58a8SIthamar R. Adema 	BEntry entry;
8262cb58a8SIthamar R. Adema 	while(dir.GetNextEntry(&entry) == B_OK) {
8362cb58a8SIthamar R. Adema 		if (!entry.IsFile())
8462cb58a8SIthamar R. Adema 			continue;
8562cb58a8SIthamar R. Adema 
8662cb58a8SIthamar R. Adema 		if (entry.GetPath(&path) != B_OK)
8762cb58a8SIthamar R. Adema 			continue;
8862cb58a8SIthamar R. Adema 
8962cb58a8SIthamar R. Adema 		// If we have loaded the transport from a previous scanned directory,
9062cb58a8SIthamar R. Adema 		// ignore this one.
9162cb58a8SIthamar R. Adema 		if (Transport::Find(path.Leaf()) != NULL)
9262cb58a8SIthamar R. Adema 			continue;
9362cb58a8SIthamar R. Adema 
9462cb58a8SIthamar R. Adema 		be_app->AddHandler(new Transport(path));
9562cb58a8SIthamar R. Adema 	}
9662cb58a8SIthamar R. Adema 
9762cb58a8SIthamar R. Adema 	return B_OK;
9862cb58a8SIthamar R. Adema 
9962cb58a8SIthamar R. Adema }
10062cb58a8SIthamar R. Adema 
10162cb58a8SIthamar R. Adema // ---------------------------------------------------------------
10262cb58a8SIthamar R. Adema // Transport [constructor]
10362cb58a8SIthamar R. Adema //
10462cb58a8SIthamar R. Adema // Initializes the transport object with data read from the
10562cb58a8SIthamar R. Adema // attributes attached to the printer definition node.
10662cb58a8SIthamar R. Adema //
10762cb58a8SIthamar R. Adema // Parameters:
10862cb58a8SIthamar R. Adema //    node - Printer definition node for this printer.
10962cb58a8SIthamar R. Adema //
11062cb58a8SIthamar R. Adema // Returns:
11162cb58a8SIthamar R. Adema //    none.
11262cb58a8SIthamar R. Adema // ---------------------------------------------------------------
11362cb58a8SIthamar R. Adema Transport::Transport(const BPath& path)
11462cb58a8SIthamar R. Adema 	: BHandler(B_EMPTY_STRING),
115*eb5a25caSIthamar R. Adema 	fPath(path),
11662cb58a8SIthamar R. Adema 	fImageID(-1),
11762cb58a8SIthamar R. Adema 	fFeatures(0)
11862cb58a8SIthamar R. Adema {
11962cb58a8SIthamar R. Adema 	// Load transport addon
12062cb58a8SIthamar R. Adema 	image_id id = ::load_add_on(path.Path());
12162cb58a8SIthamar R. Adema 	if (id < B_OK)
12262cb58a8SIthamar R. Adema 		return;
12362cb58a8SIthamar R. Adema 
12462cb58a8SIthamar R. Adema 	// Find transport_features symbol, to determine if we need to keep
12562cb58a8SIthamar R. Adema 	// this transport loaded
12662cb58a8SIthamar R. Adema 	int* transport_features_ptr;
12762cb58a8SIthamar R. Adema 	if (get_image_symbol(id, B_TRANSPORT_FEATURES_SYMBOL,
12862cb58a8SIthamar R. Adema 			B_SYMBOL_TYPE_DATA, (void**)&transport_features_ptr) != B_OK) {
12962cb58a8SIthamar R. Adema 		unload_add_on(id);
13062cb58a8SIthamar R. Adema 	} else {
13162cb58a8SIthamar R. Adema 		fFeatures = *transport_features_ptr;
13262cb58a8SIthamar R. Adema 
133*eb5a25caSIthamar R. Adema 		if (*transport_features_ptr & B_TRANSPORT_IS_HOTPLUG) {
134*eb5a25caSIthamar R. Adema 			// We are hotpluggable; so keep us loaded!
13562cb58a8SIthamar R. Adema 			fImageID = id;
13662cb58a8SIthamar R. Adema 		}
13762cb58a8SIthamar R. Adema 		else // No extended Transport support; so no need to keep loaded
13862cb58a8SIthamar R. Adema 			::unload_add_on(id);
13962cb58a8SIthamar R. Adema 	}
14062cb58a8SIthamar R. Adema 
14162cb58a8SIthamar R. Adema 	sTransports.AddItem(this);
14262cb58a8SIthamar R. Adema }
14362cb58a8SIthamar R. Adema 
14462cb58a8SIthamar R. Adema // ---------------------------------------------------------------
14562cb58a8SIthamar R. Adema Transport::~Transport()
14662cb58a8SIthamar R. Adema {
14762cb58a8SIthamar R. Adema 	sTransports.RemoveItem(this);
14862cb58a8SIthamar R. Adema }
14962cb58a8SIthamar R. Adema 
15062cb58a8SIthamar R. Adema // ---------------------------------------------------------------
151*eb5a25caSIthamar R. Adema status_t Transport::ListAvailablePorts(BMessage* msg)
152*eb5a25caSIthamar R. Adema {
153*eb5a25caSIthamar R. Adema 	status_t (*list_ports)(BMessage*);
154*eb5a25caSIthamar R. Adema 	image_id id = fImageID;
155*eb5a25caSIthamar R. Adema 	status_t rc = B_OK;
156*eb5a25caSIthamar R. Adema 
157*eb5a25caSIthamar R. Adema 	// Load image if not loaded yet
158*eb5a25caSIthamar R. Adema 	if (id == -1 && (id=load_add_on(fPath.Path())) < 0)
159*eb5a25caSIthamar R. Adema 		return id;
160*eb5a25caSIthamar R. Adema 
161*eb5a25caSIthamar R. Adema 	// Get pointer to addon function
162*eb5a25caSIthamar R. Adema 	if ((rc=get_image_symbol(id, B_TRANSPORT_LIST_PORTS_SYMBOL,
163*eb5a25caSIthamar R. Adema 			B_SYMBOL_TYPE_TEXT, (void**)&list_ports)) != B_OK)
164*eb5a25caSIthamar R. Adema 		goto done;
165*eb5a25caSIthamar R. Adema 
166*eb5a25caSIthamar R. Adema 	// run addon...
167*eb5a25caSIthamar R. Adema 	rc = (*list_ports)(msg);
168*eb5a25caSIthamar R. Adema 
169*eb5a25caSIthamar R. Adema done:
170*eb5a25caSIthamar R. Adema 	// clean up if needed
171*eb5a25caSIthamar R. Adema 	if (fImageID != id)
172*eb5a25caSIthamar R. Adema 		unload_add_on(id);
173*eb5a25caSIthamar R. Adema 
174*eb5a25caSIthamar R. Adema 	return rc;
175*eb5a25caSIthamar R. Adema }
176*eb5a25caSIthamar R. Adema 
177*eb5a25caSIthamar R. Adema // ---------------------------------------------------------------
17862cb58a8SIthamar R. Adema // MessageReceived
17962cb58a8SIthamar R. Adema //
18062cb58a8SIthamar R. Adema // Handle scripting messages.
18162cb58a8SIthamar R. Adema //
18262cb58a8SIthamar R. Adema // Parameters:
18362cb58a8SIthamar R. Adema //    msg - message.
18462cb58a8SIthamar R. Adema // ---------------------------------------------------------------
18562cb58a8SIthamar R. Adema void Transport::MessageReceived(BMessage* msg)
18662cb58a8SIthamar R. Adema {
18762cb58a8SIthamar R. Adema 	switch(msg->what) {
18862cb58a8SIthamar R. Adema 		case B_GET_PROPERTY:
18962cb58a8SIthamar R. Adema 		case B_SET_PROPERTY:
19062cb58a8SIthamar R. Adema 		case B_CREATE_PROPERTY:
19162cb58a8SIthamar R. Adema 		case B_DELETE_PROPERTY:
19262cb58a8SIthamar R. Adema 		case B_COUNT_PROPERTIES:
19362cb58a8SIthamar R. Adema 		case B_EXECUTE_PROPERTY:
19462cb58a8SIthamar R. Adema 			HandleScriptingCommand(msg);
19562cb58a8SIthamar R. Adema 			break;
19662cb58a8SIthamar R. Adema 
19762cb58a8SIthamar R. Adema 		default:
19862cb58a8SIthamar R. Adema 			Inherited::MessageReceived(msg);
19962cb58a8SIthamar R. Adema 	}
20062cb58a8SIthamar R. Adema }
201