MediaConverter: Use BNumberFormat for percentage value

This provides automatic percentage formatting according to the locale
setting.

Also replaces some occurences of sprintf with SetToFormat.

Change-Id: I330aaebfe4615f4b40b00bd04f83a1a6d8efd6a8
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6204
Tested-by: Automation <automation@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Emir SARI 2023-03-13 16:54:04 +03:00 committed by waddlesplash
parent 4a0a64d446
commit 304569520d
2 changed files with 75 additions and 46 deletions

View File

@ -15,6 +15,7 @@
#include <Locale.h>
#include <MediaFile.h>
#include <MediaTrack.h>
#include <NumberFormat.h>
#include <Mime.h>
#include <Path.h>
#include <String.h>
@ -380,6 +381,8 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
status_t ret = B_OK;
bool multiTrack = false;
BNumberFormat fNumberFormat;
int32 tracks = inFile->CountTracks();
for (int32 i = 0; i < tracks && (!outAudTrack || !outVidTrack); i++) {
BMediaTrack* inTrack = inFile->TrackAt(i);
@ -565,11 +568,8 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
for (int64 i = start; (i < end) && !fCancel; i += framesRead) {
if ((ret = inVidTrack->ReadFrames(videoBuffer, &framesRead,
&mh)) != B_OK) {
fprintf(stderr, "Error reading video frame %" B_PRId64 ": %s\n",
i, strerror(ret));
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Error read video frame %" B_PRId64), i);
status.UnlockBuffer();
fprintf(stderr, "Error reading video frame %" B_PRId64 ": %s\n", i, strerror(ret));
status.SetToFormat(B_TRANSLATE("Error read video frame %" B_PRId64), i);
SetStatusMessage(status.String());
break;
@ -577,11 +577,8 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
if ((ret = outVidTrack->WriteFrames(videoBuffer, framesRead,
mh.u.encoded_video.field_flags)) != B_OK) {
fprintf(stderr, "Error writing video frame %" B_PRId64 ": %s\n",
i, strerror(ret));
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Error writing video frame %" B_PRId64), i);
status.UnlockBuffer();
fprintf(stderr, "Error writing video frame %" B_PRId64 ": %s\n", i, strerror(ret));
status.SetToFormat(B_TRANSLATE("Error writing video frame %" B_PRId64), i);
SetStatusMessage(status.String());
break;
@ -590,12 +587,16 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
currPercent = (int32)completePercent;
if (currPercent > lastPercent) {
lastPercent = currPercent;
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Writing video track: %" B_PRId32 "%% complete"),
currPercent);
status.UnlockBuffer();
SetStatusMessage(status.String());
BString data;
double percentValue = (double)currPercent;
if (fNumberFormat.FormatPercent(data, percentValue / 100) != B_OK) {
status.SetToFormat(B_TRANSLATE("Writing video track: %" B_PRId32 "%% \
complete"), currPercent);
}
status.SetToFormat(B_TRANSLATE("Writing video track: %s complete"), data.String());
SetStatusMessage(status.String());
}
}
outVidTrack->Flush();
@ -626,9 +627,7 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
if ((ret = inAudTrack->ReadFrames(audioBuffer, &framesRead,
&mh)) != B_OK) {
fprintf(stderr, "Error reading audio frames: %s\n", strerror(ret));
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Error read audio frame %" B_PRId64), i);
status.UnlockBuffer();
status.SetToFormat(B_TRANSLATE("Error read audio frame %" B_PRId64), i);
SetStatusMessage(status.String());
break;
@ -637,9 +636,7 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
if ((ret = outAudTrack->WriteFrames(audioBuffer,
framesRead)) != B_OK) {
fprintf(stderr, "Error writing audio frames: %s\n", strerror(ret));
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Error writing audio frame %" B_PRId64), i);
status.UnlockBuffer();
status.SetToFormat(B_TRANSLATE("Error writing audio frame %" B_PRId64), i);
SetStatusMessage(status.String());
break;
@ -648,10 +645,15 @@ MediaConverterApp::_ConvertFile(BMediaFile* inFile, BMediaFile* outFile,
currPercent = (int32)completePercent;
if (currPercent > lastPercent) {
lastPercent = currPercent;
snprintf(status.LockBuffer(128), 128,
B_TRANSLATE("Writing audio track: %" B_PRId32 "%% complete"),
currPercent);
status.UnlockBuffer();
BString data;
double percentValue = (double)currPercent;
if (fNumberFormat.FormatPercent(data, percentValue / 100) != B_OK) {
status.SetToFormat(B_TRANSLATE("Writing audio track: %" B_PRId32 "%% \
complete"), currPercent);
}
status.SetToFormat(B_TRANSLATE("Writing audio track: %s complete"), data.String());
SetStatusMessage(status.String());
}
}

View File

@ -25,6 +25,7 @@
#include <MenuBar.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <NumberFormat.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <Roster.h>
@ -306,8 +307,9 @@ MediaConverterWindow::MessageReceived(BMessage* message)
{
entry_ref inRef;
char buffer[40];
BEntry inEntry;
BNumberFormat fNumberFormat;
BString buffer;
switch (message->what) {
#if B_BEOS_VERSION <= B_BEOS_VERSION_6
@ -489,22 +491,34 @@ MediaConverterWindow::MessageReceived(BMessage* message)
case VIDEO_QUALITY_CHANGED_MESSAGE:
{
int32 value;
BString data;
message->FindInt32("be:value", &value);
snprintf(buffer, sizeof(buffer),
B_TRANSLATE("Video quality: %3d%%"), (int8)value);
fVideoQualitySlider->SetLabel(buffer);
fVideoQuality = value;
double percentValue = value / 100.0;
if (fNumberFormat.FormatPercent(data, percentValue) != B_OK)
buffer.SetToFormat(B_TRANSLATE("Video quality: %d%%"), (int8)value);
buffer.SetToFormat(B_TRANSLATE("Video quality: %s"), data.String());
fVideoQualitySlider->SetLabel(buffer.String());
fVideoQuality = (int)percentValue;
break;
}
case AUDIO_QUALITY_CHANGED_MESSAGE:
{
int32 value;
BString data;
message->FindInt32("be:value", &value);
snprintf(buffer, sizeof(buffer),
B_TRANSLATE("Audio quality: %3d%%"), (int8)value);
fAudioQualitySlider->SetLabel(buffer);
fAudioQuality = value;
double percentValue = value / 100.0;
if (fNumberFormat.FormatPercent(data, percentValue) != B_OK)
buffer.SetToFormat(B_TRANSLATE("Audio quality: %d%%"), (int8)value);
buffer.SetToFormat(B_TRANSLATE("Audio quality: %s"), data.String());
fAudioQualitySlider->SetLabel(buffer.String());
fAudioQuality = (int)percentValue;
break;
}
@ -896,6 +910,8 @@ MediaConverterWindow::TruncateOutputFolderPath()
void
MediaConverterWindow::_UpdateLabels()
{
BNumberFormat fNumberFormat;
if (fSourcesBox != NULL) {
fSourcesBox->SetLabel(B_TRANSLATE("Source files"));
_UpdateBBoxLayoutInsets(fSourcesBox);
@ -919,21 +935,32 @@ MediaConverterWindow::_UpdateLabels()
fDestButton->SetLabel(B_TRANSLATE("Output folder"));
if (fVideoQualitySlider != NULL) {
char buffer[40];
snprintf(buffer, sizeof(buffer), B_TRANSLATE("Video quality: %3d%%"),
(int8)fVideoQuality);
fVideoQualitySlider->SetLabel(buffer);
fVideoQualitySlider->SetLimitLabels(B_TRANSLATE("Low"),
B_TRANSLATE("High"));
BString buffer;
BString data;
double percentValue = fVideoQuality / 100.0;
if (fNumberFormat.FormatPercent(data, percentValue) != B_OK)
buffer.SetToFormat(B_TRANSLATE("Video quality: %d%%"), (int8)fVideoQuality);
buffer.SetToFormat(B_TRANSLATE("Video quality: %s"), data.String());
fVideoQuality = (int)percentValue;
fVideoQualitySlider->SetLabel(buffer.String());
fVideoQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
}
if (fAudioQualitySlider != NULL) {
char buffer[40];
snprintf(buffer, sizeof(buffer), B_TRANSLATE("Audio quality: %3d%%"),
(int8)fAudioQuality);
fAudioQualitySlider->SetLabel(buffer);
fAudioQualitySlider->SetLimitLabels(B_TRANSLATE("Low"),
B_TRANSLATE("High"));
BString buffer;
BString data;
double percentValue = fAudioQuality / 100.0;
if (fNumberFormat.FormatPercent(data, percentValue) != B_OK) {
buffer.SetToFormat(B_TRANSLATE("Audio quality: %d%%"), (int8)fAudioQuality);
}
buffer.SetToFormat(B_TRANSLATE("Audio quality: %s"), data.String());
fAudioQuality = (int)percentValue;
fAudioQualitySlider->SetLabel(buffer.String());
fAudioQualitySlider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
}
if (fStartDurationTC != NULL)