mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-23 18:32:50 +00:00
kdirshare: implement option to control the shared directories server port
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
2ebe90b420
commit
b9370d28f8
7 changed files with 211 additions and 29 deletions
|
@ -27,6 +27,9 @@
|
|||
|
||||
#include "kdirshareplugin.h"
|
||||
|
||||
static const quint16 s_kdirshareportmin = 1000;
|
||||
static const quint16 s_kdirshareportmax = 32000;
|
||||
|
||||
K_PLUGIN_FACTORY(KDirSharePluginFactory, registerPlugin<KDirSharePlugin>();)
|
||||
K_EXPORT_PLUGIN(KDirSharePluginFactory("kdirshareplugin"))
|
||||
|
||||
|
@ -56,17 +59,41 @@ KDirSharePlugin::KDirSharePlugin(QObject *parent, const QList<QVariant> &args)
|
|||
if (m_kdirshareiface.isValid()) {
|
||||
QDBusReply<bool> kdirsharereply = m_kdirshareiface.call("isShared", m_url);
|
||||
if (!kdirsharereply.isValid()) {
|
||||
kWarning() << "Invalid kdirshare module reply";
|
||||
kWarning() << "Invalid kdirshare module reply for isShared()";
|
||||
m_ui.sharebox->setChecked(false);
|
||||
m_ui.portgroup->setEnabled(false);
|
||||
} else {
|
||||
m_ui.sharebox->setChecked(kdirsharereply.value());
|
||||
m_ui.portgroup->setEnabled(kdirsharereply.value());
|
||||
}
|
||||
|
||||
QDBusReply<quint16> kdirsharereply2 = m_kdirshareiface.call("getPortMin", m_url);
|
||||
if (!kdirsharereply2.isValid()) {
|
||||
kWarning() << "Invalid kdirshare module reply for getPortMin()";
|
||||
m_ui.portmininput->setValue(s_kdirshareportmin);
|
||||
} else {
|
||||
m_ui.portmininput->setValue(kdirsharereply2.value());
|
||||
}
|
||||
kdirsharereply2 = m_kdirshareiface.call("getPortMax", m_url);
|
||||
if (!kdirsharereply2.isValid()) {
|
||||
kWarning() << "Invalid kdirshare module reply for getPortMax()";
|
||||
m_ui.portmaxinput->setValue(s_kdirshareportmax);
|
||||
} else {
|
||||
m_ui.portmaxinput->setValue(kdirsharereply2.value());
|
||||
}
|
||||
const bool randomport = (m_ui.portmininput->value() != m_ui.portmaxinput->value());
|
||||
m_ui.randombox->setChecked(randomport);
|
||||
m_ui.portmininput->setVisible(randomport);
|
||||
} else {
|
||||
kWarning() << "kdirshare module interface is not valid";
|
||||
m_ui.sharebox->setEnabled(false);
|
||||
m_ui.portgroup->setEnabled(false);
|
||||
}
|
||||
|
||||
connect(m_ui.sharebox, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
|
||||
connect(m_ui.sharebox, SIGNAL(toggled(bool)), this, SLOT(slotShare(bool)));
|
||||
connect(m_ui.randombox, SIGNAL(toggled(bool)), this, SLOT(slotRandomPort(bool)));
|
||||
connect(m_ui.portmininput, SIGNAL(valueChanged(int)), this, SLOT(slotPortMin(int)));
|
||||
connect(m_ui.portmaxinput, SIGNAL(valueChanged(int)), this, SLOT(slotPortMax(int)));
|
||||
}
|
||||
|
||||
KDirSharePlugin::~KDirSharePlugin()
|
||||
|
@ -79,7 +106,10 @@ void KDirSharePlugin::applyChanges()
|
|||
if (m_ui.sharebox->isEnabled()) {
|
||||
QDBusReply<QString> kdirsharereply;
|
||||
if (m_ui.sharebox->isChecked()) {
|
||||
kdirsharereply = m_kdirshareiface.call("share", m_url);
|
||||
kdirsharereply = m_kdirshareiface.call("share",
|
||||
m_url,
|
||||
uint(m_ui.portmininput->value()), uint(m_ui.portmaxinput->value())
|
||||
);
|
||||
} else {
|
||||
kdirsharereply = m_kdirshareiface.call("unshare", m_url);
|
||||
}
|
||||
|
@ -94,4 +124,39 @@ void KDirSharePlugin::applyChanges()
|
|||
}
|
||||
}
|
||||
|
||||
void KDirSharePlugin::slotShare(const bool value)
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << value;
|
||||
m_ui.portgroup->setEnabled(value);
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void KDirSharePlugin::slotRandomPort(const bool value)
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << value;
|
||||
m_ui.portmininput->setVisible(value);
|
||||
if (!value) {
|
||||
m_ui.portmininput->setValue(m_ui.portmaxinput->value());
|
||||
} else {
|
||||
m_ui.portmininput->setValue(s_kdirshareportmin);
|
||||
}
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void KDirSharePlugin::slotPortMin(const int value)
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << value;
|
||||
Q_UNUSED(value);
|
||||
emit changed();
|
||||
}
|
||||
|
||||
void KDirSharePlugin::slotPortMax(const int value)
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << value;
|
||||
if (!m_ui.portmininput->isVisible()) {
|
||||
m_ui.portmininput->setValue(value);
|
||||
}
|
||||
emit changed();
|
||||
}
|
||||
|
||||
#include "moc_kdirshareplugin.cpp"
|
||||
|
|
|
@ -33,6 +33,12 @@ public:
|
|||
|
||||
void applyChanges() final;
|
||||
|
||||
private Q_SLOTS:
|
||||
void slotShare(const bool value);
|
||||
void slotRandomPort(const bool value);
|
||||
void slotPortMin(const int value);
|
||||
void slotPortMax(const int value);
|
||||
|
||||
private:
|
||||
Ui_KDirShareUI m_ui;
|
||||
QDBusInterface m_kdirshareiface;
|
||||
|
|
|
@ -25,6 +25,46 @@
|
|||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QGroupBox" name="portgroup">
|
||||
<property name="title">
|
||||
<string>Port</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="randombox">
|
||||
<property name="text">
|
||||
<string>Random in the range of:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="KIntNumInput" name="portmininput">
|
||||
<property name="minimum">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>32000</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="KIntNumInput" name="portmaxinput">
|
||||
<property name="minimum">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>32000</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
|
@ -40,6 +80,13 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>KIntNumInput</class>
|
||||
<extends></extends>
|
||||
<header>knuminput.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
|
|
@ -23,15 +23,6 @@
|
|||
#include <kpluginfactory.h>
|
||||
#include <kdebug.h>
|
||||
|
||||
static quint16 getRandomPort()
|
||||
{
|
||||
quint16 portnumber = 0;
|
||||
while (portnumber < 1000 || portnumber > 30000) {
|
||||
portnumber = quint16(qrand());
|
||||
}
|
||||
return portnumber;
|
||||
}
|
||||
|
||||
K_PLUGIN_FACTORY(KDirShareModuleFactory, registerPlugin<KDirShareModule>();)
|
||||
K_EXPORT_PLUGIN(KDirShareModuleFactory("kdirshare"))
|
||||
|
||||
|
@ -40,8 +31,13 @@ KDirShareModule::KDirShareModule(QObject *parent, const QList<QVariant>&)
|
|||
{
|
||||
KSettings kdirsharesettings("kdirsharerc", KSettings::SimpleConfig);
|
||||
foreach (const QString &kdirsharekey, kdirsharesettings.keys()) {
|
||||
const QString kdirsharedir = kdirsharesettings.value(kdirsharekey).toString();
|
||||
const QString kdirshareerror = share(kdirsharedir);
|
||||
const QString kdirsharedirpathkey = QString::fromLatin1("%1/dirpath").arg(kdirsharekey);
|
||||
const QString kdirshareportminkey = QString::fromLatin1("%1/portmin").arg(kdirsharekey);
|
||||
const QString kdirshareportmaxkey = QString::fromLatin1("%1/portmax").arg(kdirsharekey);
|
||||
const QString kdirshareerror = share(
|
||||
kdirsharesettings.value(kdirsharedirpathkey).toString(),
|
||||
kdirsharesettings.value(kdirshareportminkey).toUInt(), kdirsharesettings.value(kdirshareportmaxkey).toUInt()
|
||||
);
|
||||
if (!kdirshareerror.isEmpty()) {
|
||||
kWarning() << kdirshareerror;
|
||||
}
|
||||
|
@ -53,27 +49,38 @@ KDirShareModule::~KDirShareModule()
|
|||
KSettings kdirsharesettings("kdirsharerc", KSettings::SimpleConfig);
|
||||
foreach (const KDirShareImpl *kdirshareimpl, m_dirshares) {
|
||||
const QByteArray kdirsharekey = kdirshareimpl->directory().toLocal8Bit().toHex();
|
||||
kdirsharesettings.setValue(kdirsharekey, kdirshareimpl->directory());
|
||||
const QString kdirsharedirpathkey = QString::fromLatin1("%1/dirpath").arg(kdirsharekey.constData());
|
||||
const QString kdirshareportminkey = QString::fromLatin1("%1/portmin").arg(kdirsharekey.constData());
|
||||
const QString kdirshareportmaxkey = QString::fromLatin1("%1/portmax").arg(kdirsharekey.constData());
|
||||
kdirsharesettings.setValue(kdirsharedirpathkey, kdirshareimpl->directory());
|
||||
kdirsharesettings.setValue(kdirshareportminkey, kdirshareimpl->portMin());
|
||||
kdirsharesettings.setValue(kdirshareportmaxkey, kdirshareimpl->portMax());
|
||||
}
|
||||
qDeleteAll(m_dirshares);
|
||||
}
|
||||
|
||||
QString KDirShareModule::share(const QString &dirpath)
|
||||
QString KDirShareModule::share(const QString &dirpath, const uint portmin, const uint portmax)
|
||||
{
|
||||
if (isShared(dirpath)) {
|
||||
const QString unshareerror = unshare(dirpath);
|
||||
if (!unshareerror.isEmpty()) {
|
||||
return unshareerror;
|
||||
}
|
||||
}
|
||||
|
||||
KDirShareImpl *kdirshareimpl = new KDirShareImpl(this);
|
||||
if (!kdirshareimpl->setDirectory(dirpath)) {
|
||||
kdirshareimpl->deleteLater();
|
||||
delete kdirshareimpl;
|
||||
return i18n("Directory does not exist: %1", dirpath);
|
||||
}
|
||||
const quint16 randomport = getRandomPort();
|
||||
// qDebug() << Q_FUNC_INFO << randomport;
|
||||
if (!kdirshareimpl->serve(QHostAddress(QHostAddress::Any), randomport)) {
|
||||
kdirshareimpl->deleteLater();
|
||||
// qDebug() << Q_FUNC_INFO << serverport;
|
||||
if (!kdirshareimpl->serve(QHostAddress(QHostAddress::Any), portmin, portmax)) {
|
||||
delete kdirshareimpl;
|
||||
return i18n("Could not serve: %1", kdirshareimpl->errorString());
|
||||
}
|
||||
if (!kdirshareimpl->publish()) {
|
||||
kdirshareimpl->stop();
|
||||
kdirshareimpl->deleteLater();
|
||||
delete kdirshareimpl;
|
||||
return i18n("Could not publish service for: %1", dirpath);
|
||||
}
|
||||
m_dirshares.append(kdirshareimpl);
|
||||
|
@ -85,7 +92,7 @@ QString KDirShareModule::unshare(const QString &dirpath)
|
|||
foreach (KDirShareImpl *kdirshareimpl, m_dirshares) {
|
||||
if (kdirshareimpl->directory() == dirpath) {
|
||||
kdirshareimpl->stop();
|
||||
kdirshareimpl->deleteLater();
|
||||
delete kdirshareimpl;
|
||||
m_dirshares.removeAll(kdirshareimpl);
|
||||
return QString();
|
||||
}
|
||||
|
@ -103,4 +110,24 @@ bool KDirShareModule::isShared(const QString &dirpath) const
|
|||
return false;
|
||||
}
|
||||
|
||||
quint16 KDirShareModule::getPortMin(const QString &dirpath) const
|
||||
{
|
||||
foreach (const KDirShareImpl *kdirshareimpl, m_dirshares) {
|
||||
if (kdirshareimpl->directory() == dirpath) {
|
||||
return kdirshareimpl->portMin();
|
||||
}
|
||||
}
|
||||
return s_kdirshareportmin;
|
||||
}
|
||||
|
||||
quint16 KDirShareModule::getPortMax(const QString &dirpath) const
|
||||
{
|
||||
foreach (const KDirShareImpl *kdirshareimpl, m_dirshares) {
|
||||
if (kdirshareimpl->directory() == dirpath) {
|
||||
return kdirshareimpl->portMax();
|
||||
}
|
||||
}
|
||||
return s_kdirshareportmax;
|
||||
}
|
||||
|
||||
#include "moc_kded_kdirshare.cpp"
|
||||
|
|
|
@ -34,11 +34,14 @@ public:
|
|||
~KDirShareModule();
|
||||
|
||||
public Q_SLOTS:
|
||||
Q_SCRIPTABLE QString share(const QString &dirpath);
|
||||
Q_SCRIPTABLE QString share(const QString &dirpath, const uint portmin, const uint portmax);
|
||||
Q_SCRIPTABLE QString unshare(const QString &dirpath);
|
||||
|
||||
Q_SCRIPTABLE bool isShared(const QString &dirpath) const;
|
||||
|
||||
Q_SCRIPTABLE quint16 getPortMin(const QString &dirpath) const;
|
||||
Q_SCRIPTABLE quint16 getPortMax(const QString &dirpath) const;
|
||||
|
||||
private:
|
||||
QList<KDirShareImpl*> m_dirshares;
|
||||
};
|
||||
|
|
|
@ -29,6 +29,18 @@
|
|||
|
||||
static const QDir::SortFlags s_dirsortflags = (QDir::Name | QDir::DirsFirst);
|
||||
|
||||
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 contentForDirectory(const QString &path, const QString &basedir)
|
||||
{
|
||||
QByteArray data;
|
||||
|
@ -98,7 +110,9 @@ static QByteArray contentForDirectory(const QString &path, const QString &basedi
|
|||
KDirShareImpl::KDirShareImpl(QObject *parent)
|
||||
: KHTTP(parent),
|
||||
m_directory(QDir::currentPath()),
|
||||
m_port(0)
|
||||
m_port(0),
|
||||
m_portmin(s_kdirshareportmin),
|
||||
m_portmax(s_kdirshareportmax)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -122,10 +136,12 @@ bool KDirShareImpl::setDirectory(const QString &dirpath)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool KDirShareImpl::serve(const QHostAddress &address, const quint16 port)
|
||||
bool KDirShareImpl::serve(const QHostAddress &address, const quint16 portmin, const quint16 portmax)
|
||||
{
|
||||
m_port = port;
|
||||
return start(address, port);
|
||||
m_port = getPort(portmin, portmax);
|
||||
m_portmin = portmin;
|
||||
m_portmax = portmax;
|
||||
return start(address, m_port);
|
||||
}
|
||||
|
||||
bool KDirShareImpl::publish()
|
||||
|
@ -136,6 +152,16 @@ bool KDirShareImpl::publish()
|
|||
);
|
||||
}
|
||||
|
||||
quint16 KDirShareImpl::portMin() const
|
||||
{
|
||||
return m_portmin;
|
||||
}
|
||||
|
||||
quint16 KDirShareImpl::portMax() const
|
||||
{
|
||||
return m_portmax;
|
||||
}
|
||||
|
||||
void KDirShareImpl::respond(const QByteArray &url, QByteArray *outdata, ushort *outhttpstatus, KHTTPHeaders *outheaders)
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << url;
|
||||
|
|
|
@ -22,6 +22,9 @@
|
|||
#include <khttp.h>
|
||||
#include <kdnssd.h>
|
||||
|
||||
static const quint16 s_kdirshareportmin = 1000;
|
||||
static const quint16 s_kdirshareportmax = 32000;
|
||||
|
||||
class KDirShareImpl : public KHTTP
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -31,15 +34,20 @@ public:
|
|||
|
||||
QString directory() const;
|
||||
bool setDirectory(const QString &dirpath);
|
||||
bool serve(const QHostAddress &address, const quint16 port);
|
||||
bool serve(const QHostAddress &address, const quint16 portmin, const quint16 portmax);
|
||||
bool publish();
|
||||
|
||||
quint16 portMin() const;
|
||||
quint16 portMax() const;
|
||||
|
||||
protected:
|
||||
void respond(const QByteArray &url, QByteArray *outdata, ushort *outhttpstatus, KHTTPHeaders *outheaders) final;
|
||||
|
||||
private:
|
||||
QString m_directory;
|
||||
quint16 m_port;
|
||||
quint16 m_portmin;
|
||||
quint16 m_portmax;
|
||||
KDNSSD m_kdnssd;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue