• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kfilesharedialog.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (c) 2001 David Faure <faure@kde.org>
00003    Copyright (c) 2001 Laurent Montel <lmontel@mandrakesoft.com>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kfilesharedialog.h"
00021 #include "kfsprocess.h"
00022 #include <kvbox.h>
00023 #include <QtGui/QLabel>
00024 #include <QtCore/QDir>
00025 #include <QtGui/QRadioButton>
00026 #include <QtGui/QButtonGroup>
00027 #include <QtGui/QLayout>
00028 #include <klocale.h>
00029 #include <kglobalsettings.h>
00030 #include <kstandarddirs.h>
00031 #include <kdebug.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <errno.h>
00035 #include <kio/kfileshare.h>
00036 #include <kseparator.h>
00037 #include <QtGui/QPushButton>
00038 #include <kapplication.h>
00039 #include <kconfig.h>
00040 #include <kmessagebox.h>
00041 
00042 class KFileSharePropsPlugin::Private
00043 {
00044 public:
00045     KVBox *m_vBox;
00046     KfsProcess *m_configProc;
00047     bool m_bAllShared;
00048     bool m_bAllUnshared;
00049     QWidget *m_widget;
00050     QRadioButton *m_rbShare;
00051     QRadioButton *m_rbUnShare;
00052     QPushButton *m_pbConfig;
00053 };
00054 
00055 KFileSharePropsPlugin::KFileSharePropsPlugin( KPropertiesDialog *_props )
00056     : KPropertiesDialogPlugin( _props ),d(new Private)
00057 {
00058     d->m_vBox = new KVBox();
00059     _props->addPage( d->m_vBox, i18n("&Share") );
00060 
00061     d->m_configProc = 0;
00062     properties->setFileSharingPage(d->m_vBox);
00063     d->m_widget = 0L;
00064     init();
00065 }
00066 
00067 KFileSharePropsPlugin::~KFileSharePropsPlugin()
00068 {
00069     if (d->m_configProc)
00070         d->m_configProc->detach(); // Detach to prevent that we kill the process
00071     delete d;
00072 }
00073 
00074 bool KFileSharePropsPlugin::supports( const KFileItemList& items )
00075 {
00076     // Do not show dialog if in advanced mode,
00077     // because the advanced dialog is shown already.
00078     if (KFileShare::shareMode() == KFileShare::Advanced) {
00079         kDebug() << "KFileSharePropsPlugin::supports: false because sharemode is advanced";
00080         return false;
00081     }
00082 
00083     KFileItemList::const_iterator kit = items.begin();
00084     const KFileItemList::const_iterator kend = items.end();
00085     for ( ; kit != kend; ++kit )
00086     {
00087         bool isLocal = (*kit).isLocalFile();
00088         // We only support local dirs
00089         if ( !(*kit).isDir() || !isLocal )
00090             return false;
00091     }
00092     return true;
00093 }
00094 
00095 void KFileSharePropsPlugin::init()
00096 {
00097     // We store the main widget, so that it's possible (later) to call init()
00098     // more than once, to update the page if something changed (e.g. after
00099     // the user has been authorized)
00100     delete d->m_widget;
00101     d->m_rbShare = 0L;
00102     d->m_rbUnShare = 0L;
00103     d->m_widget = new QWidget( d->m_vBox );
00104     QVBoxLayout * vbox = new QVBoxLayout( d->m_widget );
00105 
00106     switch ( KFileShare::authorization() ) {
00107     case KFileShare::Authorized:
00108     {
00109         // Check if all selected dirs are in $HOME
00110         QString home = QDir::homePath();
00111         if ( home[home.length()-1] != '/' )
00112             home += '/';
00113         bool ok = true;
00114         const KFileItemList items = properties->items();
00115         // We have 3 possibilities: all shared, all unshared, or mixed.
00116         d->m_bAllShared = true;
00117         d->m_bAllUnshared = true;
00118         KFileItemList::const_iterator kit = items.begin();
00119         const KFileItemList::const_iterator kend = items.end();
00120         for ( ; kit != kend && ok; ++kit )
00121         {
00122             // We know it's local, see supports()
00123             const QString path = (*kit).url().path();
00124             if ( !path.startsWith( home ) )
00125                 ok = false;
00126             if ( KFileShare::isDirectoryShared( path ) )
00127                 d->m_bAllUnshared = false;
00128             else
00129                 d->m_bAllShared = false;
00130         }
00131         if ( !ok )
00132         {
00133             vbox->addWidget( new QLabel( i18n( "Only folders in your home folder can be shared."),
00134                                          d->m_widget ), 0 );
00135         }
00136         else
00137         {
00138             // Everything ok, show the share/unshare GUI
00139             vbox->setSpacing( KDialog::spacingHint() );
00140             vbox->setMargin( KDialog::marginHint() );
00141 
00142             QButtonGroup *rbGroup = new QButtonGroup( d->m_widget );
00143             d->m_rbUnShare = new QRadioButton( i18n("Not shared"), d->m_widget );
00144             connect( d->m_rbUnShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00145             vbox->addWidget( d->m_rbUnShare, 0 );
00146             rbGroup->addButton( d->m_rbUnShare );
00147 
00148             d->m_rbShare = new QRadioButton( i18n("Shared"), d->m_widget );
00149             connect( d->m_rbShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00150             vbox->addWidget( d->m_rbShare, 0 );
00151             rbGroup->addButton( d->m_rbShare );
00152 
00153             // Activate depending on status
00154             if ( d->m_bAllShared )
00155                 d->m_rbShare->setChecked(true);
00156             if ( d->m_bAllUnshared )
00157                 d->m_rbUnShare->setChecked(true);
00158 
00159             // Some help text
00160             QLabel *label = new QLabel( i18n("Sharing this folder makes it available under Linux/UNIX (NFS) and Windows (Samba).") , d->m_widget );
00161             label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
00162         label->setWordWrap(true);
00163             vbox->addWidget( label, 0 );
00164 
00165         KSeparator* sep=new KSeparator(d->m_widget);
00166         vbox->addWidget( sep, 0 );
00167         label = new QLabel( i18n("You can also reconfigure file sharing authorization.") , d->m_widget );
00168             label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
00169         label->setWordWrap(true);
00170         vbox->addWidget( label, 0 );
00171         d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
00172         connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00173         vbox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
00174 
00175             vbox->addStretch( 10 );
00176         }
00177     }
00178     break;
00179     case KFileShare::ErrorNotFound:
00180         vbox->addWidget( new QLabel( i18n("Error running 'filesharelist'. Check if installed and in $PATH or /usr/sbin."),
00181                     d->m_widget ), 0 );
00182         break;
00183     case KFileShare::UserNotAllowed:
00184     {
00185         vbox->setSpacing( 10 );
00186         if (KFileShare::sharingEnabled()) {
00187           vbox->addWidget( new QLabel( i18n("You need to be authorized to share folders."),
00188                     d->m_widget ), 0 );
00189         } else {
00190           vbox->addWidget( new QLabel( i18n("File sharing is disabled."),
00191                     d->m_widget ), 0 );
00192         }
00193         QHBoxLayout* hBox = new QHBoxLayout( (QWidget *)0L );
00194         vbox->addLayout( hBox, 0 );
00195         d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
00196         connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00197         hBox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
00198         vbox->addStretch( 10 ); // align items on top
00199         break;
00200     }
00201     case KFileShare::NotInitialized:
00202         kWarning() << "KFileShare Authorization still NotInitialized after calling authorization() - impossible";
00203         break;
00204     }
00205     d->m_widget->show(); // In case the dialog was shown already.
00206 }
00207 
00208 void KFileSharePropsPlugin::slotConfigureFileSharing()
00209 {
00210     if (d->m_configProc) return;
00211 
00212     d->m_configProc = new KfsProcess(this);
00213     (*d->m_configProc) << KStandardDirs::findExe("kdesu") << "kcmshell4" << "fileshare";
00214     if (!d->m_configProc->start())
00215     {
00216        delete d->m_configProc;
00217        d->m_configProc = 0;
00218        return;
00219     }
00220     connect(d->m_configProc, SIGNAL(processExited()),
00221             this, SLOT(slotConfigureFileSharingDone()));
00222     d->m_pbConfig->setEnabled(false);
00223 }
00224 
00225 void KFileSharePropsPlugin::slotConfigureFileSharingDone()
00226 {
00227     delete d->m_configProc;
00228     d->m_configProc = 0;
00229     KFileShare::readConfig();
00230     KFileShare::readShareList();
00231     init();
00232 }
00233 
00234 void KFileSharePropsPlugin::applyChanges()
00235 {
00236     kDebug() << "KFileSharePropsPlugin::applyChanges";
00237     if ( d->m_rbShare && d->m_rbUnShare )
00238     {
00239         bool share = d->m_rbShare->isChecked();
00240 
00241         if (share && d->m_bAllShared)
00242            return; // Nothing to do
00243         if (!share && d->m_bAllUnshared)
00244            return; // Nothing to do
00245 
00246         const KFileItemList items = properties->items();
00247         bool ok = true;
00248         KFileItemList::const_iterator kit = items.begin();
00249         const KFileItemList::const_iterator kend = items.end();
00250         for ( ; kit != kend && ok; ++kit )
00251         {
00252              const QString path = (*kit).url().path();
00253              ok = setShared( path, share );
00254              if (!ok) {
00255                 if (share)
00256                   KMessageBox::detailedError(properties,
00257                     i18n("Sharing folder '%1' failed.", path),
00258                     i18n("An error occurred while trying to share folder '%1'. "
00259                          "Make sure that the Perl script 'fileshareset' is set suid root.",
00260                           path));
00261                 else
00262                   KMessageBox::error(properties,
00263                     i18n("Unsharing folder '%1' failed.", path),
00264                     i18n("An error occurred while trying to unshare folder '%1'. "
00265                          "Make sure that the Perl script 'fileshareset' is set suid root.",
00266                           path));
00267 
00268                 properties->abortApplying();
00269                 break;
00270              }
00271         }
00272 
00273         // Get the change back into our cached info
00274         KFileShare::readShareList();
00275     }
00276 }
00277 
00278 bool KFileSharePropsPlugin::setShared( const QString& path, bool shared )
00279 {
00280     kDebug() << "KFileSharePropsPlugin::setShared " << path << "," << shared;
00281     return KFileShare::setShared( path, shared );
00282 }
00283 
00284 QWidget* KFileSharePropsPlugin::page() const
00285 {
00286     return d->m_vBox;
00287 }
00288 
00289 #include "kfilesharedialog.moc"
00290 
00291 //TODO: do we need to monitor /etc/security/fileshare.conf ?
00292 // if the user is added to the 'fileshare' group, we wouldn't be notified
00293 // Of course the config module can notify us.
00294 // TODO: listen to such notifications ;)

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal