xref: /haiku/src/kits/tracker/MountMenu.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
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 MountMenu::MountMenu(const char *name)
60 	:	BMenu(name)
61 {
62 	SetFont(be_plain_font);
63 }
64 
65 
66 #if _INCLUDES_CLASS_DEVICE_MAP
67 struct AddOneAsMenuItemParams {
68 	BMenu *mountMenu;
69 };
70 
71 
72 static Partition *
73 AddOnePartitionAsMenuItem(Partition *partition, void *castToParams)
74 {
75 	if (partition->Hidden())
76 		return NULL;
77 
78 	AddOneAsMenuItemParams *params = (AddOneAsMenuItemParams *)castToParams;
79 	BBitmap *icon = new BBitmap(BRect(0, 0, B_MINI_ICON - 1, B_MINI_ICON - 1),
80 		B_COLOR_8_BIT);
81 	get_device_icon(partition->GetDevice()->Name(), icon->Bits(), B_MINI_ICON);
82 
83 
84 	const char *name = partition->GetDevice()->DisplayName();
85 
86 	if (!partition->GetDevice()->IsFloppy() ||
87 		partition->Mounted() == kMounted) {
88 		if (*partition->VolumeName())
89 			name = partition->VolumeName();
90 		else if (*partition->Type())
91 			name = partition->Type();
92 	}
93 
94 	BMessage *message = new BMessage;
95 
96 	if (partition->Mounted() == kMounted) {
97 		message->what = kUnmountVolume;
98 		message->AddInt32("device_id", partition->VolumeDeviceID());
99 	} else {
100 		message->what = kMountVolume;
101 
102 		//
103 		//	Floppies have an ID of -1, because they don't have
104 		//	partition (and hence no parititon ID).
105 		//
106 		if (partition->GetDevice()->IsFloppy())
107 			message->AddInt32("id", kFloppyID);
108 		else
109 			message->AddInt32("id", partition->UniqueID());
110 	}
111 
112 	BMenuItem *item = new IconMenuItem(name, message, icon);
113 	if (partition->Mounted() == kMounted)
114 		item->SetMarked(true);
115 
116 	if (partition->Mounted() == kMounted) {
117 		BVolume partVolume(partition->VolumeDeviceID());
118 
119 		BVolume bootVolume;
120 		BVolumeRoster().GetBootVolume(&bootVolume);
121 		if (partVolume == bootVolume)
122 			item->SetEnabled(false);
123 	}
124 
125 	params->mountMenu->AddItem(item);
126 
127 	return NULL;
128 }
129 #endif
130 
131 
132 bool
133 MountMenu::AddDynamicItem(add_state)
134 {
135 #if _INCLUDES_CLASS_DEVICE_MAP
136 	for (;;) {
137 		BMenuItem *item = RemoveItem(0L);
138 		if (item == NULL)
139 			break;
140 		delete item;
141 	}
142 
143 	AddOneAsMenuItemParams params;
144 	params.mountMenu = this;
145 
146 	AutoMounter *autoMounter = dynamic_cast<TTracker *>(be_app)->
147 		AutoMounterLoop();
148 
149 	autoMounter->CheckVolumesNow();
150 	autoMounter->EachPartition(&AddOnePartitionAsMenuItem, &params);
151 
152 #ifdef SHOW_NETWORK_VOLUMES
153 
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_COLOR_8_BIT);
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
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 
206 	return false;
207 #else
208 	return true;
209 #endif
210 }
211