mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-23 18:32:50 +00:00
kdirshare: move code for determening port number to server thread class
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
269e1224e9
commit
19ed9f7462
6 changed files with 64 additions and 52 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
include_directories(
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
add_subdirectory(filepropertiesplugin)
|
add_subdirectory(filepropertiesplugin)
|
||||||
add_subdirectory(kded)
|
add_subdirectory(kded)
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,9 @@
|
||||||
Boston, MA 02110-1301, USA.
|
Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "kdirshareplugin.h"
|
||||||
|
#include "kdirshare.h"
|
||||||
|
|
||||||
#include <QDBusReply>
|
#include <QDBusReply>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <kvbox.h>
|
#include <kvbox.h>
|
||||||
|
@ -25,11 +28,6 @@
|
||||||
#include <kpluginfactory.h>
|
#include <kpluginfactory.h>
|
||||||
#include <kpluginloader.h>
|
#include <kpluginloader.h>
|
||||||
|
|
||||||
#include "kdirshareplugin.h"
|
|
||||||
|
|
||||||
static const quint16 s_kdirshareportmin = 1000;
|
|
||||||
static const quint16 s_kdirshareportmax = 32000;
|
|
||||||
|
|
||||||
K_PLUGIN_FACTORY(KDirSharePluginFactory, registerPlugin<KDirSharePlugin>();)
|
K_PLUGIN_FACTORY(KDirSharePluginFactory, registerPlugin<KDirSharePlugin>();)
|
||||||
K_EXPORT_PLUGIN(KDirSharePluginFactory("kdirshareplugin"))
|
K_EXPORT_PLUGIN(KDirSharePluginFactory("kdirshareplugin"))
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "kded_kdirshare.h"
|
#include "kded_kdirshare.h"
|
||||||
|
#include "kdirshare.h"
|
||||||
|
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
@ -26,6 +27,24 @@
|
||||||
#include <kpluginfactory.h>
|
#include <kpluginfactory.h>
|
||||||
#include <kdebug.h>
|
#include <kdebug.h>
|
||||||
|
|
||||||
|
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
|
class KDirShareThread : public QThread
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -53,6 +72,7 @@ private:
|
||||||
KDirShareImpl* m_kdirshareimpl;
|
KDirShareImpl* m_kdirshareimpl;
|
||||||
bool m_starting;
|
bool m_starting;
|
||||||
QString m_directory;
|
QString m_directory;
|
||||||
|
quint16 m_port;
|
||||||
quint16 m_portmin;
|
quint16 m_portmin;
|
||||||
quint16 m_portmax;
|
quint16 m_portmax;
|
||||||
QString m_error;
|
QString m_error;
|
||||||
|
@ -62,8 +82,9 @@ KDirShareThread::KDirShareThread(QObject *parent)
|
||||||
: QThread(parent),
|
: QThread(parent),
|
||||||
m_kdirshareimpl(new KDirShareImpl(this)),
|
m_kdirshareimpl(new KDirShareImpl(this)),
|
||||||
m_starting(false),
|
m_starting(false),
|
||||||
m_portmin(0),
|
m_port(0),
|
||||||
m_portmax(0)
|
m_portmin(s_kdirshareportmin),
|
||||||
|
m_portmax(s_kdirshareportmax)
|
||||||
{
|
{
|
||||||
connect(
|
connect(
|
||||||
this, SIGNAL(unblock()),
|
this, SIGNAL(unblock()),
|
||||||
|
@ -105,7 +126,7 @@ void KDirShareThread::run()
|
||||||
emit unblock();
|
emit unblock();
|
||||||
return;
|
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 serveError(i18n("Could not serve: %1", m_kdirshareimpl->errorString()));
|
||||||
emit unblock();
|
emit unblock();
|
||||||
return;
|
return;
|
||||||
|
@ -121,8 +142,9 @@ void KDirShareThread::run()
|
||||||
|
|
||||||
QString KDirShareThread::serve(const QString &dirpath, const quint16 portmin, const quint16 portmax)
|
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_directory = dirpath;
|
||||||
|
m_port = getPort(portmin, portmax);
|
||||||
m_portmin = portmin;
|
m_portmin = portmin;
|
||||||
m_portmax = portmax;
|
m_portmax = portmax;
|
||||||
m_starting = true;
|
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<KDirShareModule>();)
|
K_PLUGIN_FACTORY(KDirShareModuleFactory, registerPlugin<KDirShareModule>();)
|
||||||
K_EXPORT_PLUGIN(KDirShareModuleFactory("kdirshare"))
|
K_EXPORT_PLUGIN(KDirShareModuleFactory("kdirshare"))
|
||||||
|
|
||||||
|
@ -192,7 +209,7 @@ KDirShareModule::~KDirShareModule()
|
||||||
{
|
{
|
||||||
KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
|
KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
|
||||||
foreach (const KDirShareThread *kdirsharethread, m_dirshares) {
|
foreach (const KDirShareThread *kdirsharethread, m_dirshares) {
|
||||||
const QByteArray kdirsharekey = getDirShareKey(kdirsharethread);
|
const QByteArray kdirsharekey = getDirShareKey(kdirsharethread->directory());
|
||||||
KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
|
KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
|
||||||
// qDebug() << Q_FUNC_INFO << kdirsharekey << kdirsharethread->directory() << kdirsharethread->portMin() << kdirsharethread->portMax();
|
// qDebug() << Q_FUNC_INFO << kdirsharekey << kdirsharethread->directory() << kdirsharethread->portMin() << kdirsharethread->portMax();
|
||||||
kdirsharegroup.writeEntry("dirpath", kdirsharethread->directory());
|
kdirsharegroup.writeEntry("dirpath", kdirsharethread->directory());
|
||||||
|
@ -228,7 +245,7 @@ QString KDirShareModule::unshare(const QString &dirpath)
|
||||||
foreach (KDirShareThread *kdirsharethread, m_dirshares) {
|
foreach (KDirShareThread *kdirsharethread, m_dirshares) {
|
||||||
if (kdirsharethread->directory() == dirpath) {
|
if (kdirsharethread->directory() == dirpath) {
|
||||||
KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
|
KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
|
||||||
const QByteArray kdirsharekey = getDirShareKey(kdirsharethread);
|
const QByteArray kdirsharekey = getDirShareKey(kdirsharethread->directory());
|
||||||
KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
|
KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
|
||||||
kdirsharegroup.writeEntry("dirpath", QString());
|
kdirsharegroup.writeEntry("dirpath", QString());
|
||||||
kdirsharethread->terminate();
|
kdirsharethread->terminate();
|
||||||
|
|
|
@ -33,18 +33,6 @@ static const QByteArray s_data500("<html>500 Internal Server Error</html>");
|
||||||
// TODO: figure out what the Avahi limit is
|
// TODO: figure out what the Avahi limit is
|
||||||
static const int s_sharenamelimit = 40;
|
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)
|
static QString getShareName(const QString &dirpath)
|
||||||
{
|
{
|
||||||
const QString absolutedirpath = QDir(dirpath).absolutePath();
|
const QString absolutedirpath = QDir(dirpath).absolutePath();
|
||||||
|
@ -150,9 +138,7 @@ static QByteArray contentForDirectory(const QString &path, const QString &basedi
|
||||||
KDirShareImpl::KDirShareImpl(QObject *parent)
|
KDirShareImpl::KDirShareImpl(QObject *parent)
|
||||||
: KHTTP(parent),
|
: KHTTP(parent),
|
||||||
m_directory(QDir::currentPath()),
|
m_directory(QDir::currentPath()),
|
||||||
m_port(0),
|
m_port(0)
|
||||||
m_portmin(s_kdirshareportmin),
|
|
||||||
m_portmax(s_kdirshareportmax)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,11 +162,9 @@ bool KDirShareImpl::setDirectory(const QString &dirpath)
|
||||||
return true;
|
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_port = port;
|
||||||
m_portmin = portmin;
|
|
||||||
m_portmax = portmax;
|
|
||||||
return start(address, m_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
|
QString KDirShareImpl::publishError() const
|
||||||
{
|
{
|
||||||
return m_kdnssd.errorString();
|
return m_kdnssd.errorString();
|
||||||
|
|
|
@ -22,9 +22,6 @@
|
||||||
#include <khttp.h>
|
#include <khttp.h>
|
||||||
#include <kdnssd.h>
|
#include <kdnssd.h>
|
||||||
|
|
||||||
static const quint16 s_kdirshareportmin = 1000;
|
|
||||||
static const quint16 s_kdirshareportmax = 32000;
|
|
||||||
|
|
||||||
class KDirShareImpl : public KHTTP
|
class KDirShareImpl : public KHTTP
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -34,12 +31,9 @@ public:
|
||||||
|
|
||||||
QString directory() const;
|
QString directory() const;
|
||||||
bool setDirectory(const QString &dirpath);
|
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();
|
bool publish();
|
||||||
|
|
||||||
quint16 portMin() const;
|
|
||||||
quint16 portMax() const;
|
|
||||||
|
|
||||||
QString publishError() const;
|
QString publishError() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
25
kdirshare/kdirshare.h
Normal file
25
kdirshare/kdirshare.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/* This file is part of the KDE project
|
||||||
|
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
|
||||||
|
|
||||||
|
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
|
Loading…
Add table
Reference in a new issue