File: userdialog.cpp

package info (click to toggle)
lxqt-admin 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,104 kB
  • sloc: cpp: 1,654; sh: 92; makefile: 11
file content (145 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *
 * Copyright (C) 2014  Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "userdialog.h"

#include <LXQt/Globals>

#include <QMessageBox>
#include <QListWidgetItem>
#include <QDebug>
#include "usermanager.h"

#define DEFAULT_UID_MIN 1000
#define DEFAULT_UID_MAX 32768

UserDialog::UserDialog(UserManager* userManager, UserInfo* user, QWidget* parent):
    QDialog(parent),
    mUserManager(userManager),
    mUser(user),
    mFullNameChanged(false),
    mHomeDirChanged(false)
{
    ui.setupUi(this);
    bool isNewUser = (user->uid() == 0 && user->name().isEmpty());
    // load all groups
    for(const GroupInfo* group: mUserManager->groups())
    {
        ui.mainGroup->addItem(group->name());

        QListWidgetItem* item = new QListWidgetItem();
        item->setText(group->name());
        item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsUserCheckable|Qt::ItemIsSelectable);
        if(!isNewUser)
        {
            if(group->hasMember(user->name())) // the user is in this group
                item->setCheckState(Qt::Checked);
            else
                item->setCheckState(Qt::Unchecked);
        }
        else
            item->setCheckState(Qt::Unchecked);
        ui.groupList->addItem(item);
    }
    connect(ui.loginName, SIGNAL(textChanged(QString)), SLOT(onLoginNameChanged(QString)));

    ui.loginShell->addItems(mUserManager->availableShells());

    if(isNewUser) // new user
    {
        ui.mainGroup->setCurrentIndex(-1);
        ui.loginShell->setCurrentIndex(-1);
    }
    else  // edit an existing user
    {
        ui.loginName->setText(user->name());
        ui.uid->setValue(user->uid());
        ui.fullName->setText(user->fullName());
        ui.loginShell->setEditText(user->shell());
        ui.homeDir->setText(user->homeDir());
        GroupInfo* group = userManager->findGroupInfo(user->gid());
        if(group)
            ui.mainGroup->setEditText(group->name());
    }
}

UserDialog::~UserDialog()
{
}

void UserDialog::onLoginNameChanged(const QString& text)
{
    if(!mFullNameChanged)
    {
        ui.fullName->blockSignals(true);
        ui.fullName->setText(text);
        ui.fullName->blockSignals(false);
    }

    if(!mHomeDirChanged)
    {
        ui.homeDir->blockSignals(true);
        ui.homeDir->setText(QSL("/home/") + text);
        ui.homeDir->blockSignals(false);
    }
}

void UserDialog::onFullNameChanged(const QString& text)
{
   mFullNameChanged = true;
}

void UserDialog::onHomeDirChanged(const QString& text)
{
    mHomeDirChanged = true;
}

void UserDialog::accept()
{
    mUser->setUid(ui.uid->value());
    QString loginName = ui.loginName->text();
    if(loginName.isEmpty())
    {
        QMessageBox::critical(this, tr("Error"), tr("The user name cannot be empty."));
        return;
    }
    mUser->setName(loginName);
    mUser->setFullName(ui.fullName->text());

    mUser->setHomeDir(ui.homeDir->text());
    mUser->setShell(ui.loginShell->currentText());
    // main group
    QString groupName = ui.mainGroup->currentText();
    if(!groupName.isEmpty()) {
        GroupInfo* group = mUserManager->findGroupInfo(groupName);
        if(group)
            mUser->setGid(group->gid());
    }

    // other groups
    mUser->removeAllGroups();
    for(int row = 0; row < ui.groupList->count(); ++row) {
        QListWidgetItem* item = ui.groupList->item(row);
        if(item->checkState() == Qt::Checked) {
            mUser->addGroup(item->text());
        }
    }
    QDialog::accept();
}