sambausershareplugin.cpp 9.67 KB
Newer Older
1
2
/*
  Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
3
  Copyright (c) 2011 Rodrigo Belem <rclbelem@gmail.com>
4
  Copyright (c) 2015 Harald Sitter <sitter@kde.org>
5
6
7
8
9
10
11
12
13
14
15
16
17

  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
Dirk Mueller's avatar
Dirk Mueller committed
18
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20
21

*/

Harald Sitter's avatar
Harald Sitter committed
22
23
24
#include <QDialogButtonBox>
#include <QFileInfo>
#include <QFrame>
25
#include <QPushButton>
26
#include <QStandardPaths>
Harald Sitter's avatar
Harald Sitter committed
27
#include <QStringList>
28
#include <QDebug>
Harald Sitter's avatar
Harald Sitter committed
29
30

#include <KMessageBox>
Ranveer Aggarwal's avatar
Ranveer Aggarwal committed
31
32
#include <KPluginFactory>
#include <KPluginLoader>
Harald Sitter's avatar
Harald Sitter committed
33
34
#include <KSambaShare>
#include <KSambaShareData>
35

36
#include "sambausershareplugin.h"
37
38
#include "model.h"
#include "delegate.h"
39

40
41
K_PLUGIN_FACTORY(SambaUserSharePluginFactory, registerPlugin<SambaUserSharePlugin>();)
K_EXPORT_PLUGIN(SambaUserSharePluginFactory("fileshare_propsdlgplugin"))
42

Pino Toscano's avatar
Pino Toscano committed
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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;
}

56
57
SambaUserSharePlugin::SambaUserSharePlugin(QObject *parent, const QList<QVariant> &args)
    : KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
Kai Uwe Broulik's avatar
Kai Uwe Broulik committed
58
    , m_url(properties->item().mostLocalUrl().toLocalFile())
59
    , shareData()
60
{
61
62
    Q_UNUSED(args);

63
    if (m_url.isEmpty()) {
64
65
        return;
    }
Rodrigo Belem's avatar
Rodrigo Belem committed
66

67
    QFileInfo pathInfo(m_url);
68
    if (!pathInfo.permission(QFile::ReadUser | QFile::WriteUser)) {
Rodrigo Belem's avatar
Rodrigo Belem committed
69
70
71
        return;
    }

72
    QFrame *vbox = new QFrame();
Rodrigo Belem's avatar
Rodrigo Belem committed
73
74
    properties->addPage(vbox, i18n("&Share"));
    properties->setFileSharingPage(vbox);
75
    QVBoxLayout *vLayoutMaster = new QVBoxLayout(vbox);
Rodrigo Belem's avatar
Rodrigo Belem committed
76

77
78
79
80
81
82
83
84
85
    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();

86
87
88
89
90
    m_installSambaWidgets = new QWidget(vbox);
    vLayoutMaster->addWidget(m_installSambaWidgets);
    QVBoxLayout *vLayout = new QVBoxLayout(m_installSambaWidgets);
    vLayout->setAlignment(Qt::AlignJustify);
    vLayout->setMargin(0);
Rodrigo Belem's avatar
Rodrigo Belem committed
91

92
    vLayout->addWidget(new QLabel(i18n("Samba is not installed on your system."), m_installSambaWidgets));
Rodrigo Belem's avatar
Rodrigo Belem committed
93

94
#ifdef SAMBA_INSTALL
95
96
97
98
99
100
101
    m_installSambaButton = new QPushButton(i18n("Install Samba..."), m_installSambaWidgets);
    m_installSambaButton->setDefault(false);
    vLayout->addWidget(m_installSambaButton);
    connect(m_installSambaButton, SIGNAL(clicked()), SLOT(installSamba()));
    m_installProgress = new QProgressBar();
    vLayout->addWidget(m_installProgress);
    m_installProgress->hide();
102
#endif
103

104
105
106
107
108
    // align items on top
    vLayout->addStretch();
    m_shareWidgets = new QWidget(vbox);
    vLayoutMaster->addWidget(m_shareWidgets);
    propertiesUi.setupUi(m_shareWidgets);
109

110
    QList<KSambaShareData> shareList = KSambaShare::instance()->getSharesByPath(m_url);
111
112
113
114
115
116
117
118
119
120
121
122

    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()));
Laurent Montel's avatar
Laurent Montel committed
123
    connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SLOT(checkShareName(QString)));
124
    connect(propertiesUi.sambaAllowGuestChk, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
Laurent Montel's avatar
Laurent Montel committed
125
    connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SIGNAL(changed()));
126
127
128
129

    for (int i = 0; i < model->rowCount(); ++i) {
        propertiesUi.tableView->openPersistentEditor(model->index(i, 1, QModelIndex()));
    }
Pino Toscano's avatar
Pino Toscano committed
130
    if (!isSambaInstalled()) {
131
132
133
134
135
136
        m_installSambaWidgets->show();
        m_shareWidgets->hide();
    } else {
        m_installSambaWidgets->hide();
        m_shareWidgets->show();
    }
137
138
139
140
141
}

SambaUserSharePlugin::~SambaUserSharePlugin()
{
}
Rodrigo Belem's avatar
Rodrigo Belem committed
142

