Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adapt time format feature #342

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 92 additions & 9 deletions src/session-widgets/lockcontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -198,24 +203,104 @@ void LockContent::initUserListWidget()
connect(m_userListWidget, &UserFrameList::requestSwitchUser, this, &LockContent::requestSwitchToUser);
}

void LockContent::onCurrentUserChanged(std::shared_ptr<User> user)
void LockContent::onValueChanged(const QDBusMessage &dbusMessage)
{
QList<QVariant> 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::configPath(std::shared_ptr<User> user) const
{
if (!user)
return QString();

QDBusInterface configInter(dsg_config, "/", "org.desktopspec.ConfigManager", QDBusConnection::systemBus());
if (!configInter.isValid()) {
qWarning("Can't acquire config manager. error:\"%s\"",
qPrintable(QDBusConnection::systemBus().lastError().message()));
return QString();
}
QDBusReply<QDBusObjectPath> dbusReply = configInter.call("acquireManagerV2",
(uint)user->uid(),
QString(QCoreApplication::applicationName()),
QString("org.deepin.region-format"),
QString(""));
if (configInter.lastError().isValid() ) {
qWarning("Call failed: %s", qPrintable(configInter.lastError().message()));
return QString();
}
return dbusReply.value().path();
}

QString LockContent::regionValue(const QString &key) const
{
QString dbusPath = configPath(m_user);
if (dbusPath.isEmpty())
return QString();
QDBusInterface managerInter(dsg_config, dbusPath, "org.desktopspec.ConfigManager.Manager", QDBusConnection::systemBus());
QDBusReply<QVariant> reply = managerInter.call("value", key);
if (managerInter.lastError().isValid() ) {
qWarning("Call failed: %s", qPrintable(managerInter.lastError().message()));
return QString();
}
return reply.value().toString();
}

void LockContent::buildConnect()
{
QString dbusPath = configPath(m_user);
if (dbusPath.isEmpty())
return;
QDBusConnection::systemBus().connect(dsg_config, dbusPath, "org.desktopspec.ConfigManager.Manager",
"valueChanged", "s", this,
SLOT(onValueChanged(const QDBusMessage&)));
}

void LockContent::disconnect(std::shared_ptr<User> user)
{
if (!user)
return;

//如果是锁屏就用系统语言,如果是登陆界面就用用户语言
auto locale = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : user->locale();
m_logoWidget->updateLocale(locale);
m_timeWidget->updateLocale(locale);
QString dbusPath = configPath(user);
if (dbusPath.isEmpty())
return;
QDBusConnection::systemBus().disconnect(dsg_config, dbusPath, "org.desktopspec.ConfigManager.Manager",
"valueChanged", "s", this,
SLOT(onValueChanged(const QDBusMessage&)));
}

void LockContent::onCurrentUserChanged(std::shared_ptr<User> user)
{
if (!user)
return;

for (auto connect : m_currentUserConnects) {
m_user.get()->disconnect(connect);
}

disconnect(m_user);
m_currentUserConnects.clear();

m_user = user;
FeiWang1119 marked this conversation as resolved.
Show resolved Hide resolved

m_localeName = regionValue(localeName_key);
QLocale locale = m_localeName.isEmpty()? QLocale::system() : QLocale(m_localeName);
m_shortTimeFormat = regionValue(shortTimeFormat_key);
m_longDateFormat = regionValue(longDateFormat_key);
buildConnect();
m_logoWidget->updateLocale(locale.name());
m_timeWidget->updateLocale(locale, m_shortTimeFormat, m_longDateFormat);

m_timeWidget->set24HourFormat(user->isUse24HourFormat());
m_timeWidget->setWeekdayFormatType(user->weekdayFormat());
m_timeWidget->setShortDateFormat(user->shortDateFormat());
Expand All @@ -232,7 +317,7 @@ void LockContent::onCurrentUserChanged(std::shared_ptr<User> user)
updateTimeFormat(user->isUse24HourFormat());
}, Qt::QueuedConnection);

m_logoWidget->updateLocale(locale);
m_logoWidget->updateLocale(locale.name());
}

void LockContent::pushPasswordFrame()
Expand Down Expand Up @@ -423,8 +508,6 @@ 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);
m_timeWidget->set24HourFormat(use24);
m_timeWidget->setVisible(true);
}
Expand Down
14 changes: 14 additions & 0 deletions src/session-widgets/lockcontent.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User;
class ShutdownWidget;
class LogoWidget;
class TimeWidget;
class QDBusInterface;

class LockContent : public SessionBaseWindow
{
Expand Down Expand Up @@ -87,6 +88,15 @@ public slots:
void initSFAWidget();
void initUserListWidget();

private slots:
void onValueChanged(const QDBusMessage &dbusMessage);

private:
QString configPath(std::shared_ptr<User>) const;
QString regionValue(const QString& key) const;
void buildConnect();
void disconnect(std::shared_ptr<User> user);

protected:
SessionBaseModel *m_model;
ControlWidget *m_controlWidget; // 右下角图标
Expand All @@ -106,6 +116,10 @@ public slots:

int m_lockFailTimes; // 尝试锁屏时失败的次数
QLocalServer *m_localServer;

QString m_localeName;
QString m_shortTimeFormat;
QString m_longDateFormat;
};

#endif // LOCKCONTENT_H
11 changes: 10 additions & 1 deletion src/widgets/timewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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));
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/timewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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