Move network interface filtering to separate file

This commit is contained in:
Felix Kauselmann 2023-04-07 20:19:11 +02:00
parent 9f67bb5e53
commit c100edfcd0
4 changed files with 43 additions and 30 deletions

View File

@ -150,7 +150,8 @@ HEADERS += comic_flow.h \
yacreader_comic_info_helper.h \
db/reading_list.h \
db/query_parser.h \
current_comic_view_helper.h
current_comic_view_helper.h \
ip_config_helper.h
!CONFIG(no_opengl) {
HEADERS += ../common/gl/yacreader_flow_gl.h
@ -232,7 +233,8 @@ SOURCES += comic_flow.cpp \
yacreader_comic_info_helper.cpp\
db/reading_list.cpp \
current_comic_view_helper.cpp \
db/query_parser.cpp
db/query_parser.cpp \
ip_config_helper.cpp
!CONFIG(no_opengl) {
SOURCES += ../common/gl/yacreader_flow_gl.cpp

View File

@ -0,0 +1,33 @@
#include <QNetworkInterface>
#include "ip_config_helper.h"
#include "qnaturalsorting.h"
// 192.168 (most comon local subnet for ips are always put first)
// IPs are sorted using natoral sorting
QList<QString> getIpAddresses()
{
auto ipComparator = [](const QString &ip1, const QString &ip2) {
if (ip1.startsWith("192.168") && ip2.startsWith("192.168"))
return naturalSortLessThanCI(ip1, ip2);
if (ip1.startsWith("192.168"))
return true;
if (ip2.startsWith("192.168"))
return false;
return naturalSortLessThanCI(ip1, ip2);
};
QList<QString> addresses;
for (auto add : QNetworkInterface::allAddresses()) {
// Exclude loopback, local, multicast
if (add.isGlobal()) {
addresses.push_back(add.toString());
}
}
std::sort(addresses.begin(), addresses.end(), ipComparator);
return addresses;
}

View File

@ -0,0 +1,4 @@
#ifndef YR_IP_CONFIG_HELPER
#define YR_IP_CONFIG_HELPER
QList<QString> getIpAddresses();
#endif

View File

@ -1,5 +1,4 @@
#include <QGridLayout>
#include <QNetworkInterface>
#include <QSettings>
#include <QPalette>
#include <QBitmap>
@ -10,25 +9,9 @@
#include "yacreader_http_server.h"
#include "yacreader_global_gui.h"
#include "qnaturalsorting.h"
#include "ip_config_helper.h"
#include "qrcodegen.hpp"
// 192.168 (most comon local subnet for ips are always put first)
// IPs are sorted using natoral sorting
bool ipComparator(const QString &ip1, const QString &ip2)
{
if (ip1.startsWith("192.168") && ip2.startsWith("192.168"))
return naturalSortLessThanCI(ip1, ip2);
if (ip1.startsWith("192.168"))
return true;
if (ip2.startsWith("192.168"))
return false;
return naturalSortLessThanCI(ip1, ip2);
}
extern YACReaderHttpServer *httpServer;
ServerConfigDialog::ServerConfigDialog(QWidget *parent)
@ -174,16 +157,7 @@ void ServerConfigDialog::generateQR()
{
ip->clear();
QList<QString> addresses;
for (auto add : QNetworkInterface::allAddresses()) {
// Exclude loopback, local, multicast
if (add.isGlobal()) {
addresses.push_back(add.toString());
}
}
std::sort(addresses.begin(), addresses.end(), ipComparator);
auto addresses = getIpAddresses();
if (addresses.length() > 0) {
generateQR(addresses.first() + ":" + httpServer->getPort());
ip->addItems(addresses);