From 89934d31b65f070b1954ae2f6201841bfd90d3e2 Mon Sep 17 00:00:00 2001
From: Kiyoshi Aman <adelie@aerdan.vulpine.house>
Date: Mon, 10 Feb 2020 08:57:09 -0600
Subject: Qt UI: Add subnet input widget

---
 ui/qt5/CMakeLists.txt |  1 +
 ui/qt5/subnetbox.cc   | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 ui/qt5/subnetbox.hh   | 40 +++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 ui/qt5/subnetbox.cc
 create mode 100644 ui/qt5/subnetbox.hh

diff --git a/ui/qt5/CMakeLists.txt b/ui/qt5/CMakeLists.txt
index 22391c4..cc56ed4 100644
--- a/ui/qt5/CMakeLists.txt
+++ b/ui/qt5/CMakeLists.txt
@@ -8,6 +8,7 @@ set(UI_SOURCES
     main.cc
     ${CMAKE_SOURCE_DIR}/3rdparty/Section.cpp
     stepprogresswidget.cc
+    subnetbox.cc
     useraccountwidget.cc
     avatardialog.cc
     crypt_sha512.c
diff --git a/ui/qt5/subnetbox.cc b/ui/qt5/subnetbox.cc
new file mode 100644
index 0000000..54f1732
--- /dev/null
+++ b/ui/qt5/subnetbox.cc
@@ -0,0 +1,65 @@
+/*
+ * 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::subnetEdited(const QString &text) {
+    int value = subnet_mask_to_cidr(text.toUtf8());
+    if(value > 0) {
+        cidr->setValue(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);
+
+    subnet->setText(data);
+}
diff --git a/ui/qt5/subnetbox.hh b/ui/qt5/subnetbox.hh
new file mode 100644
index 0000000..9f2edbf
--- /dev/null
+++ b/ui/qt5/subnetbox.hh
@@ -0,0 +1,40 @@
+/*
+ * subnetbox.hh - Definition 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
+ */
+
+#ifndef SUBNETBOX_HH
+#define SUBNETBOX_HH
+
+#include <QWidget>
+#include <QHBoxLayout>
+#include <QLineEdit>
+#include <QSpinBox>
+#include <QString>
+
+class SubnetBox : public QWidget {
+    Q_OBJECT
+
+public:
+    SubnetBox(QWidget *parent = nullptr);
+    QString subnetMask() const;
+    int subnetCIDR() const;
+
+public slots:
+    void subnetEdited(const QString &text);
+    void cidrEdited(int value);
+
+private:
+    QHBoxLayout *layout;
+    QLineEdit *subnet;
+    QSpinBox *cidr;
+};
+
+#endif
-- 
cgit v1.2.3-70-g09d2