2
Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
3
Copyright (c) 2011 Rodrigo Belem <rclbelem@gmail.com>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
#include <QStringList>
23
#include <QStandardItemModel>
24
#include <QDBusInterface>
30
#include <kpushbutton.h>
31
#include <ksambashare.h>
32
#include <ksambasharedata.h>
33
#include <kmessagebox.h>
34
#include <KDE/KPluginFactory>
35
#include <KDE/KPluginLoader>
37
#include "sambausershareplugin.h"
41
K_PLUGIN_FACTORY(SambaUserSharePluginFactory, registerPlugin<SambaUserSharePlugin>();)
42
K_EXPORT_PLUGIN(SambaUserSharePluginFactory("fileshare_propsdlgplugin"))
44
SambaUserSharePlugin::SambaUserSharePlugin(QObject *parent, const QList<QVariant> &args)
45
: KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
49
url = properties->kurl().path(KUrl::RemoveTrailingSlash);
54
QFileInfo pathInfo(url);
55
if (!pathInfo.permission(QFile::ReadUser | QFile::WriteUser)) {
59
KGlobal::locale()->insertCatalog("kfileshare");
61
KVBox *vbox = new KVBox();
62
properties->addPage(vbox, i18n("&Share"));
63
properties->setFileSharingPage(vbox);
65
if (!QFile::exists("/usr/sbin/smbd")
66
&& !QFile::exists("/usr/local/sbin/smbd")) {
68
QWidget *widget = new QWidget(vbox);
69
QVBoxLayout *vLayout = new QVBoxLayout(widget);
70
vLayout->setAlignment(Qt::AlignJustify);
71
vLayout->setSpacing(KDialog::spacingHint());
72
vLayout->setMargin(0);
74
vLayout->addWidget(new QLabel(i18n("Samba is not installed on your system."), widget));
77
KPushButton *btn = new KPushButton(i18n("Install Samba..."), widget);
78
btn->setDefault(false);
79
vLayout->addWidget(btn);
80
connect(btn, SIGNAL(clicked()), SLOT(installSamba()));
84
vLayout->addStretch();
89
QWidget *widget = new QWidget(vbox);
90
propertiesUi.setupUi(widget);
92
QList<KSambaShareData> shareList = KSambaShare::instance()->getSharesByPath(url);
94
if (!shareList.isEmpty()) {
95
shareData = shareList.at(0); // FIXME: using just the first in the list for a while
102
connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SLOT(toggleShareStatus(bool)));
103
connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
104
connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
105
connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SLOT(checkShareName(QString)));
106
connect(propertiesUi.sambaAllowGuestChk, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
107
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(changed()));
109
for (int i = 0; i < model->rowCount(); ++i) {
110
propertiesUi.tableView->openPersistentEditor(model->index(i, 1, QModelIndex()));
114
SambaUserSharePlugin::~SambaUserSharePlugin()
118
void SambaUserSharePlugin::installSamba()
120
unsigned int xid = 0;
121
QStringList packages;
122
packages << SAMBA_PACKAGE_NAME;
123
QString interaction("show-confirm-install,show-progress");
125
QDBusInterface device("org.freedesktop.PackageKit", "/org/freedesktop/PackageKit",
126
"org.freedesktop.PackageKit.Modify");
127
if (!device.isValid()) {
128
KMessageBox::sorry(qobject_cast<KPropertiesDialog *>(this),
129
i18n("<qt><strong>Samba could not be installed.</strong><br />Please, check if kpackagekit is properly installed</qt>"));
132
QDBusReply<int> reply = device.call("InstallPackageNames", xid, packages, interaction);
135
void SambaUserSharePlugin::setupModel()
137
model = new UserPermissionModel(shareData, this);
140
void SambaUserSharePlugin::setupViews()
142
propertiesUi.tableView->setModel(model);
143
propertiesUi.tableView->setSelectionMode(QAbstractItemView::NoSelection);
144
propertiesUi.tableView->setItemDelegate(new UserPermissionDelegate(this));
147
void SambaUserSharePlugin::load()
149
bool guestAllowed = false;
150
bool sambaShared = KSambaShare::instance()->isDirectoryShared(url);
152
propertiesUi.sambaChk->setChecked(sambaShared);
153
toggleShareStatus(sambaShared);
155
guestAllowed = (bool) shareData.guestPermission();
157
propertiesUi.sambaAllowGuestChk->setChecked(guestAllowed);
159
propertiesUi.sambaNameEdit->setText(shareData.name());
162
void SambaUserSharePlugin::applyChanges()
164
KSambaShareData::UserShareError result;
166
if (propertiesUi.sambaChk->isChecked()) {
167
if (shareData.setAcl(model->getAcl()) != KSambaShareData::UserShareAclOk) {
171
shareData.setName(propertiesUi.sambaNameEdit->text());
173
shareData.setPath(url);
175
KSambaShareData::GuestPermission guestOk(shareData.guestPermission());
177
guestOk = (propertiesUi.sambaAllowGuestChk->isChecked() == false)
178
? KSambaShareData::GuestsNotAllowed : KSambaShareData::GuestsAllowed;
180
shareData.setGuestPermission(guestOk);
182
result = shareData.save();
183
} else if (KSambaShare::instance()->isDirectoryShared(url)) {
184
result = shareData.remove();
188
void SambaUserSharePlugin::toggleShareStatus(bool checked)
190
propertiesUi.sambaNameEdit->setEnabled(checked);
191
propertiesUi.sambaAllowGuestChk->setCheckable(checked);
192
propertiesUi.tableView->setEnabled(checked);
193
if (checked && propertiesUi.sambaNameEdit->text().isEmpty()) {
194
propertiesUi.sambaNameEdit->setText(getNewShareName());
198
void SambaUserSharePlugin::checkShareName(const QString &name)
200
bool disableButton = false;
202
if (name.isEmpty()) {
203
disableButton = true;
204
} else if (!KSambaShare::instance()->isShareNameAvailable(name)) {
205
// There is another Share with the same name
206
KMessageBox::sorry(qobject_cast<KPropertiesDialog *>(this),
207
i18n("<qt>There is already a share with the name <strong>%1</strong>.<br /> Please choose another name.</qt>",
208
propertiesUi.sambaNameEdit->text()));
209
propertiesUi.sambaNameEdit->selectAll();
210
disableButton = true;
214
properties->enableButtonOk(false);
215
propertiesUi.sambaNameEdit->setFocus();
219
if (!properties->isButtonEnabled(KPropertiesDialog::Ok)) {
220
properties->enableButtonOk(true);
224
QString SambaUserSharePlugin::getNewShareName()
226
QString shareName = KUrl(url).fileName();
228
if (!propertiesUi.sambaNameEdit->text().isEmpty()) {
229
shareName = propertiesUi.sambaNameEdit->text();
232
// Windows could have problems with longer names
233
shareName = shareName.left(12);
238
#include "moc_sambausershareplugin.cpp"