From 0be6cbd37d215a535f13e564d597a11146e1ad04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sat, 10 Nov 2007 23:48:53 +0000 Subject: [PATCH] * extract a Support.cpp/h with utility functions * fix the problem with duplicated parent partition entries, we need to recurse into child rows when findig a particular BRow, or else RowAt() or CountRows() will only return the top level list entries * disabled the bitmap column for now, to get a better overview of "level" * display the device path also for partitions, not just devices git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22889 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/preferences/drivesetup/Jamfile | 1 + src/preferences/drivesetup/MainWindow.cpp | 53 +-------------- src/preferences/drivesetup/PartitionList.cpp | 43 ++++++------ src/preferences/drivesetup/PartitionList.h | 6 +- src/preferences/drivesetup/Support.cpp | 69 ++++++++++++++++++++ src/preferences/drivesetup/Support.h | 20 ++++++ 6 files changed, 118 insertions(+), 74 deletions(-) create mode 100644 src/preferences/drivesetup/Support.cpp create mode 100644 src/preferences/drivesetup/Support.h diff --git a/src/preferences/drivesetup/Jamfile b/src/preferences/drivesetup/Jamfile index 6c416a091c..f5decdbad6 100644 --- a/src/preferences/drivesetup/Jamfile +++ b/src/preferences/drivesetup/Jamfile @@ -10,6 +10,7 @@ Preference DriveSetup : DriveSetup.cpp MainWindow.cpp PartitionList.cpp + Support.cpp : be ; diff --git a/src/preferences/drivesetup/MainWindow.cpp b/src/preferences/drivesetup/MainWindow.cpp index a004fce208..acb302275d 100644 --- a/src/preferences/drivesetup/MainWindow.cpp +++ b/src/preferences/drivesetup/MainWindow.cpp @@ -9,6 +9,7 @@ */ #include "MainWindow.h" #include "PartitionList.h" +#include "Support.h" #include @@ -37,58 +38,6 @@ private: PartitionListView* fPartitionList; }; -const char* -SizeAsString(off_t size, char *string) -{ - double kb = size / 1024.0; - if (kb < 1.0) { - sprintf(string, "%Ld B", size); - return string; - } - float mb = kb / 1024.0; - if (mb < 1.0) { - sprintf(string, "%3.1f KB", kb); - return string; - } - float gb = mb / 1024.0; - if (gb < 1.0) { - sprintf(string, "%3.1f MB", mb); - return string; - } - float tb = gb / 1024.0; - if (tb < 1.0) { - sprintf(string, "%3.1f GB", gb); - return string; - } - sprintf(string, "%.1f TB", tb); - return string; -} - - -static void -dump_partition_info(BPartition* partition) -{ - char size[1024]; - printf("\tOffset(): %Ld\n", partition->Offset()); - printf("\tSize(): %s\n", SizeAsString(partition->Size(),size)); - printf("\tContentSize(): %s\n", SizeAsString(partition->ContentSize(), size)); - printf("\tBlockSize(): %ld\n", partition->BlockSize()); - printf("\tIndex(): %ld\n", partition->Index()); - printf("\tStatus(): %ld\n\n", partition->Status()); - printf("\tContainsFileSystem(): %s\n", partition->ContainsFileSystem() ? "true" : "false"); - printf("\tContainsPartitioningSystem(): %s\n\n", partition->ContainsPartitioningSystem() ? "true" : "false"); - printf("\tIsDevice(): %s\n", partition->IsDevice() ? "true" : "false"); - printf("\tIsReadOnly(): %s\n", partition->IsReadOnly() ? "true" : "false"); - printf("\tIsMounted(): %s\n", partition->IsMounted() ? "true" : "false"); - printf("\tIsBusy(): %s\n\n", partition->IsBusy() ? "true" : "false"); - printf("\tFlags(): %lx\n\n", partition->Flags()); - printf("\tName(): %s\n", partition->Name()); - printf("\tContentName(): %s\n", partition->ContentName()); - printf("\tType(): %s\n", partition->Type()); - printf("\tContentType(): %s\n", partition->ContentType()); - printf("\tID(): %lx\n\n", partition->ID()); -} - enum { MSG_MOUNT_ALL = 'mnta', diff --git a/src/preferences/drivesetup/PartitionList.cpp b/src/preferences/drivesetup/PartitionList.cpp index be2ff872da..beb01f4b69 100644 --- a/src/preferences/drivesetup/PartitionList.cpp +++ b/src/preferences/drivesetup/PartitionList.cpp @@ -6,15 +6,12 @@ * Ithamar R. Adema */ #include "PartitionList.h" +#include "Support.h" #include #include -extern const char* -SizeAsString(off_t size, char* string); //FIXME: from MainWindow.cpp - - PartitionListRow::PartitionListRow(BPartition* partition) : Inherited() , fPartitionID(partition->ID()) @@ -24,12 +21,12 @@ PartitionListRow::PartitionListRow(BPartition* partition) partition->GetPath(&path); - SetField(new BBitmapField(NULL), 0); +// SetField(new BBitmapField(NULL), 0); - if (partition->IsDevice()) // Only show device path for actual devices (so only for /dev/disk/..../raw entries) +// if (partition->IsDevice()) // Only show device path for actual devices (so only for /dev/disk/..../raw entries) SetField(new BStringField(path.Path()), 1); - else - SetField(new BStringField(""), 1); +// else +// SetField(new BStringField("n/a"), 1); // if (partition->ContainsPartitioningSystem()) { // SetField(new BStringField(partition->ContentType()), 2); @@ -51,14 +48,14 @@ PartitionListRow::PartitionListRow(BPartition* partition) SetField(new BStringField(""), 4); } - SetField(new BStringField(SizeAsString(partition->Size(), size)), 5); + SetField(new BStringField(string_for_size(partition->Size(), size)), 5); } PartitionListView::PartitionListView(const BRect& frame) : Inherited(frame, "storagelist", B_FOLLOW_ALL, 0, B_NO_BORDER, true) { - AddColumn(new BBitmapColumn("", 20, 20, 100, B_ALIGN_CENTER), 0); +// AddColumn(new BBitmapColumn("", 20, 20, 100, B_ALIGN_CENTER), 0); AddColumn(new BStringColumn("Device", 100, 50, 500, B_TRUNCATE_MIDDLE), 1); AddColumn(new BStringColumn("Filesystem", 100, 50, 500, B_TRUNCATE_MIDDLE), 2); AddColumn(new BStringColumn("Volume Name", 100, 50, 500, B_TRUNCATE_MIDDLE), 3); @@ -68,12 +65,18 @@ PartitionListView::PartitionListView(const BRect& frame) PartitionListRow* -PartitionListView::FindRow(partition_id id) +PartitionListView::FindRow(partition_id id, PartitionListRow* parent) { - for (int32 i = 0; i < CountRows(); i++) { - PartitionListRow* item = dynamic_cast(RowAt(i)); + for (int32 i = 0; i < CountRows(parent); i++) { + PartitionListRow* item = dynamic_cast(RowAt(i, parent)); if (item != NULL && item->ID() == id) return item; + if (CountRows(item) > 0) { + // recurse into child rows + item = FindRow(id, item); + if (item) + return item; + } } return NULL; @@ -83,28 +86,28 @@ PartitionListView::FindRow(partition_id id) PartitionListRow* PartitionListView::AddPartition(BPartition* partition) { - PartitionListRow* parent = NULL; - PartitionListRow* partitionrow = NULL; + PartitionListRow* partitionrow = FindRow(partition->ID()); // Forget about it if this partition is already in the listview - if ((partitionrow = FindRow(partition->ID())) != NULL) + if (partitionrow != NULL) { return partitionrow; + } // Create the row for this partition partitionrow = new PartitionListRow(partition); // If this partition has a parent... if (partition->Parent() != NULL) { -printf("partition has parent\n"); // check if it is in the listview - parent = FindRow(partition->Parent()->ID()); + PartitionListRow* parent = FindRow(partition->Parent()->ID()); // If parent of this partition is not yet in the list - if (parent == NULL) //add it + if (parent == NULL) { + // add it parent = AddPartition(partition->Parent()); + } // Now it is ok to add this partition under its parent AddRow(partitionrow, parent); } else { -printf("partition has NO parent\n"); // If this partition has no parent, add it in the 'root' AddRow(partitionrow); } diff --git a/src/preferences/drivesetup/PartitionList.h b/src/preferences/drivesetup/PartitionList.h index 0509c2909b..f0ef4a925a 100644 --- a/src/preferences/drivesetup/PartitionList.h +++ b/src/preferences/drivesetup/PartitionList.h @@ -22,7 +22,8 @@ class PartitionListRow : public BRow { public: PartitionListRow(BPartition* partition); - partition_id ID() { return fPartitionID; } + partition_id ID() const + { return fPartitionID; } private: partition_id fPartitionID; }; @@ -33,7 +34,8 @@ class PartitionListView : public BColumnListView { public: PartitionListView(const BRect& frame); - PartitionListRow* FindRow(partition_id id); + PartitionListRow* FindRow(partition_id id, + PartitionListRow* parent = NULL); PartitionListRow* AddPartition(BPartition* partition); }; diff --git a/src/preferences/drivesetup/Support.cpp b/src/preferences/drivesetup/Support.cpp new file mode 100644 index 0000000000..597c895d2f --- /dev/null +++ b/src/preferences/drivesetup/Support.cpp @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2007 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT license. + * + * Authors: + * Erik Jaesler + * Ithamar R. Adema + * Stephan Aßmus + */ +#include "Support.h" + +#include + + +const char* +string_for_size(off_t size, char *string) +{ + double kb = size / 1024.0; + if (kb < 1.0) { + sprintf(string, "%Ld B", size); + return string; + } + float mb = kb / 1024.0; + if (mb < 1.0) { + sprintf(string, "%3.1f KB", kb); + return string; + } + float gb = mb / 1024.0; + if (gb < 1.0) { + sprintf(string, "%3.1f MB", mb); + return string; + } + float tb = gb / 1024.0; + if (tb < 1.0) { + sprintf(string, "%3.1f GB", gb); + return string; + } + sprintf(string, "%.1f TB", tb); + return string; +} + + +void +dump_partition_info(const BPartition* partition) +{ + char size[1024]; + printf("\tOffset(): %Ld\n", partition->Offset()); + printf("\tSize(): %s\n", string_for_size(partition->Size(),size)); + printf("\tContentSize(): %s\n", string_for_size(partition->ContentSize(), + size)); + printf("\tBlockSize(): %ld\n", partition->BlockSize()); + printf("\tIndex(): %ld\n", partition->Index()); + printf("\tStatus(): %ld\n\n", partition->Status()); + printf("\tContainsFileSystem(): %s\n", + partition->ContainsFileSystem() ? "true" : "false"); + printf("\tContainsPartitioningSystem(): %s\n\n", + partition->ContainsPartitioningSystem() ? "true" : "false"); + printf("\tIsDevice(): %s\n", partition->IsDevice() ? "true" : "false"); + printf("\tIsReadOnly(): %s\n", partition->IsReadOnly() ? "true" : "false"); + printf("\tIsMounted(): %s\n", partition->IsMounted() ? "true" : "false"); + printf("\tIsBusy(): %s\n\n", partition->IsBusy() ? "true" : "false"); + printf("\tFlags(): %lx\n\n", partition->Flags()); + printf("\tName(): %s\n", partition->Name()); + printf("\tContentName(): %s\n", partition->ContentName()); + printf("\tType(): %s\n", partition->Type()); + printf("\tContentType(): %s\n", partition->ContentType()); + printf("\tID(): %lx\n\n", partition->ID()); +} + diff --git a/src/preferences/drivesetup/Support.h b/src/preferences/drivesetup/Support.h new file mode 100644 index 0000000000..82d0f37624 --- /dev/null +++ b/src/preferences/drivesetup/Support.h @@ -0,0 +1,20 @@ +/* + * Copyright 2002-2007 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT license. + */ +#ifndef SUPPORT_H +#define SUPPORT_H + + +#include + + +class BPartition; + + +const char* string_for_size(off_t size, char *string); + +void dump_partition_info(const BPartition* partition); + + +#endif // SUPPORT_H