KDeclarative

kuserproxy.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2014 Sebastian K├╝gler <[email protected]>
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 <QFile>
10 #include <QDir>
11 #include <QHostInfo>
12 #include <QTextStream>
13 #include <QUrl>
14 
15 
16 
17 const QString etcPasswd = QStringLiteral("/etc/passwd");
18 const QString accountsServiceIconPath = QStringLiteral("/var/lib/AccountsService/icons");
19 
20 KUserProxy::KUserProxy (QObject *parent)
21  : QObject(parent),
22  m_temporaryEmptyFaceIconPath(false)
23 {
24  QString pathToFaceIcon(m_user.faceIconPath());
25  if (pathToFaceIcon.isEmpty()) {
26  //KUser returns null if the current faceIconPath is empty
27  //so we should explicitly watch ~/.face.icon rather than faceIconPath()
28  //as we want to watch for this file being created
29  pathToFaceIcon = QDir::homePath() + QStringLiteral("/.face.icon");
30  }
31 
32  m_dirWatch.addFile(pathToFaceIcon);
33  m_dirWatch.addFile(accountsServiceIconPath + QLatin1Char('/') + m_user.loginName());
34  if (QFile::exists(etcPasswd)) {
35  m_dirWatch.addFile(etcPasswd);
36  }
37 
38  connect(&m_dirWatch, &KDirWatch::dirty, this, &KUserProxy::update);
39  connect(&m_dirWatch, &KDirWatch::created, this, &KUserProxy::update);
40  connect(&m_dirWatch, &KDirWatch::deleted, this, &KUserProxy::update);
41 }
42 
43 KUserProxy::~KUserProxy()
44 {
45 }
46 
47 void KUserProxy::update(const QString &path)
48 {
49  if (path == m_user.faceIconPath() || path == QDir::homePath() + QLatin1String("/.face.icon")
50  || path == accountsServiceIconPath + QLatin1Char('/') + m_user.loginName()) {
51  // we need to force updates, even when the path doesn't change,
52  // but the underlying image does. Change path temporarily, to
53  // make the Image reload.
54  // Needs cache: false in the Image item to actually reload
55  m_temporaryEmptyFaceIconPath = true;
56  Q_EMIT faceIconUrlChanged();
57  m_temporaryEmptyFaceIconPath = false;
58  Q_EMIT faceIconUrlChanged();
59  } else if (path == etcPasswd) {
60  m_user = KUser();
61  Q_EMIT nameChanged();
62  }
63 }
64 
66 {
67  QString fullName = m_user.property(KUser::FullName).toString();
68  if (!fullName.isEmpty()) {
69  return fullName;
70  }
71  return loginName();
72 }
73 
75 {
76  return m_user.loginName();
77 }
78 
80 {
81  if (m_temporaryEmptyFaceIconPath) {
82  return QUrl();
83  }
84  const QString u = m_user.faceIconPath();
85  const QFile f(u);
86  if (f.exists(u)) {
87  // We need to return a file URL, not a simple path
88  return QUrl::fromLocalFile(u);
89  }
90  return QUrl();
91 }
92 
94 {
95  if (m_os.isEmpty()) {
96  QFile osfile(QStringLiteral("/etc/os-release"));
97  if (osfile.exists()) {
98  if (!osfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
99  return QString();
100  }
101 
102  QTextStream in(&osfile);
103  while(!in.atEnd()) {
104  QString line = in.readLine();
105  if (line.startsWith(QLatin1String("PRETTY_NAME"))) {
106  QStringList fields = line.split(QLatin1String("PRETTY_NAME=\""));
107  if (fields.count() == 2) {
108  osfile.close();
109  QString pretty = fields.at(1);
110  pretty.chop(1);
111  m_os = pretty;
112  return pretty;
113  }
114  }
115  }
116  }
117  }
118  return m_os;
119 }
120 
122 {
123  return QHostInfo::localHostName();
124 }
125 
QString os()
const T & at(int i) const const
bool exists() const const
void chop(int n)
QString homePath()
void deleted(const QString &path)
QString fullName(const PartType &type)
int count(const T &value) const const
QString loginName() const
QString host() const
QUrl faceIconUrl() const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
void created(const QString &path)
QString fullName() const
QString localHostName()
void dirty(const QString &path)
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 22:41:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.