on xenial help sign in
Watch for user real name updates in greeter
commit9aee0c99702511465e9f4813c97eb8dc44aebc86 authorMichael Terry <michael.terry@canonical.com> dateTue 1 Mar 2016, 09:09 (11 years ago) parent57f68ef
6 files changed+69−47
plugins/IntegratedLightDM/liblightdm/CMakeLists.txt+6−2 plugins/IntegratedLightDM/liblightdm/UsersModel.cpp+4−18 plugins/IntegratedLightDM/liblightdm/UsersModel.h+1−1 plugins/IntegratedLightDM/liblightdm/UsersModelPrivate.cpp+43−9 plugins/IntegratedLightDM/liblightdm/UsersModelPrivate.h+15−3 tests/mocks/IntegratedLightDM/liblightdm/UsersModel.cpp−14
plugins/IntegratedLightDM/liblightdm/CMakeLists.txt+6−2
@@ -1,10 +1,13 @@
11pkg_check_modules(LIBUSERMETRICSOUTPUT REQUIRED libusermetricsoutput-1)
22
3+add_definitions(-DSM_BUSNAME=systemBus)
4+
35set(LibLightDM_SOURCES
46 Greeter.cpp
57 UsersModel.cpp
68 GreeterPrivate.cpp
79 UsersModelPrivate.cpp
10+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService/AccountsServiceDBusAdaptor.cpp
811 ${CMAKE_SOURCE_DIR}/plugins/Utils/qvariantlistmodel.cpp
912 )
1013
@@ -12,6 +15,7 @@ add_library(integratedLightDM STATIC ${LibLightDM_SOURCES})
1215add_library(MockLightDM-demo-shared SHARED ${LibLightDM_SOURCES})
1316
1417include_directories(
18+ ${CMAKE_SOURCE_DIR}/plugins/AccountsService
1519 ${CMAKE_CURRENT_BINARY_DIR}
1620 ${GLIB_INCLUDE_DIRS}
1721 ${LIBUSERMETRICSOUTPUT_INCLUDE_DIRS}
@@ -28,8 +32,8 @@ target_link_libraries(MockLightDM-demo-shared
2832 -lpam
2933)
3034
31−qt5_use_modules(integratedLightDM Concurrent Gui)
32−qt5_use_modules(MockLightDM-demo-shared Concurrent Gui)
35+qt5_use_modules(integratedLightDM Concurrent DBus Gui)
36+qt5_use_modules(MockLightDM-demo-shared Concurrent DBus Gui)
3337
3438set_target_properties(integratedLightDM PROPERTIES COMPILE_FLAGS -fPIC)
3539set_target_properties(MockLightDM-demo-shared PROPERTIES
plugins/IntegratedLightDM/liblightdm/UsersModel.cpp+4−18
@@ -54,24 +54,10 @@ UsersModel::UsersModel(QObject *parent) :
5454 roles[ImagePathRole] = "imagePath";
5555 setRoleNames(roles);
5656
57− // Now modify our mock user backgrounds
58− QDir bgdir = QDir(QStringLiteral("/usr/share/demo-assets/shell/backgrounds/"));
59− QStringList backgrounds = bgdir.entryList(QDir::Files | QDir::NoDotAndDotDot);
60−
61− for (int i = 0, j = 0; i < d->entries.size(); i++) {
62− Entry &entry = d->entries[i];
63− if (entry.background.isNull() && !backgrounds.isEmpty()) {
64− entry.background = bgdir.filePath(backgrounds[j++]);
65− if (j >= backgrounds.length()) {
66− j = 0;
67− }
68− }
69− }
70−}
71−
72−UsersModel::~UsersModel()
73−{
74− delete d_ptr;
57+ connect(d_ptr, &UsersModelPrivate::dataChanged, this, [this](int i) {
58+ QModelIndex index = createIndex(i, 0);
59+ Q_EMIT dataChanged(index, index);
60+ });
7561}
7662
7763int UsersModel::rowCount(const QModelIndex &parent) const
plugins/IntegratedLightDM/liblightdm/UsersModel.h+1−1
@@ -41,7 +41,7 @@ class Q_DECL_EXPORT UsersModel : public QAbstractListModel
4141
4242public:
4343 explicit UsersModel(QObject *parent = 0);
44− virtual ~UsersModel();
44+ virtual ~UsersModel() = default;
4545
4646 enum UserModelRoles {NameRole = Qt::UserRole,
4747 RealNameRole,
plugins/IntegratedLightDM/liblightdm/UsersModelPrivate.cpp+43−9
@@ -18,7 +18,10 @@
1818
1919#include "UsersModelPrivate.h"
2020
21+#include "AccountsServiceDBusAdaptor.h"
22+
2123#include <glib.h>
24+#include <QDebug>
2225#include <QDir>
2326#include <QSettings>
2427#include <QStringList>
@@ -27,7 +30,9 @@ namespace QLightDM
2730{
2831
2932UsersModelPrivate::UsersModelPrivate(UsersModel* parent)
30− : q_ptr(parent)
33+ : QObject((QObject*)parent),
34+ q_ptr(parent),
35+ m_service(new AccountsServiceDBusAdaptor(this))
3136{
3237 QFileInfo demoFile(QDir::homePath() + "/.unity8-greeter-demo");
3338 QString currentUser = g_get_user_name();
@@ -43,15 +48,44 @@ UsersModelPrivate::UsersModelPrivate(UsersModel* parent)
4348 entries.append({user, name, 0, 0, false, false, 0, 0});
4449 }
4550 } else {
46− // If we were using the actual liblightdm, we could just ask it
47− // for the user's real name. But we aren't. We *should* ask
48− // AccountsService for the real name, like liblightdm does internally,
49− // but this is close enough since AS and passwd are always in sync.
50− QString realName = QString::fromUtf8(g_get_real_name()); // gets name from passwd entry
51− if (realName == QStringLiteral("Unknown")) { // glib doesn't translate this string
52− realName.clear();
51+ entries.append({currentUser, 0, 0, 0, false, false, 0, 0});
52+
53+ connect(m_service, &AccountsServiceDBusAdaptor::maybeChanged,
54+ this, [this](const QString &user) {
55+ if (user == entries[0].username) {
56+ updateName(true);
57+ }
58+ });
59+ updateName(false);
60+ }
61+}
62+
63+void UsersModelPrivate::updateName(bool async)
64+{
65+ auto pendingReply = m_service->getUserPropertyAsync(entries[0].username,
66+ QStringLiteral("org.freedesktop.Accounts.User"),
67+ QStringLiteral("RealName"));
68+ auto *watcher = new QDBusPendingCallWatcher(pendingReply, this);
69+
70+ connect(watcher, &QDBusPendingCallWatcher::finished,
71+ this, [this](QDBusPendingCallWatcher* watcher) {
72+
73+ QDBusPendingReply<QVariant> reply = *watcher;
74+ watcher->deleteLater();
75+ if (reply.isError()) {
76+ qWarning() << "Failed to get 'RealName' property - " << reply.error().message();
77+ return;
78+ }
79+
80+ const QString realName = reply.value().toString();
81+ if (entries[0].real_name != realName) {
82+ entries[0].real_name = realName;
83+ Q_EMIT dataChanged(0);
5384 }
54− entries.append({currentUser, realName, 0, 0, false, false, 0, 0});
85+ });
86+ if (!async) {
87+ watcher->waitForFinished();
88+ delete watcher;
5589 }
5690}
5791
plugins/IntegratedLightDM/liblightdm/UsersModelPrivate.h+15−3
@@ -19,8 +19,11 @@
1919#ifndef UNITY_MOCK_USERSMODEL_PRIVATE_H
2020#define UNITY_MOCK_USERSMODEL_PRIVATE_H
2121
22−#include <QtCore/QList>
23−#include <QtCore/QString>
22+#include <QList>
23+#include <QObject>
24+#include <QString>
25+
26+class AccountsServiceDBusAdaptor;
2427
2528namespace QLightDM
2629{
@@ -39,19 +42,28 @@ public:
3942 QString infographic;
4043};
4144
42−class UsersModelPrivate
45+class UsersModelPrivate : public QObject
4346{
47+ Q_OBJECT
48+
4449public:
4550 explicit UsersModelPrivate(UsersModel *parent = 0);
4651 virtual ~UsersModelPrivate() = default;
4752
4853 QList<Entry> entries;
4954
55+Q_SIGNALS:
56+ void dataChanged(int);
57+
5058protected:
5159 UsersModel * const q_ptr;
5260
5361private:
5462 Q_DECLARE_PUBLIC(UsersModel)
63+
64+ void updateName(bool async);
65+
66+ AccountsServiceDBusAdaptor *m_service;
5567};
5668
5769}
tests/mocks/IntegratedLightDM/liblightdm/UsersModel.cpp−14
@@ -47,20 +47,6 @@ UsersModel::UsersModel(QObject *parent) :
4747 roles[HasMessagesRole] = "hasMessages";
4848 roles[ImagePathRole] = "imagePath";
4949 setRoleNames(roles);
50−
51− // Now modify our mock user backgrounds
52− QDir bgdir = QDir("/usr/share/demo-assets/shell/backgrounds/");
53− QStringList backgrounds = bgdir.entryList(QDir::Files | QDir::NoDotAndDotDot);
54−
55− for (int i = 0, j = 0; i < d->entries.size(); i++) {
56− Entry &entry = d->entries[i];
57− if (entry.background.isNull() && !backgrounds.isEmpty()) {
58− entry.background = bgdir.filePath(backgrounds[j++]);
59− if (j >= backgrounds.length()) {
60− j = 0;
61− }
62− }
63− }
6450}
6551
6652UsersModel::~UsersModel()