KIO
ksambashare.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 "ksambashare.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 KSambaShare::KSambaSharePrivate
00033 {
00034 public:
00035 KSambaSharePrivate(KSambaShare *parent);
00036
00037 void _k_slotFileChange(const QString&);
00038
00039 bool readSmbConf();
00040 bool findSmbConf();
00041 bool load();
00042
00043 KSambaShare *q;
00044 QSet<QString> sharedPaths;
00045 QString smbConf;
00046 };
00047
00048 KSambaShare::KSambaSharePrivate::KSambaSharePrivate(KSambaShare *parent)
00049 : q(parent)
00050 {
00051 load();
00052 }
00053
00054
00055 #define FILESHARECONF "/etc/security/fileshare.conf"
00056
00057 bool KSambaShare::KSambaSharePrivate::load()
00058 {
00059 if (!findSmbConf())
00060 return false;
00061
00062 return readSmbConf();
00063 }
00064
00071 bool KSambaShare::KSambaSharePrivate::findSmbConf()
00072 {
00073 KConfig config(QLatin1String(FILESHARECONF));
00074 const KConfigGroup group(&config, QString());
00075 smbConf = group.readEntry("SMBCONF");
00076
00077 if ( QFile::exists(smbConf) )
00078 return true;
00079
00080 if ( QFile::exists("/etc/samba/smb.conf") )
00081 smbConf = "/etc/samba/smb.conf";
00082 else
00083 if ( QFile::exists("/etc/smb.conf") )
00084 smbConf = "/etc/smb.conf";
00085 else
00086 if ( QFile::exists("/usr/local/samba/lib/smb.conf") )
00087 smbConf = "/usr/local/samba/lib/smb.conf";
00088 else
00089 if ( QFile::exists("/usr/samba/lib/smb.conf") )
00090 smbConf = "/usr/samba/lib/smb.conf";
00091 else
00092 if ( QFile::exists("/usr/lib/smb.conf") )
00093 smbConf = "/usr/lib/smb.conf";
00094 else
00095 if ( QFile::exists("/usr/local/lib/smb.conf") )
00096 smbConf = "/usr/local/lib/smb.conf";
00097 else {
00098 kDebug(7000) << "KSambaShare: Could not found smb.conf!";
00099 return false;
00100 }
00101
00102 return true;
00103 }
00104
00105
00110 bool KSambaShare::KSambaSharePrivate::readSmbConf()
00111 {
00112 QFile f(smbConf);
00113
00114
00115
00116 if (!f.open(QIODevice::ReadOnly)) {
00117 kError() << "KSambaShare: Could not open" << smbConf;
00118 return false;
00119 }
00120
00121 sharedPaths.clear();
00122
00123 QTextStream s(&f);
00124
00125 bool continuedLine = false;
00126 QString completeLine;
00127
00128 while (!s.atEnd())
00129 {
00130 QString currentLine = s.readLine().trimmed();
00131
00132 if (continuedLine) {
00133 completeLine += currentLine;
00134 continuedLine = false;
00135 }
00136 else
00137 completeLine = currentLine;
00138
00139
00140 if ( !completeLine.isEmpty() && completeLine[completeLine.length()-1] == '\\' )
00141 {
00142 continuedLine = true;
00143
00144 completeLine.truncate( completeLine.length()-1 );
00145 continue;
00146 }
00147
00148
00149 if (completeLine.isEmpty() ||
00150 '#' == completeLine[0] ||
00151 ';' == completeLine[0])
00152 {
00153 continue;
00154 }
00155
00156
00157 const int i = completeLine.indexOf('=');
00158
00159 if (i>-1)
00160 {
00161 QString name = completeLine.left(i).trimmed().toLower();
00162 QString value = completeLine.mid(i+1).trimmed();
00163
00164 if (name == KGlobal::staticQString("path")) {
00165
00166 if ( value[0] == '"' )
00167 value.remove(0,1);
00168
00169 if ( value[value.length()-1] == '"' )
00170 value.truncate(value.length()-1);
00171
00172
00173 if ( value[value.length()-1] != '/' )
00174 value += '/';
00175
00176 sharedPaths.insert(value);
00177
00178 }
00179 }
00180 }
00181
00182 f.close();
00183
00184 return true;
00185
00186 }
00187
00188 KSambaShare::KSambaShare()
00189 : d(new KSambaSharePrivate(this))
00190 {
00191 if (QFile::exists(d->smbConf)) {
00192 KDirWatch::self()->addFile(d->smbConf);
00193 KDirWatch::self()->addFile(FILESHARECONF);
00194 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this,
00195 SLOT(_k_slotFileChange(const QString&)));
00196 }
00197 }
00198
00199 KSambaShare::~KSambaShare()
00200 {
00201 if (QFile::exists(d->smbConf)) {
00202 KDirWatch::self()->removeFile(d->smbConf);
00203 KDirWatch::self()->removeFile(FILESHARECONF);
00204 }
00205 delete d;
00206 }
00207
00208 QString KSambaShare::smbConfPath() const
00209 {
00210 return d->smbConf;
00211 }
00212
00213 bool KSambaShare::isDirectoryShared( const QString & path ) const
00214 {
00215 if(path.isEmpty())
00216 return false;
00217 QString fixedPath = path;
00218 if ( path[path.length()-1] != '/' )
00219 fixedPath += '/';
00220
00221 return d->sharedPaths.contains(fixedPath);
00222 }
00223
00224 QStringList KSambaShare::sharedDirectories() const
00225 {
00226 return d->sharedPaths.values();
00227 }
00228
00229 void KSambaShare::KSambaSharePrivate::_k_slotFileChange( const QString & path )
00230 {
00231 if (path == smbConf)
00232 readSmbConf();
00233 else
00234 if (path == FILESHARECONF)
00235 load();
00236
00237 emit q->changed();
00238 }
00239
00240 KSambaShare* KSambaShare::instance()
00241 {
00242 K_GLOBAL_STATIC(KSambaShare, _instance)
00243 return _instance;
00244 }
00245
00246 #include "ksambashare.moc"
00247