xref: /haiku/src/kits/tracker/MountMenu.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 // MountMenu implements a context menu used for mounting/unmounting volumes
36 
37 #include <MenuItem.h>
38 #include <Mime.h>
39 #include <InterfaceDefs.h>
40 #include <VolumeRoster.h>
41 #include <Volume.h>
42 #include <fs_info.h>
43 
44 #include "AutoMounter.h"
45 #include "Commands.h"
46 #include "MountMenu.h"
47 #include "IconMenuItem.h"
48 #include "Tracker.h"
49 #include "Bitmaps.h"
50 
51 #if OPEN_TRACKER
52 #	include "DeviceMap.h"
53 #else
54 #	include <private/storage/DeviceMap.h>
55 #endif
56 
57 #define SHOW_NETWORK_VOLUMES
58 
59 
60 MountMenu::MountMenu(const char *name)
61 	: BMenu(name)
62 {
63 	SetFont(be_plain_font);
64 }
65 
66 
67 #if _INCLUDES_CLASS_DEVICE_MAP
68 struct AddOneAsMenuItemParams {
69 	BMenu *mountMenu;
70 };
71 
72 
73 static Partition *
74 AddOnePartitionAsMenuItem(Partition *partition, void *castToParams)
75 {
76 	if (partition->Hidden())
77 		return NULL;
78 
79 	AddOneAsMenuItemParams *params = (AddOneAsMenuItemParams *)castToParams;
80 	BBitmap *icon = new BBitmap(BRect(0, 0, B_MINI_ICON - 1, B_MINI_ICON - 1),
81 		B_CMAP8);
82 	get_device_icon(partition->GetDevice()->Name(), icon->Bits(), B_MINI_ICON);
83 
84 
85 	const char *name = partition->GetDevice()->DisplayName();
86 
87 	if (!partition->GetDevice()->IsFloppy() ||
88 		partition->Mounted() == kMounted) {
89 		if (*partition->VolumeName())
90 			name = partition->VolumeName();
91 		else if (*partition->Type())
92 			name = partition->Type();
93 	}
94 
95 	BMessage *message = new BMessage;
96 
97 	if (partition->Mounted() == kMounted) {
98 		message->what = kUnmountVolume;
99 		message->AddInt32("device_id", partition->VolumeDeviceID());
100 	} else {
101 		message->what = kMountVolume;
102 
103 		//
104 		//	Floppies have an ID of -1, because they don't have
105 		//	partition (and hence no parititon ID).
106 		//
107 		if (partition->GetDevice()->IsFloppy())
108 			message->AddInt32("id", kFloppyID);
109 		else
110 			message->AddInt32("id", partition->UniqueID());
111 	}
112 
113 	BMenuItem *item = new IconMenuItem(name, message, icon);
114 	if (partition->Mounted() == kMounted)
115 		item->SetMarked(true);
116 
117 	if (partition->Mounted() == kMounted) {
118 		BVolume partVolume(partition->VolumeDeviceID());
119 
120 		BVolume bootVolume;
121 		BVolumeRoster().GetBootVolume(&bootVolume);
122 		if (partVolume == bootVolume)
123 			item->SetEnabled(false);
124 	}
125 
126 	params->mountMenu->AddItem(item);
127 
128 	return NULL;
129 }
130 #endif	// _INCLUDES_CLASS_DEVICE_MAP
131 
132 
133 bool
134 MountMenu::AddDynamicItem(add_state)
135 {
136 #if _INCLUDES_CLASS_DEVICE_MAP
137 	for (;;) {
138 		BMenuItem *item = RemoveItem(0L);
139 		if (item == NULL)
140 			break;
141 		delete item;
142 	}
143 
144 	AddOneAsMenuItemParams params;
145 	params.mountMenu = this;
146 
147 	AutoMounter *autoMounter = dynamic_cast<TTracker *>(be_app)->
148 		AutoMounterLoop();
149 
150 	autoMounter->CheckVolumesNow();
151 	autoMounter->EachPartition(&AddOnePartitionAsMenuItem, &params);
152 
153 #ifdef SHOW_NETWORK_VOLUMES
154 	// iterate the volume roster and look for volumes with the
155 	// 'shared' attributes -- these same volumes will not be returned
156 	// by the autoMounter because they do not show up in the /dev tree
157 	BVolumeRoster volumeRoster;
158 	BVolume volume;
159 	bool needSeparator = false;
160 	while (volumeRoster.GetNextVolume(&volume) == B_OK) {
161 		if (volume.IsShared()) {
162 			needSeparator = true;
163 			BBitmap *icon = new BBitmap(BRect(0, 0, 15, 15), B_CMAP8);
164 			fs_info info;
165 			if (fs_stat_dev(volume.Device(), &info) != B_OK) {
166 				PRINT(("Cannot get mount menu item icon; bad device ID\n"));
167 				delete icon;
168 				continue;
169 			}
170 			// Use the shared icon instead of the device icon
171 			if (get_device_icon(info.device_name, icon->Bits(), B_MINI_ICON) != B_OK)
172 				GetTrackerResources()->GetIconResource(kResShareIcon, B_MINI_ICON, icon);
173 
174 			BMessage *message = new BMessage(kUnmountVolume);
175 			message->AddInt32("device_id", volume.Device());
176 			char volumeName[B_FILE_NAME_LENGTH];
177 			volume.GetName(volumeName);
178 
179 			BMenuItem *item = new IconMenuItem(volumeName, message, icon);
180 			item->SetMarked(true);
181 			AddItem(item);
182 		}
183 	}
184 #endif	// SHOW_NETWORK_VOLUMES
185 
186 	AddSeparatorItem();
187 
188 	// add an option to rescan the scsii bus, etc.
189 	BMenuItem *rescanItem = NULL;
190 	if (modifiers() & B_SHIFT_KEY) {
191 		rescanItem = new BMenuItem("Rescan Devices", new BMessage(kAutomounterRescan));
192 		AddItem(rescanItem);
193 	}
194 
195 	BMenuItem *mountAll = new BMenuItem("Mount All", new BMessage(kMountAllNow));
196 	AddItem(mountAll);
197 	BMenuItem *mountSettings = new BMenuItem("Settings"B_UTF8_ELLIPSIS,
198 		new BMessage(kRunAutomounterSettings));
199 	AddItem(mountSettings);
200 
201 	SetTargetForItems(be_app);
202 
203 	if (rescanItem)
204 		rescanItem->SetTarget(autoMounter);
205 #endif	// _INCLUDES_CLASS_DEVICE_MAP
206 
207 	return false;
208 }
209