kfileshare.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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()
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
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
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
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
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
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
00144 if ( s_authorization == NotInitialized )
00145 readConfig();
00146 return s_authorization;
00147 }
00148
00149 QString KFileShare::findExe( const char* exeName )
00150 {
00151
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 );
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
00180 }
00181 return ok;
00182 }
00183 return false;
00184 }
00185
00186 #include "kfileshare.moc"
This file is part of the documentation for kdelibs Version 3.1.0.