xref: /haiku/src/apps/drivesetup/Support.cpp (revision 2710b4f5d4251c5cf88c82b0114ea99b0ef46d22)
1 /*
2  * Copyright 2002-2013 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Ithamar R. Adema <ithamar@unet.nl>
7  *		Stephan Aßmus <superstippi@gmx.de>
8  *		Axel Dörfler, axeld@pinc-software.de.
9  *		Bryce Groff <bgroff@hawaii.edu>
10  *		Erik Jaesler <ejakowatz@users.sourceforge.net>
11  */
12 
13 
14 #include "Support.h"
15 
16 #include <stdio.h>
17 
18 #include <Catalog.h>
19 #include <Locale.h>
20 #include <Partition.h>
21 #include <String.h>
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Support"
26 
27 
28 static const int32 kMaxSliderLimit = 0x7fffff80;
29 	// this is the maximum value that BSlider seem to work with fine
30 
31 
32 void
dump_partition_info(const BPartition * partition)33 dump_partition_info(const BPartition* partition)
34 {
35 	char size[1024];
36 	printf("\tOffset(): %" B_PRIdOFF "\n", partition->Offset());
37 	printf("\tSize(): %s\n", string_for_size(partition->Size(), size,
38 		sizeof(size)));
39 	printf("\tContentSize(): %s\n", string_for_size(partition->ContentSize(),
40 		size, sizeof(size)));
41 	printf("\tBlockSize(): %" B_PRId32 "\n", partition->BlockSize());
42 	printf("\tPhysicalBlockSize(): %" B_PRId32 "\n", partition->PhysicalBlockSize());
43 	printf("\tIndex(): %" B_PRId32 "\n", partition->Index());
44 	printf("\tStatus(): %" B_PRId32 "\n\n", partition->Status());
45 	printf("\tContainsFileSystem(): %s\n",
46 		partition->ContainsFileSystem() ? "true" : "false");
47 	printf("\tContainsPartitioningSystem(): %s\n\n",
48 		partition->ContainsPartitioningSystem() ? "true" : "false");
49 	printf("\tIsDevice(): %s\n", partition->IsDevice() ? "true" : "false");
50 	printf("\tIsReadOnly(): %s\n", partition->IsReadOnly() ? "true" : "false");
51 	printf("\tIsMounted(): %s\n", partition->IsMounted() ? "true" : "false");
52 	printf("\tIsBusy(): %s\n\n", partition->IsBusy() ? "true" : "false");
53 	printf("\tFlags(): %" B_PRIx32 "\n\n", partition->Flags());
54 	printf("\tName(): %s\n", partition->Name());
55 	printf("\tContentName(): %s\n", partition->RawContentName());
56 	printf("\tType(): %s\n", partition->Type());
57 	printf("\tContentType(): %s\n", partition->ContentType());
58 	printf("\tID(): %" B_PRIx32 "\n\n", partition->ID());
59 }
60 
61 
62 bool
is_valid_partitionable_space(size_t size)63 is_valid_partitionable_space(size_t size)
64 {
65 	// TODO: remove this again, the DiskDeviceAPI should
66 	// not even show these spaces to begin with
67 	return size >= 8 * 1024 * 1024;
68 }
69 
70 
71 // #pragma mark - SpaceIDMap
72 
73 
SpaceIDMap()74 SpaceIDMap::SpaceIDMap()
75 	:
76 	HashMap<HashString, partition_id>(),
77 	fNextSpaceID(-2)
78 {
79 }
80 
81 
~SpaceIDMap()82 SpaceIDMap::~SpaceIDMap()
83 {
84 }
85 
86 
87 partition_id
SpaceIDFor(partition_id parentID,off_t spaceOffset)88 SpaceIDMap::SpaceIDFor(partition_id parentID, off_t spaceOffset)
89 {
90 	BString key;
91 	key << parentID << ':' << (uint64)spaceOffset;
92 
93 	if (ContainsKey(key.String()))
94 		return Get(key.String());
95 
96 	partition_id newID = fNextSpaceID--;
97 	Put(key.String(), newID);
98 
99 	return newID;
100 }
101 
102 
103 // #pragma mark -
104 
105 
SizeSlider(const char * name,const char * label,BMessage * message,off_t offset,off_t size,uint32 minGranularity)106 SizeSlider::SizeSlider(const char* name, const char* label,
107 		BMessage* message, off_t offset, off_t size, uint32 minGranularity)
108 	:
109 	BSlider(name, label, message, 0, kMaxSliderLimit, B_HORIZONTAL,
110 		B_TRIANGLE_THUMB),
111 	fStartOffset(offset),
112 	fEndOffset(offset + size),
113 	fMaxPartitionSize(size),
114 	fGranularity(minGranularity)
115 {
116 	rgb_color fillColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
117 	UseFillColor(true, &fillColor);
118 
119 	// Lazy loop to get a power of two granularity
120 	while (size / fGranularity > kMaxSliderLimit)
121 		fGranularity *= 2;
122 
123 	SetKeyIncrementValue(int32(1024 * 1024 * 1.0 * kMaxSliderLimit
124 		/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
125 
126 	char buffer[64];
127 	char minString[64];
128 	char maxString[64];
129 	snprintf(minString, sizeof(minString), B_TRANSLATE("Offset: %s"),
130 		string_for_size(fStartOffset, buffer, sizeof(buffer)));
131 	snprintf(maxString, sizeof(maxString), B_TRANSLATE("End: %s"),
132 		string_for_size(fEndOffset, buffer, sizeof(buffer)));
133 	SetLimitLabels(minString, maxString);
134 }
135 
136 
~SizeSlider()137 SizeSlider::~SizeSlider()
138 {
139 }
140 
141 
142 void
SetValue(int32 value)143 SizeSlider::SetValue(int32 value)
144 {
145 	BSlider::SetValue(value);
146 
147 	fSize = (off_t(1.0 * (MaxPartitionSize() - 1) * Value()
148 		/ kMaxSliderLimit + 0.5) / fGranularity) * fGranularity;
149 }
150 
151 
152 const char*
UpdateText() const153 SizeSlider::UpdateText() const
154 {
155 	return string_for_size(Size(), fStatusLabel, sizeof(fStatusLabel));
156 }
157 
158 
159 off_t
Size() const160 SizeSlider::Size() const
161 {
162 	return fSize;
163 }
164 
165 
166 void
SetSize(off_t size)167 SizeSlider::SetSize(off_t size)
168 {
169 	if (size == fSize)
170 		return;
171 
172 	SetValue(int32(1.0 * kMaxSliderLimit / fGranularity * size
173 		/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
174 	fSize = size;
175 	UpdateTextChanged();
176 }
177 
178 
179 off_t
Offset() const180 SizeSlider::Offset() const
181 {
182 	// TODO: This should be the changed offset once a double
183 	// headed slider is implemented.
184 	return fStartOffset;
185 }
186 
187 
188 off_t
MaxPartitionSize() const189 SizeSlider::MaxPartitionSize() const
190 {
191 	return fMaxPartitionSize;
192 }
193