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
28static const int32 kMaxSliderLimit = 0x7fffff80;
29	// this is the maximum value that BSlider seem to work with fine
30
31
32void
33dump_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
62bool
63is_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
74SpaceIDMap::SpaceIDMap()
75	:
76	HashMap<HashString, partition_id>(),
77	fNextSpaceID(-2)
78{
79}
80
81
82SpaceIDMap::~SpaceIDMap()
83{
84}
85
86
87partition_id
88SpaceIDMap::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
106SizeSlider::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
137SizeSlider::~SizeSlider()
138{
139}
140
141
142void
143SizeSlider::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
152const char*
153SizeSlider::UpdateText() const
154{
155	return string_for_size(Size(), fStatusLabel, sizeof(fStatusLabel));
156}
157
158
159off_t
160SizeSlider::Size() const
161{
162	return fSize;
163}
164
165
166void
167SizeSlider::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
179off_t
180SizeSlider::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
188off_t
189SizeSlider::MaxPartitionSize() const
190{
191	return fMaxPartitionSize;
192}
193