xref: /haiku/src/apps/diskprobe/OpenWindow.cpp (revision 066522e87b5aa9f25957e3f419ac3a0565edbb4f)
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 
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_APP_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:"), fDevicesMenu,
46 		NULL);
47 
48 	BButton *probeDeviceButton = new BButton("device", B_TRANSLATE("Probe device"),
49 		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 
60 	SetLayout(new BGroupLayout(B_HORIZONTAL));
61 
62 	AddChild(BGridLayoutBuilder(8, 8)
63 		.Add(field, 0, 0, 3)
64 		.Add(cancelButton, 0, 1)
65 		.Add(probeFileButton, 1, 1)
66 		.Add(probeDeviceButton, 2, 1)
67 		.SetInsets(8, 8, 8, 8)
68 	);
69 
70 	CenterOnScreen();
71 }
72 
73 
74 OpenWindow::~OpenWindow()
75 {
76 }
77 
78 
79 void
80 OpenWindow::MessageReceived(BMessage *message)
81 {
82 	switch (message->what) {
83 		case kMsgProbeDevice: {
84 			BMenuItem *item = fDevicesMenu->FindMarked();
85 			if (item == NULL)
86 				break;
87 
88 			be_app_messenger.SendMessage(item->Message());
89 			PostMessage(B_QUIT_REQUESTED);
90 			break;
91 		}
92 
93 		case kMsgProbeFile:
94 			be_app_messenger.SendMessage(kMsgOpenFilePanel);
95 			PostMessage(B_QUIT_REQUESTED);
96 			break;
97 
98 		case B_SIMPLE_DATA: {
99 			// if it's a file drop, open it
100 			entry_ref ref;
101 			if (message->FindRef("refs", 0, &ref) == B_OK) {
102 				BMessage openMessage(*message);
103 				openMessage.what = B_REFS_RECEIVED;
104 
105 				be_app_messenger.SendMessage(&openMessage);
106 				PostMessage(B_QUIT_REQUESTED);
107 			}
108 			break;
109 		}
110 
111 		default:
112 			BWindow::MessageReceived(message);
113 			break;
114 	}
115 }
116 
117 
118 bool
119 OpenWindow::QuitRequested()
120 {
121 	be_app_messenger.SendMessage(kMsgOpenWindowClosed);
122 	return true;
123 }
124 
125 
126 void
127 OpenWindow::CollectDevices(BMenu *menu, BEntry *startEntry)
128 {
129 	BDirectory directory;
130 	if (startEntry != NULL)
131 		directory.SetTo(startEntry);
132 	else
133 		directory.SetTo("/dev/disk");
134 
135 	BEntry entry;
136 	while (directory.GetNextEntry(&entry) == B_OK) {
137 		if (entry.IsDirectory()) {
138 			CollectDevices(menu, &entry);
139 			continue;
140 		}
141 
142 		entry_ref ref;
143 		if (entry.GetRef(&ref) != B_OK)
144 			continue;
145 
146 		BPath path;
147 		if (entry.GetPath(&path) != B_OK)
148 			continue;
149 
150 		BMessage *message = new BMessage(B_REFS_RECEIVED);
151 		message->AddRef("refs", &ref);
152 
153 		menu->AddItem(new BMenuItem(path.Path(), message));
154 	}
155 }
156 
157