2 Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <qfileinfo.h>
26 #include <qtextstream.h>
27 #include <qstringlist.h>
28 #include <KStandardDirs>
30 #include <kmessagebox.h>
32 #include <knfsshare.h>
33 #include <ktemporaryfile.h>
38 NFSFile::NFSFile(const KUrl
& url
, bool readonly
)
40 _lines
.setAutoDelete(true);
41 _entries
.setAutoDelete(false);
50 void NFSFile::addEntry(NFSEntry
*entry
)
53 _entries
.append(entry
);
56 void NFSFile::removeEntry(NFSEntry
*entry
)
58 _entries
.remove(entry
);
62 bool NFSFile::hasEntry(NFSEntry
*entry
)
64 return 0 < _entries
.contains(entry
);
68 NFSEntry
* NFSFile::getEntryByPath(const QString
& path
)
73 QString testPath
= path
.trimmed();
74 if ( testPath
[testPath
.length()-1] != '/' )
77 for (NFSEntry
* entry
= _entries
.first(); entry
; entry
= _entries
.next())
79 if (entry
->path()==testPath
)
86 bool NFSFile::removeEntryByPath(const QString
& path
) {
87 NFSEntry
* entry
= getEntryByPath(path
);
95 EntryIterator
NFSFile::getEntries()
97 return EntryIterator(_entries
);
105 QFile
f(_url
.path());
107 if ( !f
.open(QIODevice::ReadOnly
) ) {
108 kError() << "NFSFile::load: Could not open " << _url
.path() << endl
;
117 bool continuedLine
= false; // is true if the line before ended with a backslash
118 QString completeLine
;
122 QString currentLine
= s
.readLine().trimmed();
125 completeLine
+= currentLine
;
126 continuedLine
= false;
129 completeLine
= currentLine
;
131 // is the line continued in the next line ?
132 if ( completeLine
[completeLine
.length()-1] == '\\' )
134 continuedLine
= true;
135 // remove the ending backslash
136 completeLine
.truncate( completeLine
.length()-1 );
141 if (completeLine
.isEmpty()) {
142 _lines
.append(new NFSEmptyLine());
147 if ('#' == completeLine
[0]) {
148 _lines
.append(new NFSComment(completeLine
));
155 // Handle quotation marks
156 if ( completeLine
[0] == '"' ) {
157 int i
= completeLine
.indexOf('"',1);
159 kError() << "NFSFile: Parse error: Missing quotation mark: "
160 << completeLine
<< endl
;
163 path
= completeLine
.mid(1,i
-1);
164 hosts
= completeLine
.mid(i
+1);
166 } else { // no quotation marks
167 int i
= completeLine
.indexOf(' ');
169 i
= completeLine
.indexOf('\t');
174 path
= completeLine
.left(i
);
175 hosts
= completeLine
.mid(i
+1).trimmed();
180 if ( path
[path
.length()-1] != '/' )
183 kDebug(5009) << "KNFSShare: Found path: '" << path
<< "'";
184 NFSEntry
*entry
= new NFSEntry(path
);
185 QStringList hostList
= QStringList::split(' ', hosts
);
187 if (hostList
.isEmpty()) {
188 NFSHost
* host
= new NFSHost("*");
189 entry
->addHost(host
);
191 QStringList::iterator it
;
192 for ( it
= hostList
.begin(); it
!= hostList
.end(); ++it
) {
193 NFSHost
* host
= new NFSHost((*it
).trimmed());
194 entry
->addHost(host
);
195 kDebug(5009) << "KNFSShare: Found host: " << (*it
) << " name='"
196 << host
->name
<< "'" << endl
;
200 kDebug(5009) << "KNFSShare: Found hosts: " << hosts
<< "'";
201 this->addEntry(entry
);
211 void NFSFile::saveTo(QTextStream
* stream
) {
212 Q3PtrListIterator
<NFSLine
> it(_lines
);
215 while ( (line
= it
.current()) != 0 ) {
217 *stream
<< line
->toString() << endl
;
221 bool NFSFile::saveTo(const QString
& fileName
) {
222 QFile
file(fileName
);
223 if (!file
.open(QIODevice::WriteOnly
))
226 QTextStream
stream(&file
);
234 if (QFileInfo(_url
.path()).isWritable() ) {
239 KTemporaryFile tempFile
;
241 saveTo(tempFile
.fileName());
243 QString command
= QString("cp %1 %2")
244 .arg(KShell::quoteArg( tempFile
.fileName() ),
245 KShell::quoteArg( _url
.path() ));
247 if (restartNFSServer
)
248 command
+=" && exportfs -ra";
251 if (!QFileInfo(_url
.path()).isWritable() || restartNFSServer
)
252 proc
<<KStandardDirs::findExe("kdesu") << "-d" << "-c"<<command
;
254 proc
.setShellCommand(command
);
256 if (proc
.execute()) {