summaryrefslogtreecommitdiff
path: root/ui/qt5/useraccountwidget.cc
blob: f499c31ab3808d0ba49273d0383505fff256c459 (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * useraccountwidget.cc - Implementation of a widget for managing user accounts
 * horizon-qt5, the Qt 5 user interface for
 * Project Horizon
 *
 * Copyright (c) 2019 Adélie Linux and contributors.  All rights reserved.
 * This code is licensed under the AGPL 3.0 license, as noted in the
 * LICENSE-code file in the root directory of this repository.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

#include "useraccountwidget.hh"

#include "avatardialog.hh"
#include <QAction>
#include <QHBoxLayout>
#include <QMenu>
#include <QVBoxLayout>

UserAccountWidget::UserAccountWidget(QWidget *parent)
    : QWidget(parent), acctEverTouched(false) {
    QHBoxLayout *nameLayout = new QHBoxLayout;

    personalName = new QLineEdit;
    QFont personalFont = personalName->font();
    personalFont.setPointSize(personalFont.pointSize() + 2);
    personalName->setFont(personalFont);
    personalName->setPlaceholderText(tr("Personal Name"));
    personalName->setToolTip(tr("Used to address this user"));
    personalName->setWhatsThis(tr("This name will be used to address this user.  It may be their real name, or a nickname."));
    nameLayout->addWidget(personalName, 20);

    accountName = new QLineEdit;
    accountName->setMaxLength(32);
    accountName->setPlaceholderText(tr("Username"));
    accountName->setToolTip(tr("Used to identify this user; letters and numbers only"));
    accountName->setWhatsThis(tr("This name will be used to identify this user to the computer.  It must contain only letters and numbers."));
    nameLayout->addWidget(accountName, 15);

    QHBoxLayout *passAdminLayout = new QHBoxLayout;
    passphrase = new QLineEdit;
    passphrase->setEchoMode(QLineEdit::Password);
    passphrase->setPlaceholderText(tr("Passphrase"));
    passphrase->setToolTip(tr("This user's passphrase"));
    passphrase->setWhatsThis(tr("The passphrase will be used to log in to the computer."));
    QAction *togglePass = passphrase->addAction(QIcon::fromTheme("visibility"),
                                                QLineEdit::TrailingPosition);
    togglePass->setToolTip(tr("Show the passphrase"));
    togglePass->setData(true);
    connect(togglePass, &QAction::triggered,
            [=](void) {
        if(togglePass->data().toBool() == true) {
            togglePass->setData(false);
            togglePass->setIcon(QIcon::fromTheme("hint"));
            togglePass->setToolTip(tr("Hide the passphrase"));
            passphrase->setEchoMode(QLineEdit::Normal);
        } else {
            togglePass->setData(true);
            togglePass->setIcon(QIcon::fromTheme("visibility"));
            togglePass->setToolTip(tr("Show the passphrase"));
            passphrase->setEchoMode(QLineEdit::Password);
        }
    });
    passAdminLayout->addWidget(passphrase);

    adminTick = new QCheckBox(tr("Admin"));
    adminTick->setToolTip(tr("Allows this user to perform administrative tasks"));
    adminTick->setWhatsThis(tr("If ticked, allows this user to perform administrative tasks on the computer."));
    connect(adminTick, &QCheckBox::stateChanged, [=]{
        emit validityChanged();
    });
    passAdminLayout->addWidget(adminTick);

    QVBoxLayout *detailLayout = new QVBoxLayout;
    detailLayout->addLayout(nameLayout);
    detailLayout->addLayout(passAdminLayout);

    QHBoxLayout *overallLayout = new QHBoxLayout;
    aviButton = new QPushButton;
    aviButton->setIcon(QIcon::fromTheme("user"));
    aviButton->setIconSize(QSize(32, 32));
    aviButton->setToolTip(tr("Change this user's avatar"));
    aviButton->setWhatsThis(tr("Allows you to choose the user's avatar, which will be shown on the log in screen."));
    connect(aviButton, &QPushButton::clicked, [=]{
        AvatarDialog *d = new AvatarDialog;
        if(d->exec() == QDialog::Accepted) {
            aviPath = d->avatar();
            aviButton->setIcon(QPixmap(aviPath));
        }
        d->deleteLater();
    });
    overallLayout->addWidget(aviButton);
    overallLayout->addLayout(detailLayout);

    setLayout(overallLayout);

    connect(accountName, &QLineEdit::textEdited,
            [=]{
        emit validityChanged();
        this->acctEverTouched = true;
    });
    connect(personalName, &QLineEdit::textEdited,
            [=]{
        if(this->acctEverTouched) {
            emit validityChanged();
            return;
        }

        /* REQ: UI.Accounts.UserAcct.AcctName.Default */
        QString result = personalName->text()
                /* NFKC */
                .normalized(QString::NormalizationForm_KC)
                /* Casefold */
                .toLower();
        QStringList components = result.split(" ", QString::SkipEmptyParts);
        if(components.size() > 1) {
            result = "";
            for(int next = 0; next < components.size() - 1; next++) {
                result += components.at(next).left(1);
            }
            result += components.at(components.size() - 1);
        }
        /* if SkipEmptyParts causes components to be 1, but still has space */
        result.replace(" ", "");
        accountName->setText(result.left(32));
        emit validityChanged();
    });
    connect(passphrase, &QLineEdit::textEdited,
            [=]{ emit validityChanged(); });
}

QString UserAccountWidget::accountText(void) const {
    return accountName->text();
}

void UserAccountWidget::setAccountText(QString account) {
    accountName->setText(account);
    accountName->textEdited(account);
}

QString UserAccountWidget::avatarPath() const {
    return aviPath;
}

QString UserAccountWidget::passphraseText(void) const {
    return passphrase->text();
}

QString UserAccountWidget::personalText(void) const {
    return personalName->text();
}

void UserAccountWidget::setPersonalText(QString personal) {
    personalName->setText(personal);
    personalName->textEdited(personal);
}

bool UserAccountWidget::isAdmin(void) const {
    return adminTick->isChecked();
}

void UserAccountWidget::setAdmin(bool ticked) {
    adminTick->setChecked(ticked);
    /* adminTick being ticked can cause validity to change */
    emit validityChanged();
}

bool UserAccountWidget::isValid() const {
    /* The widget is valid if absolutely no account information has been specified. */
    if(accountText().isEmpty() && personalText().isEmpty() &&
            passphraseText().isEmpty() && !adminTick->isChecked()) {
        return true;
    }

    if(accountText().isEmpty() || personalText().isEmpty()) {
        return false;
    }

    return true;
}