diff --git a/src/session-widgets/lockcontent.cpp b/src/session-widgets/lockcontent.cpp index 50cc2038..74d1d836 100644 --- a/src/session-widgets/lockcontent.cpp +++ b/src/session-widgets/lockcontent.cpp @@ -23,6 +23,11 @@ using namespace dss; using namespace dss::module; +static const QString dsg_config = "org.desktopspec.ConfigManager"; +static const QString localeName_key = "localeName"; +static const QString longDateFormat_key = "longDateFormat"; +static const QString shortTimeFormat_key = "shortTimeFormat"; + LockContent::LockContent(SessionBaseModel *const model, QWidget *parent) : SessionBaseWindow(parent) , m_model(model) @@ -198,15 +203,73 @@ void LockContent::initUserListWidget() connect(m_userListWidget, &UserFrameList::requestSwitchUser, this, &LockContent::requestSwitchToUser); } +void LockContent::onValueChanged(const QDBusMessage &dbusMessage) +{ + QList arguments = dbusMessage.arguments(); + if (1 != arguments.count()) + return; + + QString interfaceName = dbusMessage.arguments().at(0).toString(); + if (interfaceName == localeName_key) { + m_localeName = regionValue(localeName_key); + } else if (interfaceName == shortTimeFormat_key) { + m_shortTimeFormat = regionValue(shortTimeFormat_key); + } else if (interfaceName == longDateFormat_key) { + m_longDateFormat = regionValue(longDateFormat_key); + } + m_timeWidget->updateLocale(QLocale(m_localeName), m_shortTimeFormat, m_longDateFormat); +} + +QString LockContent::regionValue(const QString &key) +{ + m_iface = new QDBusInterface(dsg_config, "/", "org.desktopspec.ConfigManager", QDBusConnection::systemBus(), this); + if (!m_iface->isValid()) { + qWarning("Can't acquire config manager. error:\"%s\"", + qPrintable(QDBusConnection::systemBus().lastError().message())); + return QString(); + } + QDBusReply dbusReply = m_iface->call("acquireManagerV2", + (uint)m_user->uid(), + QString(QCoreApplication::applicationName()), + QString("org.deepin.region-format"), + QString("")); + if (m_iface->lastError().isValid() ) { + qWarning("Call failed: %s", qPrintable(m_iface->lastError().message())); + return QString(); + } + m_dbusPath = dbusReply.value().path(); + + m_iface = new QDBusInterface(dsg_config, m_dbusPath, "org.desktopspec.ConfigManager.Manager", QDBusConnection::systemBus(), this); + QDBusReply reply = m_iface->call("value", key); + if (m_iface->lastError().isValid() ) { + qWarning("Call failed: %s", qPrintable(m_iface->lastError().message())); + return QString(); + } + return reply.value().toString(); +} + void LockContent::onCurrentUserChanged(std::shared_ptr user) { if (!user) return; + m_user = user; + + m_localeName = regionValue(localeName_key); + QLocale locale; + if (!m_localeName.isEmpty()) + locale = QLocale(m_localeName); + m_shortTimeFormat = regionValue(shortTimeFormat_key); + m_longDateFormat = regionValue(longDateFormat_key); + + QDBusConnection::systemBus().connect(dsg_config, m_dbusPath, "org.desktopspec.ConfigManager.Manager", + "valueChanged", "s", this, + SLOT(onValueChanged(const QDBusMessage&))); + //如果是锁屏就用系统语言,如果是登陆界面就用用户语言 - auto locale = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : user->locale(); - m_logoWidget->updateLocale(locale); - m_timeWidget->updateLocale(locale); +// auto locale = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : user->locale(); + m_logoWidget->updateLocale(locale.name()); + m_timeWidget->updateLocale(locale, m_shortTimeFormat, m_longDateFormat); for (auto connect : m_currentUserConnects) { m_user.get()->disconnect(connect); @@ -214,7 +277,6 @@ void LockContent::onCurrentUserChanged(std::shared_ptr user) m_currentUserConnects.clear(); - m_user = user; m_timeWidget->set24HourFormat(user->isUse24HourFormat()); m_timeWidget->setWeekdayFormatType(user->weekdayFormat()); @@ -232,7 +294,7 @@ void LockContent::onCurrentUserChanged(std::shared_ptr user) updateTimeFormat(user->isUse24HourFormat()); }, Qt::QueuedConnection); - m_logoWidget->updateLocale(locale); + m_logoWidget->updateLocale(locale.name()); } void LockContent::pushPasswordFrame() @@ -423,8 +485,8 @@ void LockContent::updateDesktopBackgroundPath(const QString &path) void LockContent::updateTimeFormat(bool use24) { if (m_user != nullptr) { - auto locale = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : m_user->locale(); - m_timeWidget->updateLocale(locale); +// auto locale = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : m_user->locale(); +// m_timeWidget->updateLocale(locale); m_timeWidget->set24HourFormat(use24); m_timeWidget->setVisible(true); } diff --git a/src/session-widgets/lockcontent.h b/src/session-widgets/lockcontent.h index 57b7a92a..c9072c59 100644 --- a/src/session-widgets/lockcontent.h +++ b/src/session-widgets/lockcontent.h @@ -27,6 +27,7 @@ class User; class ShutdownWidget; class LogoWidget; class TimeWidget; +class QDBusInterface; class LockContent : public SessionBaseWindow { @@ -87,6 +88,12 @@ public slots: void initSFAWidget(); void initUserListWidget(); +private slots: + void onValueChanged(const QDBusMessage &dbusMessage); + +private: + QString regionValue(const QString& key); + protected: SessionBaseModel *m_model; ControlWidget *m_controlWidget; // 右下角图标 @@ -106,6 +113,12 @@ public slots: int m_lockFailTimes; // 尝试锁屏时失败的次数 QLocalServer *m_localServer; + + QString m_localeName; + QString m_shortTimeFormat; + QString m_longDateFormat; + QString m_dbusPath; + QDBusInterface *m_iface; }; #endif // LOCKCONTENT_H diff --git a/src/widgets/timewidget.cpp b/src/widgets/timewidget.cpp index 640da242..1e81914c 100644 --- a/src/widgets/timewidget.cpp +++ b/src/widgets/timewidget.cpp @@ -61,9 +61,13 @@ void TimeWidget::set24HourFormat(bool use24HourFormat) refreshTime(); } -void TimeWidget::updateLocale(const QLocale &locale) +void TimeWidget::updateLocale(const QLocale &locale, const QString &shortTimeFormat, const QString &longDateFormat) { m_locale = locale; + if (!shortTimeFormat.isEmpty()) + m_shortTimeFormat = shortTimeFormat; + if (!longDateFormat.isEmpty()) + m_longDateFormat = longDateFormat; refreshTime(); } @@ -77,6 +81,11 @@ void TimeWidget::refreshTime() QString date_format = shortDateFormat.at(m_shortDateIndex) + " " + weekdayFormat.at(m_weekdayIndex); m_dateLabel->setText(m_locale.toString(QDateTime::currentDateTime(), date_format)); + + if (!m_shortTimeFormat.isEmpty() && !m_longDateFormat.isEmpty()) { + m_timeLabel->setText(m_locale.toString(QTime::currentTime(), m_shortTimeFormat)); + m_dateLabel->setText(m_locale.toString(QDate::currentDate(), m_longDateFormat)); + } } /** diff --git a/src/widgets/timewidget.h b/src/widgets/timewidget.h index a96cf126..60b29c44 100644 --- a/src/widgets/timewidget.h +++ b/src/widgets/timewidget.h @@ -20,7 +20,7 @@ class TimeWidget : public QWidget explicit TimeWidget(QWidget *parent = nullptr); inline bool get24HourFormat() const { return m_use24HourFormat; } void set24HourFormat(bool use24HourFormat); - void updateLocale(const QLocale &locale); + void updateLocale(const QLocale &locale, const QString &shortTimeFormat = "", const QString &longDateFormat = ""); public Q_SLOTS: void setWeekdayFormatType(int type); @@ -41,6 +41,9 @@ public Q_SLOTS: int m_weekdayIndex = 0; int m_shortDateIndex = 0; int m_shortTimeIndex = 0; + + QString m_shortTimeFormat; + QString m_longDateFormat; }; #endif // TIMEWIDGET_H