~ubuntu-branches/ubuntu/natty/kdenetwork/natty-proposed

« back to all changes in this revision

Viewing changes to filesharing/advanced/propsdlgplugin/propsdlgshareplugin.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-02-21 14:26:58 UTC
  • Revision ID: package-import@ubuntu.com-20110221142658-mzt9flk82tzdunxj
Tags: 4:4.6.0-0ubuntu4
Update kubuntu_05_samba_sharing.diff to match master

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
 
 
25
 
#include <kvbox.h>
26
 
#include <kuser.h>
27
 
#include <kdebug.h>
28
 
#include <ksambashare.h>
29
 
#include <ksambasharedata.h>
30
 
#include <kmessagebox.h>
31
 
#include <KDE/KPluginFactory>
32
 
#include <KDE/KPluginLoader>
33
 
 
34
 
#include "propsdlgshareplugin.h"
35
 
#include "model.h"
36
 
#include "delegate.h"
37
 
 
38
 
K_PLUGIN_FACTORY(SambaUserSharePluginFactory, registerPlugin<SambaUserSharePlugin>();)
39
 
K_EXPORT_PLUGIN(SambaUserSharePluginFactory("fileshare_propsdlgplugin"))
40
 
 
41
 
SambaUserSharePlugin::SambaUserSharePlugin(QObject *parent, const QList<QVariant> &args)
42
 
    : KPropertiesDialogPlugin(qobject_cast<KPropertiesDialog *>(parent))
43
 
    , url()
44
 
    , shareData()
45
 
{
46
 
    url = properties->kurl().path(KUrl::RemoveTrailingSlash);
47
 
    if (url.isEmpty()) {
48
 
        return;
49
 
    }
50
 
 
51
 
    QFileInfo pathInfo(url);
52
 
    if (!pathInfo.permission(QFile::ReadUser | QFile::WriteUser)) {
53
 
        return;
54
 
    }
55
 
 
56
 
    KGlobal::locale()->insertCatalog("kfileshare");
57
 
 
58
 
    KVBox *vbox = new KVBox();
59
 
    properties->addPage(vbox, i18n("&Share"));
60
 
    properties->setFileSharingPage(vbox);
61
 
 
62
 
    QWidget *widget = new QWidget(vbox);
63
 
    propertiesUi.setupUi(widget);
64
 
 
65
 
    QList<KSambaShareData> shareList = KSambaShare::instance()->getSharesByPath(url);
66
 
 
67
 
    if (!shareList.isEmpty()) {
68
 
        shareData = shareList.at(0); // FIXME: using just the first in the list for a while
69
 
    }
70
 
 
71
 
    setupModel();
72
 
    setupViews();
73
 
    load();
74
 
 
75
 
    connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SLOT(toggleShareStatus(bool)));
