xref: /haiku/src/apps/diskusage/StatusView.cpp (revision 973f8e214d612688c4a30fd48572a7eb57c902be)
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 	sprintf(testLabel, B_TRANSLATE("%d files"), 999999);
48 
49 	fCountView = new BStringView(NULL, kEmptyStr);
50 	fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel), B_SIZE_UNSET));
51 	fCountView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
52 
53 	fPathView = new BStringView(NULL, kEmptyStr);
54 	fPathView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
55 
56 	fRefreshBtn = new BButton(NULL, B_TRANSLATE("Scan"), new BMessage(kBtnRescan));
57 
58 	fRefreshBtn->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED));
59 
60 	BBox* divider1 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
61 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
62 
63 	BBox* divider2 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
64 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
65 
66 	divider1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
67 	divider2->SetExplicitMaxSize(BSize(1, B_SIZE_UNLIMITED));
68 
69 	SetLayout(new BGroupLayout(B_VERTICAL));
70 
71 	AddChild(BLayoutBuilder::Group<>(B_HORIZONTAL)
72 		.AddGroup(B_VERTICAL)
73 			.Add(fPathView)
74 			.Add(divider1)
75 			.AddGroup(B_HORIZONTAL)
76 				.Add(fCountView)
77 				.Add(divider2)
78 				.Add(fSizeView)
79 				.End()
80 			.End()
81 		.AddStrut(kSmallHMargin)
82 		.Add(fRefreshBtn)
83 		.SetInsets(kSmallHMargin, kSmallVMargin, kSmallVMargin, kSmallVMargin)
84 	);
85 }
86 
87 
88 StatusView::~StatusView()
89 {
90 }
91 
92 
93 void
94 StatusView::SetRescanEnabled(bool enabled)
95 {
96 	fRefreshBtn->SetEnabled(enabled);
97 }
98 
99 
100 void
101 StatusView::SetBtnLabel(const char* label)
102 {
103 	fRefreshBtn->SetLabel(label);
104 }
105 
106 
107 void
108 StatusView::ShowInfo(const FileInfo* info)
109 {
110 	if (info == fCurrentFileInfo)
111 		return;
112 
113 	fCurrentFileInfo = info;
114 
115 	if (info == NULL) {
116 		fPathView->SetText(kEmptyStr);
117 		fSizeView->SetText(kEmptyStr);
118 		fCountView->SetText(kEmptyStr);
119 		return;
120 	}
121 
122 	if (!info->pseudo) {
123 		BNode node(&info->ref);
124 		if (node.InitCheck() != B_OK) {
125 			fPathView->SetText(B_TRANSLATE("file unavailable"));
126 			fSizeView->SetText(kEmptyStr);
127 			fCountView->SetText(kEmptyStr);
128 			return;
129 		}
130 	}
131 
132 	float viewWidth = fPathView->Bounds().Width();
133 	string path;
134 	info->GetPath(path);
135 	BString pathLabel = path.c_str();
136 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
137 	fPathView->SetText(pathLabel.String());
138 
139 	char label[B_PATH_NAME_LENGTH];
140 	size_to_string(info->size, label);
141 	fSizeView->SetText(label);
142 
143 	if (info->count > 0) {
144 		char label[256];
145 		sprintf(label, (info->count == 1) ? B_TRANSLATE("%d file") :
146 			B_TRANSLATE("%d files"), info->count);
147 		fCountView->SetText(label);
148 	} else {
149 		fCountView->SetText(kEmptyStr);
150 	}
151 }
152