109750a22SMichael Pfeiffer/*
209750a22SMichael Pfeiffer * Copyright 2002-2010, Haiku.
309750a22SMichael Pfeiffer * Distributed under the terms of the MIT License.
409750a22SMichael Pfeiffer *
509750a22SMichael Pfeiffer * Authors:
609750a22SMichael Pfeiffer *		Michael Pfeiffer
709750a22SMichael Pfeiffer *		Philippe Houdoin
809750a22SMichael Pfeiffer */
909750a22SMichael Pfeiffer#include "TransportMenu.h"
1009750a22SMichael Pfeiffer
1109750a22SMichael Pfeiffer
1209750a22SMichael Pfeiffer#include <Catalog.h>
1309750a22SMichael Pfeiffer#include <MenuItem.h>
1409750a22SMichael Pfeiffer
1509750a22SMichael Pfeiffer
16546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
17546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "TransportMenu"
1809750a22SMichael Pfeiffer
1909750a22SMichael Pfeiffer
2009750a22SMichael PfeifferTransportMenu::TransportMenu(const char* title, uint32 what,
2109750a22SMichael Pfeiffer	const BMessenger& messenger, const BString& transportName)
2209750a22SMichael Pfeiffer	:
2309750a22SMichael Pfeiffer	BMenu(title),
2409750a22SMichael Pfeiffer	fWhat(what),
2509750a22SMichael Pfeiffer	fMessenger(messenger),
2609750a22SMichael Pfeiffer	fTransportName(transportName)
2709750a22SMichael Pfeiffer{
2809750a22SMichael Pfeiffer}
2909750a22SMichael Pfeiffer
3009750a22SMichael Pfeiffer
3109750a22SMichael Pfeifferbool
3209750a22SMichael PfeifferTransportMenu::AddDynamicItem(add_state state)
3309750a22SMichael Pfeiffer{
3409750a22SMichael Pfeiffer	if (state != B_INITIAL_ADD)
3509750a22SMichael Pfeiffer		return false;
3609750a22SMichael Pfeiffer
3709750a22SMichael Pfeiffer	BMenuItem* item = RemoveItem((int32)0);
3809750a22SMichael Pfeiffer	while (item != NULL) {
3909750a22SMichael Pfeiffer		delete item;
4009750a22SMichael Pfeiffer		item = RemoveItem((int32)0);
4109750a22SMichael Pfeiffer	}
4209750a22SMichael Pfeiffer
4309750a22SMichael Pfeiffer	BMessage msg;
4409750a22SMichael Pfeiffer	msg.MakeEmpty();
4509750a22SMichael Pfeiffer	msg.what = B_GET_PROPERTY;
4609750a22SMichael Pfeiffer	msg.AddSpecifier("Ports");
4709750a22SMichael Pfeiffer	BMessage reply;
4809750a22SMichael Pfeiffer	if (fMessenger.SendMessage(&msg, &reply) != B_OK)
4909750a22SMichael Pfeiffer		return false;
5009750a22SMichael Pfeiffer
5109750a22SMichael Pfeiffer	BString portId;
5209750a22SMichael Pfeiffer	BString portName;
5309750a22SMichael Pfeiffer	if (reply.FindString("port_id", &portId) != B_OK) {
5409750a22SMichael Pfeiffer		// Show error message in submenu
5509750a22SMichael Pfeiffer		BMessage* portMsg = new BMessage(fWhat);
5609750a22SMichael Pfeiffer		AddItem(new BMenuItem(
5709750a22SMichael Pfeiffer			B_TRANSLATE("No printer found!"), portMsg));
5809750a22SMichael Pfeiffer		return false;
5909750a22SMichael Pfeiffer	}
6009750a22SMichael Pfeiffer
6109750a22SMichael Pfeiffer	// Add ports to submenu
6209750a22SMichael Pfeiffer	for (int32 i = 0; reply.FindString("port_id", i, &portId) == B_OK;
6309750a22SMichael Pfeiffer		i++) {
6409750a22SMichael Pfeiffer		if (reply.FindString("port_name", i, &portName) != B_OK
6509750a22SMichael Pfeiffer			|| !portName.Length())
6609750a22SMichael Pfeiffer			portName = portId;
6709750a22SMichael Pfeiffer
6809750a22SMichael Pfeiffer		// Create menu item in submenu for port
6909750a22SMichael Pfeiffer		BMessage* portMsg = new BMessage(fWhat);
7009750a22SMichael Pfeiffer		portMsg->AddString("name", fTransportName);
7109750a22SMichael Pfeiffer		portMsg->AddString("path", portId);
7209750a22SMichael Pfeiffer		AddItem(new BMenuItem(portName.String(), portMsg));
7309750a22SMichael Pfeiffer	}
7409750a22SMichael Pfeiffer
7509750a22SMichael Pfeiffer	return false;
7609750a22SMichael Pfeiffer}
77