kio
knfsshare.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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;
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
00107 if ( completeLine[completeLine.length()-1] == '\\' )
00108 {
00109 continuedLine = true;
00110
00111 completeLine.truncate( completeLine.length()-1 );
00112 continue;
00113 }
00114
00115
00116 if (completeLine.isEmpty() ||
00117 '#' == completeLine[0])
00118 {
00119 continue;
00120 }
00121
00122 QString path;
00123
00124
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 {
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
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 if (QFile::exists(d->exportsFile)) {
00172 KDirWatch::self()->removeFile(d->exportsFile);
00173 }
00174 delete d;
00175 }
00176
00177
00178 bool KNFSShare::isDirectoryShared( const QString & path ) const {
00179 QString fixedPath = path;
00180 if ( path[path.length()-1] != '/' )
00181 fixedPath += '/';
00182
00183 return d->sharedPaths.find(fixedPath) != 0;
00184 }
00185
00186 QStringList KNFSShare::sharedDirectories() const {
00187 QStringList result;
00188 QDictIterator<bool> it(d->sharedPaths);
00189 for( ; it.current(); ++it )
00190 result << it.currentKey();
00191
00192 return result;
00193 }
00194
00195 QString KNFSShare::exportsPath() const {
00196 return d->exportsFile;
00197 }
00198
00199
00200
00201 void KNFSShare::slotFileChange( const QString & path ) {
00202 if (path == d->exportsFile)
00203 d->readExportsFile();
00204
00205 emit changed();
00206 }
00207
00208 KNFSShare* KNFSShare::_instance = 0L;
00209 static KStaticDeleter<KNFSShare> ksdNFSShare;
00210
00211 KNFSShare* KNFSShare::instance() {
00212 if (! _instance )
00213 _instance = ksdNFSShare.setObject(_instance, new KNFSShare());
00214
00215 return _instance;
00216 }
00217
00218 #include "knfsshare.moc"
00219