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