File indexing completed on 2025-01-05 03:52:08

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-09-24
0007  * Description : a media server to export collections through DLNA.
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dmediaserver.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QFile>
0021 #include <QStandardPaths>
0022 
0023 // KDE includes
0024 
0025 #include <klocalizedstring.h>
0026 
0027 // Local includes
0028 
0029 #include "digikam_debug.h"
0030 #include "digikam_version.h"
0031 #include "daboutdata.h"
0032 #include "dlnaserver.h"
0033 
0034 // Platinum includes
0035 
0036 #include "PltDeviceHost.h"
0037 #include "Platinum.h"
0038 
0039 using namespace Digikam;
0040 
0041 void NPT_Console::Output(const char* msg)
0042 {
0043     qCDebug(DIGIKAM_MEDIASRV_LOG) << msg;
0044 }
0045 
0046 void UPnPLogger(const NPT_LogRecord* record)
0047 {
0048     QString msg = QLatin1String("Platinum [")             +
0049                   QString::fromUtf8(record->m_LoggerName) +
0050                   QLatin1String("] : ")                   +
0051                   QString::fromUtf8(record->m_Message);
0052 
0053     switch (record->m_Level)
0054     {
0055         case NPT_LOG_LEVEL_FATAL:
0056         {
0057             qCDebug(DIGIKAM_MEDIASRV_LOG_FATAL) << msg;
0058             break;
0059         }
0060 
0061         case NPT_LOG_LEVEL_SEVERE:
0062         {
0063             qCDebug(DIGIKAM_MEDIASRV_LOG_CRITICAL) << msg;
0064             break;
0065         }
0066 
0067         case NPT_LOG_LEVEL_WARNING:
0068         {
0069             qCDebug(DIGIKAM_MEDIASRV_LOG_WARN) << msg;
0070             break;
0071         }
0072 
0073         case NPT_LOG_LEVEL_INFO:
0074         {
0075             qCDebug(DIGIKAM_MEDIASRV_LOG_INFO) << msg;
0076             break;
0077         }
0078 
0079         case NPT_LOG_LEVEL_FINE:
0080         {
0081             qCDebug(DIGIKAM_MEDIASRV_LOG) << msg;
0082             break;
0083         }
0084 
0085         default: // NPT_LOG_LEVEL_DEBUG:
0086         {
0087             qCDebug(DIGIKAM_MEDIASRV_LOG_DEBUG) << msg;
0088             break;
0089         }
0090     }
0091 }
0092 
0093 namespace DigikamGenericMediaServerPlugin
0094 {
0095 
0096 class Q_DECL_HIDDEN CDeviceHostReferenceHolder
0097 {
0098 public:
0099 
0100     PLT_DeviceHostReference m_device;
0101 };
0102 
0103 class Q_DECL_HIDDEN DMediaServer::Private
0104 {
0105 public:
0106 
0107     explicit Private()
0108       : upnp        (nullptr),
0109         logHandler  (nullptr),
0110         serverHolder(nullptr)
0111     {
0112         NPT_LogManager::GetDefault().Configure("plist:.level=INFO;.handlers=CustomHandler;");
0113         NPT_LogHandler::Create("digiKam", "CustomHandler", logHandler);
0114         logHandler->SetCustomHandlerFunction(&UPnPLogger);
0115     }
0116 
0117     PLT_UPnP*                   upnp;
0118     NPT_LogHandler*             logHandler;
0119     CDeviceHostReferenceHolder* serverHolder;
0120 };
0121 
0122 DMediaServer::DMediaServer(QObject* const parent)
0123     : QObject(parent),
0124       d      (new Private)
0125 {
0126     d->serverHolder = new CDeviceHostReferenceHolder();
0127     d->upnp         = new PLT_UPnP();
0128     d->upnp->Start();
0129 }
0130 
0131 DMediaServer::~DMediaServer()
0132 {
0133     d->upnp->Stop();
0134     d->upnp->RemoveDevice(d->serverHolder->m_device);
0135 
0136     delete d->upnp;
0137     delete d->logHandler;
0138     delete d->serverHolder;
0139     delete d;
0140 }
0141 
0142 bool DMediaServer::init(int port)
0143 {
0144     QString devDesc               = i18n("%1 Media Server", qApp->applicationName());
0145 
0146     DLNAMediaServer* const device = new DLNAMediaServer(devDesc.toUtf8().data(),
0147                                                         false,
0148                                                         nullptr,
0149                                                         port);
0150 
0151     device->m_ModelName           = "digiKam";
0152     device->m_ModelNumber         = digikam_version;
0153     device->m_ModelDescription    = NPT_String(DAboutData::digiKamSlogan().toUtf8().data());
0154     device->m_ModelURL            = NPT_String(DAboutData::webProjectUrl().toString().toUtf8().data());
0155     device->m_Manufacturer        = NPT_String("digiKam.org");
0156     device->m_ManufacturerURL     = NPT_String(DAboutData::webProjectUrl().toString().toUtf8().data());
0157     device->SetDelegate(device);
0158 
0159     d->serverHolder->m_device     = device;
0160 
0161     NPT_Result res                = d->upnp->AddDevice(d->serverHolder->m_device);
0162 
0163     qCDebug(DIGIKAM_MEDIASRV_LOG) << "Upnp device created:" << res;
0164 
0165     return true;
0166 }
0167 
0168 void DMediaServer::addAlbumsOnServer(const MediaServerMap& map)
0169 {
0170     static_cast<DLNAMediaServer*>(d->serverHolder->m_device.AsPointer())->addAlbumsOnServer(map);
0171 }
0172 
0173 } // namespace DigikamGenericMediaServerPlugin
0174 
0175 #include "moc_dmediaserver.cpp"