// Qt #include // Project #include "VDateTime.h" #include "Logger.h" using namespace View; using namespace Gui; using namespace Storage; VIEW_DEF_CLASS(VDateTime) /*! * \brief VDateTime::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void VDateTime::initConnections() { // outgoing connect(this, SIGNAL(didAdjustment(const AdjustHDDateTimeRequestData)), &_GuiController, SLOT(doAdjustment(const AdjustHDDateTimeRequestData))); connect(this, SIGNAL(didAdjustment(const AdjustDGDateTimeRequestData)), &_GuiController, SLOT(doAdjustment(const AdjustDGDateTimeRequestData))); // incoming connect(&_GuiController, SIGNAL(didActionReceive(const AdjustHDDateTimeResponseData)), this, SLOT(doActionReceive(const AdjustHDDateTimeResponseData))); connect(&_GuiController, SIGNAL(didActionReceive(const AdjustDGDateTimeResponseData)), this, SLOT(doActionReceive(const AdjustDGDateTimeResponseData))); connect(&_process, SIGNAL(finished(int)), this, SLOT(onFinishedSetDateUI(const int))); startTimer(_timerInterval); } /*! * \brief VDateTime::doGetCurrentTime * Reads and displays the current date and time */ void VDateTime::doGetCurrentTime() { _setDateTimeHD = NOT_SET; _setDateTimeDG = NOT_SET; _setDateTimeUI = NOT_SET; status(""); _currentTime = QDateTime::currentDateTime(); hour (_currentTime.toString("hh")); minute(_currentTime.toString("mm")); second(_currentTime.toString("ss")); month (_currentTime.toString("MM")); day (_currentTime.toString("dd")); year (_currentTime.toString("yyyy")); } /*! * \brief doSetHour * Sets the hour * \param vHour (int) - the hour */ void VDateTime::doSetHour(const int &vHour) { hour(QStringLiteral("%1").arg(vHour, 2, 10, QLatin1Char('0'))); } /*! * \brief doSetMinute * Sets the minute * \param vMinute (int) - the minute */ void VDateTime::doSetMinute(const int &vMinute) { minute(QStringLiteral("%1").arg(vMinute, 2, 10, QLatin1Char('0'))); } /*! * \brief doSetMonth * Sets the month * \param vMonth (int) - the month */ void VDateTime::doSetMonth(const int &vMonth) { month(QStringLiteral("%1").arg(vMonth, 2, 10, QLatin1Char('0'))); } /*! * \brief doSetday * Sets the day * \param vDay (int) - the day */ void VDateTime::doSetDay(const int &vDay) { day(QStringLiteral("%1").arg(vDay, 2, 10, QLatin1Char('0'))); } /*! * \brief doSetyear * Sets the year * \param vYear (int) - the year */ void VDateTime::doSetYear(const int &vYear) { year(QStringLiteral("%1").arg(vYear, 4, 10, QLatin1Char('0'))); } /*! * \brief doSave * Issues request to save the current date and time to UI, HD and DG */ void VDateTime::doSave() { quint32 epoch = QDateTime(QDate(year().toInt(), month().toInt(), day().toInt()), QTime(hour().toInt(), minute().toInt())).toSecsSinceEpoch(); AdjustHDDateTimeRequestData hdDateTimeReq; hdDateTimeReq.mEpoch = epoch; AdjustDGDateTimeRequestData dgDateTimeReq; dgDateTimeReq.mEpoch = epoch; emit didAdjustment(hdDateTimeReq); emit didAdjustment(dgDateTimeReq); status("Setting date and time..."); // 2021-03-16 16:24:00 QString setTime = QString("%1-%2-%3 %4:%5:%6").arg(year()).arg(month()).arg(day()).arg(hour()).arg(minute()).arg(second()); _process.start(Date_Time_Set, QStringList() << setTime); } /*! * \brief VDateTime::onFinishedSetDateUI * Called when the process that sets the UI date and time has finished. * \param vExitCode - (int) the exit code of the process */ void VDateTime::onFinishedSetDateUI(const int &vExitCode) { LOG_DEBUG(QString("%1 - exit code %2").arg(__FUNCTION__).arg(vExitCode)); if (vExitCode == 0) { _setDateTimeUI = SUCCESS; } else { _setDateTimeUI = FAILURE; } updateStatus(); } /*! * \brief VDateTime::doActionReceive * Called when we receive a response back from the HD after requesting to * set the epoch * \param vResponse (AdjustHDDateTimeResponseData) - the response */ void VDateTime::doActionReceive(const AdjustHDDateTimeResponseData &vResponse) { if (vResponse.mAccepted == 1) { _setDateTimeHD = SUCCESS; } else { _setDateTimeHD = FAILURE; } updateStatus(); } /*! * \brief VDateTime::doActionReceive * Called when we receive a response back from the DG after requesting to * set the epoch * \param vResponse (AdjustDGDateTimeResponseData) - the response */ void VDateTime::doActionReceive(const AdjustDGDateTimeResponseData &vResponse) { if (vResponse.mAccepted == 1) { _setDateTimeDG = SUCCESS; } else { _setDateTimeDG = FAILURE; } updateStatus(); } /*! * \brief VDateTime::updateStatus * Update the notification bar's status */ void VDateTime::updateStatus() { status(QString("HD: %1 DG: %2 UI: %3").arg(enumToString(_setDateTimeHD)) .arg(enumToString(_setDateTimeDG)) .arg(enumToString(_setDateTimeUI))); } /** * \brief VDateTime::enumToString * Convenience functiont to convert an enum to a string * \param vEnum - the enum value * \return QString - the enum name */ QString VDateTime::enumToString(DateTimeSetStatus vEnum) { const QMetaObject *mo = qt_getEnumMetaObject(vEnum); int enumIdx = mo->indexOfEnumerator(qt_getEnumName(vEnum)); QString text = mo->enumerator(enumIdx).valueToKey(vEnum); if (!text.isEmpty() ) { return text; } return QString("[%1] Unknown DateTime Status").arg(vEnum); }