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