Skip to content

Commit 6e121d4

Browse files
committed
fix: lock and greeter display correct datetime format in snipe branch
get user's region format config by using dConfig system dBus Log: as title Pms: BUG-316295
1 parent e10e61a commit 6e121d4

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/widgets/centertopwidget.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ using namespace DDESESSIONCC;
1717
const int TOP_TIP_MAX_LENGTH = 60;
1818
const int TOP_TIP_SPACING = 10;
1919

20+
#ifdef ENABLE_DSS_SNIPE
21+
static const QString localeNameKey = "localeName";
22+
static const QString longDateFormatKey = "longDateFormat";
23+
static const QString shortTimeFormatKey = "shortTimeFormat";
24+
#endif // ENABLE_DSS_SNIPE
25+
2026
CenterTopWidget::CenterTopWidget(QWidget *parent)
2127
: QWidget(parent)
2228
, m_currentUser(nullptr)
@@ -64,6 +70,9 @@ void CenterTopWidget::setCurrentUser(User *user)
6470
return;
6571
}
6672

73+
#ifdef ENABLE_DSS_SNIPE
74+
updateRegionFormatConnection(user);
75+
#endif // ENABLE_DSS_SNIPE
6776
m_currentUser = QPointer<User>(user);
6877
if (!m_currentUser.isNull()) {
6978
for (auto connect : m_currentUserConnects) {
@@ -86,6 +95,10 @@ void CenterTopWidget::setCurrentUser(User *user)
8695
QTimer::singleShot(0, this, [this, user] {
8796
updateTimeFormat(user->isUse24HourFormat());
8897
});
98+
99+
#ifdef ENABLE_DSS_SNIPE
100+
updateUserDateTimeFormat();
101+
#endif // ENABLE_DSS_SNIPE
89102
}
90103

91104

@@ -157,3 +170,89 @@ void CenterTopWidget::onDConfigPropertyChanged(const QString &key, const QVarian
157170
}
158171
}
159172

173+
#ifdef ENABLE_DSS_SNIPE
174+
void CenterTopWidget::onUserRegionFormatValueChanged(const QDBusMessage &dbusMessage)
175+
{
176+
QList<QVariant> arguments = dbusMessage.arguments();
177+
if (1 != arguments.count())
178+
return;
179+
180+
QString key = dbusMessage.arguments().at(0).toString();
181+
if (key == localeNameKey || key == shortTimeFormatKey || key == longDateFormatKey) {
182+
updateUserDateTimeFormat();
183+
}
184+
}
185+
186+
QString CenterTopWidget::getRegionFormatConfigPath(const User *user) const
187+
{
188+
if (!user)
189+
return QString();
190+
191+
QDBusInterface configInter("org.desktopspec.ConfigManager", "/", "org.desktopspec.ConfigManager", QDBusConnection::systemBus());
192+
if (!configInter.isValid()) {
193+
qWarning("Can't acquire config manager. error:\"%s\"",
194+
qPrintable(QDBusConnection::systemBus().lastError().message()));
195+
return QString();
196+
}
197+
QDBusReply<QDBusObjectPath> dbusReply = configInter.call("acquireManagerV2",
198+
(uint)user->uid(),
199+
QString(QCoreApplication::applicationName()),
200+
QString("org.deepin.region-format"),
201+
QString(""));
202+
if (configInter.lastError().isValid() ) {
203+
qWarning("Call failed: %s", qPrintable(configInter.lastError().message()));
204+
return QString();
205+
}
206+
return dbusReply.value().path();
207+
}
208+
209+
QString CenterTopWidget::getRegionFormatValue(const QString &userConfigDbusPath, const QString &key) const
210+
{
211+
if (userConfigDbusPath.isEmpty())
212+
return QString();
213+
QDBusInterface managerInter("org.desktopspec.ConfigManager", userConfigDbusPath, "org.desktopspec.ConfigManager.Manager", QDBusConnection::systemBus());
214+
QDBusReply<QVariant> reply = managerInter.call("value", key);
215+
if (managerInter.lastError().isValid() ) {
216+
qWarning("Call failed: %s", qPrintable(managerInter.lastError().message()));
217+
return QString();
218+
}
219+
return reply.value().toString();
220+
}
221+
222+
void CenterTopWidget::updateRegionFormatConnection(const User *user)
223+
{
224+
if (!user) {
225+
return;
226+
}
227+
228+
// disconnect old user
229+
if (m_currentUser) {
230+
QString dbusPath = getRegionFormatConfigPath(m_currentUser);
231+
if (dbusPath.isEmpty())
232+
return;
233+
QDBusConnection::systemBus().disconnect("org.desktopspec.ConfigManager", dbusPath, "org.desktopspec.ConfigManager.Manager",
234+
"valueChanged", "s", this,
235+
SLOT(onUserRegionFormatValueChanged(const QDBusMessage&)));
236+
}
237+
238+
// connect new user
239+
QString dbusPath = getRegionFormatConfigPath(user);
240+
if (dbusPath.isEmpty())
241+
return;
242+
243+
QDBusConnection::systemBus().connect("org.desktopspec.ConfigManager", dbusPath, "org.desktopspec.ConfigManager.Manager",
244+
"valueChanged", "s", this,
245+
SLOT(onUserRegionFormatValueChanged(const QDBusMessage&)));
246+
}
247+
248+
void CenterTopWidget::updateUserDateTimeFormat()
249+
{
250+
QString userConfigDbusPath = getRegionFormatConfigPath(m_currentUser);
251+
252+
QString localeName = qApp->applicationName() == "dde-lock" ? QLocale::system().name() : getRegionFormatValue(userConfigDbusPath, localeNameKey);
253+
QString shortTimeFormat = getRegionFormatValue(userConfigDbusPath, shortTimeFormatKey);
254+
QString longDateFormat = getRegionFormatValue(userConfigDbusPath, longDateFormatKey);
255+
256+
m_timeWidget->updateLocale(localeName, shortTimeFormat, longDateFormat);
257+
}
258+
#endif // ENABLE_DSS_SNIPE

