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().toLocalFile(); 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 QButtonGroup *rbGroup = new QButtonGroup( d->m_widget ); 00140 d->m_rbUnShare = new QRadioButton( i18n("Not shared"), d->m_widget ); 00141 connect( d->m_rbUnShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) ); 00142 vbox->addWidget( d->m_rbUnShare, 0 ); 00143 rbGroup->addButton( d->m_rbUnShare ); 00144 00145 d->m_rbShare = new QRadioButton( i18n("Shared"), d->m_widget ); 00146 connect( d->m_rbShare, SIGNAL( toggled(bool) ), SIGNAL( changed() ) ); 00147 vbox->addWidget( d->m_rbShare, 0 ); 00148 rbGroup->addButton( d->m_rbShare ); 00149 00150 // Activate depending on status 00151 if ( d->m_bAllShared ) 00152 d->m_rbShare->setChecked(true); 00153 if ( d->m_bAllUnshared ) 00154 d->m_rbUnShare->setChecked(true); 00155 00156 // Some help text 00157 QLabel *label = new QLabel( i18n("Sharing this folder makes it available under Linux/UNIX (NFS) and Windows (Samba).") , d->m_widget ); 00158 label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter); 00159 label->setWordWrap(true); 00160 vbox->addWidget( label, 0 ); 00161 00162 KSeparator* sep=new KSeparator(d->m_widget); 00163 vbox->addWidget( sep, 0 ); 00164 label = new QLabel( i18n("You can also reconfigure file sharing authorization.") , d->m_widget ); 00165 label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter); 00166 label->setWordWrap(true); 00167 vbox->addWidget( label, 0 ); 00168 d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget ); 00169 connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) ); 00170 vbox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter ); 00171 00172 vbox->addStretch( 10 ); 00173 } 00174 } 00175 break; 00176 case KFileShare::ErrorNotFound: 00177 vbox->addWidget( new QLabel( i18n("Error running 'filesharelist'. Check if installed and in $PATH or /usr/sbin."), 00178 d->m_widget ), 0 ); 00179 break; 00180 case KFileShare::UserNotAllowed: 00181 { 00182 vbox->setSpacing( 10 ); 00183 if (KFileShare::sharingEnabled()) { 00184 vbox->addWidget( new QLabel( i18n("You need to be authorized to share folders."), 00185 d->m_widget ), 0 ); 00186 } else { 00187 vbox->addWidget( new QLabel( i18n("File sharing is disabled."), 00188 d->m_widget ), 0 ); 00189 } 00190 QHBoxLayout* hBox = new QHBoxLayout( (QWidget *)0L ); 00191 vbox->addLayout( hBox, 0 ); 00192 d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget ); 00193 connect( d->m_pbConfig, SIGNAL( clicked() ), SLOT( slotConfigureFileSharing() ) ); 00194 hBox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter ); 00195 vbox->addStretch( 10 ); // align items on top 00196 break; 00197 } 00198 case KFileShare::NotInitialized: 00199 kWarning() << "KFileShare Authorization still NotInitialized after calling authorization() - impossible"; 00200 break; 00201 } 00202 d->m_widget->show(); // In case the dialog was shown already. 00203 } 00204 00205 void KFileSharePropsPlugin::slotConfigureFileSharing() 00206 { 00207 if (d->m_configProc) return; 00208 00209 d->m_configProc = new KfsProcess(this); 00210 (*d->m_configProc) << KStandardDirs::findExe("kdesu") << "kcmshell4" << "fileshare"; 00211 if (!d->m_configProc->start()) 00212 { 00213 delete d->m_configProc; 00214 d->m_configProc = 0; 00215 return; 00216 } 00217 connect(d->m_configProc, SIGNAL(processExited()), 00218 this, SLOT(slotConfigureFileSharingDone())); 00219 d->m_pbConfig->setEnabled(false); 00220 } 00221 00222 void KFileSharePropsPlugin::slotConfigureFileSharingDone() 00223 { 00224 delete d->m_configProc; 00225 d->m_configProc = 0; 00226 KFileShare::readConfig(); 00227 KFileShare::readShareList(); 00228 init(); 00229 } 00230 00231 void KFileSharePropsPlugin::applyChanges() 00232 { 00233 kDebug() << "KFileSharePropsPlugin::applyChanges"; 00234 if ( d->m_rbShare && d->m_rbUnShare ) 00235 { 00236 bool share = d->m_rbShare->isChecked(); 00237 00238 if (share && d->m_bAllShared) 00239 return; // Nothing to do 00240 if (!share && d->m_bAllUnshared) 00241 return; // Nothing to do 00242 00243 const KFileItemList items = properties->items(); 00244 bool ok = true; 00245 KFileItemList::const_iterator kit = items.begin(); 00246 const KFileItemList::const_iterator kend = items.end(); 00247 for ( ; kit != kend && ok; ++kit ) 00248 { 00249 const QString path = (*kit).url().toLocalFile(); 00250 ok = setShared( path, share ); 00251 if (!ok) { 00252 if (share) 00253 KMessageBox::detailedError(properties, 00254 i18n("Sharing folder '%1' failed.", path), 00255 i18n("An error occurred while trying to share folder '%1'. " 00256 "Make sure that the Perl script 'fileshareset' is set suid root.", 00257 path)); 00258 else 00259 KMessageBox::error(properties, 00260 i18n("Unsharing folder '%1' failed.", path), 00261 i18n("An error occurred while trying to unshare folder '%1'. " 00262 "Make sure that the Perl script 'fileshareset' is set suid root.", 00263 path)); 00264 00265 properties->abortApplying(); 00266 break; 00267 } 00268 } 00269 00270 // Get the change back into our cached info 00271 KFileShare::readShareList(); 00272 } 00273 } 00274 00275 bool KFileSharePropsPlugin::setShared( const QString& path, bool shared ) 00276 { 00277 kDebug() << "KFileSharePropsPlugin::setShared " << path << "," << shared; 00278 return KFileShare::setShared( path, shared ); 00279 } 00280 00281 QWidget* KFileSharePropsPlugin::page() const 00282 { 00283 return d->m_vBox; 00284 } 00285 00286 #include "kfilesharedialog.moc" 00287 00288 //TODO: do we need to monitor /etc/security/fileshare.conf ? 00289 // if the user is added to the 'fileshare' group, we wouldn't be notified 00290 // Of course the config module can notify us. 00291 // TODO: listen to such notifications ;)
KDE 4.6 API Reference