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., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include <tqdict.h> 00020 #include <tqfile.h> 00021 #include <tqtextstream.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 TQDict<bool> sharedPaths; 00040 TQString 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(TQString::fromLatin1(FILESHARECONF),true); 00066 smbConf = config.readEntry("SMBCONF"); 00067 00068 if ( TQFile::exists(smbConf) ) 00069 return true; 00070 00071 if ( TQFile::exists("/etc/samba/smb.conf") ) 00072 smbConf = "/etc/samba/smb.conf"; 00073 else 00074 if ( TQFile::exists("/etc/smb.conf") ) 00075 smbConf = "/etc/smb.conf"; 00076 else 00077 if ( TQFile::exists("/usr/local/samba/lib/smb.conf") ) 00078 smbConf = "/usr/local/samba/lib/smb.conf"; 00079 else 00080 if ( TQFile::exists("/usr/samba/lib/smb.conf") ) 00081 smbConf = "/usr/samba/lib/smb.conf"; 00082 else 00083 if ( TQFile::exists("/usr/lib/smb.conf") ) 00084 smbConf = "/usr/lib/smb.conf"; 00085 else 00086 if ( TQFile::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 TQFile 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 TQTextStream s(&f); 00114 00115 bool continuedLine = false; // is true if the line before ended with a backslash 00116 TQString completeLine; 00117 00118 while (!s.eof()) 00119 { 00120 TQString 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 TQString name = completeLine.left(i).stripWhiteSpace().lower(); 00152 TQString value = completeLine.mid(i+1).stripWhiteSpace(); 00153 00154 if (name == TDEGlobal::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 (TQFile::exists(d->smbConf)) { 00182 KDirWatch::self()->addFile(d->smbConf); 00183 KDirWatch::self()->addFile(FILESHARECONF); 00184 connect(KDirWatch::self(), TQT_SIGNAL(dirty (const TQString&)),this, 00185 TQT_SLOT(slotFileChange(const TQString&))); 00186 } 00187 } 00188 00189 KSambaShare::~KSambaShare() { 00190 if (TQFile::exists(d->smbConf)) { 00191 KDirWatch::self()->removeFile(d->smbConf); 00192 KDirWatch::self()->removeFile(FILESHARECONF); 00193 } 00194 delete d; 00195 } 00196 00197 TQString KSambaShare::smbConfPath() const { 00198 return d->smbConf; 00199 } 00200 00201 bool KSambaShare::isDirectoryShared( const TQString & path ) const { 00202 TQString fixedPath = path; 00203 if ( path[path.length()-1] != '/' ) 00204 fixedPath += '/'; 00205 00206 return d->sharedPaths.find(fixedPath) != 0; 00207 } 00208 00209 TQStringList KSambaShare::sharedDirectories() const { 00210 TQStringList result; 00211 TQDictIterator<bool> it(d->sharedPaths); 00212 for( ; it.current(); ++it ) 00213 result << it.currentKey(); 00214 00215 return result; 00216 } 00217 00218 void KSambaShare::slotFileChange( const TQString & path ) { 00219 if (path == d->smbConf) 00220 d->readSmbConf(); 00221 else 00222 if (path == FILESHARECONF) 00223 d->load(); 00224 00225 emit changed(); 00226 } 00227 00228 KSambaShare* KSambaShare::_instance = 0L; 00229 static KStaticDeleter<KSambaShare> ksdSambaShare; 00230 00231 KSambaShare* KSambaShare::instance() { 00232 if (! _instance ) 00233 _instance = ksdSambaShare.setObject(_instance, new KSambaShare()); 00234 00235 return _instance; 00236 } 00237 00238 #include "ksambashare.moc" 00239