summaryrefslogtreecommitdiff
path: root/ui/qt5/subnetbox.cc
blob: 071cec8506f0814dbaa7fc7dd7690f2d5309d975 (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
/*
 * subnetbox.cc - Implementation of a widget for inputting an IPv4 subnet mask
 * horizon-qt5, the Qt 5 user interface for
 * Project Horizon
 *
 * Copyright (c) 2020 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 "subnetbox.hh"
#include "util/net.hh"

SubnetBox::SubnetBox(QWidget *parent) : QWidget(parent) {
    SubnetBox::layout = new QHBoxLayout(this);
    SubnetBox::subnet = new QLineEdit(this);
    SubnetBox::cidr   = new QSpinBox(this);

    this->setLayout(layout);

    layout->addWidget(subnet);
    layout->addWidget(cidr);
    layout->setMargin(0);

    subnet->setInputMask("000.000.000.000;_");
    cidr->setRange(1, 32);

    connect(subnet, &QLineEdit::textEdited, this, &SubnetBox::subnetEdited);
    connect(cidr, QOverload<int>::of(&QSpinBox::valueChanged),
            this, &SubnetBox::cidrEdited);
}

QString SubnetBox::subnetMask(void) const {
    return subnet->text();
}

int SubnetBox::subnetCIDR(void) const {
    return cidr->value();
}

void SubnetBox::setSubnetCIDR(int value) {
    cidr->setValue(value);
    cidrEdited(value);
}

void SubnetBox::subnetEdited(const QString &text) {
    int value = subnet_mask_to_cidr(text.toUtf8());
    if(value > 0) {
        cidr->setValue(value);
        emit valueChanged(value);
    }
}

void SubnetBox::cidrEdited(int value) {
    int bytes = value / 8, bits = value % 8, lastfilled = 0;
    QString data;
    char temp[4];

    for(int i = 0; i < bytes; i++) {
        data.append("255.");
    }
    for(int i = bits; i > 0; i--) {
        lastfilled |= (1 << (8 - i));
    }
    snprintf(temp, 4, "%d", lastfilled);
    data.append(temp);
    for(int i = 3; i > bytes; i--) {
        data.append(".0");
    }

    subnet->setText(data);

    emit valueChanged(value);
}