vfs: check userland buffers before reading them.
[haiku.git] / src / apps / haikudepot / ui / WorkStatusView.cpp
blobd9d43b096e521c2c56720bebc43d9d7e4fd7a6fa
1 /*
2 * Copyright 2017 Julian Harnath <julian.harnath@rwth-aachen.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
7 #include "WorkStatusView.h"
9 #include <CardLayout.h>
10 #include <Catalog.h>
11 #include <LayoutBuilder.h>
12 #include <SeparatorView.h>
13 #include <StatusBar.h>
14 #include <StringView.h>
16 #include <stdio.h>
18 #include "BarberPole.h"
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "WorkStatusView"
25 WorkStatusView::WorkStatusView(const char* name)
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))
34 fProgressView->SetLayout(fProgressLayout);
35 fProgressLayout->AddView(fBarberPole);
36 fProgressLayout->AddView(fProgressBar);
38 fProgressBar->SetMaxValue(1.0f);
39 fProgressBar->SetBarHeight(20);
41 fStatusText->SetFontSize(be_plain_font->Size() * 0.9f);
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()
55 WorkStatusView::~WorkStatusView()
60 void
61 WorkStatusView::SetBusy(const BString& text)
63 SetText(text);
64 SetBusy();
68 void
69 WorkStatusView::SetBusy()
71 fBarberPole->Start();
72 if (fProgressLayout->VisibleIndex() != 0)
73 fProgressLayout->SetVisibleItem((int32)0);
77 void
78 WorkStatusView::SetIdle()
80 fBarberPole->Stop();
81 fProgressLayout->SetVisibleItem((int32)0);
82 SetText(NULL);
86 void
87 WorkStatusView::SetProgress(float value)
89 fProgressBar->SetTo(value);
90 if (fProgressLayout->VisibleIndex() != 1)
91 fProgressLayout->SetVisibleItem(1);
95 void
96 WorkStatusView::SetText(const BString& text)
98 fStatusText->SetText(text);
102 void
103 WorkStatusView::PackageStatusChanged(const PackageInfoRef& package)
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());
112 SetProgress(package->DownloadProgress());
113 break;
115 case PENDING:
116 fPendingPackages.insert(package->Name());
117 if (package->Name() == fDownloadingPackage)
118 fDownloadingPackage = "";
119 if (fDownloadingPackage.IsEmpty()) {
120 _SetTextPendingDownloads();
121 SetBusy();
123 break;
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();
138 break;
143 void
144 WorkStatusView::_SetTextPendingDownloads()
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);
157 void
158 WorkStatusView::_SetTextDownloading(const BString& title)
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)");
167 SetText(text);