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 <LayoutBuilder.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_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "OpenWindow"
29
30 static const uint32 kMsgProbeFile = 'prDv';
31 static const uint32 kMsgProbeDevice = 'prFl';
32
33
OpenWindow()34 OpenWindow::OpenWindow()
35 :
36 BWindow(BRect(0, 0, 350, 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 BLayoutBuilder::Group<>(this, B_VERTICAL)
60 .SetInsets(B_USE_WINDOW_SPACING)
61 .Add(field)
62 .AddGroup(B_HORIZONTAL)
63 .Add(cancelButton)
64 .Add(probeFileButton)
65 .AddGlue()
66 .Add(probeDeviceButton);
67
68 CenterOnScreen();
69 }
70
71
~OpenWindow()72 OpenWindow::~OpenWindow()
73 {
74 }
75
76
77 void
MessageReceived(BMessage * message)78 OpenWindow::MessageReceived(BMessage *message)
79 {
80 switch (message->what) {
81 case kMsgProbeDevice:
82 {
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 {
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
QuitRequested()119 OpenWindow::QuitRequested()
120 {
121 be_app_messenger.SendMessage(kMsgOpenWindowClosed);
122 return true;
123 }
124
125
126 void
CollectDevices(BMenu * menu,BEntry * startEntry)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