143
#ifdef SAMBA_INSTALL
144
145
void SambaUserSharePlugin::installSamba()
{
146
    QString package = QStringLiteral(SAMBA_PACKAGE_NAME);
147
    PackageKit::Transaction *transaction = PackageKit::Daemon::resolve(package,
148
                                                   PackageKit::Transaction::FilterArch);
Harald Sitter's avatar
Harald Sitter committed
149
150
151
    connect(transaction,
            SIGNAL(package(PackageKit::Transaction::Info,QString,QString)),
            SLOT(packageInstall(PackageKit::Transaction::Info,QString,QString)));
152
153
154
155
    m_installProgress->setMaximum(0);
    m_installProgress->setMinimum(0);
    m_installProgress->show();
    m_installSambaButton->hide();
156
157
}

Harald Sitter's avatar
Harald Sitter committed
158
159
160
161
162
163
void SambaUserSharePlugin::packageInstall(PackageKit::Transaction::Info info,
                                          const QString &packageId,
                                          const QString &summary)
{
    Q_UNUSED(info);
    Q_UNUSED(summary);
164
165
166
167
168
169
170
171
    PackageKit::Transaction *installTransaction = PackageKit::Daemon::installPackage(packageId);
    connect(installTransaction,
            SIGNAL(finished(PackageKit::Transaction::Exit, uint)),
            SLOT(packageFinished(PackageKit::Transaction::Exit, uint)));
}

void SambaUserSharePlugin::packageFinished(PackageKit::Transaction::Exit status, uint runtime)
{
Jonathan Esk-Riddell's avatar
Jonathan Esk-Riddell committed
172
    Q_UNUSED(runtime);
173
174
    if (status == PackageKit::Transaction::ExitSuccess) {
        m_installSambaWidgets->hide();
175
        m_failedSambaWidgets->hide();
176
177
        m_shareWidgets->show();
    } else {
178
        m_shareWidgets->hide();
179
180
181
        m_installSambaWidgets->hide();
        m_failedSambaWidgets->show();
    }
Rodrigo Belem's avatar
Rodrigo Belem committed
182
}
183
#endif // SAMBA_INSTALL
184

185
void SambaUserSharePlugin::setupModel()
186
{
187
    model = new UserPermissionModel(shareData, this);
188
189
}

190
void SambaUserSharePlugin::setupViews()
191
{
192
193
194
    propertiesUi.tableView->setModel(model);
    propertiesUi.tableView->setSelectionMode(QAbstractItemView::NoSelection);
    propertiesUi.tableView->setItemDelegate(new UserPermissionDelegate(this));
195
196
}

197
void SambaUserSharePlugin::load()
198
{
199
    bool guestAllowed = false;
200
    bool sambaShared = KSambaShare::instance()->isDirectoryShared(m_url);
201
202
203
204
205

    propertiesUi.sambaChk->setChecked(sambaShared);
    toggleShareStatus(sambaShared);
    if (sambaShared) {
        guestAllowed = (bool) shareData.guestPermission();
Rodrigo Belem's avatar
Rodrigo Belem committed
206
    }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    propertiesUi.sambaAllowGuestChk->setChecked(guestAllowed);

    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());

223
        shareData.setPath(m_url);
224
225
226
227
228
229
230
231
232

        KSambaShareData::GuestPermission guestOk(shareData.guestPermission());

        guestOk = (propertiesUi.sambaAllowGuestChk->isChecked() == false)
                  ? KSambaShareData::GuestsNotAllowed : KSambaShareData::GuestsAllowed;

        shareData.setGuestPermission(guestOk);

        result = shareData.save();
233
    } else if (KSambaShare::instance()->isDirectoryShared(m_url)) {
234
235
236
237
238
239
        result = shareData.remove();
    }
}

void SambaUserSharePlugin::toggleShareStatus(bool checked)
{
Nate Graham's avatar
Nate Graham committed
240
    propertiesUi.textLabel1->setEnabled(checked);
241
242
    propertiesUi.sambaNameEdit->setEnabled(checked);
    propertiesUi.sambaAllowGuestChk->setEnabled(checked);
243
244
245
    propertiesUi.tableView->setEnabled(checked);
    if (checked && propertiesUi.sambaNameEdit->text().isEmpty()) {
        propertiesUi.sambaNameEdit->setText(getNewShareName());
246
247
    } else {
        propertiesUi.sambaNameEdit->setText(QString());
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    }
}

void SambaUserSharePlugin::checkShareName(const QString &name)
{
    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<KPropertiesDialog *>(this),
                i18n("<qt>There is already a share with the name <strong>%1</strong>.<br /> Please choose another name.</qt>",
                    propertiesUi.sambaNameEdit->text()));
        propertiesUi.sambaNameEdit->selectAll();
        disableButton = true;
    }

    if (disableButton) {
267
        properties->button(QDialogButtonBox::Ok)->setEnabled(false);
268
269
270
271
        propertiesUi.sambaNameEdit->setFocus();
        return;
    }

272
273
    if (!properties->button(QDialogButtonBox::Ok)->isEnabled()) {
        properties->button(QDialogButtonBox::Ok)->setEnabled(true);
274
275
276
277
278
    }
}

QString SambaUserSharePlugin::getNewShareName()
{
Harald Sitter's avatar
Harald Sitter committed
279
    QString shareName = QUrl(m_url).fileName();
280
281
282
283
284
285
286
287
288

    if (!propertiesUi.sambaNameEdit->text().isEmpty()) {
        shareName = propertiesUi.sambaNameEdit->text();
    }

    // Windows could have problems with longer names
    shareName = shareName.left(12);

    return shareName;
289
290
}

Ranveer Aggarwal's avatar
Ranveer Aggarwal committed
291
#include "sambausershareplugin.moc"