src/widgets/centertopwidget.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public slots:
3434
void setTopTipText(const QString &text);
3535
void updateTopTipWidget();
3636

37+
#ifdef ENABLE_DSS_SNIPE
38+
private slots:
39+
void onUserRegionFormatValueChanged(const QDBusMessage &dbusMessage);
40+
41+
private:
42+
QString getRegionFormatConfigPath(const User *user) const;
43+
QString getRegionFormatValue(const QString &userConfigDbusPath, const QString& key) const;
44+
void updateRegionFormatConnection(const User *user);
45+
void updateUserDateTimeFormat();
46+
#endif // ENABLE_DSS_SNIPE
47+
3748
private:
3849
QPointer<User> m_currentUser;
3950
TimeWidget *m_timeWidget;

src/widgets/timewidget.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ void TimeWidget::refreshTime()
8080

8181
QString date_format = shortDateFormat.at(m_shortDateIndex) + " " + weekdayFormat.at(m_weekdayIndex);
8282
m_dateLabel->setText(m_locale.toString(QDateTime::currentDateTime(), date_format));
83+
84+
#ifdef ENABLE_DSS_SNIPE
85+
if (!m_shortTimeFormat.isEmpty() && !m_longDateFormat.isEmpty()) {
86+
m_timeLabel->setText(m_locale.toString(QTime::currentTime(), m_shortTimeFormat));
87+
m_dateLabel->setText(m_locale.toString(QDate::currentDate(), m_longDateFormat));
88+
}
89+
#endif // ENABLE_DSS_SNIPE
8390
}
8491

8592
/**
@@ -122,3 +129,16 @@ QSize TimeWidget::sizeHint() const
122129
{
123130
return QSize(QWidget::sizeHint().width(), m_dateLabel->fontMetrics().height() + m_timeLabel->fontMetrics().height());
124131
}
132+
133+
#ifdef ENABLE_DSS_SNIPE
134+
void TimeWidget::updateLocale(const QString &locale, const QString &shortTimeFormat, const QString &longDateFormat)
135+
{
136+
m_locale = QLocale(locale);
137+
if (!shortTimeFormat.isEmpty())
138+
m_shortTimeFormat = shortTimeFormat;
139+
if (!longDateFormat.isEmpty())
140+
m_longDateFormat = longDateFormat;
141+
142+
refreshTime();
143+
}
144+
#endif // ENABLE_DSS_SNIPE

src/widgets/timewidget.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class TimeWidget : public QWidget
2323
void updateLocale(const QLocale &locale);
2424
QSize sizeHint() const override;
2525

26+
#ifdef ENABLE_DSS_SNIPE
27+
void updateLocale(const QString &locale, const QString &shortTimeFormat = "", const QString &longDateFormat = "");
28+
#endif // ENABLE_DSS_SNIPE
29+
2630
public Q_SLOTS:
2731
void setWeekdayFormatType(int type);
2832
void setShortDateFormat(int type);
@@ -42,6 +46,11 @@ public Q_SLOTS:
4246
int m_weekdayIndex = 0;
4347
int m_shortDateIndex = 0;
4448
int m_shortTimeIndex = 0;
49+
50+
#ifdef ENABLE_DSS_SNIPE
51+
QString m_shortTimeFormat;
52+
QString m_longDateFormat;
53+
#endif // ENABLE_DSS_SNIPE
4554
};
4655

4756
#endif // TIMEWIDGET_H

0 commit comments

Comments
 (0)