Back to index

lightdm  1.3.2
usersmodel.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 David Edmundson.
00003  * Author: David Edmundson <kde@davidedmundson.co.uk>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License as published by the Free
00007  * Software Foundation; either version 3 of the License, or (at your option) any
00008  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include "QLightDM/usersmodel.h"
00013 
00014 #include <QtCore/QString>
00015 #include <QtCore/QDebug>
00016 #include <QtGui/QIcon>
00017 
00018 #include <lightdm.h>
00019 
00020 using namespace QLightDM;
00021 
00022 class UserItem
00023 {
00024 public:
00025     QString name;
00026     QString realName;
00027     QString homeDirectory;
00028     QString image;
00029     QString background;
00030     QString session;
00031     bool isLoggedIn;
00032     bool hasMessages;
00033     QString displayName() const;
00034 };
00035 
00036 QString UserItem::displayName() const {
00037     if (realName.isEmpty()){
00038         return name;
00039     }
00040     else {
00041         return realName;
00042     }
00043 }
00044 
00045 namespace QLightDM {
00046 class UsersModelPrivate {
00047 public:
00048     UsersModelPrivate(UsersModel *parent);
00049     virtual ~UsersModelPrivate();
00050     QList<UserItem> users;
00051 
00052     protected:
00053         UsersModel * const q_ptr;
00054 
00055         void loadUsers();
00056 
00057         static void cb_userAdded(LightDMUserList *user_list, LightDMUser *user, gpointer data);
00058         static void cb_userChanged(LightDMUserList *user_list, LightDMUser *user, gpointer data);
00059         static void cb_userRemoved(LightDMUserList *user_list, LightDMUser *user, gpointer data);
00060     private:
00061         Q_DECLARE_PUBLIC(UsersModel)
00062 };
00063 }
00064 
00065 UsersModelPrivate::UsersModelPrivate(UsersModel* parent) :
00066     q_ptr(parent)
00067 {
00068     g_type_init();
00069 }
00070 
00071 UsersModelPrivate::~UsersModelPrivate()
00072 {
00073     g_signal_handlers_disconnect_by_func(lightdm_user_list_get_instance(), NULL, this);
00074 }
00075 
00076 void UsersModelPrivate::loadUsers()
00077 {
00078     Q_Q(UsersModel);
00079 
00080     int rowCount = lightdm_user_list_get_length(lightdm_user_list_get_instance());
00081 
00082     if (rowCount == 0) {
00083         return;
00084     } else {
00085         q->beginInsertRows(QModelIndex(), 0, rowCount-1);
00086 
00087         const GList *items, *item;
00088         items = lightdm_user_list_get_users(lightdm_user_list_get_instance());
00089         for (item = items; item; item = item->next) {
00090             LightDMUser *ldmUser = static_cast<LightDMUser*>(item->data);
00091 
00092             UserItem user;
00093             user.name = QString::fromUtf8(lightdm_user_get_name(ldmUser));
00094             user.homeDirectory = QString::fromUtf8(lightdm_user_get_home_directory(ldmUser));
00095             user.realName = QString::fromUtf8(lightdm_user_get_real_name(ldmUser));
00096             user.image = QString::fromUtf8(lightdm_user_get_image(ldmUser));
00097             user.background = QString::fromUtf8(lightdm_user_get_background(ldmUser));
00098             user.session = QString::fromUtf8(lightdm_user_get_session(ldmUser));
00099             user.isLoggedIn = lightdm_user_get_logged_in(ldmUser);
00100             user.hasMessages = lightdm_user_get_has_messages(ldmUser);
00101             users.append(user);
00102         }
00103 
00104         q->endInsertRows();
00105     }
00106     g_signal_connect(lightdm_user_list_get_instance(), "user-added", G_CALLBACK (cb_userAdded), this);
00107     g_signal_connect(lightdm_user_list_get_instance(), "user-changed", G_CALLBACK (cb_userChanged), this);
00108     g_signal_connect(lightdm_user_list_get_instance(), "user-removed", G_CALLBACK (cb_userRemoved), this);
00109 }
00110 
00111 
00112 
00113 void UsersModelPrivate::cb_userAdded(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
00114 {
00115     Q_UNUSED(user_list)
00116     UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);
00117 
00118     that->q_func()->beginInsertRows(QModelIndex(), that->users.size(), that->users.size());
00119 
00120     UserItem user;
00121     user.name = QString::fromUtf8(lightdm_user_get_name(ldmUser));
00122     user.homeDirectory = QString::fromUtf8(lightdm_user_get_home_directory(ldmUser));
00123     user.realName = QString::fromUtf8(lightdm_user_get_real_name(ldmUser));
00124     user.image = QString::fromUtf8(lightdm_user_get_image(ldmUser));
00125     user.background = QString::fromUtf8(lightdm_user_get_background(ldmUser));
00126     user.isLoggedIn = lightdm_user_get_logged_in(ldmUser);
00127     user.hasMessages = lightdm_user_get_has_messages(ldmUser);
00128     that->users.append(user);
00129 
00130     that->q_func()->endInsertRows();
00131 
00132 }
00133 
00134 void UsersModelPrivate::cb_userChanged(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
00135 {
00136     Q_UNUSED(user_list)
00137     UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);
00138 
00139     QString userToChange = QString::fromUtf8(lightdm_user_get_name(ldmUser));
00140 
00141     for (int i=0;i<that->users.size();i++) {
00142         if (that->users[i].name == userToChange) {
00143 
00144             that->users[i].homeDirectory = QString::fromUtf8(lightdm_user_get_home_directory(ldmUser));
00145             that->users[i].realName = QString::fromUtf8(lightdm_user_get_real_name(ldmUser));
00146             that->users[i].image = QString::fromUtf8(lightdm_user_get_image(ldmUser));
00147             that->users[i].background = QString::fromUtf8(lightdm_user_get_background(ldmUser));
00148             that->users[i].isLoggedIn = lightdm_user_get_logged_in(ldmUser);
00149             that->users[i].hasMessages = lightdm_user_get_has_messages(ldmUser);
00150 
00151             QModelIndex index = that->q_ptr->createIndex(i, 0);
00152             that->q_ptr->dataChanged(index, index);
00153             break;
00154         }
00155     }
00156 }
00157 
00158 
00159 void UsersModelPrivate::cb_userRemoved(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
00160 {
00161     Q_UNUSED(user_list)
00162 
00163     UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);
00164     QString userToRemove = QString::fromUtf8(lightdm_user_get_name(ldmUser));
00165 
00166     for (int i=0;i<that->users.size();i++) {
00167         if (that->users[i].name == userToRemove) {
00168             that->q_ptr->beginRemoveRows(QModelIndex(), i, i);
00169             that->users.removeAt(i);
00170             that->q_ptr->endRemoveRows();
00171             break;
00172         }
00173     }
00174 }
00175 
00176 UsersModel::UsersModel(QObject *parent) :
00177     QAbstractListModel(parent),
00178     d_ptr(new UsersModelPrivate(this))
00179 {
00180     Q_D(UsersModel);
00181     // Extend roleNames (we want to keep the "display" role)
00182     QHash<int, QByteArray> roles = roleNames();
00183     roles[NameRole] = "name";
00184     roles[LoggedInRole] = "loggedIn";
00185     roles[SessionRole] = "session";
00186     roles[HasMessagesRole] = "hasMessages";
00187     setRoleNames(roles);
00188     d->loadUsers();
00189 
00190 }
00191 
00192 UsersModel::~UsersModel()
00193 {
00194     delete d_ptr;
00195 }
00196 
00197 
00198 int UsersModel::rowCount(const QModelIndex &parent) const
00199 {
00200     Q_D(const UsersModel);
00201     if (parent == QModelIndex()) {
00202         return d->users.size();
00203     }
00204 
00205     return 0;
00206 }
00207 
00208 QVariant UsersModel::data(const QModelIndex &index, int role) const
00209 {
00210     Q_D(const UsersModel);
00211 
00212     if (!index.isValid()) {
00213         return QVariant();
00214     }
00215 
00216     int row = index.row();
00217     switch (role) {
00218     case Qt::DisplayRole:
00219         return d->users[row].displayName();
00220     case Qt::DecorationRole:
00221         return QIcon(d->users[row].image);
00222     case UsersModel::NameRole:
00223         return d->users[row].name;
00224     case UsersModel::RealNameRole:
00225         return d->users[row].realName;
00226     case UsersModel::SessionRole:
00227         return d->users[row].session;
00228     case UsersModel::LoggedInRole:
00229         return d->users[row].isLoggedIn;
00230     case UsersModel::BackgroundRole:
00231         return QPixmap(d->users[row].background);
00232     case UsersModel::HasMessagesRole:
00233         return d->users[row].hasMessages;
00234     }
00235 
00236     return QVariant();
00237 }
00238 
00239 
00240 #include "usersmodel_moc.cpp"