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 <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 <ksimpleconfig.h>
00027
00028 #include "ksambashare.h"
00029
00030 class KSambaSharePrivate
00031 {
00032 public:
00033 KSambaSharePrivate();
00034
00035 bool readSmbConf();
00036 bool findSmbConf();
00037 bool load();
00038
00039 QDict<bool> sharedPaths;
00040 QString smbConf;
00041 };
00042
00043 KSambaSharePrivate::KSambaSharePrivate()
00044 {
00045 load();
00046 }
00047
00048
00049 #define FILESHARECONF "/etc/security/fileshare.conf"
00050
00051 bool KSambaSharePrivate::load() {
00052 if (!findSmbConf())
00053 return false;
00054
00055 return readSmbConf();
00056 }
00057
00064 bool KSambaSharePrivate::findSmbConf() {
00065 KSimpleConfig config(QString::fromLatin1(FILESHARECONF),true);
00066 smbConf = config.readEntry("SMBCONF");
00067
00068 if ( QFile::exists(smbConf) )
00069 return true;
00070
00071 if ( QFile::exists("/etc/samba/smb.conf") )
00072 smbConf = "/etc/samba/smb.conf";
00073 else
00074 if ( QFile::exists("/etc/smb.conf") )
00075 smbConf = "/etc/smb.conf";
00076 else
00077 if ( QFile::exists("/usr/local/samba/lib/smb.conf") )
00078 smbConf = "/usr/local/samba/lib/smb.conf";
00079 else
00080 if ( QFile::exists("/usr/samba/lib/smb.conf") )
00081 smbConf = "/usr/samba/lib/smb.conf";
00082 else
00083 if ( QFile::exists("/usr/lib/smb.conf") )
00084 smbConf = "/usr/lib/smb.conf";
00085 else
00086 if ( QFile::exists("/usr/local/lib/smb.conf") )
00087 smbConf = "/usr/local/lib/smb.conf";
00088 else {
00089 kdDebug(7000) << "KSambaShare: Could not found smb.conf!" << endl;
00090 return false;
00091 }
00092
00093 return true;
00094 }
00095
00096
00101 bool KSambaSharePrivate::readSmbConf() {
00102 QFile f(smbConf);
00103
00104 kdDebug(7000) << "KSambaShare::readSmbConf " << smbConf << endl;
00105
00106 if (!f.open(IO_ReadOnly)) {
00107 kdError() << "KSambaShare: Could not open " << smbConf << endl;
00108 return false;
00109 }
00110
00111 sharedPaths.clear();
00112
00113 QTextStream s(&f);
00114
00115 bool continuedLine = false;
00116 QString completeLine;
00117
00118 while (!s.eof())
00119 {
00120 QString currentLine = s.readLine().stripWhiteSpace();
00121
00122 if (continuedLine) {
00123 completeLine += currentLine;
00124 continuedLine = false;
00125 }
00126 else
00127 completeLine = currentLine;
00128
00129
00130 if ( completeLine[completeLine.length()-1] == '\\' )
00131 {
00132 continuedLine = true;
00133
00134 completeLine.truncate( completeLine.length()-1 );
00135 continue;
00136 }
00137
00138
00139 if (completeLine.isEmpty() ||
00140 '#' == completeLine[0] ||
00141 ';' == completeLine[0])
00142 {
00143 continue;
00144 }
00145
00146
00147 int i = completeLine.find('=');
00148
00149 if (i>-1)
00150 {
00151 QString name = completeLine.left(i).stripWhiteSpace().lower();
00152 QString value = completeLine.mid(i+1).stripWhiteSpace();
00153
00154 if (name == KGlobal::staticQString("path")) {
00155
00156 if ( value[0] == '"' )
00157 value.remove(0,1);
00158
00159 if ( value[value.length()-1] == '"' )
00160 value.truncate(value.length()-1);
00161
00162
00163 if ( value[value.length()-1] != '/' )
00164 value += '/';
00165
00166 bool b = true;
00167 sharedPaths.insert(value,&b);
00168 kdDebug(7000) << "KSambaShare: Found path: " << value << endl;
00169 }
00170 }
00171 }
00172
00173 f.close();
00174
00175 return true;
00176
00177 }
00178
00179 KSambaShare::KSambaShare() {
00180 d = new KSambaSharePrivate();
00181 if (QFile::exists(d->smbConf)) {
00182 KDirWatch::self()->addFile(d->smbConf);
00183 KDirWatch::self()->addFile(FILESHARECONF);
00184 connect(KDirWatch::self(), SIGNAL(dirty (const QString&)),this,
00185 SLOT(slotFileChange(const QString&)));
00186 }
00187 }
00188
00189 KSambaShare::~KSambaShare() {
00190 if (QFile::exists(d->smbConf)) {
00191 KDirWatch::self()->removeFile(d->smbConf);
00192 KDirWatch::self()->removeFile(FILESHARECONF);
00193 }
00194 delete d;
00195 }
00196
00197 QString KSambaShare::smbConfPath() const {
00198 return d->smbConf;
00199 }
00200
00201 bool KSambaShare::isDirectoryShared( const QString & path ) const {
00202 QString fixedPath = path;
00203 if ( path[path.length()-1] != '/' )
00204 fixedPath += '/';
00205
00206 return d->sharedPaths.find(fixedPath) != 0;
00207 }
00208
00209 QStringList KSambaShare::sharedDirectories() const {
00210 QStringList result;
00211 QDictIterator<bool> it(d->sharedPaths);
00212 for( ; it.current(); ++it )
00213 result << it.currentKey();
00214
00215 return result;
00216 }
00217
00218 void KSambaShare::slotFileChange( const QString & path ) {
00219 if (path == d->smbConf)
00220 d->readSmbConf();
00221 else
00222 if (path == FILESHARECONF)
00223 d->load();
00224
00225 emit changed();
00226 }
00227
00228 KSambaShare* KSambaShare::_instance = 0L;
00229 static KStaticDeleter<KSambaShare> ksdSambaShare;
00230
00231 KSambaShare* KSambaShare::instance() {
00232 if (! _instance )
00233 _instance = ksdSambaShare.setObject(_instance, new KSambaShare());
00234
00235 return _instance;
00236 }
00237
00238 #include "ksambashare.moc"
00239