kio Library API Documentation

kfileshare.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 "kfileshare.h"
00021 #include <qdir.h>
00022 #include <kprocess.h>
00023 #include <kprocio.h>
00024 #include <klocale.h>
00025 #include <kstaticdeleter.h>
00026 #include <kstandarddirs.h>
00027 #include <kdebug.h>
00028 #include <kdirwatch.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 #include <kdirnotify_stub.h>
00033 
00034 KFileShare::Authorization KFileShare::s_authorization = NotInitialized;
00035 QStringList* KFileShare::s_shareList = 0L;
00036 static KStaticDeleter<QStringList> sdShareList;
00037 
00038 
00039 KFileSharePrivate::KFileSharePrivate()
00040 {
00041   if (KStandardDirs::exists("/etc/security/fileshare.conf")) {
00042         m_watchFile=new KDirWatch();
00043         m_watchFile->addFile("/etc/security/fileshare.conf");
00044         m_watchFile->startScan();
00045         connect(m_watchFile, SIGNAL(dirty (const QString&)),this,
00046             SLOT(slotFileChange(const QString &)));
00047   } else 
00048     m_watchFile = 0;
00049 }
00050 
00051 KFileSharePrivate::~KFileSharePrivate()
00052 {
00053   delete m_watchFile;
00054 }
00055 
00056 KFileSharePrivate *KFileSharePrivate::_self=0L;
00057 
00058 static KStaticDeleter<KFileSharePrivate> kstFileShare;
00059 
00060 KFileSharePrivate* KFileSharePrivate::self()
00061 {
00062    if (!_self)
00063       _self = kstFileShare.setObject(new KFileSharePrivate());
00064    return _self;
00065 }
00066 
00067 void KFileSharePrivate::slotFileChange(const QString &file)
00068 {
00069   if(file=="/etc/security/fileshare.conf")
00070     KFileShare::readConfig();
00071 }
00072 
00073 void KFileShare::readConfig() // static
00074 {
00075   KFileSharePrivate::self();
00076     s_authorization = UserNotAllowed;
00077     if ( !s_shareList )
00078         sdShareList.setObject( s_shareList, new QStringList );
00079     else
00080         s_shareList->clear();
00081 
00082     // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions
00083     QString exe = findExe( "filesharelist" );
00084     if (exe.isEmpty()) {
00085         s_authorization = ErrorNotFound;
00086         return;
00087     }
00088     KProcIO proc;
00089     proc << exe;
00090     if ( !proc.start( KProcess::Block ) ) {
00091         kdError() << "Can't run " << exe << endl;
00092         s_authorization = ErrorNotFound;
00093         return;
00094     }
00095 
00096     // Reading code shamelessly stolen from khostname.cpp ;)
00097     QString line;
00098     int length;
00099     do {
00100         length = proc.fgets(line, true);
00101     if ( length > 0 )
00102     {
00103             if ( line[length-1] != '/' )
00104                 line += '/';
00105             s_shareList->append(line);
00106             kdDebug() << "Shared dir:" << line << endl;
00107         }
00108     } while (length > -1);
00109 
00110     //kdDebug(7000) << "KFileShare: normalExit()=" << proc.normalExit() << " exitStatus()=" << proc.exitStatus() << endl;
00111     if ( proc.normalExit() )
00112         switch (proc.exitStatus())
00113         {
00114         case 0:
00115             s_authorization = Authorized;
00116             kdDebug(7000) << "KFileShare::readConfig: s_authorization = Authorized" << endl;
00117         // move while loop here
00118             return;
00119         case 1:
00120             s_authorization = UserNotAllowed;
00121             kdDebug(7000) << "KFileShare::readConfig: s_authorization = UserNotAllowed" << endl;
00122             return;
00123         default:
00124             break;
00125         }
00126     s_authorization = UserNotAllowed;
00127 }
00128 
00129 bool KFileShare::isDirectoryShared( const QString& _path )
00130 {
00131     // The app should do this on startup, but if it doesn't, let's do here.
00132     if ( s_authorization == NotInitialized )
00133         readConfig();
00134 
00135     QString path( _path );
00136     if ( path[path.length()-1] != '/' )
00137         path += '/';
00138     return s_shareList && s_shareList->contains( path );
00139 }
00140 
00141 KFileShare::Authorization KFileShare::authorization()
00142 {
00143     // The app should do this on startup, but if it doesn't, let's do here.
00144     if ( s_authorization == NotInitialized )
00145         readConfig();
00146     return s_authorization;
00147 }
00148 
00149 QString KFileShare::findExe( const char* exeName )
00150 {
00151    // /usr/sbin on Mandrake, $PATH allows flexibility for other distributions
00152    QString path = QString::fromLocal8Bit(getenv("PATH")) + QString::fromLatin1(":/usr/sbin");
00153    QString exe = KStandardDirs::findExe( exeName, path );
00154    if (exe.isEmpty())
00155        kdError() << exeName << " not found in " << path << endl;
00156    return exe;
00157 }
00158 
00159 bool KFileShare::setShared( const QString& path, bool shared )
00160 {
00161     kdDebug(7000) << "KFileShare::setShared " << path << "," << shared << endl;
00162     QString exe = KFileShare::findExe( "fileshareset" );
00163     if (!exe.isEmpty())
00164     {
00165         KProcess proc;
00166         proc << exe;
00167         if ( shared )
00168             proc << "--add";
00169         else
00170             proc << "--remove";
00171         proc << path;
00172         proc.start( KProcess::Block ); // should be ok, the perl script terminates fast
00173         bool ok = proc.normalExit() && (proc.exitStatus() == 0);
00174         kdDebug(7000) << "KFileSharePropsPlugin::setShared ok=" << ok << endl;
00175         if ( proc.normalExit() )
00176           switch( proc.exitStatus() )
00177           case 1:
00178           {
00179                   // TODO KMessageBox
00180           }
00181         return ok;
00182     }
00183     return false;
00184 }
00185 
00186 #include "kfileshare.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:30 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001