xref: /haiku/src/apps/haikudepot/ui/WorkStatusView.cpp (revision 3995592cdf304335132305e27c40cbb0b1ac46e3)
1 /*
2  * Copyright 2017 Julian Harnath <julian.harnath@rwth-aachen.de>
3  * All rights reserved. Distributed under the terms of the MIT license.
4  */
5 
6 
7 #include "WorkStatusView.h"
8 
9 #include <CardLayout.h>
10 #include <Catalog.h>
11 #include <LayoutBuilder.h>
12 #include <SeparatorView.h>
13 #include <StatusBar.h>
14 #include <StringView.h>
15 
16 #include <stdio.h>
17 
18 #include "BarberPole.h"
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "WorkStatusView"
23 
24 
25 WorkStatusView::WorkStatusView(const char* name)
26 	:
27 	BView(name, 0),
28 	fProgressBar(new BStatusBar("progress bar")),
29 	fBarberPole(new BarberPole("barber pole")),
30 	fProgressLayout(new BCardLayout()),
31 	fProgressView(new BView("progress view", 0)),
32 	fStatusText(new BStringView("status text", NULL))
33 {
34 	fProgressView->SetLayout(fProgressLayout);
35 	fProgressLayout->AddView(fBarberPole);
36 	fProgressLayout->AddView(fProgressBar);
37 
38 	fProgressBar->SetMaxValue(1.0f);
39 	fProgressBar->SetBarHeight(20);
40 
41 	fStatusText->SetFontSize(be_plain_font->Size() * 0.9f);
42 
43 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
44 		.Add(new BSeparatorView())
45 		.AddGroup(B_HORIZONTAL)
46 			.SetInsets(5, 2, 5, 2)
47 			.Add(fProgressLayout, 0.2f)
48 			.Add(fStatusText)
49 			.AddGlue()
50 			.End()
51 	;
52 }
53 
54 
55 WorkStatusView::~WorkStatusView()
56 {
57 }
58 
59 
60 void
61 WorkStatusView::SetBusy(const BString& text)
62 {
63 	SetText(text);
64 	SetBusy();
65 }
66 
67 
68 void
69 WorkStatusView::SetBusy()
70 {
71 	fBarberPole->Start();
72 	if (fProgressLayout->VisibleIndex() != 0)
73 		fProgressLayout->SetVisibleItem((int32)0);
74 }
75 
76 
77 void
78 WorkStatusView::SetIdle()
79 {
80 	fBarberPole->Stop();
81 	fProgressLayout->SetVisibleItem((int32)0);
82 	SetText(NULL);
83 }
84 
85 
86 void
87 WorkStatusView::SetProgress(float value)
88 {
89 	fProgressBar->SetTo(value);
90 	if (fProgressLayout->VisibleIndex() != 1)
91 		fProgressLayout->SetVisibleItem(1);
92 }
93 
94 
95 void
96 WorkStatusView::SetText(const BString& text)
97 {
98 	fStatusText->SetText(text);
99 }
100 
101 
102 void
103 WorkStatusView::PackageStatusChanged(const PackageInfoRef& package)
104 {
105 	switch (package->State()) {
106 		case DOWNLOADING:
107 			fPendingPackages.erase(package->Name());
108 			if (package->Name() != fDownloadingPackage) {
109 				fDownloadingPackage = package->Name();
110 				_SetTextDownloading(package->Title());
111 			}
112 			SetProgress(package->DownloadProgress());
113 			break;
114 
115 		case PENDING:
116 			fPendingPackages.insert(package->Name());
117 			if (package->Name() == fDownloadingPackage)
118 				fDownloadingPackage = "";
119 			if (fDownloadingPackage.IsEmpty()) {
120 				_SetTextPendingDownloads();
121 				SetBusy();
122 			}
123 			break;
124 
125 		case NONE:
126 		case ACTIVATED:
127 		case INSTALLED:
128 		case UNINSTALLED:
129 			if (package->Name() == fDownloadingPackage)
130 				fDownloadingPackage = "";
131 			fPendingPackages.erase(package->Name());
132 			if (fPendingPackages.empty())
133 				SetIdle();
134 			else {
135 				_SetTextPendingDownloads();
136 				SetBusy();
137 			}
138 			break;
139 	}
140 }
141 
142 
143 void
144 WorkStatusView::_SetTextPendingDownloads()
145 {
146 	BString text;
147 	const size_t pendingCount = fPendingPackages.size();
148 	text << pendingCount;
149 	if (pendingCount > 1)
150 		text << B_TRANSLATE(" packages to download");
151 	else
152 		text << B_TRANSLATE(" package to download");
153 	SetText(text);
154 }
155 
156 
157 void
158 WorkStatusView::_SetTextDownloading(const BString& title)
159 {
160 	BString text(B_TRANSLATE("Downloading package "));
161 	text << title;
162 	if (!fPendingPackages.empty()) {
163 		text << " (";
164 		text << fPendingPackages.size();
165 		text << B_TRANSLATE(" more to download)");
166 	}
167 	SetText(text);
168 }
169