• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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 <kstandarddirs.h>
00030 #include <kdebug.h>
00031 #include <kio/kfileshare.h>
00032 #include <kseparator.h>
00033 #include <QtGui/QPushButton>
00034 #include <kmessagebox.h>
00035 
00036 class KFileSharePropsPlugin::Private
00037 {
00038 public:
00039     KVBox *m_vBox;
00040     KfsProcess *m_configProc;
00041     bool m_bAllShared;
00042     bool m_bAllUnshared;
00043     QWidget *m_widget;
00044     QRadioButton *m_rbShare;
00045     QRadioButton *m_rbUnShare;
00046     QPushButton *m_pbConfig;
00047 };
00048 
00049 KFileSharePropsPlugin::KFileSharePropsPlugin( KPropertiesDialog *_props )
00050     : KPropertiesDialogPlugin( _props ),d(new Private)
00051 {
00052     d->m_vBox = new KVBox();
00053     _props->addPage( d->m_vBox, i18n("&Share") );
00054 
00055     d->m_configProc = 0;
00056     properties->setFileSharingPage(d->m_vBox);
00057     d->m_widget = 0L;
00058     init();
00059 }
00060 
00061 KFileSharePropsPlugin::~KFileSharePropsPlugin()
00062 {
00063     if (d->m_configProc)
00064         d->m_configProc->detach(); // Detach to prevent that we kill the process
00065     delete d;
00066 }
00067 
00068 bool KFileSharePropsPlugin::supports( const KFileItemList& items )
00069 {
00070     // Do not show dialog if in advanced mode,
00071     // because the advanced dialog is shown already.
00072     if (KFileShare::shareMode() == KFileShare::Advanced) {
00073         kDebug() << "KFileSharePropsPlugin::supports: false because sharemode is advanced";
00074         return false;
00075     }
00076 
00077     KFileItemList::const_iterator kit = items.begin();
00078     const KFileItemList::const_iterator kend = items.end();
00079     for ( ; kit != kend; ++kit )
00080     {
00081         bool isLocal = (*kit).isLocalFile();
00082         // We only support local dirs
00083         if ( !(*kit).isDir() || !isLocal )
00084             return false;
00085     }
00086     return true;
00087 }
00088 
00089 void KFileSharePropsPlugin::init()
00090 {
00091     // We store the main widget, so that it's possible (later) to call init()
00092     // more than once, to update the page if something changed (e.g. after
00093     // the user has been authorized)
00094     delete d->m_widget;
00095     d->m_rbShare = 0L;
00096     d->m_rbUnShare = 0L;
00097     d->m_widget = new QWidget( d->m_vBox );
00098     QVBoxLayout * vbox = new QVBoxLayout( d->m_widget );
00099 
00100     switch ( KFileShare::authorization() ) {
00101     case KFileShare::Authorized:
00102     {
00103         // Check if all selected dirs are in $HOME
00104         QString home = QDir::homePath();
00105         if ( home[home.length()-1] != '/' )
00106             home += '/';
00107         bool ok = true;
00108         const KFileItemList items = properties->items();
00109         // We have 3 possibilities: all shared, all unshared, or mixed.
00110         d->m_bAllShared = true;
00111         d->m_bAllUnshared = true;
00112         KFileItemList::const_iterator kit = items.begin();
00113         const KFileItemList::const_iterator kend = items.end();
00114         for ( ; kit != kend && ok; ++kit )
00115         {
00116             // We know it's local, see supports()
00117             const QString path = (*kit).url().toLocalFile();
00118             if ( !path.startsWith( home ) )
00119                 ok = false;
00120             if ( KFileShare::isDirectoryShared( path ) )
00121                 d->m_bAllUnshared = false;
00122             else
00123                 d->m_bAllShared = false;
00124         }
00125         if ( !ok )
00126         {
00127             vbox->addWidget( new QLabel( i18n( "Only folders in your home folder can be shared."),
00128                                          d->m_widget ), 0 );
00129         }
00130         else
00131         {
00132             // Everything ok, show the share/unshare GUI
00133             QButtonGroup *rbGroup = new QButtonGroup( d->m_widget );
00134             d->m_rbUnShare = new QRadioButton( i18n("Not shared"), d->m_widget );
00135             connect( d->m_rbUnShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00136             vbox->addWidget( d->m_rbUnShare, 0 );
00137             rbGroup->addButton( d->m_rbUnShare );
00138 
00139             d->m_rbShare = new QRadioButton( i18n("Shared"), d->m_widget );
00140             connect( d->m_rbShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00141             vbox->addWidget( d->m_rbShare, 0 );
00142             rbGroup->addButton( d->m_rbShare );
00143 
00144             // Activate depending on status
00145             if ( d->m_bAllShared )
00146                 d->m_rbShare->setChecked(true);
00147             if ( d->m_bAllUnshared )
00148                 d->m_rbUnShare->setChecked(true);
00149 
00150             // Some help text
00151             QLabel *label = new QLabel( i18n("Sharing this folder makes it available under Linux/UNIX (NFS) and Windows (Samba).") , d->m_widget );
00152             label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
00153         label->setWordWrap(true);
00154             vbox->addWidget( label, 0 );
00155 
00156         KSeparator* sep=new KSeparator(d->m_widget);
00157         vbox->addWidget( sep, 0 );
00158         label = new QLabel( i18n("You can also reconfigure file sharing authorization.") , d->m_widget );
00159             label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
00160         label->setWordWrap(true);
00161         vbox->addWidget( label, 0 );
00162         d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
00163         connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00164         vbox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
00165 
00166             vbox->addStretch( 10 );
00167         }
00168     }
00169     break;
00170     case KFileShare::ErrorNotFound:
00171         vbox->addWidget( new QLabel( i18n("Error running 'filesharelist'. Check if installed and in $PATH or /usr/sbin."),
00172                     d->m_widget ), 0 );
00173         break;
00174     case KFileShare::UserNotAllowed:
00175     {
00176         vbox->setSpacing( 10 );
00177         if (KFileShare::sharingEnabled()) {
00178           vbox->addWidget( new QLabel( i18n("You need to be authorized to share folders."),
00179                     d->m_widget ), 0 );
00180         } else {
00181           vbox->addWidget( new QLabel( i18n("File sharing is disabled."),
00182                     d->m_widget ), 0 );
00183         }
00184         QHBoxLayout* hBox = new QHBoxLayout( (QWidget *)0L );
00185         vbox->addLayout( hBox, 0 );
00186         d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
00187         connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00188         hBox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
00189         vbox->addStretch( 10 ); // align items on top
00190         break;
00191     }
00192     case KFileShare::NotInitialized:
00193         kWarning() << "KFileShare Authorization still NotInitialized after calling authorization() - impossible";
00194         break;
00195     }
00196     d->m_widget->show(); // In case the dialog was shown already.
00197 }
00198 
00199 void KFileSharePropsPlugin::slotConfigureFileSharing()
00200 {
00201     if (d->m_configProc) return;
00202 
00203     d->m_configProc = new KfsProcess(this);
00204     (*d->m_configProc) << KStandardDirs::findExe("kdesu") << "kcmshell4" << "fileshare";
00205     if (!d->m_configProc->start())
00206     {
00207        delete d->m_configProc;
00208        d->m_configProc = 0;
00209        return;
00210     }
00211     connect(d->m_configProc, SIGNAL(processExited()),
00212             this, SLOT(slotConfigureFileSharingDone()));
00213     d->m_pbConfig->setEnabled(false);
00214 }
00215 
00216 void KFileSharePropsPlugin::slotConfigureFileSharingDone()
00217 {
00218     delete d->m_configProc;
00219     d->m_configProc = 0;
00220     KFileShare::readConfig();
00221     KFileShare::readShareList();
00222     init();
00223 }
00224 
00225 void KFileSharePropsPlugin::applyChanges()
00226 {
00227     kDebug() << "KFileSharePropsPlugin::applyChanges";
00228     if ( d->m_rbShare && d->m_rbUnShare )
00229     {
00230         bool share = d->m_rbShare->isChecked();
00231 
00232         if (share && d->m_bAllShared)
00233            return; // Nothing to do
00234         if (!share && d->m_bAllUnshared)
00235            return; // Nothing to do
00236 
00237         const KFileItemList items = properties->items();
00238         bool ok = true;
00239         KFileItemList::const_iterator kit = items.begin();
00240         const KFileItemList::const_iterator kend = items.end();
00241         for ( ; kit != kend && ok; ++kit )
00242         {
00243              const QString path = (*kit).url().toLocalFile();
00244              ok = setShared( path, share );
00245              if (!ok) {
00246                 if (share)
00247                   KMessageBox::detailedError(properties,
00248                     i18n("Sharing folder '%1' failed.", path),
00249                     i18n("An error occurred while trying to share folder '%1'. "
00250                          "Make sure that the Perl script 'fileshareset' is set suid root.",
00251                           path));
00252                 else
00253                   KMessageBox::error(properties,
00254                     i18n("Unsharing folder '%1' failed.", path),
00255                     i18n("An error occurred while trying to unshare folder '%1'. "
00256                          "Make sure that the Perl script 'fileshareset' is set suid root.",
00257                           path));
00258 
00259                 properties->abortApplying();
00260                 break;
00261              }
00262         }
00263 
00264         // Get the change back into our cached info
00265         KFileShare::readShareList();
00266     }
00267 }
00268 
00269 bool KFileSharePropsPlugin::setShared( const QString& path, bool shared )
00270 {
00271     kDebug() << "KFileSharePropsPlugin::setShared " << path << "," << shared;
00272     return KFileShare::setShared( path, shared );
00273 }
00274 
00275 QWidget* KFileSharePropsPlugin::page() const
00276 {
00277     return d->m_vBox;
00278 }
00279 
00280 #include "kfilesharedialog.moc"
00281 
00282 //TODO: do we need to monitor /etc/security/fileshare.conf ?
00283 // if the user is added to the 'fileshare' group, we wouldn't be notified
00284 // Of course the config module can notify us.
00285 // TODO: listen to such notifications ;)

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.5
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