Skip to content

fix: lock and greeter display correct datetime format in snipe branch #8

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
99 changes: 99 additions & 0 deletions src/widgets/centertopwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ using namespace DDESESSIONCC;
const int TOP_TIP_MAX_LENGTH = 60;
const int TOP_TIP_SPACING = 10;

#ifdef ENABLE_DSS_SNIPE
static const QString localeNameKey = "localeName";
static const QString longDateFormatKey = "longDateFormat";
static const QString shortTimeFormatKey = "shortTimeFormat";
#endif // ENABLE_DSS_SNIPE

CenterTopWidget::CenterTopWidget(QWidget *parent)
: QWidget(parent)
, m_currentUser(nullptr)
Expand Down Expand Up @@ -64,6 +70,9 @@ void CenterTopWidget::setCurrentUser(User *user)
return;
}

#ifdef ENABLE_DSS_SNIPE
updateRegionFormatConnection(user);
#endif // ENABLE_DSS_SNIPE
m_currentUser = QPointer<User>(user);
if (!m_currentUser.isNull()) {
for (auto connect : m_currentUserConnects) {
Expand All @@ -86,6 +95,10 @@ void CenterTopWidget::setCurrentUser(User *user)
QTimer::singleShot(0, this, [this, user] {
updateTimeFormat(user->isUse24HourFormat());
});

#ifdef ENABLE_DSS_SNIPE
updateUserDateTimeFormat();
#endif // ENABLE_DSS_SNIPE
}


Expand Down Expand Up @@ -157,3 +170,89 @@ void CenterTopWidget::onDConfigPropertyChanged(const QString &key, const QVarian
}
}

#ifdef ENABLE_DSS_SNIPE
void CenterTopWidget::onUserRegionFormatValueChanged(const QDBusMessage &dbusMessage)
{
QList<QVariant> arguments = dbusMessage.arguments();
if (1 != arguments.count())
return;

QString key = dbusMessage.arguments().at(0).toString();
if (key == localeNameKey || key == shortTimeFormatKey || key == longDateFormatKey) {
updateUserDateTimeFormat();
}
}

QString CenterTopWidget::getRegionFormatConfigPath(const User *user) const
{
if (!user)
return QString();

QDBusInterface configInter("org.desktopspec.ConfigManager", "/", "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 CenterTopWidget::getRegionFormatValue(const QString &userConfigDbusPath, const QString &key) const
{
if (userConfigDbusPath.isEmpty())
return QString();
QDBusInterface managerInter("org.desktopspec.ConfigManager", userConfigDbusPath, "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 CenterTopWidget::updateRegionFormatConnection(const User *user)
{
if (!user) {
return;
}

// disconnect old user
if (m_currentUser) {
QString dbusPath = getRegionFormatConfigPath(m_currentUser);
if (dbusPath.isEmpty())
return;
QDBusConnection::systemBus().disconnect("org.desktopspec.ConfigManager", dbusPath, "org.desktopspec.ConfigManager.Manager",
"valueChanged", "s", this,
SLOT(onUserRegionFormatValueChanged(const QDBusMessage&)));
}

// connect new user
QString dbusPath = getRegionFormatConfigPath(user);
if (dbusPath.isEmpty())
return;

QDBusConnection::systemBus().connect("org.desktopspec.ConfigManager", dbusPath, "org.desktopspec.ConfigManager.Manager",
"valueChanged", "s", this,
SLOT(onUserRegionFormatValueChanged(const QDBusMessage&)));
}

void CenterTopWidget::updateUserDateTimeFormat()
{
QString userConfigDbusPath = getRegionFormatConfigPath(m_currentUser);

QString localeName = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : getRegionFormatValue(userConfigDbusPath, localeNameKey);
QString shortTimeFormat = getRegionFormatValue(userConfigDbusPath, shortTimeFormatKey);
QString longDateFormat = getRegionFormatValue(userConfigDbusPath, longDateFormatKey);

m_timeWidget->updateLocale(localeName, shortTimeFormat, longDateFormat);
}
#endif // ENABLE_DSS_SNIPE
11 changes: 11 additions & 0 deletions src/widgets/centertopwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public slots:
void setTopTipText(const QString &text);
void updateTopTipWidget();

#ifdef ENABLE_DSS_SNIPE
private slots:
void onUserRegionFormatValueChanged(const QDBusMessage &dbusMessage);

private:
QString getRegionFormatConfigPath(const User *user) const;
QString getRegionFormatValue(const QString &userConfigDbusPath, const QString& key) const;
void updateRegionFormatConnection(const User *user);
void updateUserDateTimeFormat();
#endif // ENABLE_DSS_SNIPE

private:
QPointer<User> m_currentUser;
TimeWidget *m_timeWidget;
Expand Down
20 changes: 20 additions & 0 deletions src/widgets/timewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ void TimeWidget::refreshTime()

QString date_format = shortDateFormat.at(m_shortDateIndex) + " " + weekdayFormat.at(m_weekdayIndex);
m_dateLabel->setText(m_locale.toString(QDateTime::currentDateTime(), date_format));

#ifdef ENABLE_DSS_SNIPE
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));
}
#endif // ENABLE_DSS_SNIPE
}

/**
Expand Down Expand Up @@ -122,3 +129,16 @@ QSize TimeWidget::sizeHint() const
{
return QSize(QWidget::sizeHint().width(), m_dateLabel->fontMetrics().height() + m_timeLabel->fontMetrics().height());
}

#ifdef ENABLE_DSS_SNIPE
void TimeWidget::updateLocale(const QString &locale, const QString &shortTimeFormat, const QString &longDateFormat)
{
m_locale = QLocale(locale);
if (!shortTimeFormat.isEmpty())
m_shortTimeFormat = shortTimeFormat;
if (!longDateFormat.isEmpty())
m_longDateFormat = longDateFormat;

refreshTime();
}
#endif // ENABLE_DSS_SNIPE
9 changes: 9 additions & 0 deletions src/widgets/timewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
void updateLocale(const QLocale &locale);
QSize sizeHint() const override;

#ifdef ENABLE_DSS_SNIPE
void updateLocale(const QString &locale, const QString &shortTimeFormat = "", const QString &longDateFormat = "");
#endif // ENABLE_DSS_SNIPE

public Q_SLOTS:

Check warning on line 30 in src/widgets/timewidget.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void setWeekdayFormatType(int type);
void setShortDateFormat(int type);
void setShortTimeFormat(int type);
Expand All @@ -42,6 +46,11 @@
int m_weekdayIndex = 0;
int m_shortDateIndex = 0;
int m_shortTimeIndex = 0;

#ifdef ENABLE_DSS_SNIPE
QString m_shortTimeFormat;
QString m_longDateFormat;
#endif // ENABLE_DSS_SNIPE
};

#endif // TIMEWIDGET_H
Loading