xref: /haiku/src/preferences/printers/TransportMenu.cpp (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 /*
2  * Copyright 2002-2010, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Pfeiffer
7  *		Philippe Houdoin
8  */
9 #include "TransportMenu.h"
10 
11 
12 #include <Catalog.h>
13 #include <MenuItem.h>
14 
15 
16 #undef B_TRANSLATE_CONTEXT
17 #define B_TRANSLATE_CONTEXT "TransportMenu"
18 
19 
20 TransportMenu::TransportMenu(const char* title, uint32 what,
21 	const BMessenger& messenger, const BString& transportName)
22 	:
23 	BMenu(title),
24 	fWhat(what),
25 	fMessenger(messenger),
26 	fTransportName(transportName)
27 {
28 }
29 
30 
31 bool
32 TransportMenu::AddDynamicItem(add_state state)
33 {
34 	if (state != B_INITIAL_ADD)
35 		return false;
36 
37 	BMenuItem* item = RemoveItem((int32)0);
38 	while (item != NULL) {
39 		delete item;
40 		item = RemoveItem((int32)0);
41 	}
42 
43 	BMessage msg;
44 	msg.MakeEmpty();
45 	msg.what = B_GET_PROPERTY;
46 	msg.AddSpecifier("Ports");
47 	BMessage reply;
48 	if (fMessenger.SendMessage(&msg, &reply) != B_OK)
49 		return false;
50 
51 	BString portId;
52 	BString portName;
53 	if (reply.FindString("port_id", &portId) != B_OK) {
54 		// Show error message in submenu
55 		BMessage* portMsg = new BMessage(fWhat);
56 		AddItem(new BMenuItem(
57 			B_TRANSLATE("No printer found!"), portMsg));
58 		return false;
59 	}
60 
61 	// Add ports to submenu
62 	for (int32 i = 0; reply.FindString("port_id", i, &portId) == B_OK;
63 		i++) {
64 		if (reply.FindString("port_name", i, &portName) != B_OK
65 			|| !portName.Length())
66 			portName = portId;
67 
68 		// Create menu item in submenu for port
69 		BMessage* portMsg = new BMessage(fWhat);
70 		portMsg->AddString("name", fTransportName);
71 		portMsg->AddString("path", portId);
72 		AddItem(new BMenuItem(portName.String(), portMsg));
73 	}
74 
75 	return false;
76 }
77