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 <QtCore/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 // Default smb.conf locations 00033 // sorted by priority, most priority first 00034 static const char * const DefaultSambaConfigFilePathList[] = 00035 { 00036 "/etc/samba/smb.conf", 00037 "/etc/smb.conf", 00038 "/usr/local/etc/smb.conf", 00039 "/usr/local/samba/lib/smb.conf", 00040 "/usr/samba/lib/smb.conf", 00041 "/usr/lib/smb.conf", 00042 "/usr/local/lib/smb.conf" 00043 }; 00044 static const int DefaultSambaConfigFilePathListSize = sizeof( DefaultSambaConfigFilePathList ) / sizeof(char*); 00045 00046 class KSambaShare::KSambaSharePrivate 00047 { 00048 public: 00049 KSambaSharePrivate(KSambaShare *parent); 00050 00051 void _k_slotFileChange(const QString&); 00052 00053 bool readSmbConf(); 00054 bool findSmbConf(); 00055 bool load(); 00056 00057 KSambaShare *q; 00058 QSet<QString> sharedPaths; 00059 QString smbConf; 00060 }; 00061 00062 KSambaShare::KSambaSharePrivate::KSambaSharePrivate(KSambaShare *parent) 00063 : q(parent) 00064 { 00065 load(); 00066 } 00067 00068 00069 #define FILESHARECONF "/etc/security/fileshare.conf" 00070 00071 bool KSambaShare::KSambaSharePrivate::load() 00072 { 00073 if (!findSmbConf()) 00074 return false; 00075 00076 return readSmbConf(); 00077 } 00078 00085 bool KSambaShare::KSambaSharePrivate::findSmbConf() 00086 { 00087 KConfig config( QLatin1String( FILESHARECONF ) ); 00088 const KConfigGroup group( &config, QString() ); 00089 smbConf = group.readEntry( "SMBCONF" ); 00090 00091 if ( QFile::exists( smbConf ) ) 00092 return true; 00093 00094 bool success = false; 00095 for( int i = 0; i<DefaultSambaConfigFilePathListSize; ++i ) 00096 { 00097 const QString filePath( DefaultSambaConfigFilePathList[i] ); 00098 if( QFile::exists( filePath ) ) 00099 { 00100 smbConf = filePath; 00101 success = true; 00102 break; 00103 } 00104 } 00105 00106 if( ! success ) 00107 kDebug(7000) << "KSambaShare: Could not find smb.conf!"; 00108 00109 return success; 00110 00111 } 00112 00113 00118 bool KSambaShare::KSambaSharePrivate::readSmbConf() 00119 { 00120 QFile f(smbConf); 00121 00122 //kDebug(7000) << smbConf; 00123 00124 if (!f.open(QIODevice::ReadOnly)) { 00125 kError() << "KSambaShare: Could not open" << smbConf; 00126 return false; 00127 } 00128 00129 sharedPaths.clear(); 00130 00131 QTextStream s(&f); 00132 00133 bool continuedLine = false; // is true if the line before ended with a backslash 00134 QString completeLine; 00135 00136 while (!s.atEnd()) 00137 { 00138 QString currentLine = s.readLine().trimmed(); 00139 00140 if (continuedLine) { 00141 completeLine += currentLine; 00142 continuedLine = false; 00143 } 00144 else 00145 completeLine = currentLine; 00146 00147 // is the line continued in the next line ? 00148 if ( !completeLine.isEmpty() && completeLine[completeLine.length()-1] == '\\' ) 00149 { 00150 continuedLine = true; 00151 // remove the ending backslash 00152 completeLine.truncate( completeLine.length()-1 ); 00153 continue; 00154 } 00155 00156 // comments or empty lines 00157 if (completeLine.isEmpty() || 00158 '#' == completeLine[0] || 00159 ';' == completeLine[0]) 00160 { 00161 continue; 00162 } 00163 00164 // parameter 00165 const int i = completeLine.indexOf('='); 00166 00167 if (i>-1) 00168 { 00169 QString name = completeLine.left(i).trimmed().toLower(); 00170 QString value = completeLine.mid(i+1).trimmed(); 00171 00172 if (name == KGlobal::staticQString("path") && !value.isEmpty()) { 00173 // Handle quotation marks 00174 if ( value[0] == '"' ) 00175 value.remove(0,1); 00176 00177 if ( value[value.length()-1] == '"' ) 00178 value.truncate(value.length()-1); 00179 00180 // Normalize path 00181 if ( value[value.length()-1] != '/' ) 00182 value += '/'; 00183 00184 sharedPaths.insert(value); 00185 //kDebug(7000) << "KSambaShare: Found path: " << value; 00186 } 00187 } 00188 } 00189 00190 f.close(); 00191 00192 return true; 00193 00194 } 00195 00196 KSambaShare::KSambaShare() 00197 : d(new KSambaSharePrivate(this)) 00198 { 00199 if (QFile::exists(d->smbConf)) { 00200 KDirWatch::self()->addFile(d->smbConf); 00201 KDirWatch::self()->addFile(FILESHARECONF); 00202 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this, 00203 SLOT(_k_slotFileChange(const QString&))); 00204 } 00205 } 00206 00207 KSambaShare::~KSambaShare() 00208 { 00209 if (QFile::exists(d->smbConf)) { 00210 KDirWatch::self()->removeFile(d->smbConf); 00211 KDirWatch::self()->removeFile(FILESHARECONF); 00212 } 00213 delete d; 00214 } 00215 00216 QString KSambaShare::smbConfPath() const 00217 { 00218 return d->smbConf; 00219 } 00220 00221 bool KSambaShare::isDirectoryShared( const QString & path ) const 00222 { 00223 if(path.isEmpty()) 00224 return false; 00225 QString fixedPath = path; 00226 if ( path[path.length()-1] != '/' ) 00227 fixedPath += '/'; 00228 00229 return d->sharedPaths.contains(fixedPath); 00230 } 00231 00232 QStringList KSambaShare::sharedDirectories() const 00233 { 00234 return d->sharedPaths.values(); 00235 } 00236 00237 void KSambaShare::KSambaSharePrivate::_k_slotFileChange( const QString & path ) 00238 { 00239 if (path == smbConf) 00240 readSmbConf(); 00241 else 00242 if (path == FILESHARECONF) 00243 load(); 00244 00245 emit q->changed(); 00246 } 00247 00248 KSambaShare* KSambaShare::instance() 00249 { 00250 K_GLOBAL_STATIC(KSambaShare, _instance) 00251 return _instance; 00252 } 00253 00254 #include "ksambashare.moc" 00255
KDE 4.6 API Reference