From e095eaf16e655ece70d5e0f239df051f8b861d60 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Tue, 28 Apr 2020 19:12:36 +0200 Subject: [PATCH] Qt: update playtime every 10 seconds --- rpcs3/rpcs3qt/gui_application.cpp | 27 ++++++++++++++++++++++++++- rpcs3/rpcs3qt/gui_application.h | 3 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/rpcs3/rpcs3qt/gui_application.cpp b/rpcs3/rpcs3qt/gui_application.cpp index a50794e149..599e578418 100644 --- a/rpcs3/rpcs3qt/gui_application.cpp +++ b/rpcs3/rpcs3qt/gui_application.cpp @@ -179,6 +179,7 @@ QStringList gui_application::GetAvailableLanguageCodes() void gui_application::InitializeConnects() { + connect(&m_timer, &QTimer::timeout, this, &gui_application::UpdatePlaytime); connect(this, &gui_application::OnEmulatorRun, this, &gui_application::StartPlaytime); connect(this, &gui_application::OnEmulatorStop, this, &gui_application::StopPlaytime); connect(this, &gui_application::OnEmulatorPause, this, &gui_application::StopPlaytime); @@ -321,12 +322,36 @@ void gui_application::StartPlaytime(bool start_playtime = true) return; } - m_persistent_settings->SetLastPlayed(serial, QDate::currentDate().toString("MMMM d yyyy")); + m_persistent_settings->SetLastPlayed(serial, QDate::currentDate().toString(gui::persistent::last_played_date_format)); m_timer_playtime.start(); + m_timer.start(10000); // Update every 10 seconds in case the emulation crashes +} + +void gui_application::UpdatePlaytime() +{ + if (!m_timer_playtime.isValid()) + { + m_timer.stop(); + return; + } + + const QString serial = qstr(Emu.GetTitleID()); + if (serial.isEmpty()) + { + m_timer_playtime.invalidate(); + m_timer.stop(); + return; + } + + const qint64 playtime = m_persistent_settings->GetPlaytime(serial) + m_timer_playtime.restart(); + m_persistent_settings->SetPlaytime(serial, playtime); + m_persistent_settings->SetLastPlayed(serial, QDate::currentDate().toString(gui::persistent::last_played_date_format)); } void gui_application::StopPlaytime() { + m_timer.stop(); + if (!m_timer_playtime.isValid()) return; diff --git a/rpcs3/rpcs3qt/gui_application.h b/rpcs3/rpcs3qt/gui_application.h index 88438d08e1..6c43e9ceeb 100644 --- a/rpcs3/rpcs3qt/gui_application.h +++ b/rpcs3/rpcs3qt/gui_application.h @@ -4,6 +4,7 @@ #include #include +#include #include #include "main_application.h" @@ -55,12 +56,14 @@ private: void InitializeConnects(); void StartPlaytime(bool start_playtime); + void UpdatePlaytime(); void StopPlaytime(); QTranslator m_translator; QTranslator m_translator_qt; QString m_language_code; + QTimer m_timer; QElapsedTimer m_timer_playtime; std::shared_ptr m_emu_settings;