diff --git a/CHANGELOG.md b/CHANGELOG.md index cd1c3573..e56b5a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * KColorScheme migration has been completed. * KIconThemes migration has been completed. * The MeshMC News section is now displayed via KF6 Syndication. +* KFormat migration has been completed. ## Removed diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 3efa871a..19f58d00 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -306,10 +306,6 @@ target_sources(MeshMC_logic PRIVATE # A Recursive file system watcher RecursiveFileSystemWatcher.h RecursiveFileSystemWatcher.cpp - - # Time - MMCTime.h - MMCTime.cpp ) add_subdirectory(pathmatcher) diff --git a/launcher/MMCTime.cpp b/launcher/MMCTime.cpp deleted file mode 100644 index 6ea890b1..00000000 --- a/launcher/MMCTime.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* SPDX-FileCopyrightText: 2026 Project Tick - * SPDX-FileContributor: Project Tick - * SPDX-License-Identifier: Apache-2.0 - * - * Copyright 2015 Petr Mrazek - * Copyright 2021 Jamie Mansfield - * Copyright (C) 2026 Project Tick - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include - -QString Time::prettifyDuration(int64_t duration) -{ - int seconds = (int)(duration % 60); - duration /= 60; - int minutes = (int)(duration % 60); - duration /= 60; - int hours = (int)(duration % 24); - int days = (int)(duration / 24); - if ((hours == 0) && (days == 0)) { - return i18n("%1m %2s", minutes, seconds); - } - if (days == 0) { - return i18n("%1h %2m", hours, minutes); - } - return i18n("%1d %2h %3m", days, hours, minutes); -} diff --git a/launcher/MMCTime.h b/launcher/MMCTime.h deleted file mode 100644 index 5ae3a125..00000000 --- a/launcher/MMCTime.h +++ /dev/null @@ -1,30 +0,0 @@ -/* SPDX-FileCopyrightText: 2026 Project Tick - * SPDX-FileContributor: Project Tick - * SPDX-License-Identifier: Apache-2.0 - * - * Copyright 2021 Jamie Mansfield - * Copyright (C) 2026 Project Tick - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include - -namespace Time -{ - - QString prettifyDuration(int64_t duration); - -} diff --git a/launcher/minecraft/MinecraftInstance.cpp b/launcher/minecraft/MinecraftInstance.cpp index f5be66d0..010b005d 100644 --- a/launcher/minecraft/MinecraftInstance.cpp +++ b/launcher/minecraft/MinecraftInstance.cpp @@ -31,7 +31,7 @@ #include "pathmatcher/MultiMatcher.h" #include "FileSystem.h" #include "java/JavaVersion.h" -#include "MMCTime.h" +#include #include "launch/LaunchTask.h" #include "launch/steps/LookupServerAddress.h" @@ -100,6 +100,16 @@ class OrSetting : public Setting std::shared_ptr m_b; }; +namespace +{ + // A time played, given in seconds, as "1 hr 2 min". + QString playTime(qint64 seconds) + { + return KFormat().formatDuration(static_cast(seconds) * 1000, + KFormat::AbbreviatedDuration); + } +} // namespace + MinecraftInstance::MinecraftInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString& rootDir) @@ -955,12 +965,12 @@ QString MinecraftInstance::getStatusbarDescription() if (m_settings->get("ShowGameTime").toBool()) { if (lastTimePlayed() > 0) { description.append( - i18n(", last played for %1", Time::prettifyDuration(lastTimePlayed()))); + i18n(", last played for %1", playTime(lastTimePlayed()))); } if (totalTimePlayed() > 0) { description.append( - i18n(", total played for %1", Time::prettifyDuration(totalTimePlayed()))); + i18n(", total played for %1", playTime(totalTimePlayed()))); } } if (hasCrashed()) { diff --git a/launcher/net/NetJob.cpp b/launcher/net/NetJob.cpp index 8fdd815c..681aee5d 100644 --- a/launcher/net/NetJob.cpp +++ b/launcher/net/NetJob.cpp @@ -22,7 +22,7 @@ #include "Download.h" #include -#include +#include #include TaskStepProgressList NetJob::getStepProgress() const @@ -57,12 +57,16 @@ void NetJob::emitPartStep(int index, TaskStepState state) } } - QLocale locale; + const KFormat format; + const auto size = [&format](qint64 bytes) { + return format.formatByteSize(bytes, 1, KFormat::IECBinaryDialect); + }; QString details; if (slot.total_progress > 0) { - details = i18n("%1 of %2", locale.formattedDataSize(slot.current_progress), locale.formattedDataSize(slot.total_progress)); + details = i18n("%1 of %2", size(slot.current_progress), + size(slot.total_progress)); } else if (slot.current_progress > 0) { - details = locale.formattedDataSize(slot.current_progress); + details = size(slot.current_progress); } slot.step->state = state; diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index d97e7227..1b739fd2 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -63,6 +63,7 @@ #include #include #include +#include #include #include @@ -117,7 +118,6 @@ #include "InstanceImportTask.h" #include "InstanceCopyTask.h" -#include "MMCTime.h" #include namespace @@ -2161,6 +2161,8 @@ void MainWindow::updateStatusCenter() int timePlayed = APPLICATION->instances()->getTotalPlayTime(); if (timePlayed > 0) { m_statusCenter->setText( - i18n("Total playtime: %1", Time::prettifyDuration(timePlayed))); + i18n("Total playtime: %1", + KFormat().formatDuration(static_cast(timePlayed) * 1000, + KFormat::AbbreviatedDuration))); } } diff --git a/launcher/ui/pages/instance/BackupPage.cpp b/launcher/ui/pages/instance/BackupPage.cpp index 4a26fd15..332435c9 100644 --- a/launcher/ui/pages/instance/BackupPage.cpp +++ b/launcher/ui/pages/instance/BackupPage.cpp @@ -30,6 +30,7 @@ #include "backup/BackupTask.h" #include "ui/dialogs/ProgressDialog.h" +#include #include BackupPage::BackupPage(BaseInstance* inst, QWidget* parent) @@ -73,7 +74,8 @@ void BackupPage::refreshList() item->setText(0, entry.name.isEmpty() ? entry.fileName : entry.name); item->setText(1, entry.timestamp.toString( QStringLiteral("yyyy-MM-dd HH:mm:ss"))); - item->setText(2, humanFileSize(entry.sizeBytes)); + item->setText(2, KFormat().formatByteSize( + entry.sizeBytes, 1, KFormat::IECBinaryDialect)); item->setData(0, Qt::UserRole, entry.fullPath); } @@ -246,17 +248,3 @@ void BackupPage::on_btnDelete_clicked() m_manager->deleteBackup(entry); refreshList(); } - -/* static */ -QString BackupPage::humanFileSize(qint64 bytes) -{ - if (bytes < 1024) - return QStringLiteral("%1 B").arg(bytes); - if (bytes < 1024 * 1024) - return QStringLiteral("%1 KiB").arg(bytes / 1024.0, 0, 'f', 1); - if (bytes < 1024LL * 1024 * 1024) - return QStringLiteral("%1 MiB").arg(bytes / (1024.0 * 1024.0), 0, 'f', - 1); - return QStringLiteral("%1 GiB").arg(bytes / (1024.0 * 1024.0 * 1024.0), 0, - 'f', 2); -} diff --git a/launcher/ui/pages/instance/BackupPage.h b/launcher/ui/pages/instance/BackupPage.h index 6430a5d6..72e9b48a 100644 --- a/launcher/ui/pages/instance/BackupPage.h +++ b/launcher/ui/pages/instance/BackupPage.h @@ -72,7 +72,6 @@ class BackupPage : public QWidget, public BasePage void refreshList(); void updateButtons(); BackupEntry selectedEntry() const; - static QString humanFileSize(qint64 bytes); Ui::BackupPage* ui; BaseInstance* m_inst;