• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

knfsshare.cpp

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 <tqdict.h>
00020 #include <tqfile.h>
00021 #include <tqtextstream.h>
00022 
00023 #include <kdirwatch.h>
00024 #include <kstaticdeleter.h>
00025 #include <kdebug.h>
00026 #include <tdeconfig.h>
00027 
00028 #include "knfsshare.h"
00029 
00030 class KNFSSharePrivate
00031 {
00032 public:
00033   KNFSSharePrivate();
00034   
00035   bool readExportsFile();
00036   bool findExportsFile();
00037   
00038   TQDict<bool> sharedPaths;
00039   TQString exportsFile;
00040 };
00041 
00042 KNFSSharePrivate::KNFSSharePrivate() 
00043 {
00044   if (findExportsFile())
00045       readExportsFile();
00046 }  
00047 
00054 bool KNFSSharePrivate::findExportsFile() {
00055   TDEConfig config("knfsshare");
00056   config.setGroup("General");
00057   exportsFile = config.readPathEntry("exportsFile");
00058 
00059   if ( TQFile::exists(exportsFile) )
00060     return true;
00061 
00062   if ( TQFile::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   TQFile 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   TQTextStream s( &f );
00091   
00092   bool continuedLine = false; // is true if the line before ended with a backslash
00093   TQString completeLine;
00094   
00095   while ( !s.eof() )
00096   {
00097     TQString currentLine = s.readLine().stripWhiteSpace();
00098 
00099     if (continuedLine) {
00100       completeLine += currentLine;
00101       continuedLine = false;
00102     }      
00103     else
00104       completeLine = currentLine;
00105 
00106     // is the line continued in the next line ?
00107     if ( completeLine[completeLine.length()-1] == '\\' )
00108     {
00109       continuedLine = true;
00110       // remove the ending backslash
00111       completeLine.truncate( completeLine.length()-1 ); 
00112       continue;
00113     }
00114     
00115     // comments or empty lines
00116     if (completeLine.isEmpty() ||
00117         '#' == completeLine[0])
00118     {
00119       continue;
00120     }
00121 
00122     TQString path;
00123     
00124     // Handle quotation marks
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 { // no quotation marks
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     // normalize path
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 (TQFile::exists(d->exportsFile)) {
00164     KDirWatch::self()->addFile(d->exportsFile);
00165     connect(KDirWatch::self(), TQT_SIGNAL(dirty (const TQString&)),this,
00166             TQT_SLOT(slotFileChange(const TQString&)));
00167   }
00168 }
00169 
00170 KNFSShare::~KNFSShare() {
00171   if (TQFile::exists(d->exportsFile)) {
00172     KDirWatch::self()->removeFile(d->exportsFile);
00173   }
00174   delete d;
00175 }
00176 
00177 
00178 bool KNFSShare::isDirectoryShared( const TQString & path ) const {
00179   TQString fixedPath = path;
00180   if ( path[path.length()-1] != '/' )
00181        fixedPath += '/';
00182   
00183   return d->sharedPaths.find(fixedPath) != 0;
00184 }
00185 
00186 TQStringList KNFSShare::sharedDirectories() const {
00187   TQStringList result;
00188   TQDictIterator<bool> it(d->sharedPaths);
00189   for( ; it.current(); ++it )
00190       result << it.currentKey();
00191       
00192   return result;       
00193 }
00194 
00195 TQString KNFSShare::exportsPath() const {
00196   return d->exportsFile;
00197 }
00198 
00199 
00200 
00201 void KNFSShare::slotFileChange( const TQString & 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 

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.7.1
This website is maintained by Timothy Pearson.