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 "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) << "KNFSShare: Could not find exports file!";
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;
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
00114 if ( completeLine.endsWith(QLatin1String("\\")) )
00115 {
00116 continuedLine = true;
00117
00118 completeLine.chop(1);
00119 continue;
00120 }
00121
00122
00123 if (completeLine.startsWith(QLatin1String("#")))
00124 {
00125 continue;
00126 }
00127
00128 QString path;
00129
00130
00131 if ( completeLine.startsWith(QLatin1String("\"")) ) {
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 {
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
00154 if ( !path.endsWith(QLatin1String("/")) )
00155 path += QLatin1Char('/');
00156
00157 sharedPaths.insert(path);
00158 }
00159
00160 f.close();
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 QString fixedPath = path;
00187 if ( path[path.length()-1] != '/' )
00188 fixedPath += '/';
00189
00190 return d->sharedPaths.contains(fixedPath);
00191 }
00192
00193 QStringList KNFSShare::sharedDirectories() const
00194 {
00195 return d->sharedPaths.values();
00196 }
00197
00198 QString KNFSShare::exportsPath() const
00199 {
00200 return d->exportsFile;
00201 }
00202
00203
00204
00205 void KNFSShare::KNFSSharePrivate::_k_slotFileChange( const QString & path )
00206 {
00207 if (path == exportsFile)
00208 readExportsFile();
00209
00210 emit q->changed();
00211 }
00212
00213 KNFSShare* KNFSShare::instance()
00214 {
00215 K_GLOBAL_STATIC(KNFSShare, _instance)
00216
00217 return _instance;
00218 }
00219
00220 #include "knfsshare.moc"
00221