File indexing completed on 2024-05-12 03:55:02

0001 /*
0002     SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2014 Sebastian Kügler <sebas@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kuserproxy.h"
0009 #include <QDir>
0010 #include <QFile>
0011 #include <QHostInfo>
0012 #include <QTextStream>
0013 #include <QUrl>
0014 
0015 const QString etcPasswd = QStringLiteral("/etc/passwd");
0016 const QString accountsServiceIconPath = QStringLiteral("/var/lib/AccountsService/icons");
0017 
0018 KUserProxy::KUserProxy(QObject *parent)
0019     : QObject(parent)
0020     , m_temporaryEmptyFaceIconPath(false)
0021 {
0022     QString pathToFaceIcon(m_user.faceIconPath());
0023     if (pathToFaceIcon.isEmpty()) {
0024         // KUser returns null if the current faceIconPath is empty
0025         // so we should explicitly watch ~/.face.icon rather than faceIconPath()
0026         // as we want to watch for this file being created
0027         pathToFaceIcon = QDir::homePath() + QStringLiteral("/.face.icon");
0028     }
0029 
0030     m_dirWatch.addFile(pathToFaceIcon);
0031     m_dirWatch.addFile(accountsServiceIconPath + QLatin1Char('/') + m_user.loginName());
0032     if (QFile::exists(etcPasswd)) {
0033         m_dirWatch.addFile(etcPasswd);
0034     }
0035 
0036     connect(&m_dirWatch, &KDirWatch::dirty, this, &KUserProxy::update);
0037     connect(&m_dirWatch, &KDirWatch::created, this, &KUserProxy::update);
0038     connect(&m_dirWatch, &KDirWatch::deleted, this, &KUserProxy::update);
0039 }
0040 
0041 KUserProxy::~KUserProxy()
0042 {
0043 }
0044 
0045 void KUserProxy::update(const QString &path)
0046 {
0047     if (path == m_user.faceIconPath() || path == QDir::homePath() + QLatin1String("/.face.icon")
0048         || path == accountsServiceIconPath + QLatin1Char('/') + m_user.loginName()) {
0049         // we need to force updates, even when the path doesn't change,
0050         // but the underlying image does. Change path temporarily, to
0051         // make the Image reload.
0052         // Needs cache: false in the Image item to actually reload
0053         m_temporaryEmptyFaceIconPath = true;
0054         Q_EMIT faceIconUrlChanged();
0055         m_temporaryEmptyFaceIconPath = false;
0056         Q_EMIT faceIconUrlChanged();
0057     } else if (path == etcPasswd) {
0058         m_user = KUser();
0059         Q_EMIT nameChanged();
0060     }
0061 }
0062 
0063 QString KUserProxy::fullName() const
0064 {
0065     QString fullName = m_user.property(KUser::FullName).toString();
0066     if (!fullName.isEmpty()) {
0067         return fullName;
0068     }
0069     return loginName();
0070 }
0071 
0072 QString KUserProxy::loginName() const
0073 {
0074     return m_user.loginName();
0075 }
0076 
0077 QUrl KUserProxy::faceIconUrl() const
0078 {
0079     if (m_temporaryEmptyFaceIconPath) {
0080         return QUrl();
0081     }
0082     const QString u = m_user.faceIconPath();
0083     const QFile f(u);
0084     if (f.exists(u)) {
0085         // We need to return a file URL, not a simple path
0086         return QUrl::fromLocalFile(u);
0087     }
0088     return QUrl();
0089 }
0090 
0091 QString KUserProxy::os()
0092 {
0093     if (m_os.isEmpty()) {
0094         QFile osfile(QStringLiteral("/etc/os-release"));
0095         if (osfile.exists()) {
0096             if (!osfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
0097                 return QString();
0098             }
0099 
0100             QTextStream in(&osfile);
0101             while (!in.atEnd()) {
0102                 QString line = in.readLine();
0103                 if (line.startsWith(QLatin1String("PRETTY_NAME"))) {
0104                     QStringList fields = line.split(QLatin1String("PRETTY_NAME=\""));
0105                     if (fields.count() == 2) {
0106                         osfile.close();
0107                         QString pretty = fields.at(1);
0108                         pretty.chop(1);
0109                         m_os = pretty;
0110                         return pretty;
0111                     }
0112                 }
0113             }
0114         }
0115     }
0116     return m_os;
0117 }
0118 
0119 QString KUserProxy::host() const
0120 {
0121     return QHostInfo::localHostName();
0122 }
0123 
0124 #include "moc_kuserproxy.cpp"