kio Library: ksambashare.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# ksambashare.cpp

    00001 /* This file is part of the KDE project
    00002    Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 #include <qdict.h>
    00020 #include <qfile.h>
    00021 #include <qtextstream.h>
    00022
    00023 #include <kdirwatch.h>
    00024 #include <kstaticdeleter.h>
    00025 #include <kdebug.h>
    00026 #include <ksimpleconfig.h>
    00027
    00028 #include "ksambashare.h"
    00029
    00030 class KSambaSharePrivate
    00031 {
    00032 public:
    00033   KSambaSharePrivate();
    00034
    00035   bool readSmbConf();
    00036   bool findSmbConf();
    00037   bool load();
    00038
    00039   QDict<bool> sharedPaths;
    00040   QString smbConf;
    00041 };
    00042
    00043 KSambaSharePrivate::KSambaSharePrivate()
    00044 {
    00045     load();
    00046 }
    00047
    00048
    00049 #define FILESHARECONF "/etc/security/fileshare.conf"
    00050
    00051 bool KSambaSharePrivate::load() {
    00052   if (!findSmbConf())
    00053       return false;
    00054
    00055   return readSmbConf();
    00056 }
    00057
    00064 bool KSambaSharePrivate::findSmbConf() {
    00065   KSimpleConfig config(QString::fromLatin1(FILESHARECONF),true);
    00066   smbConf = config.readEntry("SMBCONF");
    00067
    00068   if ( QFile::exists(smbConf) )
    00069     return true;
    00070
    00071   if ( QFile::exists("/etc/samba/smb.conf") )
    00072     smbConf = "/etc/samba/smb.conf";
    00073   else
    00074   if ( QFile::exists("/etc/smb.conf") )
    00075     smbConf = "/etc/smb.conf";
    00076   else
    00077   if ( QFile::exists("/usr/local/samba/lib/smb.conf") )
    00078     smbConf = "/usr/local/samba/lib/smb.conf";
    00079   else
    00080   if ( QFile::exists("/usr/samba/lib/smb.conf") )
    00081     smbConf = "/usr/samba/lib/smb.conf";
    00082   else
    00083   if ( QFile::exists("/usr/lib/smb.conf") )
    00084     smbConf = "/usr/lib/smb.conf";
    00085   else
    00086   if ( QFile::exists("/usr/local/lib/smb.conf") )
    00087     smbConf = "/usr/local/lib/smb.conf";
    00088   else {
    00089     kdDebug(7000) << "KSambaShare: Could not found smb.conf!" << endl;
    00090     return false;
    00091   }
    00092
    00093   return true;
    00094 }
    00095
    00096
    00101 bool KSambaSharePrivate::readSmbConf() {
    00102   QFile f(smbConf);
    00103
    00104   kdDebug(7000) << "KSambaShare::readSmbConf " << smbConf << endl;
    00105
    00106   if (!f.open(IO_ReadOnly)) {
    00107     kdError() << "KSambaShare: Could not open " << smbConf << endl;
    00108     return false;
    00109   }
    00110
    00111   sharedPaths.clear();
    00112
    00113   QTextStream s(&f);
    00114
    00115   bool continuedLine = false; // is true if the line before ended with a backslash
    00116   QString completeLine;
    00117
    00118   while (!s.eof())
    00119   {
    00120     QString currentLine = s.readLine().stripWhiteSpace();
    00121
    00122     if (continuedLine) {
    00123       completeLine += currentLine;
    00124       continuedLine = false;
    00125     }
    00126     else
    00127       completeLine = currentLine;
    00128
    00129     // is the line continued in the next line ?
    00130     if ( completeLine[completeLine.length()-1] == '\\' )
    00131     {
    00132       continuedLine = true;
    00133       // remove the ending backslash
    00134       completeLine.truncate( completeLine.length()-1 );
    00135       continue;
    00136     }
    00137
    00138     // comments or empty lines
    00139     if (completeLine.isEmpty() ||
    00140         '#' == completeLine[0] ||
    00141         ';' == completeLine[0])
    00142     {
    00143       continue;
    00144     }
    00145
    00146     // parameter
    00147     int i = completeLine.find('=');
    00148
    00149     if (i>-1)
    00150     {
    00151       QString name = completeLine.left(i).stripWhiteSpace().lower();
    00152       QString value = completeLine.mid(i+1).stripWhiteSpace();
    00153
    00154       if (name == KGlobal::staticQString("path")) {
    00155         // Handle quotation marks
    00156         if ( value[0] == '"' )
    00157           value.remove(0,1);
    00158
    00159         if ( value[value.length()-1] == '"' )
    00160           value.truncate(value.length()-1);
    00161
    00162         // Normalize path
    00163         if ( value[value.length()-1] != '/' )
    00164              value += '/';
    00165
    00166         bool b = true;
    00167         sharedPaths.insert(value,&b);
    00168         kdDebug(7000) << "KSambaShare: Found path: " << value << endl;
    00169       }
    00170     }
    00171   }
    00172
    00173   f.close();
    00174
    00175   return true;
    00176
    00177 }
    00178
    00179 KSambaShare::KSambaShare() {
    00180   d = new KSambaSharePrivate();
    00181   if (QFile::exists(d->smbConf)) {
    00182     KDirWatch::self()->addFile(d->smbConf);
    00183     KDirWatch::self()->addFile(FILESHARECONF);
    00184     connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this,
    00185             SLOT(slotFileChange(const QString&)));
    00186   }
    00187 }
    00188
    00189 KSambaShare::~KSambaShare() {
    00190   delete d;
    00191 }
    00192
    00193 QString KSambaShare::smbConfPath() const {
    00194   return d->smbConf;
    00195 }
    00196
    00197 bool KSambaShare::isDirectoryShared( const QString & path ) const {
    00198   QString fixedPath = path;
    00199   if ( path[path.length()-1] != '/' )
    00200        fixedPath += '/';
    00201
    00202   return d->sharedPaths.find(fixedPath) > 0;
    00203 }
    00204
    00205 QStringList KSambaShare::sharedDirectories() const {
    00206   QStringList result;
    00207   QDictIterator<bool> it(d->sharedPaths);
    00208   for( ; it.current(); ++it )
    00209       result << it.currentKey();
    00210
    00211   return result;
    00212 }
    00213
    00214 void KSambaShare::slotFileChange( const QString & path ) {
    00215   if (path == d->smbConf)
    00216      d->readSmbConf();
    00217   else
    00218   if (path == FILESHARECONF)
    00219      d->load();
    00220
    00221   emit changed();
    00222 }
    00223
    00224 KSambaShare* KSambaShare::_instance = 0L;
    00225 static KStaticDeleter<KSambaShare> ksdSambaShare;
    00226
    00227 KSambaShare* KSambaShare::instance() {
    00228   if (! _instance )
    00229       _instance = ksdSambaShare.setObject(_instance, new KSambaShare());
    00230
    00231   return _instance;
    00232 }
    00233
    00234 #include "ksambashare.moc"
    00235

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:03 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003