File indexing completed on 2024-04-21 04:44:13

0001 /*
0002    SPDX-FileCopyrightText: 2015 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0003 
0004    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "upnpdevicesoapserver.h"
0008 #include "upnpabstractdevice.h"
0009 #include "upnpdevicesoapserverobject.h"
0010 
0011 #include <QList>
0012 #include <QUrl>
0013 
0014 #include <QNetworkInterface>
0015 
0016 class UpnpDeviceSoapServerPrivate
0017 {
0018 public:
0019     QList<UpnpAbstractDevice *> mDevices;
0020 };
0021 
0022 UpnpDeviceSoapServer::UpnpDeviceSoapServer(QObject *parent)
0023     : KDSoapServer(parent)
0024     , d(std::make_unique<UpnpDeviceSoapServerPrivate>())
0025 {
0026     listen();
0027 }
0028 
0029 UpnpDeviceSoapServer::~UpnpDeviceSoapServer() = default;
0030 
0031 int UpnpDeviceSoapServer::addDevice(UpnpAbstractDevice *device)
0032 {
0033     d->mDevices.push_back(device);
0034     return d->mDevices.count() - 1;
0035 }
0036 
0037 void UpnpDeviceSoapServer::removeDevice(int index)
0038 {
0039     d->mDevices.removeAt(index);
0040 }
0041 
0042 QObject *UpnpDeviceSoapServer::createServerObject()
0043 {
0044     return new UpnpDeviceSoapServerObject(d->mDevices);
0045 }
0046 
0047 QUrl UpnpDeviceSoapServer::urlPrefix() const
0048 {
0049     QHostAddress publicAddress;
0050 
0051     const auto &list = QNetworkInterface::allAddresses();
0052     for (const auto &address : list) {
0053         if (!address.isLoopback()) {
0054             if (address.protocol() == QAbstractSocket::IPv4Protocol) {
0055                 publicAddress = address;
0056                 break;
0057             }
0058         }
0059     }
0060 
0061     QUrl webServerUrl;
0062 
0063     if (!publicAddress.isNull()) {
0064         webServerUrl.setHost(publicAddress.toString());
0065     } else {
0066         webServerUrl.setHost(QStringLiteral("127.0.0.1"));
0067     }
0068 
0069     webServerUrl.setPort(serverPort());
0070     webServerUrl.setScheme(QStringLiteral("http"));
0071 
0072     return webServerUrl;
0073 }
0074 
0075 #include "moc_upnpdevicesoapserver.cpp"