00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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();
00071 delete d;
00072 }
00073
00074 bool KFileSharePropsPlugin::supports( const KFileItemList& items )
00075 {
00076
00077
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
00089 if ( !(*kit).isDir() || !isLocal )
00090 return false;
00091 }
00092 return true;
00093 }
00094
00095 void KFileSharePropsPlugin::init()
00096 {
00097
00098
00099
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
00110 QString home = QDir::homePath();
00111 if ( home[home.length()-1] != '/' )
00112 home += '/';
00113 bool ok = true;
00114 const KFileItemList items = properties->items();
00115
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
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
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
00154 if ( d->m_bAllShared )
00155 d->m_rbShare->setChecked(true);
00156 if ( d->m_bAllUnshared )
00157 d->m_rbUnShare->setChecked(true);
00158
00159
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 );
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();
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;
00243 if (!share && d->m_bAllUnshared)
00244 return;
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
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
00292
00293
00294