~ubuntu-branches/ubuntu/trusty/kdenetwork-filesharing/trusty

« back to all changes in this revision

Viewing changes to .pc/kubuntu_05_samba_install.diff/samba/filepropertiesplugin/sambausershareplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:14:35 UTC
  • Revision ID: package-import@ubuntu.com-20130621021435-0qd400iswc8e3i82
Tags: 4:4.10.80-0ubuntu1
* New upstream release
* Initial package split from kdenetwork
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
 
3
  Copyright (c) 2011 Rodrigo Belem <rclbelem@gmail.com>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
*/
 
20
 
 
21
#include <QFileInfo>
 
22
#include <QStringList>
 
23
#include <QStandardItemModel>
 
24
#include <QDBusInterface>
 
25
#include <QDBusReply>
 
26
 
 
27
#include <kvbox.h>
 
28
#include <kuser.h>
 
29
#include <kdebug.h>
 
30
#include <kpushbutton.h>
 
31
#include <ksambashare.h>
 
32
#include <ksambasharedata.h>
 
33
#include <kmessagebox.h>
 
34
#include <KDE/KPluginFactory>
 
35
#include <KDE/KPluginLoader>
 
36
 
 
37
#include "sambausershareplugin.h"
 
38
#include "model.h"
 
39
#include "delegate.h"
 
40
 
 
41
K_PLUGIN_FACTORY(SambaUserSharePluginFactory, registerPlugin<SambaUserSharePlugin>();)
 
42
K_EXPORT_PLUGIN(SambaUserSharePluginFactory("fileshare_propsdlgplugin"))
 
43
 
 
44
SambaUserSharePlugin::SambaUserSharePlugin(QObject *parent, const QList<QVariant> &args)
 
45
    : KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
 
46
    , url()
 
47
    , shareData()
 
48
{
 
49
    url = properties->kurl().path(KUrl::RemoveTrailingSlash);
 
50
    if (url.isEmpty()) {
 
51
        return;
 
52
    }
 
53
 
 
54
    QFileInfo pathInfo(url);
 
55
    if (!pathInfo.permission(QFile::ReadUser | QFile::WriteUser)) {
 
56
        return;
 
57
    }
 
58
 
 
59
    KGlobal::locale()->insertCatalog("kfileshare");
 
60
 
 
61
    KVBox *vbox = new KVBox();
 
62
    properties->addPage(vbox, i18n("&Share"));
 
63
    properties->setFileSharingPage(vbox);
 
64
 
 
65
    if (!QFile::exists("/usr/sbin/smbd")
 
66
        && !QFile::exists("/usr/local/sbin/smbd")) {
 
67
 
 
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);
 
73
 
 
74
        vLayout->addWidget(new QLabel(i18n("Samba is not installed on your system."), widget));
 
75
 
 
76
#ifdef SAMBA_INSTALL
 
77
        KPushButton *btn = new KPushButton(i18n("Install Samba..."), widget);
 
78
        btn->setDefault(false);
 
79
        vLayout->addWidget(btn);
 
80
        connect(btn, SIGNAL(clicked()), SLOT(installSamba()));
 
81
#endif
 
82
 
 
83
        // align items on top
 
84
        vLayout->addStretch();
 
85
 
 
86
        return;
 
87
    }
 
88
 
 
89
    QWidget *widget = new QWidget(vbox);
 
90
    propertiesUi.setupUi(widget);
 
91
 
 
92
    QList<KSambaShareData> shareList = KSambaShare::instance()->getSharesByPath(url);
 
93
 
 
94
    if (!shareList.isEmpty()) {
 
95
        shareData = shareList.at(0); // FIXME: using just the first in the list for a while
 
96
    }
 
97
 
 
98
    setupModel();
 
99
    setupViews();
 
100
    load();
 
101
 
 
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()));
 
108
 
 
109
    for (int i = 0; i < model->rowCount(); ++i) {
 
110
        propertiesUi.tableView->openPersistentEditor(model->index(i, 1, QModelIndex()));
 
111
    }
 
112
}
 
113
 
 
114
SambaUserSharePlugin::~SambaUserSharePlugin()
 
115
{
 
116
}
 
