KIO
knfsshare.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 "knfsshare.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 KNFSShare::KNFSSharePrivate 00033 { 00034 public: 00035 KNFSSharePrivate( KNFSShare *parent ); 00036 00037 void _k_slotFileChange(const QString&); 00038 00039 bool readExportsFile(); 00040 bool findExportsFile(); 00041 00042 KNFSShare *q; 00043 QSet<QString> sharedPaths; 00044 QString exportsFile; 00045 }; 00046 00047 KNFSShare::KNFSSharePrivate::KNFSSharePrivate( KNFSShare *parent ) 00048 : q(parent) 00049 { 00050 if (findExportsFile()) 00051 readExportsFile(); 00052 } 00053 00060 bool KNFSShare::KNFSSharePrivate::findExportsFile() 00061 { 00062 KConfig knfsshare("knfsshare"); 00063 KConfigGroup config(&knfsshare, "General"); 00064 exportsFile = config.readPathEntry("exportsFile", QString()); 00065 00066 if ( QFile::exists(exportsFile) ) 00067 return true; 00068 00069 if ( QFile::exists("/etc/exports") ) 00070 exportsFile = "/etc/exports"; 00071 else { 00072 kDebug(7000) << "Could not find exports file! /etc/exports doesn't exist. Configure it in share/config/knfsshare, [General], exportsFile=...."; 00073 return false; 00074 } 00075 00076 config.writeEntry("exportsFile",exportsFile); 00077 return true; 00078 } 00079 00084 bool KNFSShare::KNFSSharePrivate::readExportsFile() 00085 { 00086 QFile f(exportsFile); 00087 00088 //kDebug(7000) << exportsFile; 00089 00090 if (!f.open(QIODevice::ReadOnly)) { 00091 kError() << "KNFSShare: Could not open" << exportsFile; 00092 return false; 00093 } 00094 00095 sharedPaths.clear(); 00096 00097 QTextStream s( &f ); 00098 00099 bool continuedLine = false; // is true if the line before ended with a backslash 00100 QString completeLine; 00101 00102 while ( !s.atEnd() ) 00103 { 00104 QString currentLine = s.readLine().trimmed(); 00105 00106 if (continuedLine) { 00107 completeLine += currentLine; 00108 continuedLine = false; 00109 } 00110 else 00111 completeLine = currentLine; 00112 00113 // is the line continued in the next line ? 00114 if ( completeLine.endsWith(QLatin1Char('\\')) ) 00115 { 00116 continuedLine = true; 00117 // remove the ending backslash 00118 completeLine.chop(1); 00119 continue; 00120 } 00121 00122 // comments or empty lines 00123 if (completeLine.startsWith(QLatin1Char('#')) || completeLine.isEmpty()) 00124 { 00125 continue; 00126 } 00127 00128 QString path; 00129 00130 // Handle quotation marks 00131 if ( completeLine[0] == QLatin1Char('\"') ) { 00132 int i = completeLine.indexOf(QLatin1Char('"'), 1); 00133 if (i == -1) { 00134 kError() << "KNFSShare: Parse error: Missing quotation mark:" << completeLine; 00135 continue; 00136 } 00137 path = completeLine.mid(1,i-1); 00138 00139 } else { // no quotation marks 00140 int i = completeLine.indexOf(QLatin1Char(' ')); 00141 if (i == -1) 00142 i = completeLine.indexOf(QLatin1Char('\t')); 00143 00144 if (i == -1) 00145 path = completeLine; 00146 else 00147 path = completeLine.left(i); 00148 00149 } 00150 00151 //kDebug(7000) << "KNFSShare: Found path: " << path; 00152 00153 if (!path.isEmpty()) { 00154 // normalize path 00155 if ( !path.endsWith(QLatin1Char('/')) ) 00156 path += QLatin1Char('/'); 00157 00158 sharedPaths.insert(path); 00159 } 00160 } 00161 00162 return true; 00163 } 00164 00165 KNFSShare::KNFSShare() 00166 : d(new KNFSSharePrivate(this)) 00167 { 00168 if (QFile::exists(d->exportsFile)) { 00169 KDirWatch::self()->addFile(d->exportsFile); 00170 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this, 00171 SLOT(_k_slotFileChange(const QString&))); 00172 } 00173 } 00174 00175 KNFSShare::~KNFSShare() 00176 { 00177 if (QFile::exists(d->exportsFile)) { 00178 KDirWatch::self()->removeFile(d->exportsFile); 00179 } 00180 delete d; 00181 } 00182 00183 00184 bool KNFSShare::isDirectoryShared( const QString & path ) const 00185 { 00186 if( path.isEmpty()) 00187 return false; 00188 QString fixedPath = path; 00189 if ( path[path.length()-1] != '/' ) 00190 fixedPath += '/'; 00191 00192 return d->sharedPaths.contains(fixedPath); 00193 } 00194 00195 QStringList KNFSShare::sharedDirectories() const 00196 { 00197 return d->sharedPaths.values(); 00198 } 00199 00200 QString KNFSShare::exportsPath() const 00201 { 00202 return d->exportsFile; 00203 } 00204 00205 00206 00207 void KNFSShare::KNFSSharePrivate::_k_slotFileChange( const QString & path ) 00208 { 00209 if (path == exportsFile) 00210 readExportsFile(); 00211 00212 emit q->changed(); 00213 } 00214 00215 KNFSShare* KNFSShare::instance() 00216 { 00217 K_GLOBAL_STATIC(KNFSShare, _instance) 00218 00219 return _instance; 00220 } 00221 00222 #include "knfsshare.moc" 00223
KDE 4.6 API Reference