Show invite menu in wlm chat window immediately
[kdenetwork.git] / filesharing / simple / fileshare.cpp
blob859bf9f2ee3f51efa9734ffebf3086376f53069b
1 /*
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.
17 #include <unistd.h>
19 #include <qlayout.h>
20 #include <qlabel.h>
21 #include <qdir.h>
22 #include <qradiobutton.h>
23 #include <qcheckbox.h>
25 //Added by qt3to4:
26 #include <QPixmap>
27 #include <QVBoxLayout>
28 #include <QTextStream>
29 #include <QBoxLayout>
31 #include <kpushbutton.h>
32 #include <kdebug.h>
33 #include <kdialog.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>
41 #include <kconfig.h>
42 #include <kmessagebox.h>
43 #include <kuser.h>
44 #include <kurl.h>
45 #include <kprocess.h>
46 #include <krichtextlabel.h>
47 #include <KVBox>
49 #include "propertiespage.h"
50 #include "nfsfile.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 )
66 :QWidget( parent )
68 setupUi( this );
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()
79 emit changed();
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 &not 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);
120 else
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);
160 if (getuid()==0) {
161 setButtons(Help|Apply);
162 } else {
163 setButtons(Help);
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))
181 continue;
183 dirs += *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);
197 else
198 item->setPixmap(1,cancelPix);
200 if (nfs->isDirectoryShared(*it))
201 item->setPixmap(2,okPix);
202 else
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();
216 configChanged();
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);
232 else
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) {
249 KProcess chgrp;
250 chgrp << "chgrp" << m_fileShareGroup << file;
251 KProcess chmod;
252 chmod << "chmod" << "g=rw" << file;
254 if (chgrp.execute()) {
255 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::addGroupAccessesToFile: chgrp failed";
256 return false;
260 if(chmod.execute()) {
261 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::addGroupAccessesToFile: chmod failed";
262 return false;
265 return true;
269 bool KFileShareConfig::removeGroupAccessesFromFile(const QString & file) {
270 KProcess chgrp;
271 chgrp << "chgrp" << "root" << file;
272 KProcess chmod;
273 chmod << "chmod" << "g=r" << file;
275 if (chgrp.execute()) {
276 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::removeGroupAccessesFromFile: chgrp failed";
277 return false;
281 if(chmod.execute()) {
282 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::removeGroupAccessesFromFile: chmod failed";
283 return false;
286 return true;
290 bool KFileShareConfig::setGroupAccesses() {
291 if (m_rootPassNeeded || ! m_ccgui->sambaChk->isChecked()) {
292 if (!removeGroupAccessesFromFile(KSambaShare::instance()->smbConfPath()))
293 return false;
296 if (m_rootPassNeeded || ! m_ccgui->nfsChk->isChecked()) {
297 if (!removeGroupAccessesFromFile(KNFSShare::instance()->exportsPath()))
298 return false;
301 if (! m_rootPassNeeded && m_ccgui->sambaChk->isChecked()) {
302 if (!addGroupAccessesToFile(KSambaShare::instance()->smbConfPath()))
303 return false;
306 if (! m_rootPassNeeded && m_ccgui->nfsChk->isChecked()) {
307 if (!addGroupAccessesToFile(KNFSShare::instance()->exportsPath()))
308 return false;
312 return true;
315 void KFileShareConfig::save()
317 setGroupAccesses();
319 QDir dir("/etc/security");
320 if ( !dir.exists())
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"));
330 return;
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");
351 stream << "\nNFS=";
352 stream << (m_ccgui->nfsChk->isChecked() ? "yes" : "no");
354 stream << "\nROOTPASSNEEDED=";
355 stream << (m_rootPassNeeded ? "yes" : "no");
357 stream << "\nSMBCONF=";
358 stream << m_smbConf;
360 file.close();
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)
383 : KDialog(parent)
385 setObjectName("sharedlg");
386 setModal(true);
387 setButtons(Ok|Cancel);
388 setDefaultButton(Ok);
389 setCaption(i18n("Share Folder"));
390 showButtonSeparator(true);
391 KVBox* vbox = new KVBox(this);
392 setMainWidget(vbox);
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() {
403 if (hasChanged()) {
404 if (!m_page->save())
405 return;
408 KDialog::accept();
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();
420 delete dlg;
423 void KFileShareConfig::changeShareBtnClicked() {
424 KFileItemList files;
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();
438 bool nfs = false;
439 bool samba = false;
441 foreach ( Q3ListViewItem*item, items ) {
443 if (KNFSShare::instance()->isDirectoryShared(item->text(0)))
444 nfs = true;
446 if (KSambaShare::instance()->isDirectoryShared(item->text(0)))
447 samba = true;
450 NFSFile nfsFile(KNFSShare::instance()->exportsPath());
451 if (nfs) {
452 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::removeShareBtnClicked: nfs = true";
453 nfsFile.load();
454 foreach ( Q3ListViewItem*item, items ) {
455 nfsFile.removeEntryByPath(item->text(0));
459 SambaFile smbFile(KSambaShare::instance()->smbConfPath(),false);
460 if (samba) {
461 kDebug(FILESHARE_DEBUG) << "KFileShareConfig::removeShareBtnClicked: samba = true";
462 smbFile.load();
463 foreach ( Q3ListViewItem*item, items ) {
464 smbFile.removeShareByPath(item->text(0));
468 PropertiesPage::save(&nfsFile, &smbFile, nfs,samba);
470 updateShareListView();
473 #include "fileshare.moc"