xref: /haiku/src/apps/diskprobe/AttributeWindow.cpp (revision 36a7951646e2db0768eb637a666422cb4725b816)
1d11ec082SAxel Dörfler /*
2d11ec082SAxel Dörfler ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3d11ec082SAxel Dörfler ** Distributed under the terms of the OpenBeOS License.
4d11ec082SAxel Dörfler */
5d11ec082SAxel Dörfler 
6d11ec082SAxel Dörfler 
7d11ec082SAxel Dörfler #include "AttributeWindow.h"
8d11ec082SAxel Dörfler #include "ProbeView.h"
9d11ec082SAxel Dörfler 
10d11ec082SAxel Dörfler #include <MenuBar.h>
11d11ec082SAxel Dörfler #include <MenuItem.h>
12*36a79516SAxel Dörfler #include <Alert.h>
13d11ec082SAxel Dörfler 
14d11ec082SAxel Dörfler #include <stdio.h>
15d11ec082SAxel Dörfler 
16d11ec082SAxel Dörfler 
17*36a79516SAxel Dörfler static const uint32 kMsgRemoveAttribute = 'rmat';
18*36a79516SAxel Dörfler 
19*36a79516SAxel Dörfler 
207fff3ed0SAxel Dörfler AttributeWindow::AttributeWindow(BRect rect, entry_ref *ref, const char *attribute,
217fff3ed0SAxel Dörfler 	const BMessage *settings)
22d11ec082SAxel Dörfler 	: ProbeWindow(rect, ref),
23d11ec082SAxel Dörfler 	fAttribute(strdup(attribute))
24d11ec082SAxel Dörfler {
25d11ec082SAxel Dörfler 	char buffer[256];
26d11ec082SAxel Dörfler 	snprintf(buffer, sizeof(buffer), "%s: %s", ref->name, attribute);
27d11ec082SAxel Dörfler 	SetTitle(buffer);
28d11ec082SAxel Dörfler 
29d11ec082SAxel Dörfler 	// add the menu
30d11ec082SAxel Dörfler 
31d11ec082SAxel Dörfler 	BMenuBar *menuBar = new BMenuBar(BRect(0, 0, 0, 0), NULL);
32d11ec082SAxel Dörfler 	AddChild(menuBar);
33d11ec082SAxel Dörfler 
34d11ec082SAxel Dörfler 	BMenu *menu = new BMenu("Attribute");
35d11ec082SAxel Dörfler 
36*36a79516SAxel Dörfler 	menu->AddItem(new BMenuItem("Save", NULL, 'S', B_COMMAND_KEY));
37*36a79516SAxel Dörfler 	menu->AddItem(new BMenuItem("Remove from File", new BMessage(kMsgRemoveAttribute)));
38*36a79516SAxel Dörfler 	menu->AddSeparatorItem();
39*36a79516SAxel Dörfler 
40d11ec082SAxel Dörfler 	// the ProbeView file menu items will be inserted here
41d11ec082SAxel Dörfler 	menu->AddSeparatorItem();
42d11ec082SAxel Dörfler 
43d11ec082SAxel Dörfler 	menu->AddItem(new BMenuItem("Close", new BMessage(B_CLOSE_REQUESTED), 'W', B_COMMAND_KEY));
44d11ec082SAxel Dörfler 	menu->SetTargetForItems(this);
45d11ec082SAxel Dörfler 	menuBar->AddItem(menu);
46d11ec082SAxel Dörfler 
47d11ec082SAxel Dörfler 	// add our interface widgets
48d11ec082SAxel Dörfler 
49d11ec082SAxel Dörfler 	BRect rect = Bounds();
50d11ec082SAxel Dörfler 	rect.top = menuBar->Bounds().Height() + 1;
517fff3ed0SAxel Dörfler 	ProbeView *probeView = new ProbeView(rect, ref, attribute, settings);
52*36a79516SAxel Dörfler 	probeView->AddFileMenuItems(menu, menu->CountItems() - 2);
53d11ec082SAxel Dörfler 	AddChild(probeView);
54d11ec082SAxel Dörfler 
55d11ec082SAxel Dörfler 	probeView->UpdateSizeLimits();
56d11ec082SAxel Dörfler }
57d11ec082SAxel Dörfler 
58d11ec082SAxel Dörfler 
59d11ec082SAxel Dörfler AttributeWindow::~AttributeWindow()
60d11ec082SAxel Dörfler {
61d11ec082SAxel Dörfler 	free(fAttribute);
62d11ec082SAxel Dörfler }
63d11ec082SAxel Dörfler 
64d11ec082SAxel Dörfler 
65*36a79516SAxel Dörfler void
66*36a79516SAxel Dörfler AttributeWindow::MessageReceived(BMessage *message)
67*36a79516SAxel Dörfler {
68*36a79516SAxel Dörfler 	switch (message->what) {
69*36a79516SAxel Dörfler 		case kMsgRemoveAttribute:
70*36a79516SAxel Dörfler 		{
71*36a79516SAxel Dörfler 			char buffer[1024];
72*36a79516SAxel Dörfler 			snprintf(buffer, sizeof(buffer),
73*36a79516SAxel Dörfler 				"Do you really want to remove the attribute \"%s\" from the file \"%s\"?\n\n"
74*36a79516SAxel Dörfler 				"The contents of the attribute will get lost if you click on \"Remove\".",
75*36a79516SAxel Dörfler 				fAttribute, Ref().name);
76*36a79516SAxel Dörfler 
77*36a79516SAxel Dörfler 			int32 chosen = (new BAlert("DiskProbe request",
78*36a79516SAxel Dörfler 				buffer, "Remove", "Cancel", NULL,
79*36a79516SAxel Dörfler 				B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go();
80*36a79516SAxel Dörfler 			if (chosen == 0) {
81*36a79516SAxel Dörfler 				BNode node(&Ref());
82*36a79516SAxel Dörfler 				if (node.InitCheck() == B_OK)
83*36a79516SAxel Dörfler 					node.RemoveAttr(fAttribute);
84*36a79516SAxel Dörfler 
85*36a79516SAxel Dörfler 				PostMessage(B_QUIT_REQUESTED);
86*36a79516SAxel Dörfler 			}
87*36a79516SAxel Dörfler 			break;
88*36a79516SAxel Dörfler 		}
89*36a79516SAxel Dörfler 
90*36a79516SAxel Dörfler 		default:
91*36a79516SAxel Dörfler 			ProbeWindow::MessageReceived(message);
92*36a79516SAxel Dörfler 			break;
93*36a79516SAxel Dörfler 	}
94*36a79516SAxel Dörfler }
95*36a79516SAxel Dörfler 
96*36a79516SAxel Dörfler 
97d11ec082SAxel Dörfler bool
98d11ec082SAxel Dörfler AttributeWindow::Contains(const entry_ref &ref, const char *attribute)
99d11ec082SAxel Dörfler {
100d11ec082SAxel Dörfler 	return ref == Ref() && attribute != NULL && !strcmp(attribute, fAttribute);
101d11ec082SAxel Dörfler }
102d11ec082SAxel Dörfler 
103