2022-05-09 15:35:44 +03:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kded_kdirshare.h"
|
|
|
|
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <ksettings.h>
|
2022-05-09 22:15:16 +03:00
|
|
|
#include <knotification.h>
|
2022-05-09 15:35:44 +03:00
|
|
|
#include <kpluginfactory.h>
|
2022-05-09 19:04:29 +03:00
|
|
|
#include <kdebug.h>
|
2022-05-09 15:35:44 +03:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY(KDirShareModuleFactory, registerPlugin<KDirShareModule>();)
|
|
|
|
K_EXPORT_PLUGIN(KDirShareModuleFactory("kdirshare"))
|
|
|
|
|
|
|
|
KDirShareModule::KDirShareModule(QObject *parent, const QList<QVariant>&)
|
|
|
|
: KDEDModule(parent)
|
|
|
|
{
|
2022-05-09 22:15:16 +03:00
|
|
|
bool shareerror = false;
|
2022-05-09 15:35:44 +03:00
|
|
|
KSettings kdirsharesettings("kdirsharerc", KSettings::SimpleConfig);
|
|
|
|
foreach (const QString &kdirsharekey, kdirsharesettings.keys()) {
|
2022-05-09 21:50:00 +03:00
|
|
|
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()
|
|
|
|
);
|
2022-05-09 15:35:44 +03:00
|
|
|
if (!kdirshareerror.isEmpty()) {
|
|
|
|
kWarning() << kdirshareerror;
|
2022-05-09 22:15:16 +03:00
|
|
|
shareerror = true;
|
2022-05-09 15:35:44 +03:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 22:15:16 +03:00
|
|
|
|
|
|
|
if (shareerror) {
|
|
|
|
KNotification *knotification = new KNotification("ShareError");
|
|
|
|
knotification->setComponentData(KComponentData("kdirshare"));
|
|
|
|
knotification->setTitle(i18n("Directory share"));
|
|
|
|
knotification->setText(i18n("Unable to share one or more directories"));
|
|
|
|
knotification->sendEvent();
|
|
|
|
}
|
2022-05-09 15:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KDirShareModule::~KDirShareModule()
|
|
|
|
{
|
|
|
|
KSettings kdirsharesettings("kdirsharerc", KSettings::SimpleConfig);
|
|
|
|
foreach (const KDirShareImpl *kdirshareimpl, m_dirshares) {
|
|
|
|
const QByteArray kdirsharekey = kdirshareimpl->directory().toLocal8Bit().toHex();
|
2022-05-09 21:50:00 +03:00
|
|
|
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());
|
2022-05-09 15:35:44 +03:00
|
|
|
}
|
|
|
|
qDeleteAll(m_dirshares);
|
|
|
|
}
|
|
|
|
|
2022-05-09 21:50:00 +03:00
|
|
|
QString KDirShareModule::share(const QString &dirpath, const uint portmin, const uint portmax)
|
2022-05-09 15:35:44 +03:00
|
|
|
{
|
2022-05-09 21:50:00 +03:00
|
|
|
if (isShared(dirpath)) {
|
|
|
|
const QString unshareerror = unshare(dirpath);
|
|
|
|
if (!unshareerror.isEmpty()) {
|
|
|
|
return unshareerror;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 15:35:44 +03:00
|
|
|
KDirShareImpl *kdirshareimpl = new KDirShareImpl(this);
|
|
|
|
if (!kdirshareimpl->setDirectory(dirpath)) {
|
2022-05-09 21:50:00 +03:00
|
|
|
delete kdirshareimpl;
|
2022-05-09 15:35:44 +03:00
|
|
|
return i18n("Directory does not exist: %1", dirpath);
|
|
|
|
}
|
2022-05-09 21:50:00 +03:00
|
|
|
// qDebug() << Q_FUNC_INFO << serverport;
|
|
|
|
if (!kdirshareimpl->serve(QHostAddress(QHostAddress::Any), portmin, portmax)) {
|
|
|
|
delete kdirshareimpl;
|
2022-05-09 15:35:44 +03:00
|
|
|
return i18n("Could not serve: %1", kdirshareimpl->errorString());
|
|
|
|
}
|
2022-05-09 19:04:29 +03:00
|
|
|
if (!kdirshareimpl->publish()) {
|
2022-05-09 18:52:51 +03:00
|
|
|
kdirshareimpl->stop();
|
2022-05-09 21:50:00 +03:00
|
|
|
delete kdirshareimpl;
|
2022-05-09 15:35:44 +03:00
|
|
|
return i18n("Could not publish service for: %1", dirpath);
|
|
|
|
}
|
|
|
|
m_dirshares.append(kdirshareimpl);
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString KDirShareModule::unshare(const QString &dirpath)
|
|
|
|
{
|
|
|
|
foreach (KDirShareImpl *kdirshareimpl, m_dirshares) {
|
|
|
|
if (kdirshareimpl->directory() == dirpath) {
|
|
|
|
kdirshareimpl->stop();
|
2022-05-09 21:50:00 +03:00
|
|
|
delete kdirshareimpl;
|
2022-05-09 15:35:44 +03:00
|
|
|
m_dirshares.removeAll(kdirshareimpl);
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i18n("Invalid directory share: %1", dirpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KDirShareModule::isShared(const QString &dirpath) const
|
|
|
|
{
|
|
|
|
foreach (const KDirShareImpl *kdirshareimpl, m_dirshares) {
|
|
|
|
if (kdirshareimpl->directory() == dirpath) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-09 21:50:00 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-09 15:35:44 +03:00
|
|
|
#include "moc_kded_kdirshare.cpp"
|