KCoreAddons

kuserproxy.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2014 Sebastian K├╝gler <sebas@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "kuserproxy.h"
9#include <QDir>
10#include <QFile>
11#include <QHostInfo>
12#include <QTextStream>
13#include <QUrl>
14
15const QString etcPasswd = QStringLiteral("/etc/passwd");
16const QString accountsServiceIconPath = QStringLiteral("/var/lib/AccountsService/icons");
17
18KUserProxy::KUserProxy(QObject *parent)
19 : QObject(parent)
20 , m_temporaryEmptyFaceIconPath(false)
21{
22 QString pathToFaceIcon(m_user.faceIconPath());
23 if (pathToFaceIcon.isEmpty()) {
24 // KUser returns null if the current faceIconPath is empty
25 // so we should explicitly watch ~/.face.icon rather than faceIconPath()
26 // as we want to watch for this file being created
27 pathToFaceIcon = QDir::homePath() + QStringLiteral("/.face.icon");
28 }
29
30 m_dirWatch.addFile(pathToFaceIcon);
31 m_dirWatch.addFile(accountsServiceIconPath + QLatin1Char('/') + m_user.loginName());
32 if (QFile::exists(etcPasswd)) {
33 m_dirWatch.addFile(etcPasswd);
34 }
35
36 connect(&m_dirWatch, &KDirWatch::dirty, this, &KUserProxy::update);
37 connect(&m_dirWatch, &KDirWatch::created, this, &KUserProxy::update);
38 connect(&m_dirWatch, &KDirWatch::deleted, this, &KUserProxy::update);
39}
40
41KUserProxy::~KUserProxy()
42{
43}
44
45void KUserProxy::update(const QString &path)
46{
47 if (path == m_user.faceIconPath() || path == QDir::homePath() + QLatin1String("/.face.icon")
48 || path == accountsServiceIconPath + QLatin1Char('/') + m_user.loginName()) {
49 // we need to force updates, even when the path doesn't change,
50 // but the underlying image does. Change path temporarily, to
51 // make the Image reload.
52 // Needs cache: false in the Image item to actually reload
53 m_temporaryEmptyFaceIconPath = true;
55 m_temporaryEmptyFaceIconPath = false;
57 } else if (path == etcPasswd) {
58 m_user = KUser();
60 }
61}
62
63QString KUserProxy::fullName() const
64{
65 QString fullName = m_user.property(KUser::FullName).toString();
66 if (!fullName.isEmpty()) {
67 return fullName;
68 }
69 return loginName();
70}
71
72QString KUserProxy::loginName() const
73{
74 return m_user.loginName();
75}
76
77QUrl KUserProxy::faceIconUrl() const
78{
79 if (m_temporaryEmptyFaceIconPath) {
80 return QUrl();
81 }
82 const QString u = m_user.faceIconPath();
83 const QFile f(u);
84 if (f.exists(u)) {
85 // We need to return a file URL, not a simple path
86 return QUrl::fromLocalFile(u);
87 }
88 return QUrl();
89}
90
91QString KUserProxy::os()
92{
93 if (m_os.isEmpty()) {
94 QFile osfile(QStringLiteral("/etc/os-release"));
95 if (osfile.exists()) {
96 if (!osfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
97 return QString();
98 }
99
100 QTextStream in(&osfile);
101 while (!in.atEnd()) {
102 QString line = in.readLine();
103 if (line.startsWith(QLatin1String("PRETTY_NAME"))) {
104 QStringList fields = line.split(QLatin1String("PRETTY_NAME=\""));
105 if (fields.count() == 2) {
106 osfile.close();
107 QString pretty = fields.at(1);
108 pretty.chop(1);
109 m_os = pretty;
110 return pretty;
111 }
112 }
113 }
114 }
115 }
116 return m_os;
117}
118
119QString KUserProxy::host() const
120{
122}
123
124#include "moc_kuserproxy.cpp"
void deleted(const QString &path)
Emitted when a file or directory is deleted.
void dirty(const QString &path)
Emitted when a watched object is changed.
void created(const QString &path)
Emitted when a file or directory (being watched explicitly) is created.
void faceIconUrlChanged()
signal that the user image changed
void nameChanged()
signal that the user's name or login name changed
QString fullName() const
Represents a user on your system.
Definition kuser.h:226
QString faceIconPath() const
The path to the user's face file.
QVariant property(UserProperty which) const
Returns an extended property.
QString loginName() const
The login name of the user.
QString homePath()
bool exists(const QString &fileName)
bool exists() const const
QString localHostName()
const_reference at(qsizetype i) const const
qsizetype count() const const
Q_EMITQ_EMIT
void chop(qsizetype n)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:58:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.