xref: /haiku/src/apps/diskusage/StatusView.cpp (revision 471b5ec49612ad795d34e574a26155272f3c339d)
1 /*
2  * Copyright (c) 2010 Philippe St-Pierre <stpere@gmail.com>. All rights reserved.
3  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
4  * Distributed under the terms of the MIT/X11 license.
5  *
6  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
7  * as long as it is accompanied by it's documentation and this copyright notice.
8  * The software comes with no warranty, etc.
9  */
10 
11 
12 #include "StatusView.h"
13 
14 #include <math.h>
15 #include <stdio.h>
16 
17 #include <Catalog.h>
18 #include <Box.h>
19 #include <Button.h>
20 #include <Node.h>
21 #include <String.h>
22 #include <StringView.h>
23 
24 #include <LayoutBuilder.h>
25 
26 #include "DiskUsage.h"
27 #include "Scanner.h"
28 
29 #undef B_TRANSLATE_CONTEXT
30 #define B_TRANSLATE_CONTEXT "Status View"
31 
32 StatusView::StatusView()
33 	:
34 	BView(NULL, B_WILL_DRAW),
35 	fCurrentFileInfo(NULL)
36 {
37 	SetViewColor(kPieBGColor);
38 	SetLowColor(kPieBGColor);
39 
40 	fSizeView = new BStringView(NULL, kEmptyStr);
41 	fSizeView->SetExplicitMinSize(BSize(StringWidth(B_TRANSLATE("9999.99 GB")),
42 		B_SIZE_UNSET));
43 	fSizeView->SetExplicitMaxSize(BSize(StringWidth(B_TRANSLATE("9999.99 GB")),
44 		B_SIZE_UNSET));
45 
46 	char testLabel[256];
47 	snprintf(testLabel, sizeof(testLabel), B_TRANSLATE("%d files"),
48 		999999);
49 
50 	fCountView = new BStringView(NULL, kEmptyStr);
51 	float width, height;
52 	fCountView->GetPreferredSize(&width, &height);
53 	fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel),
54 		B_SIZE_UNSET));
55 	fCountView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, height));
56 
57 	fPathView = new BStringView(NULL, kEmptyStr);
58 	fPathView->GetPreferredSize(&width, &height);
59 	fPathView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, height));
60 
61 	fRefreshBtn = new BButton(NULL, B_TRANSLATE("Scan"),
62 		new BMessage(kBtnRescan));
63 
64 	fRefreshBtn->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED));
65 
66 	BBox* divider1 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
67 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
68 
69 	BBox* divider2 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
70 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
71 
72 	divider1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
73 	divider2->SetExplicitMaxSize(BSize(1, B_SIZE_UNLIMITED));
74 
75 	SetLayout(new BGroupLayout(B_VERTICAL));
76 
77 	AddChild(BLayoutBuilder::Group<>(B_HORIZONTAL, 0)
78 		.AddGroup(B_VERTICAL, 0)
79 			.Add(fPathView)
80 			.Add(divider1)
81 			.AddGroup(B_HORIZONTAL, 0)
82 				.Add(fCountView)
83 				.Add(divider2)
84 				.Add(fSizeView)
85 				.End()
86 			.End()
87 		.AddStrut(kSmallHMargin)
88 		.Add(fRefreshBtn)
89 		.SetInsets(kSmallVMargin, kSmallVMargin, kSmallVMargin, kSmallVMargin)
90 	);
91 }
92 
93 
94 StatusView::~StatusView()
95 {
96 }
97 
98 
99 void
100 StatusView::SetRescanEnabled(bool enabled)
101 {
102 	fRefreshBtn->SetEnabled(enabled);
103 }
104 
105 
106 void
107 StatusView::SetBtnLabel(const char* label)
108 {
109 	fRefreshBtn->SetLabel(label);
110 }
111 
112 
113 void
114 StatusView::ShowInfo(const FileInfo* info)
115 {
116 	if (info == fCurrentFileInfo)
117 		return;
118 
119 	fCurrentFileInfo = info;
120 
121 	if (info == NULL) {
122 		fPathView->SetText(kEmptyStr);
123 		fSizeView->SetText(kEmptyStr);
124 		fCountView->SetText(kEmptyStr);
125 		return;
126 	}
127 
128 	if (!info->pseudo) {
129 		BNode node(&info->ref);
130 		if (node.InitCheck() != B_OK) {
131 			fPathView->SetText(B_TRANSLATE("file unavailable"));
132 			fSizeView->SetText(kEmptyStr);
133 			fCountView->SetText(kEmptyStr);
134 			return;
135 		}
136 	}
137 
138 	float viewWidth = fPathView->Bounds().Width();
139 	string path;
140 	info->GetPath(path);
141 	BString pathLabel = path.c_str();
142 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
143 	fPathView->SetText(pathLabel.String());
144 
145 	char label[B_PATH_NAME_LENGTH];
146 	size_to_string(info->size, label, sizeof(label));
147 	fSizeView->SetText(label);
148 
149 	if (info->count > 0) {
150 		char label[256];
151 		snprintf(label, sizeof(label), (info->count == 1) ?
152 			B_TRANSLATE("%d file") : B_TRANSLATE("%d files"), info->count);
153 		fCountView->SetText(label);
154 	} else {
155 		fCountView->SetText(kEmptyStr);
156 	}
157 }
158