kio Library API Documentation

kfilesharedlg.cpp

00001 /* This file is part of the KDE project
00002    Copyright (c) 2001 David Faure <david@mandrakesoft.com>
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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kfilesharedlg.h"
00021 #include <qvbox.h>
00022 #include <qlabel.h>
00023 #include <qdir.h>
00024 #include <qradiobutton.h>
00025 #include <qbuttongroup.h>
00026 #include <qlayout.h>
00027 #include <kprocess.h>
00028 #include <kprocio.h>
00029 #include <klocale.h>
00030 #include <kglobalsettings.h>
00031 #include <kstandarddirs.h>
00032 #include <kdebug.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <errno.h>
00036 #include <kio/kfileshare.h>
00037 #include <libintl.h>
00038 #include <kseparator.h>
00039 #include <qpushbutton.h>
00040 #include <kapplication.h>
00041 
00042 class KFileSharePropsPlugin::Private
00043 {
00044 public:
00045     QVBox *m_vBox;
00046 };
00047 
00048 KFileSharePropsPlugin::KFileSharePropsPlugin( KPropertiesDialog *_props )
00049     : KPropsDlgPlugin( _props )
00050 {
00051     d = new Private;
00052     d->m_vBox = _props->addVBoxPage( i18n("Local Net Sharing") );
00053     m_widget = 0L;
00054     init();
00055 }
00056 
00057 KFileSharePropsPlugin::~KFileSharePropsPlugin()
00058 {
00059     delete d;
00060 }
00061 
00062 bool KFileSharePropsPlugin::supports( const KFileItemList& items )
00063 {
00064     KFileItemListIterator it( items );
00065     for ( ; it.current(); ++it )
00066     {
00067         bool isLocal = ( *it )->isLocalFile();
00068         // We only support local dirs
00069         if ( !(*it)->isDir() || !isLocal )
00070             return false;
00071         // And sharing the trash doesn't make sense
00072         if ( isLocal && (*it)->url().path( 1 ) == KGlobalSettings::trashPath() )
00073             return false;
00074     }
00075     return true;
00076 }
00077 
00078 void KFileSharePropsPlugin::init()
00079 {
00080     // We store the main widget, so that it's possible (later) to call init()
00081     // more than once, to update the page if something changed (e.g. after
00082     // the user has been authorized)
00083     delete m_widget;
00084     m_rbShare = 0L;
00085     m_rbUnShare = 0L;
00086     m_widget = new QWidget( d->m_vBox );
00087     QVBoxLayout * vbox = new QVBoxLayout( m_widget );
00088 
00089     switch ( KFileShare::authorization() ) {
00090     case KFileShare::Authorized:
00091     {
00092         // Check if all selected dirs are in $HOME
00093         QString home = QDir::homeDirPath();
00094         if ( home[home.length()-1] != '/' )
00095             home += '/';
00096         bool ok = true;
00097         KFileItemList items = properties->items();
00098         // We have 3 possibilities: all shared, all unshared, or mixed.
00099         bool allShared = true;
00100         bool allUnshared = true;
00101         KFileItemListIterator it( items );
00102         for ( ; it.current() && ok; ++it ) {
00103             QString path = (*it)->url().path();
00104             if ( !path.startsWith( home ) )
00105                 ok = false;
00106             if ( KFileShare::isDirectoryShared( path ) )
00107                 allUnshared = false;
00108             else
00109                 allShared = false;
00110         }
00111         if ( !ok )
00112         {
00113             vbox->addWidget( new QLabel( i18n( "Only directories in your Home can be shared."),
00114                                          m_widget ), 0 );
00115         }
00116         else
00117         {
00118             // Everything ok, show the share/unshare GUI
00119             vbox->setSpacing( 20 );
00120             vbox->setMargin( 20 );
00121 
00122             QButtonGroup *rbGroup = new QButtonGroup( m_widget );
00123             rbGroup->hide();
00124             m_rbUnShare = new QRadioButton( i18n("Not shared"), m_widget );
00125             connect( m_rbUnShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00126             vbox->addWidget( m_rbUnShare, 0 );
00127             rbGroup->insert( m_rbUnShare );
00128 
00129             m_rbShare = new QRadioButton( i18n("Shared"), m_widget );
00130             connect( m_rbShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) );
00131             vbox->addWidget( m_rbShare, 0 );
00132             rbGroup->insert( m_rbShare );
00133 
00134             // Activate depending on status
00135             if ( allShared )
00136                 m_rbShare->setChecked(true);
00137             if ( allUnshared )
00138                 m_rbUnShare->setChecked(true);
00139 
00140             // Some help text
00141             QLabel *label = new QLabel( i18n("Sharing this directory makes it available under Linux/UNIX (NFS) and Windows (Samba).") , m_widget );
00142             label->setAlignment( Qt::AlignAuto | Qt::AlignVCenter | Qt::WordBreak );
00143             vbox->addWidget( label, 0 );
00144 
00145         KSeparator* sep=new KSeparator(m_widget);
00146         vbox->addWidget( sep, 0 );
00147         label = new QLabel( i18n("You can also reconfigure file sharing authorization.") , m_widget );
00148             label->setAlignment( Qt::AlignAuto | Qt::AlignVCenter | Qt::WordBreak );
00149         vbox->addWidget( label, 0 );
00150         m_pbConfig = new QPushButton( i18n("Configure File Sharing"), m_widget );
00151         connect( m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00152         vbox->addWidget( m_pbConfig, 0, Qt::AlignHCenter );
00153 
00154             vbox->addStretch( 10 );
00155         }
00156     }
00157     break;
00158     case KFileShare::ErrorNotFound:
00159         vbox->addWidget( new QLabel( i18n("Error running 'filesharelist'. Check if installed and in $PATH or /usr/sbin."),
00160                     m_widget ), 0 );
00161         break;
00162     case KFileShare::UserNotAllowed:
00163     {
00164         vbox->setSpacing( 10 );
00165         vbox->addWidget( new QLabel( i18n("You need to be authorized to share directories."),
00166                     m_widget ), 0 );
00167         QHBoxLayout* hBox = new QHBoxLayout( (QWidget *)0L );
00168         vbox->addLayout( hBox, 0 );
00169         m_pbConfig = new QPushButton( i18n("Configure File Sharing"), m_widget );
00170         connect( m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) );
00171         hBox->addWidget( m_pbConfig, 0, Qt::AlignHCenter );
00172         vbox->addStretch( 10 ); // align items on top
00173         break;
00174     }
00175     case KFileShare::NotInitialized:
00176         kdWarning() << "KFileShare Authorization still NotInitialized after calling authorization() - impossible" << endl;
00177         break;
00178     }
00179 }
00180 
00181 void KFileSharePropsPlugin::slotConfigureFileSharing()
00182 {
00183     KProcess proc;
00184     proc << KStandardDirs::findExe("kdesu") << "kcmshell" << "fileshare";
00185     proc.start( KProcess::DontCare );
00186     m_pbConfig->setEnabled(false);
00187 }
00188 
00189 void KFileSharePropsPlugin::applyChanges()
00190 {
00191     kdDebug() << "KFileSharePropsPlugin::applyChanges" << endl;
00192     if ( m_rbShare && m_rbUnShare )
00193     {
00194         if ( m_rbShare->isChecked() )
00195         {
00196             // Share selected directories
00197             KFileItemList items = properties->items();
00198             KFileItemListIterator it( items );
00199             bool ok = true;
00200             for ( ; it.current() && ok; ++it ) {
00201                 QString path = (*it)->url().path();
00202                 ok = setShared( path, true );
00203             }
00204         }
00205         else if ( m_rbUnShare->isChecked() )
00206         {
00207             // Unshare selected directories
00208             KFileItemList items = properties->items();
00209             KFileItemListIterator it( items );
00210             bool ok = true;
00211             for ( ; it.current() && ok; ++it ) {
00212                 QString path = (*it)->url().path();
00213                 ok = setShared( path, false );
00214             }
00215         }
00216         // Get the change back into our cached info
00217         KFileShare::readConfig();
00218     }
00219 }
00220 
00221 bool KFileSharePropsPlugin::setShared( const QString& path, bool shared )
00222 {
00223     kdDebug() << "KFileSharePropsPlugin::setShared " << path << "," << shared << endl;
00224     return KFileShare::setShared( path, shared );
00225 }
00226 
00227 QWidget* KFileSharePropsPlugin::page() const
00228 {
00229     return d->m_vBox;
00230 }
00231 
00232 #include "kfilesharedlg.moc"
00233 
00234 //TODO: do we need to monitor /etc/security/fileshare.conf ?
00235 // if the user is added to the 'fileshare' group, we wouldn't be notified
00236 // Of course the config module can notify us.
00237 // TODO: listen to such notifications ;)
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:29 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001