kWarning() << "Invalid kdirshare module reply for isShared()";
m_ui.sharebox->setChecked(false);
m_ui.portgroup->setEnabled(false);
+ m_ui.authgroup->setEnabled(false);
} else {
m_ui.sharebox->setChecked(kdirsharereply.value());
m_ui.portgroup->setEnabled(kdirsharereply.value());
+ m_ui.authgroup->setEnabled(kdirsharereply.value());
}
QDBusReply<quint16> kdirsharereply2 = m_kdirshareiface.call("getPortMin", m_url);
const bool randomport = (m_ui.portmininput->value() != m_ui.portmaxinput->value());
m_ui.randombox->setChecked(randomport);
m_ui.portmininput->setVisible(randomport);
+
+ QDBusReply<QString> kdirsharereply3 = m_kdirshareiface.call("getUser", m_url);
+ if (!kdirsharereply3.isValid()) {
+ kWarning() << "Invalid kdirshare module reply for getUser()";
+ m_ui.useredit->setText(QString());
+ } else {
+ m_ui.useredit->setText(kdirsharereply3.value());
+ }
+ kdirsharereply3 = m_kdirshareiface.call("getPassword", m_url);
+ if (!kdirsharereply3.isValid()) {
+ kWarning() << "Invalid kdirshare module reply for getPassword()";
+ m_ui.passwordedit->setText(QString());
+ } else {
+ m_ui.passwordedit->setText(kdirsharereply3.value());
+ }
+ if (!m_ui.useredit->text().isEmpty() || !m_ui.passwordedit->text().isEmpty()) {
+ m_ui.authbox->setChecked(true);
+ }
+ m_ui.useredit->setEnabled(m_ui.authbox->isChecked());
+ m_ui.passwordedit->setEnabled(m_ui.authbox->isChecked());
} else {
kWarning() << "kdirshare module interface is not valid";
m_ui.sharebox->setEnabled(false);
m_ui.portgroup->setEnabled(false);
+ m_ui.authgroup->setEnabled(false);
}
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)));
+ connect(m_ui.authbox, SIGNAL(toggled(bool)), this, SLOT(slotAuthorization(bool)));
+ connect(m_ui.useredit, SIGNAL(textEdited(QString)), this, SLOT(slotUserEdited(QString)));
+ connect(m_ui.passwordedit, SIGNAL(textEdited(QString)), this, SLOT(slotPasswordEdited(QString)));
}
KDirSharePlugin::~KDirSharePlugin()
if (m_ui.sharebox->isChecked()) {
kdirsharereply = m_kdirshareiface.call("share",
m_url,
- uint(m_ui.portmininput->value()), uint(m_ui.portmaxinput->value())
+ uint(m_ui.portmininput->value()), uint(m_ui.portmaxinput->value()),
+ m_ui.useredit->text(), m_ui.passwordedit->text()
);
} else {
kdirsharereply = m_kdirshareiface.call("unshare", m_url);
{
// qDebug() << Q_FUNC_INFO << value;
m_ui.portgroup->setEnabled(value);
+ m_ui.authgroup->setEnabled(value);
emit changed();
}
emit changed();
}
+void KDirSharePlugin::slotAuthorization(const bool value)
+{
+ // qDebug() << Q_FUNC_INFO << value;
+ m_ui.useredit->setEnabled(value);
+ m_ui.passwordedit->setEnabled(value);
+ if (!value) {
+ m_ui.useredit->clear();
+ m_ui.passwordedit->clear();
+ }
+ emit changed();
+}
+
+void KDirSharePlugin::slotUserEdited(const QString &value)
+{
+ // qDebug() << Q_FUNC_INFO << value;
+ emit changed();
+}
+
+void KDirSharePlugin::slotPasswordEdited(const QString &value)
+{
+ // qDebug() << Q_FUNC_INFO << value;
+ emit changed();
+}
+
#include "moc_kdirshareplugin.cpp"
#include <QThread>
#include <QCoreApplication>
+#include <QTimer>
#include <klocale.h>
#include <kconfiggroup.h>
#include <knotification.h>
KDirShareThread(QObject *parent = nullptr);
~KDirShareThread();
- QString serve(const QString &dirpath, const quint16 portmin, const quint16 portmax);
+ QString serve(const QString &dirpath,
+ const quint16 portmin, const quint16 portmax,
+ const QString &username, const QString &password);
QString directory() const;
quint16 portMin() const;
quint16 portMax() const;
+ QString user() const;
+ QString password() const;
Q_SIGNALS:
void unblock();
quint16 m_port;
quint16 m_portmin;
quint16 m_portmax;
+ QString m_user;
+ QString m_password;
QString m_error;
};
return m_portmax;
}
+QString KDirShareThread::user() const
+{
+ return m_user;
+}
+
+QString KDirShareThread::password() const
+{
+ return m_password;
+}
+
void KDirShareThread::run()
{
if (!m_kdirshareimpl->setDirectory(m_directory)) {
emit unblock();
return;
}
+ if (!m_user.isEmpty() && !m_password.isEmpty()) {
+ if (!m_kdirshareimpl->setAuthenticate(m_user.toUtf8(), m_password.toUtf8(), i18n("Not authorized"))) {
+ emit serveError(i18n("Could not set authentication: %1", m_kdirshareimpl->errorString()));
+ emit unblock();
+ return;
+ }
+ }
if (!m_kdirshareimpl->serve(QHostAddress(QHostAddress::Any), m_port)) {
emit serveError(i18n("Could not serve: %1", m_kdirshareimpl->errorString()));
emit unblock();
emit unblock();
}
-QString KDirShareThread::serve(const QString &dirpath, const quint16 portmin, const quint16 portmax)
+QString KDirShareThread::serve(const QString &dirpath,
+ const quint16 portmin, const quint16 portmax,
+ const QString &username, const QString &password)
{
// qDebug() << Q_FUNC_INFO << dirpath << port;
m_directory = dirpath;
m_port = getPort(portmin, portmax);
m_portmin = portmin;
m_portmax = portmax;
+ m_user = username;
+ m_password = password;
m_starting = true;
m_error.clear();
start();
KDirShareModule::KDirShareModule(QObject *parent, const QList<QVariant>&)
: KDEDModule(parent)
{
- bool shareerror = false;
- KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
- foreach (const QString &kdirsharekey, kdirshareconfig.groupList()) {
- // qDebug() << Q_FUNC_INFO << kdirsharekey;
- KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
- const QString kdirsharedirpath = kdirsharegroup.readEntry("dirpath", QString());
- if (kdirsharedirpath.isEmpty()) {
- continue;
- }
- const uint kdirshareportmin = kdirsharegroup.readEntry("portmin", uint(s_kdirshareportmin));
- const uint kdirshareportmax = kdirsharegroup.readEntry("portmax", uint(s_kdirshareportmax));
- // qDebug() << Q_FUNC_INFO << kdirsharekey << kdirsharedirpath << kdirshareportmin << kdirshareportmax;
- const QString kdirshareerror = share(
- kdirsharedirpath,
- quint16(kdirshareportmin), quint16(kdirshareportmax)
- );
- if (!kdirshareerror.isEmpty()) {
- kWarning() << kdirshareerror;
- shareerror = true;
- }
- }
-
- 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();
- }
+ m_passwdstore.setStoreID("KDirShare");
+ // HACK: kpasswdstore uses on-demand service so doing delayed restore
+ QTimer::singleShot(2000, this, SLOT(slotDelayedRestore()));
}
KDirShareModule::~KDirShareModule()
kdirsharegroup.writeEntry("dirpath", kdirsharethread->directory());
kdirsharegroup.writeEntry("portmin", uint(kdirsharethread->portMin()));
kdirsharegroup.writeEntry("portmax", uint(kdirsharethread->portMax()));
+ kdirsharegroup.writeEntry("user", kdirsharethread->user());
}
qDeleteAll(m_dirshares);
m_dirshares.clear();
}
-QString KDirShareModule::share(const QString &dirpath, const uint portmin, const uint portmax)
+QString KDirShareModule::share(const QString &dirpath,
+ const uint portmin, const uint portmax,
+ const QString &username, const QString &password)
{
if (isShared(dirpath)) {
const QString unshareerror = unshare(dirpath);
KDirShareThread *kdirsharethread = new KDirShareThread(this);
// qDebug() << Q_FUNC_INFO << dirpath << portmin << portmax;
- const QString serveerror = kdirsharethread->serve(dirpath, portmin, portmax);
+ const QString serveerror = kdirsharethread->serve(
+ dirpath,
+ portmin, portmax,
+ username, password
+ );
+ if (!username.isEmpty() && !password.isEmpty()) {
+ m_passwdstore.storePasswd(KPasswdStore::makeKey(dirpath), password);
+ }
if (!serveerror.isEmpty()) {
delete kdirsharethread;
return serveerror;
return s_kdirshareportmax;
}
+QString KDirShareModule::getUser(const QString &dirpath) const
+{
+ foreach (const KDirShareThread *kdirsharethread, m_dirshares) {
+ if (kdirsharethread->directory() == dirpath) {
+ return kdirsharethread->user();
+ }
+ }
+ return QString();
+}
+
+QString KDirShareModule::getPassword(const QString &dirpath) const
+{
+ foreach (const KDirShareThread *kdirsharethread, m_dirshares) {
+ if (kdirsharethread->directory() == dirpath) {
+ return kdirsharethread->password();
+ }
+ }
+ return QString();
+}
+
+void KDirShareModule::slotDelayedRestore()
+{
+ bool requiresauth = false;
+ KConfig kdirshareconfig("kdirsharerc", KConfig::SimpleConfig);
+ foreach (const QString &kdirsharekey, kdirshareconfig.groupList()) {
+ // qDebug() << Q_FUNC_INFO << kdirsharekey;
+ KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
+ const QString kdirsharedirpath = kdirsharegroup.readEntry("dirpath", QString());
+ if (kdirsharedirpath.isEmpty()) {
+ continue;
+ }
+ const QString kdirshareuser = kdirsharegroup.readEntry("user", QString());
+ if (!kdirshareuser.isEmpty()) {
+ requiresauth = true;
+ break;
+ }
+ }
+ if (requiresauth) {
+ if (!m_passwdstore.openStore()) {
+ KNotification *knotification = new KNotification("AuthError");
+ knotification->setComponentData(KComponentData("kdirshare"));
+ knotification->setTitle(i18n("Directory share"));
+ knotification->setText(i18n("Authorization is required but could not open password store"));
+ knotification->sendEvent();
+ return;
+ }
+ }
+
+ bool shareerror = false;
+ foreach (const QString &kdirsharekey, kdirshareconfig.groupList()) {
+ // qDebug() << Q_FUNC_INFO << kdirsharekey;
+ KConfigGroup kdirsharegroup = kdirshareconfig.group(kdirsharekey);
+ const QString kdirsharedirpath = kdirsharegroup.readEntry("dirpath", QString());
+ if (kdirsharedirpath.isEmpty()) {
+ continue;
+ }
+ const uint kdirshareportmin = kdirsharegroup.readEntry("portmin", uint(s_kdirshareportmin));
+ const uint kdirshareportmax = kdirsharegroup.readEntry("portmax", uint(s_kdirshareportmax));
+ const QString kdirshareuser = kdirsharegroup.readEntry("user", QString());
+ const QString kdirsharepassword = m_passwdstore.getPasswd(KPasswdStore::makeKey(kdirsharedirpath));
+ // qDebug() << Q_FUNC_INFO << kdirsharekey << kdirsharedirpath << kdirshareportmin << kdirshareportmax;
+ const QString kdirshareerror = share(
+ kdirsharedirpath,
+ quint16(kdirshareportmin), quint16(kdirshareportmax),
+ kdirshareuser, kdirsharepassword
+ );
+ if (!kdirshareerror.isEmpty()) {
+ kWarning() << kdirshareerror;
+ shareerror = true;
+ }
+ }
+ 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();
+ }
+}
+
#include "kded_kdirshare.moc"