xref: /haiku/src/apps/diskusage/StatusView.cpp (revision 3e52a3d5e5479a4de086a481f3363765149fbd4e)
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_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_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::EnableRescan()
101 {
102 	fRefreshBtn->SetLabel(B_TRANSLATE("Rescan"));
103 	fRefreshBtn->SetMessage(new BMessage(kBtnRescan));
104 }
105 
106 
107 void
108 StatusView::EnableCancel()
109 {
110 	fRefreshBtn->SetLabel(B_TRANSLATE("Abort"));
111 	fRefreshBtn->SetMessage(new BMessage(kBtnCancel));
112 }
113 
114 
115 void
116 StatusView::ShowInfo(const FileInfo* info)
117 {
118 	if (info == fCurrentFileInfo)
119 		return;
120 
121 	fCurrentFileInfo = info;
122 
123 	if (info == NULL) {
124 		fPathView->SetText(kEmptyStr);
125 		fSizeView->SetText(kEmptyStr);
126 		fCountView->SetText(kEmptyStr);
127 		return;
128 	}
129 
130 	if (!info->pseudo) {
131 		BNode node(&info->ref);
132 		if (node.InitCheck() != B_OK) {
133 			fPathView->SetText(B_TRANSLATE("file unavailable"));
134 			fSizeView->SetText(kEmptyStr);
135 			fCountView->SetText(kEmptyStr);
136 			return;
137 		}
138 	}
139 
140 	float viewWidth = fPathView->Bounds().Width();
141 	string path;
142 	info->GetPath(path);
143 	BString pathLabel = path.c_str();
144 	be_plain_font->TruncateString(&pathLabel, B_TRUNCATE_BEGINNING, viewWidth);
145 	fPathView->SetText(pathLabel.String());
146 
147 	char label[B_PATH_NAME_LENGTH];
148 	size_to_string(info->size, label, sizeof(label));
149 	fSizeView->SetText(label);
150 
151 	if (info->count > 0) {
152 		char label[256];
153 		snprintf(label, sizeof(label), (info->count == 1) ?
154 			B_TRANSLATE("%d file") : B_TRANSLATE("%d files"), info->count);
155 		fCountView->SetText(label);
156 	} else {
157 		fCountView->SetText(kEmptyStr);
158 	}
159 }
160