From 19ed9f746285b85a1934fa0ef5ab18c1673d7809 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Wed, 11 May 2022 20:16:45 +0300 Subject: [PATCH] kdirshare: move code for determening port number to server thread class Signed-off-by: Ivailo Monev --- kdirshare/CMakeLists.txt | 4 +++ kdirshare/filepropertiesplugin/kdirshareplugin.cpp | 8 ++--- kdirshare/kded/kded_kdirshare.cpp | 39 ++++++++++++++++------ kdirshare/kded/kdirshareimpl.cpp | 32 ++---------------- kdirshare/kded/kdirshareimpl.h | 8 +---- kdirshare/kdirshare.h | 25 ++++++++++++++ 6 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 kdirshare/kdirshare.h diff --git a/kdirshare/CMakeLists.txt b/kdirshare/CMakeLists.txt index f221b42f..fd3c35a7 100644 --- a/kdirshare/CMakeLists.txt +++ b/kdirshare/CMakeLists.txt @@ -1,3 +1,7 @@ +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} +) + add_subdirectory(filepropertiesplugin) add_subdirectory(kded) diff --git a/kdirshare/filepropertiesplugin/kdirshareplugin.cpp b/kdirshare/filepropertiesplugin/kdirshareplugin.cpp index 310da179..6f1139cf 100644 --- a/kdirshare/filepropertiesplugin/kdirshareplugin.cpp +++ b/kdirshare/filepropertiesplugin/kdirshareplugin.cpp @@ -16,6 +16,9 @@ Boston, MA 02110-1301, USA. */ +#include "kdirshareplugin.h" +#include "kdirshare.h" + #include #include #include @@ -25,11 +28,6 @@ #include #include -#include "kdirshareplugin.h" - -static const quint16 s_kdirshareportmin = 1000; -static const quint16 s_kdirshareportmax = 32000; - K_PLUGIN_FACTORY(KDirSharePluginFactory, registerPlugin();) K_EXPORT_PLUGIN(KDirSharePluginFactory("kdirshareplugin")) diff --git a/kdirshare/kded/kded_kdirshare.cpp b/kdirshare/kded/kded_kdirshare.cpp index 5a5f37cb..f6b4ca6f 100644 --- a/kdirshare/kded/kded_kdirshare.cpp +++ b/kdirshare/kded/kded_kdirshare.cpp @@ -17,6 +17,7 @@ */ #include "kded_kdirshare.h" +#include "kdirshare.h" #include #include @@ -26,6 +27,24 @@ #include #include +static quint16 getPort(const quint16 portmin, const quint16 portmax) +{ + if (portmin == portmax) { + return portmax; + } + quint16 portnumber = 0; + while (portnumber < portmin || portnumber > portmax) { + portnumber = quint16(qrand()); + } + return portnumber; +} + +static QByteArray getDirShareKey(const QString &kdirsharedirpath) +{ + return kdirsharedirpath.toLocal8Bit().toHex(); +}; + + class KDirShareThread : public QThread { Q_OBJECT @@ -53,6 +72,7 @@ private: KDirShareImpl* m_kdirshareimpl; bool m_starting; QString m_directory; + quint16 m_port; quint16 m_portmin; quint16 m_portmax; QString m_error; @@ -62,8 +82,9 @@ KDirShareThread::KDirShareThread(QObject *parent) : QThread(parent), m_kdirshareimpl(new KDirShareImpl(this)), m_starting(false), - m_portmin(0), - m_portmax(0) + m_port(0), + m_portmin(s_kdirshareportmin), + m_portmax(s_kdirshareportmax) { connect( this, SIGNAL(unblock()), @@ -105,7 +126,7 @@ void KDirShareThread::run() emit unblock(); return; } - if (!m_kdirshareimpl->serve(QHostAddress(QHostAddress::Any), m_portmin, m_portmax)) { + if (!m_kdirshareimpl->serve(QHostAddress(QHostAddress::Any), m_port)) { emit serveError(i18n("Could not serve: %1", m_kdirshareimpl->errorString())); emit unblock(); return; @@ -121,8 +142,9 @@ void KDirShareThread::run() QString KDirShareThread::serve(const QString &dirpath, const quint16 portmin, const quint16 portmax) { - // qDebug() << Q_FUNC_INFO << dirpath << portmin << portmax; + // qDebug() << Q_FUNC_INFO << dirpath << port; m_directory = dirpath; + m_port = getPort(portmin, portmax); m_portmin = portmin; m_portmax = portmax; m_starting = true; @@ -146,11 +168,6 @@ void KDirShareThread::slotServeError(const QString &error) } -static QByteArray getDirShareKey(const KDirShareThread *kdirsharethread) -{ - return kdirsharethread->directory().toLocal8Bit().toHex(); -}; - K_PLUGIN_FACTORY(KDirShareModuleFactory, registerPlugin();) K_EXPORT_PLUGIN(KDirShareModuleFactory("kdirshare")) @@ -192,7 +209,7 @@ KDirShareModule::~KDirShareModule() { KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig); foreach (const KDirShareThread *kdirsharethread, m_dirshares) { - const QByteArray kdirsharekey = getDirShareKey(kdirsharethread); + const QByteArray kdirsharekey = getDirShareKey(kdirsharethread->directory()); KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey); // qDebug() << Q_FUNC_INFO << kdirsharekey << kdirsharethread->directory() << kdirsharethread->portMin() << kdirsharethread->portMax(); kdirsharegroup.writeEntry("dirpath", kdirsharethread->directory()); @@ -228,7 +245,7 @@ QString KDirShareModule::unshare(const QString &dirpath) foreach (KDirShareThread *kdirsharethread, m_dirshares) { if (kdirsharethread->directory() == dirpath) { KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig); - const QByteArray kdirsharekey = getDirShareKey(kdirsharethread); + const QByteArray kdirsharekey = getDirShareKey(kdirsharethread->directory()); KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey); kdirsharegroup.writeEntry("dirpath", QString()); kdirsharethread->terminate(); diff --git a/kdirshare/kded/kdirshareimpl.cpp b/kdirshare/kded/kdirshareimpl.cpp index 77462c75..d32301d1 100644 --- a/kdirshare/kded/kdirshareimpl.cpp +++ b/kdirshare/kded/kdirshareimpl.cpp @@ -33,18 +33,6 @@ static const QByteArray s_data500("500 Internal Server Error"); // TODO: figure out what the Avahi limit is static const int s_sharenamelimit = 40; -static quint16 getPort(const quint16 portmin, const quint16 portmax) -{ - if (portmin == portmax) { - return portmax; - } - quint16 portnumber = 0; - while (portnumber < portmin || portnumber > portmax) { - portnumber = quint16(qrand()); - } - return portnumber; -} - static QString getShareName(const QString &dirpath) { const QString absolutedirpath = QDir(dirpath).absolutePath(); @@ -150,9 +138,7 @@ static QByteArray contentForDirectory(const QString &path, const QString &basedi KDirShareImpl::KDirShareImpl(QObject *parent) : KHTTP(parent), m_directory(QDir::currentPath()), - m_port(0), - m_portmin(s_kdirshareportmin), - m_portmax(s_kdirshareportmax) + m_port(0) { } @@ -176,11 +162,9 @@ bool KDirShareImpl::setDirectory(const QString &dirpath) return true; } -bool KDirShareImpl::serve(const QHostAddress &address, const quint16 portmin, const quint16 portmax) +bool KDirShareImpl::serve(const QHostAddress &address, const quint16 port) { - m_port = getPort(portmin, portmax); - m_portmin = portmin; - m_portmax = portmax; + m_port = port; return start(address, m_port); } @@ -192,16 +176,6 @@ bool KDirShareImpl::publish() ); } -quint16 KDirShareImpl::portMin() const -{ - return m_portmin; -} - -quint16 KDirShareImpl::portMax() const -{ - return m_portmax; -} - QString KDirShareImpl::publishError() const { return m_kdnssd.errorString(); diff --git a/kdirshare/kded/kdirshareimpl.h b/kdirshare/kded/kdirshareimpl.h index 76a4e1b8..c5b10d95 100644 --- a/kdirshare/kded/kdirshareimpl.h +++ b/kdirshare/kded/kdirshareimpl.h @@ -22,9 +22,6 @@ #include #include -static const quint16 s_kdirshareportmin = 1000; -static const quint16 s_kdirshareportmax = 32000; - class KDirShareImpl : public KHTTP { Q_OBJECT @@ -34,12 +31,9 @@ public: QString directory() const; bool setDirectory(const QString &dirpath); - bool serve(const QHostAddress &address, const quint16 portmin, const quint16 portmax); + bool serve(const QHostAddress &address, const quint16 port); bool publish(); - quint16 portMin() const; - quint16 portMax() const; - QString publishError() const; protected: diff --git a/kdirshare/kdirshare.h b/kdirshare/kdirshare.h new file mode 100644 index 00000000..e4107c8f --- /dev/null +++ b/kdirshare/kdirshare.h @@ -0,0 +1,25 @@ +/* This file is part of the KDE project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KDIRSHARE_H +#define KDIRSHARE_H + +static const quint16 s_kdirshareportmin = 1000; +static const quint16 s_kdirshareportmax = 32000; + +#endif // KDIRSHARE_H -- 2.11.0