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