6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
35 // MountMenu implements a context menu used for mounting/unmounting volumes
38 #include "MountMenu.h"
45 #include <InterfaceDefs.h>
46 #include <VolumeRoster.h>
52 #include "IconMenuItem.h"
56 #include <DiskDevice.h>
57 #include <DiskDeviceList.h>
59 #define SHOW_NETWORK_VOLUMES
61 #undef B_TRANSLATION_CONTEXT
62 #define B_TRANSLATION_CONTEXT "MountMenu"
65 class AddMenuItemVisitor
: public BDiskDeviceVisitor
{
67 AddMenuItemVisitor(BMenu
* menu
);
68 virtual ~AddMenuItemVisitor();
70 virtual bool Visit(BDiskDevice
* device
);
71 virtual bool Visit(BPartition
* partition
, int32 level
);
78 // #pragma mark - AddMenuItemVisitor
81 AddMenuItemVisitor::AddMenuItemVisitor(BMenu
* menu
)
88 AddMenuItemVisitor::~AddMenuItemVisitor()
94 AddMenuItemVisitor::Visit(BDiskDevice
* device
)
96 return Visit(device
, 0);
101 AddMenuItemVisitor::Visit(BPartition
* partition
, int32 level
)
103 if (!partition
->ContainsFileSystem())
106 // get name (and eventually the type)
107 BString name
= partition
->ContentName();
108 if (name
.Length() == 0) {
109 name
= partition
->Name();
110 if (name
.Length() == 0) {
111 const char* type
= partition
->ContentType();
115 uint32 divisor
= 1UL << 30;
117 if (partition
->Size() < divisor
) {
122 char* buffer
= name
.LockBuffer(256);
123 snprintf(buffer
, 256, "(%.1f %cB %s)",
124 1.0 * partition
->Size() / divisor
, unit
, type
);
131 BBitmap
* icon
= new BBitmap(BRect(0, 0, B_MINI_ICON
- 1, B_MINI_ICON
- 1),
133 if (partition
->GetIcon(icon
, B_MINI_ICON
) != B_OK
) {
138 BMessage
* message
= new BMessage(partition
->IsMounted() ?
139 kUnmountVolume
: kMountVolume
);
140 message
->AddInt32("id", partition
->ID());
142 // TODO: for now, until we actually have disk device icons
145 item
= new IconMenuItem(name
.String(), message
, icon
);
147 item
= new BMenuItem(name
.String(), message
);
148 if (partition
->IsMounted()) {
149 item
->SetMarked(true);
152 if (partition
->GetVolume(&volume
) == B_OK
) {
154 BVolumeRoster().GetBootVolume(&bootVolume
);
155 if (volume
== bootVolume
)
156 item
->SetEnabled(false);
160 fMenu
->AddItem(item
);
165 // #pragma mark - MountMenu
168 MountMenu::MountMenu(const char* name
)
171 SetFont(be_plain_font
);
176 MountMenu::AddDynamicItem(add_state
)
180 BMenuItem
* item
= RemoveItem((int32
)0);
186 BDiskDeviceList devices
;
187 status_t status
= devices
.Fetch();
188 if (status
== B_OK
) {
189 AddMenuItemVisitor
visitor(this);
190 devices
.VisitEachPartition(&visitor
);
193 #ifdef SHOW_NETWORK_VOLUMES
194 // iterate the volume roster and look for volumes with the
195 // 'shared' attributes -- these same volumes will not be returned
196 // by the autoMounter because they do not show up in the /dev tree
197 BVolumeRoster volumeRoster
;
199 while (volumeRoster
.GetNextVolume(&volume
) == B_OK
) {
200 if (volume
.IsShared()) {
201 BBitmap
* icon
= new BBitmap(BRect(0, 0, 15, 15), B_CMAP8
);
203 if (fs_stat_dev(volume
.Device(), &info
) != B_OK
) {
204 PRINT(("Cannot get mount menu item icon; bad device ID\n"));
208 // Use the shared icon instead of the device icon
209 if (get_device_icon(info
.device_name
, icon
->Bits(), B_MINI_ICON
)
211 GetTrackerResources()->GetIconResource(R_ShareIcon
,
215 BMessage
* message
= new BMessage(kUnmountVolume
);
216 message
->AddInt32("device_id", volume
.Device());
217 char volumeName
[B_FILE_NAME_LENGTH
];
218 volume
.GetName(volumeName
);
220 BMenuItem
* item
= new IconMenuItem(volumeName
, message
, icon
);
221 item
->SetMarked(true);
225 #endif // SHOW_NETWORK_VOLUMES
229 BMenuItem
* mountAll
= new BMenuItem(B_TRANSLATE("Mount all"),
230 new BMessage(kMountAllNow
));
232 BMenuItem
* mountSettings
= new BMenuItem(
233 B_TRANSLATE("Settings" B_UTF8_ELLIPSIS
),
234 new BMessage(kRunAutomounterSettings
));
235 AddItem(mountSettings
);
237 SetTargetForItems(be_app
);