File indexing completed on 2024-06-23 05:28:20

0001 /*
0002 This file is part of LightDM-KDE.
0003 
0004 Copyright 2012 David Edmundson <kde@davidedmundson.co.uk>
0005 
0006 LightDM-KDE is free software: you can redistribute it and/or modify
0007 it under the terms of the GNU General Public License as published by
0008 the Free Software Foundation, either version 3 of the License, or
0009 (at your option) any later version.
0010 
0011 LightDM-KDE is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with LightDM-KDE.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "usersmodel.h"
0021 
0022 #include <QLightDM/UsersModel>
0023 #include <QFile>
0024 
0025 #include <KLocalizedString>
0026 
0027 UsersModel::UsersModel(QObject *parent) :
0028     ExtraRowProxyModel(parent),
0029     m_showGuest(false)
0030 {
0031     setSourceModel(new QLightDM::UsersModel(this));
0032 }
0033 
0034 //workaround for LightDM sometimes returning the wrong user icon path
0035 //if the provided path does not exist, try path + ".icon" (i.e  ~/.face.icon)
0036 //if that does exist, return that instead
0037 QVariant UsersModel::data(const QModelIndex &index, int role) const
0038 {
0039     if (role == QLightDM::UsersModel::ImagePathRole) {
0040         const QString path = ExtraRowProxyModel::data(index, role).toString();
0041         if (QFile::exists(path)) {
0042             return path;
0043         } else if (QFile::exists(path + QLatin1String(".icon"))) {
0044             return path + QLatin1String(".icon");
0045         }
0046     }
0047     return ExtraRowProxyModel::data(index, role);
0048 }
0049 
0050 void UsersModel::setShowGuest(bool showGuest)
0051 {
0052     if (showGuest == m_showGuest) {
0053         return;
0054     }
0055     m_showGuest = showGuest;
0056 
0057     if (m_showGuest) {
0058         QStandardItem *guest = new QStandardItem(i18n("Guest"));
0059         guest->setData("*guest", QLightDM::UsersModel::NameRole);
0060         //set real name to the display name i18n("guest");
0061         guest->setData(guest->data(), QLightDM::UsersModel::RealNameRole);
0062         //guest is never shown as logged in, as clicking again should start a new guest session
0063         guest->setData(false, QLightDM::UsersModel::LoggedInRole);
0064         
0065         
0066         extraRowModel()->appendRow(guest);
0067     } else {
0068         extraRowModel()->removeRow(0);
0069     }
0070 }
0071 
0072 bool UsersModel::showGuest() const
0073 {
0074     return m_showGuest;
0075 }