diff --git a/samba/filepropertiesplugin/delegate.cpp b/samba/filepropertiesplugin/delegate.cpp index 0d826fe..e385360 100644 --- a/samba/filepropertiesplugin/delegate.cpp +++ b/samba/filepropertiesplugin/delegate.cpp @@ -1,81 +1,74 @@ /* * Copyright (c) 2011 Rodrigo Belem * Copyright (c) 2019 Nate Graham * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include "delegate.h" UserPermissionDelegate::UserPermissionDelegate(QObject *parent) : QItemDelegate(parent) { } QWidget *UserPermissionDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex &index) const { if (index.column() != 1) { return 0; } QComboBox *comboBox = new QComboBox(parent); comboBox->addItem(i18n("---")); comboBox->addItem(i18n("Full Control"), QLatin1String("F")); comboBox->addItem(i18n("Read Only"), QLatin1String("R")); comboBox->addItem(i18n("Deny"), QLatin1String("D")); - connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData())); - return comboBox; } void UserPermissionDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QComboBox *comboBox = qobject_cast(editor); if (!comboBox || (index.column() != 1)) { return; } int pos = comboBox->findData(index.model()->data(index, Qt::EditRole)); if (pos == -1) { pos = 0; } comboBox->setCurrentIndex(pos); } void UserPermissionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = qobject_cast(editor); if (!comboBox || (index.column() != 1)) { return; } model->setData(index, comboBox->itemData(comboBox->currentIndex())); } - -void UserPermissionDelegate::emitCommitData() -{ - emit commitData(qobject_cast(sender())); -} diff --git a/samba/filepropertiesplugin/delegate.h b/samba/filepropertiesplugin/delegate.h index 33f08d7..de064ab 100644 --- a/samba/filepropertiesplugin/delegate.h +++ b/samba/filepropertiesplugin/delegate.h @@ -1,44 +1,42 @@ /* * Copyright (c) 2011 Rodrigo Belem * Copyright (c) 2019 Nate Graham * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef delegate_h #define delegate_h #include class UserPermissionDelegate : public QItemDelegate { Q_OBJECT public: UserPermissionDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; -private Q_SLOTS: - void emitCommitData(); }; #endif diff --git a/samba/filepropertiesplugin/sambausershareplugin.cpp b/samba/filepropertiesplugin/sambausershareplugin.cpp index 742f8cd..156839a 100644 --- a/samba/filepropertiesplugin/sambausershareplugin.cpp +++ b/samba/filepropertiesplugin/sambausershareplugin.cpp @@ -1,308 +1,312 @@ /* Copyright (c) 2004 Jan Schaefer Copyright (c) 2011 Rodrigo Belem Copyright (c) 2015 Harald Sitter Copyright (c) 2019 Nate Graham This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "sambausershareplugin.h" #include "model.h" #include "delegate.h" K_PLUGIN_FACTORY(SambaUserSharePluginFactory, registerPlugin();) K_EXPORT_PLUGIN(SambaUserSharePluginFactory("fileshare_propsdlgplugin")) // copied from kio/src/core/ksambashare.cpp, KSambaSharePrivate::isSambaInstalled() static bool isSambaInstalled() { if (QFile::exists(QStringLiteral("/usr/sbin/smbd")) || QFile::exists(QStringLiteral("/usr/local/sbin/smbd"))) { return true; } //qDebug() << "Samba is not installed!"; return false; } SambaUserSharePlugin::SambaUserSharePlugin(QObject *parent, const QList &args) : KPropertiesDialogPlugin(qobject_cast(parent)) , m_url(properties->item().mostLocalUrl().toLocalFile()) , shareData() { Q_UNUSED(args); if (m_url.isEmpty()) { return; } QFileInfo pathInfo(m_url); if (!pathInfo.permission(QFile::ReadUser | QFile::WriteUser)) { return; } QFrame *vbox = new QFrame(); properties->addPage(vbox, i18n("&Share")); properties->setFileSharingPage(vbox); QVBoxLayout *vLayoutMaster = new QVBoxLayout(vbox); m_failedSambaWidgets = new QWidget(vbox); vLayoutMaster->addWidget(m_failedSambaWidgets); QVBoxLayout *vFailedLayout = new QVBoxLayout(m_failedSambaWidgets); vFailedLayout->setAlignment(Qt::AlignJustify); vFailedLayout->setMargin(0); vFailedLayout->addWidget(new QLabel(i18n("The Samba package failed to install."), m_failedSambaWidgets)); vFailedLayout->addStretch(); m_failedSambaWidgets->hide(); m_installSambaWidgets = new QWidget(vbox); vLayoutMaster->addWidget(m_installSambaWidgets); QVBoxLayout *vLayout = new QVBoxLayout(m_installSambaWidgets); vLayout->setAlignment(Qt::AlignJustify); vLayout->setMargin(0); m_sambaStatusMessage = new QLabel(i18n("Samba must be installed before folders can be shared.")); m_sambaStatusMessage->setAlignment(Qt::AlignCenter); vLayout->addWidget(m_sambaStatusMessage); #ifdef SAMBA_INSTALL m_installSambaButton = new QPushButton(i18n("Install Samba"), m_installSambaWidgets); m_installSambaButton->setDefault(false); vLayout->addWidget(m_installSambaButton); - connect(m_installSambaButton, SIGNAL(clicked()), SLOT(installSamba())); + connect(m_installSambaButton, &QPushButton::clicked, + this, &SambaUserSharePlugin::installSamba); m_installProgress = new QProgressBar(); vLayout->addWidget(m_installProgress); m_installProgress->hide(); #endif // align items on top vLayout->addStretch(); m_shareWidgets = new QWidget(vbox); vLayoutMaster->addWidget(m_shareWidgets); propertiesUi.setupUi(m_shareWidgets); QList shareList = KSambaShare::instance()->getSharesByPath(m_url); if (!shareList.isEmpty()) { shareData = shareList.at(0); // FIXME: using just the first in the list for a while } setupModel(); setupViews(); load(); - connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SLOT(toggleShareStatus(bool))); - connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SIGNAL(changed())); - connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SIGNAL(changed())); - connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SLOT(checkShareName(QString))); + connect(propertiesUi.sambaChk, &QCheckBox::toggled, + this, &SambaUserSharePlugin::toggleShareStatus); + connect(propertiesUi.sambaNameEdit, &QLineEdit::textChanged, + this, &SambaUserSharePlugin::checkShareName); connect(propertiesUi.sambaAllowGuestChk, &QCheckBox::toggled, this, [=] (bool checked) { propertiesUi.tableView->setEnabled(checked && propertiesUi.sambaChk->isChecked()); setDirty(); }); - connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(changed())); + connect(model, &UserPermissionModel::dataChanged, + this, [=] { setDirty(); }); for (int i = 0; i < model->rowCount(); ++i) { propertiesUi.tableView->openPersistentEditor(model->index(i, 1, QModelIndex())); } if (!isSambaInstalled()) { m_installSambaWidgets->show(); m_shareWidgets->hide(); } else { m_installSambaWidgets->hide(); m_shareWidgets->show(); } } SambaUserSharePlugin::~SambaUserSharePlugin() { } #ifdef SAMBA_INSTALL void SambaUserSharePlugin::installSamba() { QString package = QStringLiteral(SAMBA_PACKAGE_NAME); QStringList distroSambaPackages = package.split(QLatin1Char(',')); PackageKit::Transaction *transaction = PackageKit::Daemon::resolve(distroSambaPackages, PackageKit::Transaction::FilterArch); QSharedPointer pkgids(new QStringList); connect(transaction, &PackageKit::Transaction::package, this, [pkgids] (PackageKit::Transaction::Info /*info*/, const QString& packageId, const QString& /*summary*/) { pkgids->append(packageId); }); connect(transaction, &PackageKit::Transaction::finished, this, [this, pkgids] (PackageKit::Transaction::Exit exit) { if (exit != PackageKit::Transaction::ExitSuccess) { return; } auto installTransaction = PackageKit::Daemon::installPackages(*pkgids); connect(installTransaction, &PackageKit::Transaction::finished, this, &SambaUserSharePlugin::packageFinished); } ); m_sambaStatusMessage->setText(i18n("Installing Samba...")); m_installProgress->setMaximum(0); m_installProgress->setMinimum(0); m_installProgress->show(); m_installSambaButton->hide(); } void SambaUserSharePlugin::packageFinished(PackageKit::Transaction::Exit status, uint runtime) { Q_UNUSED(runtime); if (status == PackageKit::Transaction::ExitSuccess) { m_installSambaWidgets->hide(); m_failedSambaWidgets->hide(); m_shareWidgets->show(); } else { m_shareWidgets->hide(); m_installSambaWidgets->hide(); m_failedSambaWidgets->show(); } } #endif // SAMBA_INSTALL void SambaUserSharePlugin::setupModel() { model = new UserPermissionModel(shareData, this); } void SambaUserSharePlugin::setupViews() { propertiesUi.tableView->setModel(model); propertiesUi.tableView->setSelectionMode(QAbstractItemView::NoSelection); propertiesUi.tableView->setItemDelegate(new UserPermissionDelegate(this)); propertiesUi.tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); } void SambaUserSharePlugin::load() { bool guestAllowed = false; bool sambaShared = KSambaShare::instance()->isDirectoryShared(m_url); propertiesUi.sambaChk->setChecked(sambaShared); toggleShareStatus(sambaShared); if (sambaShared) { guestAllowed = (bool) shareData.guestPermission(); } propertiesUi.sambaAllowGuestChk->setChecked(guestAllowed); propertiesUi.tableView->setEnabled(propertiesUi.sambaChk->isChecked() && propertiesUi.sambaAllowGuestChk->isChecked()); propertiesUi.sambaNameEdit->setText(shareData.name()); } void SambaUserSharePlugin::applyChanges() { KSambaShareData::UserShareError result; if (propertiesUi.sambaChk->isChecked()) { if (shareData.setAcl(model->getAcl()) != KSambaShareData::UserShareAclOk) { return; } shareData.setName(propertiesUi.sambaNameEdit->text()); shareData.setPath(m_url); KSambaShareData::GuestPermission guestOk(shareData.guestPermission()); guestOk = (propertiesUi.sambaAllowGuestChk->isChecked() == false) ? KSambaShareData::GuestsNotAllowed : KSambaShareData::GuestsAllowed; shareData.setGuestPermission(guestOk); result = shareData.save(); } else if (KSambaShare::instance()->isDirectoryShared(m_url)) { result = shareData.remove(); } } void SambaUserSharePlugin::toggleShareStatus(bool checked) { propertiesUi.textLabel1->setEnabled(checked); propertiesUi.sambaNameEdit->setEnabled(checked); propertiesUi.sambaAllowGuestChk->setEnabled(checked); propertiesUi.tableView->setEnabled(checked && propertiesUi.sambaAllowGuestChk->isChecked()); if (checked && propertiesUi.sambaNameEdit->text().isEmpty()) { propertiesUi.sambaNameEdit->setText(getNewShareName()); } else { propertiesUi.sambaNameEdit->setText(QString()); } + setDirty(); } void SambaUserSharePlugin::checkShareName(const QString &name) { // Don't ever disable the OK button when the user is trying to remove a share if (!propertiesUi.sambaChk->isChecked()) { return; } bool disableButton = false; if (name.isEmpty()) { disableButton = true; } else if (!KSambaShare::instance()->isShareNameAvailable(name)) { // There is another Share with the same name KMessageBox::sorry(qobject_cast(this), i18n("There is already a share with the name %1.
Please choose another name.
", propertiesUi.sambaNameEdit->text())); propertiesUi.sambaNameEdit->selectAll(); disableButton = true; } if (disableButton) { properties->button(QDialogButtonBox::Ok)->setEnabled(false); propertiesUi.sambaNameEdit->setFocus(); return; } if (!properties->button(QDialogButtonBox::Ok)->isEnabled()) { properties->button(QDialogButtonBox::Ok)->setEnabled(true); + setDirty(); } } QString SambaUserSharePlugin::getNewShareName() { QString shareName = QUrl(m_url).fileName(); if (!propertiesUi.sambaNameEdit->text().isEmpty()) { shareName = propertiesUi.sambaNameEdit->text(); } // Windows could have problems with longer names shareName = shareName.left(12); return shareName; } #include "sambausershareplugin.moc" diff --git a/samba/filepropertiesplugin/sambausershareplugin.h b/samba/filepropertiesplugin/sambausershareplugin.h index e8bf5e8..b5b6fea 100644 --- a/samba/filepropertiesplugin/sambausershareplugin.h +++ b/samba/filepropertiesplugin/sambausershareplugin.h @@ -1,79 +1,78 @@ /* Copyright (c) 2004 Jan Schaefer Copyright (c) 2011 Rodrigo Belem Copyright (c) 2019 Nate Graham This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef SAMBAUSERSHAREPLUGIN_H #define SAMBAUSERSHAREPLUGIN_H #include #include #include #include #include #ifdef SAMBA_INSTALL #include #include #endif // SAMBA_INSTALL #include #include #include "ui_sambausershareplugin.h" class UserPermissionModel; class SambaUserSharePlugin : public KPropertiesDialogPlugin { Q_OBJECT public: SambaUserSharePlugin(QObject *parent, const QList &args); virtual ~SambaUserSharePlugin(); virtual void applyChanges(); -private Q_SLOTS: - void load(); - void toggleShareStatus(bool checked); - void checkShareName(const QString &name); -#ifdef SAMBA_INSTALL - void installSamba(); - void packageFinished(PackageKit::Transaction::Exit status, uint runtime); -#endif // SAMBA_INSTALL - private: QString m_url; KSambaShareData shareData; UserPermissionModel *model; Ui::PropertiesPageGUI propertiesUi; QWidget *m_failedSambaWidgets; QWidget *m_installSambaWidgets; QWidget *m_shareWidgets; QLabel *m_sambaStatusMessage; QProgressBar *m_installProgress; QPushButton *m_installSambaButton; void setupModel(); void setupViews(); QStringList getUsersList(); QString getNewShareName(); + void load(); + void toggleShareStatus(bool checked); + void checkShareName(const QString &name); +#ifdef SAMBA_INSTALL + void installSamba(); + void packageFinished(PackageKit::Transaction::Exit status, uint runtime); +#endif // SAMBA_INSTALL + }; #endif // SAMBAUSERSHAREPLUGIN_H