2 Copyright (c) 2002 Laurent Montel <montel@kde.org>
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <qradiobutton.h>
23 #include <qcheckbox.h>
27 #include <QVBoxLayout>
28 #include <QTextStream>
31 #include <kpushbutton.h>
34 #include <kgenericfactory.h>
35 #include <k3listview.h>
36 #include <kiconloader.h>
37 #include <knfsshare.h>
38 #include <ksambashare.h>
39 #include <kfileshare.h>
40 #include <kstandarddirs.h>
42 #include <kmessagebox.h>
46 #include <krichtextlabel.h>
49 #include "propertiespage.h"
51 #include "sambafile.h"
53 #include "fileshare.h"
54 #include "groupconfigdlg.h"
57 K_PLUGIN_FACTORY(ShareFactory
, registerPlugin
<KFileShareConfig
>();)
58 K_EXPORT_PLUGIN(ShareFactory("kcmfileshare"))
61 #define FILESHARECONF "/etc/security/fileshare.conf"
62 #define FILESHARE_DEBUG 5009
65 ControlCenterGUI::ControlCenterGUI( QWidget
*parent
)
69 connect( listView
, SIGNAL( selectionChanged() ), this, SLOT(listView_selectionChanged() ) );
70 connect( shareGrp
, SIGNAL(clicked(int) ),this,SLOT( changedSlot() ) );
71 connect( simpleRadio
, SIGNAL(clicked() ),this,SLOT( changedSlot() ) );
72 connect( advancedRadio
, SIGNAL(clicked() ),this,SLOT( changedSlot() ) );
73 connect( sambaChk
, SIGNAL(clicked() ),this,SLOT( changedSlot() ) );
74 connect( nfsChk
, SIGNAL(clicked() ),this,SLOT( changedSlot() ) );
77 void ControlCenterGUI::changedSlot()
82 void ControlCenterGUI::listView_selectionChanged()
84 bool empty
= listView
->selectedItems ().isEmpty();
85 changeShareBtn
->setDisabled(empty
);
86 removeShareBtn
->setDisabled(empty
);
90 KFileShareConfig::KFileShareConfig(QWidget
*parent
, const QVariantList
&):
91 KCModule(ShareFactory::componentData(), parent
/*, name*/)
93 KGlobal::locale()->insertCatalog("kfileshare");
95 QBoxLayout
* layout
= new QVBoxLayout(this,0,
96 KDialog::spacingHint());
99 QVButtonGroup *box = new QVButtonGroup( i18n("File Sharing"), this );
100 box->layout()->setSpacing( KDialog::spacingHint() );
101 layout->addWidget(box);
102 noSharing=new QRadioButton( i18n("Do ¬ allow users to share files"), box );
103 sharing=new QRadioButton( i18n("&Allow users to share files from their HOME folder"), box);
105 m_ccgui
= new ControlCenterGUI(this);
106 connect( m_ccgui
, SIGNAL( changed()), this, SLOT(configChanged()));
107 connect( m_ccgui
->allowedUsersBtn
, SIGNAL( clicked()),
108 this, SLOT(allowedUsersBtnClicked()));
110 QString path
= QString::fromLocal8Bit( getenv( "PATH" ) );
111 path
+= QString::fromLatin1( ":/usr/sbin" );
112 QString sambaExec
= KStandardDirs::findExe( QString::fromLatin1("smbd"), path
);
113 QString nfsExec
= KStandardDirs::findExe( QString::fromLatin1("rpc.nfsd"), path
);
115 if ( nfsExec
.isEmpty() && sambaExec
.isEmpty())
117 m_ccgui
->shareGrp
->setDisabled(true);
118 m_ccgui
->sharedFoldersGroupBox
->setDisabled(true);
122 if (nfsExec
.isEmpty()) {
123 m_ccgui
->nfsChk
->setDisabled(true);
124 m_ccgui
->nfsChk
->setChecked(false);
125 m_ccgui
->nfsChk
->setToolTip(i18n("No NFS server installed on this system"));
128 if (sambaExec
.isEmpty()) {
129 m_ccgui
->sambaChk
->setDisabled(true);
130 m_ccgui
->sambaChk
->setChecked(false);
131 m_ccgui
->sambaChk
->setToolTip(i18n("No Samba server installed on this system"));
134 m_ccgui
->infoLbl
->hide();
135 layout
->addWidget(m_ccgui
);
136 updateShareListView();
137 connect( KNFSShare::instance(), SIGNAL( changed()),
138 this, SLOT(updateShareListView()));
139 connect( KSambaShare::instance(), SIGNAL( changed()),
140 this, SLOT(updateShareListView()));
145 if((getuid() == 0) ||
146 ((KFileShare::shareMode() == KFileShare::Advanced
) &&
147 (KFileShare::authorization() == KFileShare::Authorized
)))
149 connect( m_ccgui
->addShareBtn
, SIGNAL(clicked()),
150 this, SLOT(addShareBtnClicked()));
151 connect( m_ccgui
->changeShareBtn
, SIGNAL(clicked()),
152 this, SLOT(changeShareBtnClicked()));
153 connect( m_ccgui
->removeShareBtn
, SIGNAL(clicked()),
154 this, SLOT(removeShareBtnClicked()));
155 m_ccgui
->listView
->setSelectionMode(Q3ListView::Extended
);
156 m_ccgui
->shareBtnPnl
->setEnabled(true);
161 setButtons(Help
|Apply
);
164 m_ccgui
->shareGrp
->setDisabled( true );
169 void KFileShareConfig::updateShareListView()
171 m_ccgui
->listView
->clear();
172 KNFSShare
* nfs
= KNFSShare::instance();
173 KSambaShare
* samba
= KSambaShare::instance();
175 QStringList dirs
= nfs
->sharedDirectories();
176 const QStringList sambaDirs
= samba
->sharedDirectories();
178 for ( QStringList::ConstIterator it
= sambaDirs
.constBegin(); it
!= sambaDirs
.constEnd(); ++it
) {
179 // Do not insert duplicates
180 if (nfs
->isDirectoryShared(*it
))
186 QPixmap folderPix
= SmallIcon("folder", 0, KIconLoader::DefaultState
, QStringList());
187 QPixmap okPix
= SmallIcon("dialog-ok");
188 QPixmap cancelPix
= SmallIcon("dialog-cancel");
190 for ( QStringList::ConstIterator it
= dirs
.constBegin(); it
!= dirs
.constEnd(); ++it
) {
191 K3ListViewItem
* item
= new K3ListViewItem(m_ccgui
->listView
);
192 item
->setText(0,*it
);
193 item
->setPixmap(0, folderPix
);
195 if (samba
->isDirectoryShared(*it
))
196 item
->setPixmap(1,okPix
);
198 item
->setPixmap(1,cancelPix
);
200 if (nfs
->isDirectoryShared(*it
))
201 item
->setPixmap(2,okPix
);
203 item
->setPixmap(2,cancelPix
);
209 void KFileShareConfig::allowedUsersBtnClicked() {
210 GroupConfigDlg
dlg(this,m_fileShareGroup
,m_restricted
,m_rootPassNeeded
,
211 m_ccgui
->simpleRadio
->isChecked());
212 if (dlg
.exec() == QDialog::Accepted
) {
213 m_fileShareGroup
= dlg
.fileShareGroup().name();
214 m_restricted
= dlg
.restricted();
215 m_rootPassNeeded
= dlg
.rootPassNeeded();
222 void KFileShareConfig::load()
224 KConfig
config(QString::fromLatin1(FILESHARECONF
), KConfig::SimpleConfig
);
226 m_ccgui
->shareGrp
->setChecked( config
.group("").readEntry("FILESHARING", "yes") == "yes" );
228 m_restricted
= config
.group("").readEntry("RESTRICT", "yes") == "yes";
230 if (config
.group("").readEntry("SHARINGMODE", "simple") == "simple")
231 m_ccgui
->simpleRadio
->setChecked(true);
233 m_ccgui
->advancedRadio
->setChecked(true);
235 m_fileShareGroup
= config
.group("").readEntry("FILESHAREGROUP", "fileshare");
237 m_ccgui
->sambaChk
->setChecked(
238 config
.group("").readEntry("SAMBA", "yes") == "yes");
240 m_ccgui
->nfsChk
->setChecked(
241 config
.group("").readEntry("NFS", "yes") == "yes");
243 m_rootPassNeeded
= config
.group("").readEntry("ROOTPASSNEEDED", "yes") == "yes";
245 m_smbConf
= KSambaShare::instance()->smbConfPath();
248 bool KFileShareConfig::addGroupAccessesToFile(const QString
& file
) {
250 chgrp
<< "chgrp" << m_fileShareGroup
<< file
;
252 chmod
<< "chmod" << "g=rw" << file
;
254 if (chgrp
.execute()) {
255 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::addGroupAccessesToFile: chgrp failed";
260 if(chmod
.execute()) {
261 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::addGroupAccessesToFile: chmod failed";
269 bool KFileShareConfig::removeGroupAccessesFromFile(const QString
& file
) {
271 chgrp
<< "chgrp" << "root" << file
;
273 chmod
<< "chmod" << "g=r" << file
;
275 if (chgrp
.execute()) {
276 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::removeGroupAccessesFromFile: chgrp failed";
281 if(chmod
.execute()) {
282 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::removeGroupAccessesFromFile: chmod failed";
290 bool KFileShareConfig::setGroupAccesses() {
291 if (m_rootPassNeeded
|| ! m_ccgui
->sambaChk
->isChecked()) {
292 if (!removeGroupAccessesFromFile(KSambaShare::instance()->smbConfPath()))
296 if (m_rootPassNeeded
|| ! m_ccgui
->nfsChk
->isChecked()) {
297 if (!removeGroupAccessesFromFile(KNFSShare::instance()->exportsPath()))
301 if (! m_rootPassNeeded
&& m_ccgui
->sambaChk
->isChecked()) {
302 if (!addGroupAccessesToFile(KSambaShare::instance()->smbConfPath()))
306 if (! m_rootPassNeeded
&& m_ccgui
->nfsChk
->isChecked()) {
307 if (!addGroupAccessesToFile(KNFSShare::instance()->exportsPath()))
315 void KFileShareConfig::save()
319 QDir
dir("/etc/security");
321 dir
.mkdir("/etc/security");
323 QFile
file(FILESHARECONF
);
324 if ( ! file
.open(QIODevice::WriteOnly
)) {
325 KMessageBox::detailedError(this,
326 i18n("Could not save settings."),
327 i18n("Could not open file '%1' for writing: %2", QString(FILESHARECONF
),
328 file
.errorString() ),
329 i18n("Saving Failed"));
334 QTextStream
stream(&file
);
336 stream
<< "FILESHARING=";
337 stream
<< (m_ccgui
->shareGrp
->isChecked() ? "yes" : "no");
339 stream
<< "\nRESTRICT=";
340 stream
<< (m_restricted
? "yes" : "no");
342 stream
<< "\nSHARINGMODE=";
343 stream
<< (m_ccgui
->simpleRadio
->isChecked() ? "simple" : "advanced");
345 stream
<< "\nFILESHAREGROUP=";
346 stream
<< m_fileShareGroup
;
348 stream
<< "\nSAMBA=";
349 stream
<< (m_ccgui
->sambaChk
->isChecked() ? "yes" : "no");
352 stream
<< (m_ccgui
->nfsChk
->isChecked() ? "yes" : "no");
354 stream
<< "\nROOTPASSNEEDED=";
355 stream
<< (m_rootPassNeeded
? "yes" : "no");
357 stream
<< "\nSMBCONF=";
363 void KFileShareConfig::defaults()
365 m_ccgui
->shareGrp
->setChecked( false );
368 QString
KFileShareConfig::quickHelp() const
370 return i18n("<h1>File Sharing</h1><p>This module can be used "
371 "to enable file sharing over the network using "
372 "the \"Network File System\" (NFS) or SMB in Konqueror. "
373 "The latter enables you to share your files with Windows(R) "
374 "computers on your network.</p>");
377 void KFileShareConfig::addShareBtnClicked() {
378 showShareDialog(KFileItemList());
382 PropertiesPageDlg::PropertiesPageDlg(QWidget
*parent
, KFileItemList files
)
385 setObjectName("sharedlg");
387 setButtons(Ok
|Cancel
);
388 setDefaultButton(Ok
);
389 setCaption(i18n("Share Folder"));
390 showButtonSeparator(true);
391 KVBox
* vbox
= new KVBox(this);
394 m_page
= new PropertiesPage(vbox
,files
,true);
395 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
398 bool PropertiesPageDlg::hasChanged() {
399 return m_page
->hasChanged();
402 void PropertiesPageDlg::slotOk() {
413 void KFileShareConfig::showShareDialog(const KFileItemList
& files
) {
414 PropertiesPageDlg
* dlg
= new PropertiesPageDlg(this,files
);
415 if (dlg
->exec() == QDialog::Accepted
) {
416 if ( dlg
->hasChanged() ) {
417 updateShareListView();
423 void KFileShareConfig::changeShareBtnClicked() {
425 QList
<Q3ListViewItem
*> items
= m_ccgui
->listView
->selectedItems();
427 foreach ( Q3ListViewItem
* item
, items
) {
428 files
.append(KFileItem(KUrl(item
->text(0)),"",0));
431 showShareDialog(files
);
434 void KFileShareConfig::removeShareBtnClicked() {
436 QList
<Q3ListViewItem
*> items
= m_ccgui
->listView
->selectedItems();
441 foreach ( Q3ListViewItem
*item
, items
) {
443 if (KNFSShare::instance()->isDirectoryShared(item
->text(0)))
446 if (KSambaShare::instance()->isDirectoryShared(item
->text(0)))
450 NFSFile
nfsFile(KNFSShare::instance()->exportsPath());
452 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::removeShareBtnClicked: nfs = true";
454 foreach ( Q3ListViewItem
*item
, items
) {
455 nfsFile
.removeEntryByPath(item
->text(0));
459 SambaFile
smbFile(KSambaShare::instance()->smbConfPath(),false);
461 kDebug(FILESHARE_DEBUG
) << "KFileShareConfig::removeShareBtnClicked: samba = true";
463 foreach ( Q3ListViewItem
*item
, items
) {
464 smbFile
.removeShareByPath(item
->text(0));
468 PropertiesPage::save(&nfsFile
, &smbFile
, nfs
,samba
);
470 updateShareListView();
473 #include "fileshare.moc"