xref: /haiku/src/apps/drivesetup/Support.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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
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("\tIndex(): %" B_PRId32 "\n", partition->Index());
43 	printf("\tStatus(): %" B_PRId32 "\n\n", partition->Status());
44 	printf("\tContainsFileSystem(): %s\n",
45 		partition->ContainsFileSystem() ? "true" : "false");
46 	printf("\tContainsPartitioningSystem(): %s\n\n",
47 		partition->ContainsPartitioningSystem() ? "true" : "false");
48 	printf("\tIsDevice(): %s\n", partition->IsDevice() ? "true" : "false");
49 	printf("\tIsReadOnly(): %s\n", partition->IsReadOnly() ? "true" : "false");
50 	printf("\tIsMounted(): %s\n", partition->IsMounted() ? "true" : "false");
51 	printf("\tIsBusy(): %s\n\n", partition->IsBusy() ? "true" : "false");
52 	printf("\tFlags(): %" B_PRIx32 "\n\n", partition->Flags());
53 	printf("\tName(): %s\n", partition->Name());
54 	printf("\tContentName(): %s\n", partition->ContentName());
55 	printf("\tType(): %s\n", partition->Type());
56 	printf("\tContentType(): %s\n", partition->ContentType());
57 	printf("\tID(): %" B_PRIx32 "\n\n", partition->ID());
58 }
59 
60 
61 bool
62 is_valid_partitionable_space(size_t size)
63 {
64 	// TODO: remove this again, the DiskDeviceAPI should
65 	// not even show these spaces to begin with
66 	return size >= 8 * 1024 * 1024;
67 }
68 
69 
70 // #pragma mark - SpaceIDMap
71 
72 
73 SpaceIDMap::SpaceIDMap()
74 	:
75 	HashMap<HashString, partition_id>(),
76 	fNextSpaceID(-2)
77 {
78 }
79 
80 
81 SpaceIDMap::~SpaceIDMap()
82 {
83 }
84 
85 
86 partition_id
87 SpaceIDMap::SpaceIDFor(partition_id parentID, off_t spaceOffset)
88 {
89 	BString key;
90 	key << parentID << ':' << (uint64)spaceOffset;
91 
92 	if (ContainsKey(key.String()))
93 		return Get(key.String());
94 
95 	partition_id newID = fNextSpaceID--;
96 	Put(key.String(), newID);
97 
98 	return newID;
99 }
100 
101 
102 // #pragma mark -
103 
104 
105 SizeSlider::SizeSlider(const char* name, const char* label,
106 		BMessage* message, off_t offset, off_t size, uint32 minGranularity)
107 	:
108 	BSlider(name, label, message, 0, kMaxSliderLimit, B_HORIZONTAL,
109 		B_TRIANGLE_THUMB),
110 	fStartOffset(offset),
111 	fEndOffset(offset + size),
112 	fMaxPartitionSize(size),
113 	fGranularity(minGranularity)
114 {
115 	rgb_color fillColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
116 	UseFillColor(true, &fillColor);
117 
118 	// Lazy loop to get a power of two granularity
119 	while (size / fGranularity > kMaxSliderLimit)
120 		fGranularity *= 2;
121 
122 	SetKeyIncrementValue(int32(1024 * 1024 * 1.0 * kMaxSliderLimit
123 		/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
124 
125 	char buffer[64];
126 	char minString[64];
127 	char maxString[64];
128 	snprintf(minString, sizeof(minString), B_TRANSLATE("Offset: %s"),
129 		string_for_size(fStartOffset, buffer, sizeof(buffer)));
130 	snprintf(maxString, sizeof(maxString), B_TRANSLATE("End: %s"),
131 		string_for_size(fEndOffset, buffer, sizeof(buffer)));
132 	SetLimitLabels(minString, maxString);
133 }
134 
135 
136 SizeSlider::~SizeSlider()
137 {
138 }
139 
140 
141 void
142 SizeSlider::SetValue(int32 value)
143 {
144 	BSlider::SetValue(value);
145 
146 	fSize = (off_t(1.0 * (MaxPartitionSize() - 1) * Value()
147 		/ kMaxSliderLimit + 0.5) / fGranularity) * fGranularity;
148 }
149 
150 
151 const char*
152 SizeSlider::UpdateText() const
153 {
154 	return string_for_size(Size(), fStatusLabel, sizeof(fStatusLabel));
155 }
156 
157 
158 off_t
159 SizeSlider::Size() const
160 {
161 	return fSize;
162 }
163 
164 
165 void
166 SizeSlider::SetSize(off_t size)
167 {
168 	if (size == fSize)
169 		return;
170 
171 	SetValue(int32(1.0 * kMaxSliderLimit / fGranularity * size
172 		/ ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
173 	fSize = size;
174 	UpdateTextChanged();
175 }
176 
177 
178 off_t
179 SizeSlider::Offset() const
180 {
181 	// TODO: This should be the changed offset once a double
182 	// headed slider is implemented.
183 	return fStartOffset;
184 }
185 
186 
187 off_t
188 SizeSlider::MaxPartitionSize() const
189 {
190 	return fMaxPartitionSize;
191 }
192