xref: /haiku/src/apps/diskusage/StatusView.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
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 <StringForSize.h>
23 #include <StringView.h>
24 
25 #include <LayoutBuilder.h>
26 
27 #include "DiskUsage.h"
28 #include "Scanner.h"
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "Status View"
32 
33 StatusView::StatusView()
34 	:
35 	BView(NULL, B_WILL_DRAW),
36 	fCurrentFileInfo(NULL)
37 {
38 	SetViewColor(kPieBGColor);
39 	SetLowColor(kPieBGColor);
40 
41 	fSizeView = new BStringView(NULL, kEmptyStr);
42 	fSizeView->SetExplicitMinSize(BSize(StringWidth(B_TRANSLATE("9999.99 GB")),
43 		B_SIZE_UNSET));
44 	fSizeView->SetExplicitMaxSize(BSize(StringWidth(B_TRANSLATE("9999.99 GB")),
45 		B_SIZE_UNSET));
46 
47 	char testLabel[256];
48 	snprintf(testLabel, sizeof(testLabel), B_TRANSLATE("%d files"),
49 		999999);
50 
51 	fCountView = new BStringView(NULL, kEmptyStr);
52 	float width, height;
53 	fCountView->GetPreferredSize(&width, &height);
54 	fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel),
55 		B_SIZE_UNSET));
56 	fCountView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, height));
57 
58 	fPathView = new BStringView(NULL, kEmptyStr);
59 	fPathView->GetPreferredSize(&width, &height);
60 	fPathView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, height));
61 
62 	fRefreshBtn = new BButton(NULL, B_TRANSLATE("Scan"),
63 		new BMessage(kBtnRescan));
64 
65 	fRefreshBtn->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED));
66 
67 	BBox* divider1 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
68 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
69 
70 	BBox* divider2 = new BBox(BRect(), B_EMPTY_STRING, B_FOLLOW_ALL_SIDES,
71 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
72 
73 	divider1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
74 	divider2->SetExplicitMaxSize(BSize(1, B_SIZE_UNLIMITED));
75 
76 	SetLayout(new BGroupLayout(B_VERTICAL));
77 
78 	AddChild(BLayoutBuilder::Group<>(B_HORIZONTAL, 0)
79 		.AddGroup(B_VERTICAL, 0)
80 			.Add(fPathView)
81 			.Add(divider1)
82 			.AddGroup(B_HORIZONTAL, 0)
83 				.Add(fCountView)
84 				.Add(divider2)
85 				.Add(fSizeView)
86 				.End()
87 			.End()
88 		.AddStrut(kSmallHMargin)
89 		.Add(fRefreshBtn)
90 		.SetInsets(kSmallVMargin, kSmallVMargin, kSmallVMargin, kSmallVMargin)
91 	);
92 }
93 
94 
95 StatusView::~StatusView()
96 {
97 }
98 
99 
100 void
101 StatusView::EnableRescan()
102 {
103 	fRefreshBtn->SetLabel(B_TRANSLATE("Rescan"));
104 	fRefreshBtn->SetMessage(new BMessage(kBtnRescan));
105 }
106 
107 
108 void
109 StatusView::EnableCancel()
110 {
111 	fRefreshBtn->SetLabel(B_TRANSLATE("Abort"));
112 	fRefreshBtn->SetMessage(new BMessage(kBtnCancel));
113 }
114 
115 
116 void
117 StatusView::ShowInfo(const FileInfo* info)
118 {
119 	if (info == fCurrentFileInfo)
120 		return;
121 
122 	fCurrentFileInfo = info;
123 
124 	if (info == NULL) {
125 		fPathView->SetText(kEmptyStr);
126 		fSizeView->SetText(kEmptyStr);
127 		fCountView->SetText(kEmptyStr);
128 		return;
129 	}
130 
131 	if (!info->pseudo) {
132 		BNode node(&info->ref);
133 		if (node.InitCheck() != B_OK) {
134 			fPathView->SetText(B_TRANSLATE("file unavailable"));
135 			fSizeView->SetText(kEmptyStr);
136 			fCountView->SetText(kEmptyStr);
137 			return;
138 		}
139 	}
140 
141 	float viewWidth = fPathView->Bounds().Width();
142 	string path;
143 	info->GetPath(path);
144 	BString pathLabel = path.c_str();
145 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
146 	fPathView->SetText(pathLabel.String());
147 
148 	char label[B_PATH_NAME_LENGTH];
149 	string_for_size(info->size, label, sizeof(label));
150 	fSizeView->SetText(label);
151 
152 	if (info->count > 0) {
153 		char label[256];
154 		snprintf(label, sizeof(label), (info->count == 1) ?
155 			B_TRANSLATE("%d file") : B_TRANSLATE("%d files"), info->count);
156 		fCountView->SetText(label);
157 	} else {
158 		fCountView->SetText(kEmptyStr);
159 	}
160 }
161