76
 
    connect(propertiesUi.sambaChk, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
77
 
    connect(propertiesUi.sambaNameEdit, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
78
 
    connect(propertiesUi.sambaAllowGuestChk, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
79
 
    connect(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SIGNAL(changed()));
80
 
 
81
 
    for (int i = 0; i < model->rowCount(); ++i) {
82
 
        propertiesUi.tableView->openPersistentEditor(model->index(i, 1, QModelIndex()));
83
 
    }
84
 
}
85
 
 
86
 
SambaUserSharePlugin::~SambaUserSharePlugin()
87
 
{
88
 
}
89
 
 
90
 
void SambaUserSharePlugin::setupModel()
91
 
{
92
 
    model = new UserPermissionModel(shareData, this);
93
 
}
94
 
 
95
 
void SambaUserSharePlugin::setupViews()
96
 
{
97
 
 
98
 
    propertiesUi.tableView->setModel(model);
99
 
    propertiesUi.tableView->setSelectionMode(QAbstractItemView::NoSelection);
100
 
    propertiesUi.tableView->setItemDelegate(new UserPermissionDelegate(this));
101
 
}
102
 
 
103
 
void SambaUserSharePlugin::load()
104
 
{
105
 
    bool guestAllowed = false;
106
 
    bool sambaShared = KSambaShare::instance()->isDirectoryShared(url);
107
 
 
108
 
    propertiesUi.sambaChk->setChecked(sambaShared);
109
 
    if (sambaShared) {
110
 
        guestAllowed = (bool) shareData.guestPermission();
111
 
    }
112
 
    propertiesUi.sambaAllowGuestChk->setChecked(guestAllowed);
113
 
 
114
 
    propertiesUi.sambaNameEdit->setText(shareData.name());
115
 
}
116
 
 
117
 
void SambaUserSharePlugin::applyChanges()
118
 
{
119
 
    KSambaShareData::UserShareError result;
120
 
 
121
 
    if (propertiesUi.sambaChk->isChecked()) {
122
 
        if (!propertiesUi.sambaNameEdit->text().isEmpty()
123
 
                && (shareData.name() != propertiesUi.sambaNameEdit->text())) {
124
 
            shareData.setName(propertiesUi.sambaNameEdit->text());
125
 
        }
126
 
 
127
 
        shareData.setPath(url);
128
 
 
129
 
        KSambaShareData::GuestPermission guestOk(shareData.guestPermission());
130
 
 
131
 
        guestOk = (propertiesUi.sambaAllowGuestChk->isChecked() == false)
132
 
                  ? KSambaShareData::GuestsNotAllowed : KSambaShareData::GuestsAllowed;
133
 
 
134
 
        shareData.setGuestPermission(guestOk);
135
 
 
136
 
        if (shareData.setAcl(model->getAcl()) != KSambaShareData::UserShareAclOk) {
137
 
            return;
138
 
        }
139
 
 
140
 
        result = shareData.save();
141
 
    } else if (KSambaShare::instance()->isDirectoryShared(url)) {
142
 
        result = shareData.remove();
143
 
    }
144
 
}
145
 
 
146
 
void SambaUserSharePlugin::toggleShareStatus(bool checked)
147
 
{
148
 
    if (propertiesUi.sambaNameEdit->text().isEmpty()) {
149
 
        propertiesUi.sambaNameEdit->setText(getNewSambaName());
150
 
    }
151
 
}
152
 
 
153
 
bool SambaUserSharePlugin::updateSambaShare()
154
 
{
155
 
    kDebug() << "SambaUserSharePlugin::updateSambaShare: url" << url;
156
 
 
157
 
    if (propertiesUi.sambaNameEdit->text().isEmpty()) {
158
 
        //KMessageBox::sorry(this, i18n("You have to enter a name for the Samba share."));
159
 
        propertiesUi.sambaNameEdit->setFocus();
160
 
        return false;
161
 
    }
162
 
 
163
 
    if (propertiesUi.sambaNameEdit->text() != shareData.name()) {
164
 
        if (!KSambaShare::instance()->isShareNameAvailable(propertiesUi.sambaNameEdit->text())) {
165
 
            // There is another Share with the same name
166
 
            //KMessageBox::sorry(this, i18n("<qt>There is already a share with the name <strong>%1</strong>.<br /> Please choose another name.</qt>", sambaNameEdit->text()));
167
 
            propertiesUi.sambaNameEdit->selectAll();
168
 
            propertiesUi.sambaNameEdit->setFocus();
169
 
            return false;
170
 
        }
171
 
 
172
 
        shareData.setName(propertiesUi.sambaNameEdit->text());
173
 
    }
174
 
 
175
 
    return true;
176
 
}
177
 
 
178
 
QString SambaUserSharePlugin::getNewSambaName()
179
 
{
180
 
    QString shareName = KUrl(url).fileName();
181
 
 
182
 
    if (!propertiesUi.sambaNameEdit->text().isEmpty()) {
183
 
        shareName = propertiesUi.sambaNameEdit->text();
184
 
    }
185
 
 
186
 
    // Windows could have problems with longer names
187
 
    shareName = shareName.left(12);
188
 
 
189
 
    return shareName;
190
 
}
191
 
 
192
 
#include "moc_propsdlgshareplugin.cpp"