kio Library API Documentation

knfsshare.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 <kconfig.h> 00027 00028 #include "knfsshare.h" 00029 00030 class KNFSSharePrivate 00031 { 00032 public: 00033 KNFSSharePrivate(); 00034 00035 bool readExportsFile(); 00036 bool findExportsFile(); 00037 00038 QDict<bool> sharedPaths; 00039 QString exportsFile; 00040 }; 00041 00042 KNFSSharePrivate::KNFSSharePrivate() 00043 { 00044 if (findExportsFile()) 00045 readExportsFile(); 00046 } 00047 00054 bool KNFSSharePrivate::findExportsFile() { 00055 KConfig config("knfsshare"); 00056 config.setGroup("General"); 00057 exportsFile = config.readPathEntry("exportsFile"); 00058 00059 if ( QFile::exists(exportsFile) ) 00060 return true; 00061 00062 if ( QFile::exists("/etc/exports") ) 00063 exportsFile = "/etc/exports"; 00064 else { 00065 kdDebug(7000) << "KNFSShare: Could not found exports file!" << endl; 00066 return false; 00067 } 00068 00069 config.writeEntry("exportsFile",exportsFile); 00070 return true; 00071 } 00072 00077 bool KNFSSharePrivate::readExportsFile() { 00078 QFile f(exportsFile); 00079 00080 kdDebug(7000) << "KNFSShare::readExportsFile " << exportsFile << endl; 00081 00082 if (!f.open(IO_ReadOnly)) { 00083 kdError() << "KNFSShare: Could not open " << exportsFile << endl; 00084 return false; 00085 } 00086 00087 00088 sharedPaths.clear(); 00089 00090 QTextStream s( &f ); 00091 00092 bool continuedLine = false; // is true if the line before ended with a backslash 00093 QString completeLine; 00094 00095 while ( !s.eof() ) 00096 { 00097 QString currentLine = s.readLine().stripWhiteSpace(); 00098 00099 if (continuedLine) { 00100 completeLine += currentLine; 00101 continuedLine = false; 00102 } 00103 else 00104 completeLine = currentLine; 00105 00106 // is the line continued in the next line ? 00107 if ( completeLine[completeLine.length()-1] == '\\' ) 00108 { 00109 continuedLine = true; 00110 // remove the ending backslash 00111 completeLine.truncate( completeLine.length()-1 ); 00112 continue; 00113 } 00114 00115 // comments or empty lines 00116 if (completeLine.isEmpty() || 00117 '#' == completeLine[0]) 00118 { 00119 continue; 00120 } 00121 00122 QString path; 00123 00124 // Handle quotation marks 00125 if ( completeLine[0] == '"' ) { 00126 int i = completeLine.find('"',1); 00127 if (i == -1) { 00128 kdError() << "KNFSShare: Parse error: Missing quotation mark: " << completeLine << endl; 00129 continue; 00130 } 00131 path = completeLine.mid(1,i-1); 00132 00133 } else { // no quotation marks 00134 int i = completeLine.find(' '); 00135 if (i == -1) 00136 i = completeLine.find('\t'); 00137 00138 if (i == -1) 00139 path = completeLine; 00140 else 00141 path = completeLine.left(i); 00142 00143 } 00144 00145 kdDebug(7000) << "KNFSShare: Found path: " << path << endl; 00146 00147 // normalize path 00148 if ( path[path.length()-1] != '/' ) 00149 path += '/'; 00150 00151 bool b = true; 00152 sharedPaths.insert(path,&b); 00153 } 00154 00155 f.close(); 00156 00157 return true; 00158 00159 } 00160 00161 KNFSShare::KNFSShare() { 00162 d = new KNFSSharePrivate(); 00163 if (QFile::exists(d->exportsFile)) { 00164 KDirWatch::self()->addFile(d->exportsFile); 00165 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this, 00166 SLOT(slotFileChange(const QString&))); 00167 } 00168 } 00169 00170 KNFSShare::~KNFSShare() { 00171 delete d; 00172 } 00173 00174 00175 bool KNFSShare::isDirectoryShared( const QString & path ) const { 00176 QString fixedPath = path; 00177 if ( path[path.length()-1] != '/' ) 00178 fixedPath += '/'; 00179 00180 return d->sharedPaths.find(fixedPath) > 0; 00181 } 00182 00183 QStringList KNFSShare::sharedDirectories() const { 00184 QStringList result; 00185 QDictIterator<bool> it(d->sharedPaths); 00186 for( ; it.current(); ++it ) 00187 result << it.currentKey(); 00188 00189 return result; 00190 } 00191 00192 QString KNFSShare::exportsPath() const { 00193 return d->exportsFile; 00194 } 00195 00196 00197 00198 void KNFSShare::slotFileChange( const QString & path ) { 00199 if (path == d->exportsFile) 00200 d->readExportsFile(); 00201 00202 emit changed(); 00203 } 00204 00205 KNFSShare* KNFSShare::_instance = 0L; 00206 static KStaticDeleter<KNFSShare> ksdNFSShare; 00207 00208 KNFSShare* KNFSShare::instance() { 00209 if (! _instance ) 00210 _instance = ksdNFSShare.setObject(_instance, new KNFSShare()); 00211 00212 return _instance; 00213 } 00214 00215 #include "knfsshare.moc" 00216
KDE Logo
This file is part of the documentation for kio Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 23:09:16 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003