• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

ksambashare.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "ksambashare.h"
00020 
00021 #include <QSet>
00022 #include <QtCore/QFile>
00023 #include <QtCore/QMutableStringListIterator>
00024 #include <QtCore/QTextIStream>
00025 
00026 #include <kdirwatch.h>
00027 #include <kdebug.h>
00028 #include <kconfig.h>
00029 #include <kconfiggroup.h>
00030 #include <kglobal.h>
00031 
00032 class KSambaShare::KSambaSharePrivate
00033 {
00034 public:
00035   KSambaSharePrivate(KSambaShare *parent);
00036 
00037   void _k_slotFileChange(const QString&);
00038 
00039   bool readSmbConf();
00040   bool findSmbConf();
00041   bool load();
00042 
00043   KSambaShare *q;
00044   QSet<QString> sharedPaths;
00045   QString smbConf;
00046 };
00047 
00048 KSambaShare::KSambaSharePrivate::KSambaSharePrivate(KSambaShare *parent)
00049     : q(parent)
00050 {
00051     load();
00052 }
00053 
00054 
00055 #define FILESHARECONF "/etc/security/fileshare.conf"
00056 
00057 bool KSambaShare::KSambaSharePrivate::load()
00058 {
00059   if (!findSmbConf())
00060       return false;
00061 
00062   return readSmbConf();
00063 }
00064 
00071 bool KSambaShare::KSambaSharePrivate::findSmbConf()
00072 {
00073   KConfig config(QLatin1String(FILESHARECONF));
00074   const KConfigGroup group(&config, QString());
00075   smbConf = group.readEntry("SMBCONF");
00076 
00077   if ( QFile::exists(smbConf) )
00078     return true;
00079 
00080   if ( QFile::exists("/etc/samba/smb.conf") )
00081     smbConf = "/etc/samba/smb.conf";
00082   else
00083   if ( QFile::exists("/etc/smb.conf") )
00084     smbConf = "/etc/smb.conf";
00085   else
00086   if ( QFile::exists("/usr/local/samba/lib/smb.conf") )
00087     smbConf = "/usr/local/samba/lib/smb.conf";
00088   else
00089   if ( QFile::exists("/usr/samba/lib/smb.conf") )
00090     smbConf = "/usr/samba/lib/smb.conf";
00091   else
00092   if ( QFile::exists("/usr/lib/smb.conf") )
00093     smbConf = "/usr/lib/smb.conf";
00094   else
00095   if ( QFile::exists("/usr/local/lib/smb.conf") )
00096     smbConf = "/usr/local/lib/smb.conf";
00097   else {
00098     kDebug(7000) << "KSambaShare: Could not found smb.conf!";
00099     return false;
00100   }
00101 
00102   return true;
00103 }
00104 
00105 
00110 bool KSambaShare::KSambaSharePrivate::readSmbConf()
00111 {
00112   QFile f(smbConf);
00113 
00114   kDebug(7000) << smbConf;
00115 
00116   if (!f.open(QIODevice::ReadOnly)) {
00117     kError() << "KSambaShare: Could not open" << smbConf;
00118     return false;
00119   }
00120 
00121   sharedPaths.clear();
00122 
00123   QTextStream s(&f);
00124 
00125   bool continuedLine = false; // is true if the line before ended with a backslash
00126   QString completeLine;
00127 
00128   while (!s.atEnd())
00129   {
00130     QString currentLine = s.readLine().trimmed();
00131 
00132     if (continuedLine) {
00133       completeLine += currentLine;
00134       continuedLine = false;
00135     }
00136     else
00137       completeLine = currentLine;
00138 
00139     // is the line continued in the next line ?
00140     if ( !completeLine.isEmpty() && completeLine[completeLine.length()-1] == '\\' )
00141     {
00142       continuedLine = true;
00143       // remove the ending backslash
00144       completeLine.truncate( completeLine.length()-1 );
00145       continue;
00146     }
00147 
00148     // comments or empty lines
00149     if (completeLine.isEmpty() ||
00150         '#' == completeLine[0] ||
00151         ';' == completeLine[0])
00152     {
00153       continue;
00154     }
00155 
00156     // parameter
00157     const int i = completeLine.indexOf('=');
00158 
00159     if (i>-1)
00160     {
00161       QString name = completeLine.left(i).trimmed().toLower();
00162       QString value = completeLine.mid(i+1).trimmed();
00163 
00164       if (name == KGlobal::staticQString("path")) {
00165         // Handle quotation marks
00166         if ( value[0] == '"' )
00167           value.remove(0,1);
00168 
00169         if ( value[value.length()-1] == '"' )
00170           value.truncate(value.length()-1);
00171 
00172         // Normalize path
00173         if ( value[value.length()-1] != '/' )
00174              value += '/';
00175 
00176         sharedPaths.insert(value);
00177         kDebug(7000) << "KSambaShare: Found path: " << value;
00178       }
00179     }
00180   }
00181 
00182   f.close();
00183 
00184   return true;
00185 
00186 }
00187 
00188 KSambaShare::KSambaShare()
00189   : d(new KSambaSharePrivate(this))
00190 {
00191   if (QFile::exists(d->smbConf)) {
00192     KDirWatch::self()->addFile(d->smbConf);
00193     KDirWatch::self()->addFile(FILESHARECONF);
00194     connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this,
00195             SLOT(_k_slotFileChange(const QString&)));
00196   }
00197 }
00198 
00199 KSambaShare::~KSambaShare()
00200 {
00201   if (QFile::exists(d->smbConf)) {
00202         KDirWatch::self()->removeFile(d->smbConf);
00203         KDirWatch::self()->removeFile(FILESHARECONF);
00204   }
00205   delete d;
00206 }
00207 
00208 QString KSambaShare::smbConfPath() const
00209 {
00210   return d->smbConf;
00211 }
00212 
00213 bool KSambaShare::isDirectoryShared( const QString & path ) const
00214 {
00215   QString fixedPath = path;
00216   if ( path[path.length()-1] != '/' )
00217        fixedPath += '/';
00218 
00219   return d->sharedPaths.contains(fixedPath);
00220 }
00221 
00222 QStringList KSambaShare::sharedDirectories() const
00223 {
00224   return d->sharedPaths.values();
00225 }
00226 
00227 void KSambaShare::KSambaSharePrivate::_k_slotFileChange( const QString & path )
00228 {
00229   if (path == smbConf)
00230      readSmbConf();
00231   else
00232   if (path == FILESHARECONF)
00233      load();
00234 
00235   emit q->changed();
00236 }
00237 
00238 KSambaShare* KSambaShare::instance()
00239 {
00240   K_GLOBAL_STATIC(KSambaShare, _instance)
00241   return _instance;
00242 }
00243 
00244 #include "ksambashare.moc"
00245 

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal