xref: /haiku/src/apps/diskprobe/OpenWindow.cpp (revision e7c8829c5d8e5d34a2a1e111f1c06aceff256013)
1 /*
2  * Copyright 2009 Ankur Sethi <get.me.ankur@gmail.com>
3  * Copyright 2004-2006, Axel Dörfler, axeld@pinc-software.de.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 
7 #include "OpenWindow.h"
8 
9 #include <Application.h>
10 #include <Button.h>
11 #include <Directory.h>
12 #include <Entry.h>
13 #include <GroupLayout.h>
14 #include <GridLayoutBuilder.h>
15 #include <MenuField.h>
16 #include <MenuItem.h>
17 #include <Path.h>
18 #include <PopUpMenu.h>
19 #include <Screen.h>
20 
21 #include "DiskProbe.h"
22 
23 
24 static const uint32 kMsgProbeFile = 'prDv';
25 static const uint32 kMsgProbeDevice = 'prFl';
26 
27 
28 OpenWindow::OpenWindow()
29 	: BWindow(BRect(0, 0, 35, 10), "DiskProbe", B_TITLED_WINDOW,
30 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
31 			 | B_AUTO_UPDATE_SIZE_LIMITS)
32 {
33 	fDevicesMenu = new BPopUpMenu("devices");
34 	CollectDevices(fDevicesMenu);
35 	if (BMenuItem *item = fDevicesMenu->ItemAt(0))
36 		item->SetMarked(true);
37 
38 	BMenuField *field = new BMenuField("Examine Device:", fDevicesMenu, NULL);
39 
40 	BButton *probeDeviceButton = new BButton("device", "Probe Device",
41 		new BMessage(kMsgProbeDevice));
42 	probeDeviceButton->MakeDefault(true);
43 
44 	BButton *probeFileButton = new BButton("file", "Probe File" B_UTF8_ELLIPSIS,
45 		new BMessage(kMsgProbeFile));
46 
47 	BButton *cancelButton = new BButton("cancel", "Cancel",
48 		new BMessage(B_QUIT_REQUESTED));
49 
50 
51 	SetLayout(new BGroupLayout(B_HORIZONTAL));
52 
53 	AddChild(BGridLayoutBuilder(8, 8)
54 		.Add(field, 0, 0, 3)
55 		.Add(cancelButton, 0, 1)
56 		.Add(probeFileButton, 1, 1)
57 		.Add(probeDeviceButton, 2, 1)
58 		.SetInsets(8, 8, 8, 8)
59 	);
60 
61 	// TODO: This does not center the window, since with layout management,
62 	// the window will still have an invalid size at this point.
63 	BScreen screen(this);
64 	MoveTo(screen.Frame().left + (screen.Frame().Width() - Frame().Width()) / 2,
65 		screen.Frame().top + (screen.Frame().Height() - Frame().Height()) / 2);
66 }
67 
68 
69 OpenWindow::~OpenWindow()
70 {
71 }
72 
73 
74 void
75 OpenWindow::MessageReceived(BMessage *message)
76 {
77 	switch (message->what) {
78 		case kMsgProbeDevice: {
79 			BMenuItem *item = fDevicesMenu->FindMarked();
80 			if (item == NULL)
81 				break;
82 
83 			be_app_messenger.SendMessage(item->Message());
84 			PostMessage(B_QUIT_REQUESTED);
85 			break;
86 		}
87 
88 		case kMsgProbeFile:
89 			be_app_messenger.SendMessage(kMsgOpenFilePanel);
90 			PostMessage(B_QUIT_REQUESTED);
91 			break;
92 
93 		case B_SIMPLE_DATA: {
94 			// if it's a file drop, open it
95 			entry_ref ref;
96 			if (message->FindRef("refs", 0, &ref) == B_OK) {
97 				BMessage openMessage(*message);
98 				openMessage.what = B_REFS_RECEIVED;
99 
100 				be_app_messenger.SendMessage(&openMessage);
101 				PostMessage(B_QUIT_REQUESTED);
102 			}
103 			break;
104 		}
105 
106 		default:
107 			BWindow::MessageReceived(message);
108 			break;
109 	}
110 }
111 
112 
113 bool
114 OpenWindow::QuitRequested()
115 {
116 	be_app_messenger.SendMessage(kMsgOpenWindowClosed);
117 	return true;
118 }
119 
120 
121 void
122 OpenWindow::CollectDevices(BMenu *menu, BEntry *startEntry)
123 {
124 	BDirectory directory;
125 	if (startEntry != NULL)
126 		directory.SetTo(startEntry);
127 	else
128 		directory.SetTo("/dev/disk");
129 
130 	BEntry entry;
131 	while (directory.GetNextEntry(&entry) == B_OK) {
132 		if (entry.IsDirectory()) {
133 			CollectDevices(menu, &entry);
134 			continue;
135 		}
136 
137 		entry_ref ref;
138 		if (entry.GetRef(&ref) != B_OK)
139 			continue;
140 
141 		BPath path;
142 		if (entry.GetPath(&path) != B_OK)
143 			continue;
144 
145 		BMessage *message = new BMessage(B_REFS_RECEIVED);
146 		message->AddRef("refs", &ref);
147 
148 		menu->AddItem(new BMenuItem(path.Path(), message));
149 	}
150 }
151 
152