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