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