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