repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / drivesetup / Support.cpp
blob5de07d6025c837c46c97c252d99e3a6a015a98ec
1 /*
2 * Copyright 2002-2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
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>
14 #include "Support.h"
16 #include <stdio.h>
18 #include <Catalog.h>
19 #include <Locale.h>
20 #include <Partition.h>
21 #include <String.h>
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Support"
28 static const int32 kMaxSliderLimit = 0x7fffff80;
29 // this is the maximum value that BSlider seem to work with fine
32 void
33 dump_partition_info(const BPartition* partition)
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());
61 bool
62 is_valid_partitionable_space(size_t size)
64 // TODO: remove this again, the DiskDeviceAPI should
65 // not even show these spaces to begin with
66 return size >= 8 * 1024 * 1024;
70 // #pragma mark - SpaceIDMap
73 SpaceIDMap::SpaceIDMap()
75 HashMap<HashString, partition_id>(),
76 fNextSpaceID(-2)
81 SpaceIDMap::~SpaceIDMap()
86 partition_id
87 SpaceIDMap::SpaceIDFor(partition_id parentID, off_t spaceOffset)
89 BString key;
90 key << parentID << ':' << (uint64)spaceOffset;
92 if (ContainsKey(key.String()))
93 return Get(key.String());
95 partition_id newID = fNextSpaceID--;
96 Put(key.String(), newID);
98 return newID;
102 // #pragma mark -
105 SizeSlider::SizeSlider(const char* name, const char* label,
106 BMessage* message, off_t offset, off_t size, uint32 minGranularity)
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)
115 rgb_color fillColor = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
116 UseFillColor(true, &fillColor);
118 // Lazy loop to get a power of two granularity
119 while (size / fGranularity > kMaxSliderLimit)
120 fGranularity *= 2;
122 SetKeyIncrementValue(int32(1024 * 1024 * 1.0 * kMaxSliderLimit
123 / ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
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);
136 SizeSlider::~SizeSlider()
141 void
142 SizeSlider::SetValue(int32 value)
144 BSlider::SetValue(value);
146 fSize = (off_t(1.0 * (MaxPartitionSize() - 1) * Value()
147 / kMaxSliderLimit + 0.5) / fGranularity) * fGranularity;
151 const char*
152 SizeSlider::UpdateText() const
154 return string_for_size(Size(), fStatusLabel, sizeof(fStatusLabel));
158 off_t
159 SizeSlider::Size() const
161 return fSize;
165 void
166 SizeSlider::SetSize(off_t size)
168 if (size == fSize)
169 return;
171 SetValue(int32(1.0 * kMaxSliderLimit / fGranularity * size
172 / ((MaxPartitionSize() - 1) / fGranularity) + 0.5));
173 fSize = size;
174 UpdateTextChanged();
178 off_t
179 SizeSlider::Offset() const
181 // TODO: This should be the changed offset once a double
182 // headed slider is implemented.
183 return fStartOffset;
187 off_t
188 SizeSlider::MaxPartitionSize() const
190 return fMaxPartitionSize;