117
 
 
118
void SambaUserSharePlugin::installSamba()
 
119
{
 
120
    unsigned int xid = 0;
 
121
    QStringList packages;
 
122
    packages << SAMBA_PACKAGE_NAME;
 
123
    QString interaction("show-confirm-install,show-progress");
 
124
 
 
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>"));
 
130
        return;
 
131
    }
 
132
    QDBusReply<int> reply = device.call("InstallPackageNames", xid, packages, interaction);
 
133
}
 
134
 
 
135
void SambaUserSharePlugin::setupModel()
 
136
{
 
137
    model = new UserPermissionModel(shareData, this);
 
138
}
 
139
 
 
140
void SambaUserSharePlugin::setupViews()
 
141
{
 
142
    propertiesUi.tableView->setModel(model);
 
143
    propertiesUi.tableView->setSelectionMode(QAbstractItemView::NoSelection);
 
144
    propertiesUi.tableView->setItemDelegate(new UserPermissionDelegate(this));
 
145
}
 
146
 
 
147
void SambaUserSharePlugin::load()
 
148
{
 
149
    bool guestAllowed = false;
 
150
    bool sambaShared = KSambaShare::instance()->isDirectoryShared(url);
 
151
 
 
152
    propertiesUi.sambaChk->setChecked(sambaShared);
 
153
    toggleShareStatus(sambaShared);
 
154
    if (sambaShared) {
 
155
        guestAllowed = (bool) shareData.guestPermission();
 
156
    }
 
157
    propertiesUi.sambaAllowGuestChk->setChecked(guestAllowed);
 
158
 
 
159
    propertiesUi.sambaNameEdit->setText(shareData.name());
 
160
}
 
161
 
 
162
void SambaUserSharePlugin::applyChanges()
 
163
{
 
164
    KSambaShareData::UserShareError result;
 
165
 
 
166
    if (propertiesUi.sambaChk->isChecked()) {
 
167
        if (shareData.setAcl(model->getAcl()) != KSambaShareData::UserShareAclOk) {
 
168
            return;
 
169
        }
 
170
 
 
171
        shareData.setName(propertiesUi.sambaNameEdit->text());
 
172
 
 
173
        shareData.setPath(url);
 
174
 
 
175
        KSambaShareData::GuestPermission guestOk(shareData.guestPermission());
 
176
 
 
177
        guestOk = (propertiesUi.sambaAllowGuestChk->isChecked() == false)
 
178
                  ? KSambaShareData::GuestsNotAllowed : KSambaShareData::GuestsAllowed;
 
179
 
 
180
        shareData.setGuestPermission(guestOk);
 
181
 
 
182
        result = shareData.save();
 
183
    } else if (KSambaShare::instance()->isDirectoryShared(url)) {
 
184
        result = shareData.remove();
 
185
    }
 
186
}
 
187
 
 
188
void SambaUserSharePlugin::toggleShareStatus(bool checked)
 
189
{
 
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());
 
195
    }
 
196
}
 
197
 
 
198
void SambaUserSharePlugin::checkShareName(const QString &name)
 
199
{
 
200
    bool disableButton = false;
 
201
 
 
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;
 
211
    }
 
212
 
 
213
    if (disableButton) {
 
214
        properties->enableButtonOk(false);
 
215
        propertiesUi.sambaNameEdit->setFocus();
 
216
        return;
 
217
    }
 
218
 
 
219
    if (!properties->isButtonEnabled(KPropertiesDialog::Ok)) {
 
220
        properties->enableButtonOk(true);
 
221
    }
 
222
}
 
223
 
 
224
QString SambaUserSharePlugin::getNewShareName()
 
225
{
 
226
    QString shareName = KUrl(url).fileName();
 
227
 
 
228
    if (!propertiesUi.sambaNameEdit->text().isEmpty()) {
 
229
        shareName = propertiesUi.sambaNameEdit->text();
 
230
    }
 
231
 
 
232
    // Windows could have problems with longer names
 
233
    shareName = shareName.left(12);
 
234
 
 
235
    return shareName;
 
236
}
 
237
 
 
238
#include "moc_sambausershareplugin.cpp"